Fix pow <small> and a very stypid bug with dummy srcs(0 equals to tmp0.x)</small...
[mesa.git] / src / mesa / main / fbobject.c
index 05ad155595043b2a88f31e15c630f50d4c40e9aa..3e425bd6724ec122357c439759dbf67c23fcee26 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Mesa 3-D graphics library
- * Version:  6.3
+ * Version:  6.5
  *
  * Copyright (C) 1999-2005  Brian Paul   All Rights Reserved.
  *
 
 #include "context.h"
 #include "fbobject.h"
+#include "framebuffer.h"
 #include "hash.h"
+#include "renderbuffer.h"
 #include "teximage.h"
 #include "texstore.h"
 
 
+/* XXX temporarily here */
+#define GL_READ_FRAMEBUFFER_EXT                0x90
+#define GL_DRAW_FRAMEBUFFER_EXT                0x9a
+#define GL_DRAW_FRAMEBUFFER_BINDING_EXT        GL_FRAMEBUFFER_BINDING_EXT
+#define GL_READ_FRAMEBUFFER_BINDING_EXT        0x9b
+
+
 /**
  * Notes:
  *
@@ -94,109 +103,13 @@ lookup_framebuffer(GLcontext *ctx, GLuint id)
 }
 
 
-/**
- * Allocate a new gl_framebuffer.
- * This is the default function for ctx->Driver.NewFramebuffer().
- */
-struct gl_framebuffer *
-_mesa_new_framebuffer(GLcontext *ctx, GLuint name)
-{
-   struct gl_framebuffer *fb = CALLOC_STRUCT(gl_framebuffer);
-   if (fb) {
-      fb->Name = name;
-      fb->RefCount = 1;
-      fb->Delete = _mesa_delete_framebuffer;
-   }
-   return fb;
-}
-
-
-/**
- * Delete a gl_framebuffer.
- * This is the default function for framebuffer->Delete().
- */
-void
-_mesa_delete_framebuffer(GLcontext *ctx, struct gl_framebuffer *fb)
-{
-   (void) ctx;
-   _mesa_free(fb);
-}
-
-
-/**
- * Allocate the actual storage for a renderbuffer with the given format
- * and dimensions.
- * This is the default function for gl_renderbuffer->AllocStorage().
- * All incoming parameters will have already been checked for errors.
- * If memory allocate fails, the function must call
- * _mesa_error(GL_OUT_OF_MEMORY) and then return.
- * \return GL_TRUE for success, GL_FALSE for failure.
- */
-static GLboolean
-alloc_renderbuffer_storage(GLcontext *ctx, struct gl_renderbuffer *rb,
-                           GLenum internalFormat, GLuint width, GLuint height)
-{
-   /* First, free any existing storage */
-   if (rb->Data) {
-      _mesa_free(rb->Data);
-   }
-
-   /* Now, allocate new storage */
-   rb->Data = _mesa_malloc(width * height * 4); /* XXX fix size! */
-   if (rb->Data == NULL) {
-      _mesa_error(ctx, GL_OUT_OF_MEMORY, "glRenderbufferStorageEXT");
-      return GL_FALSE;
-   }
-
-   /* We set these fields now if the allocation worked */
-   rb->Width = width;
-   rb->Height = height;
-   rb->InternalFormat = internalFormat;
-
-   return GL_TRUE;
-}
-
-
-/**
- * Allocate a new gl_renderbuffer.
- * This is the default function for ctx->Driver.NewRenderbuffer().
- */
-struct gl_renderbuffer *
-_mesa_new_renderbuffer(GLcontext *ctx, GLuint name)
-{
-   struct gl_renderbuffer *rb = CALLOC_STRUCT(gl_renderbuffer);
-   if (rb) {
-      rb->Name = name;
-      rb->RefCount = 1;
-      rb->Delete = _mesa_delete_renderbuffer;
-      rb->AllocStorage = alloc_renderbuffer_storage;
-      /* other fields are zero */
-   }
-   return rb;
-}
-
-
-/**
- * Delete a gl_framebuffer.
- * This is the default function for framebuffer->Delete().
- */
-void
-_mesa_delete_renderbuffer(GLcontext *ctx, struct gl_renderbuffer *rb)
-{
-   (void) ctx;
-   if (rb->Data) {
-      _mesa_free(rb->Data);
-   }
-   _mesa_free(rb);
-}
-
-
 /**
  * Given a GL_*_ATTACHMENTn token, return a pointer to the corresponding
  * gl_renderbuffer_attachment object.
  */
-static struct gl_renderbuffer_attachment *
-get_attachment(GLcontext *ctx, struct gl_framebuffer *fb, GLenum attachment)
+struct gl_renderbuffer_attachment *
+_mesa_get_attachment(GLcontext *ctx, struct gl_framebuffer *fb,
+                     GLenum attachment)
 {
    GLuint i;
 
@@ -221,11 +134,11 @@ get_attachment(GLcontext *ctx, struct gl_framebuffer *fb, GLenum attachment)
       if (i >= ctx->Const.MaxColorAttachments) {
         return NULL;
       }
-      return &fb->ColorAttachment[i];
+      return &fb->Attachment[BUFFER_COLOR0 + i];
    case GL_DEPTH_ATTACHMENT_EXT:
-      return &fb->DepthAttachment;
+      return &fb->Attachment[BUFFER_DEPTH];
    case GL_STENCIL_ATTACHMENT_EXT:
-      return &fb->StencilAttachment;
+      return &fb->Attachment[BUFFER_STENCIL];
    default:
       return NULL;
    }
@@ -236,24 +149,31 @@ get_attachment(GLcontext *ctx, struct gl_framebuffer *fb, GLenum attachment)
  * Remove any texture or renderbuffer attached to the given attachment
  * point.  Update reference counts, etc.
  */
-static void
-remove_attachment(GLcontext *ctx, struct gl_renderbuffer_attachment *att)
+void
+_mesa_remove_attachment(GLcontext *ctx, struct gl_renderbuffer_attachment *att)
 {
    if (att->Type == GL_TEXTURE) {
       ASSERT(att->Texture);
-      ASSERT(!att->Renderbuffer);
       att->Texture->RefCount--;
       if (att->Texture->RefCount == 0) {
         ctx->Driver.DeleteTexture(ctx, att->Texture);
       }
+      else {
+         /* tell driver that we're done rendering to this texture. */
+         if (ctx->Driver.FinishRenderTexture) {
+            ctx->Driver.FinishRenderTexture(ctx, att->Texture,
+                                            att->CubeMapFace,
+                                            att->TextureLevel);
+         }
+      }
       att->Texture = NULL;
    }
-   else if (att->Type == GL_RENDERBUFFER_EXT) {
+   if (att->Type == GL_TEXTURE || att->Type == GL_RENDERBUFFER_EXT) {
       ASSERT(att->Renderbuffer);
       ASSERT(!att->Texture);
       att->Renderbuffer->RefCount--;
       if (att->Renderbuffer->RefCount == 0) {
-         att->Renderbuffer->Delete(ctx, att->Renderbuffer);
+         att->Renderbuffer->Delete(att->Renderbuffer);
       }
       att->Renderbuffer = NULL;
    }
@@ -266,15 +186,25 @@ remove_attachment(GLcontext *ctx, struct gl_renderbuffer_attachment *att)
  * Bind a texture object to an attachment point.
  * The previous binding, if any, will be removed first.
  */
-static void
-set_texture_attachment(GLcontext *ctx,
-                    struct gl_renderbuffer_attachment *att,
-                    struct gl_texture_object *texObj,
-                    GLenum texTarget, GLuint level, GLuint zoffset)
+void
+_mesa_set_texture_attachment(GLcontext *ctx,
+                             struct gl_renderbuffer_attachment *att,
+                             struct gl_texture_object *texObj,
+                             GLenum texTarget, GLuint level, GLuint zoffset)
 {
-   remove_attachment(ctx, att);
-   att->Type = GL_TEXTURE;
-   att->Texture = texObj;
+   if (att->Texture == texObj) {
+      /* re-attaching same texture */
+      ASSERT(att->Type == GL_TEXTURE);
+   }
+   else {
+      /* new attachment */
+      _mesa_remove_attachment(ctx, att);
+      att->Type = GL_TEXTURE;
+      att->Texture = texObj;
+      texObj->RefCount++;
+   }
+
+   /* always update these fields */
    att->TextureLevel = level;
    if (IS_CUBE_FACE(texTarget)) {
       att->CubeMapFace = texTarget - GL_TEXTURE_CUBE_MAP_POSITIVE_X;
@@ -284,12 +214,6 @@ set_texture_attachment(GLcontext *ctx,
    }
    att->Zoffset = zoffset;
    att->Complete = GL_FALSE;
-   texObj->RefCount++;
-
-   /* XXX when we attach to a texture, we should probably set the
-    * att->Renderbuffer pointer to a "wrapper renderbuffer" which
-    * makes the texture image look like renderbuffer.
-    */
 }
 
 
@@ -297,12 +221,12 @@ set_texture_attachment(GLcontext *ctx,
  * Bind a renderbuffer to an attachment point.
  * The previous binding, if any, will be removed first.
  */
-static void
-set_renderbuffer_attachment(GLcontext *ctx,
-                            struct gl_renderbuffer_attachment *att,
-                            struct gl_renderbuffer *rb)
+void
+_mesa_set_renderbuffer_attachment(GLcontext *ctx,
+                                  struct gl_renderbuffer_attachment *att,
+                                  struct gl_renderbuffer *rb)
 {
-   remove_attachment(ctx, att);
+   _mesa_remove_attachment(ctx, att);
    att->Type = GL_RENDERBUFFER_EXT;
    att->Renderbuffer = rb;
    att->Texture = NULL; /* just to be safe */
@@ -311,6 +235,28 @@ set_renderbuffer_attachment(GLcontext *ctx,
 }
 
 
+/**
+ * Fallback for ctx->Driver.FramebufferRenderbuffer()
+ * Attach a renderbuffer object to a framebuffer object.
+ */
+void
+_mesa_framebuffer_renderbuffer(GLcontext *ctx, struct gl_framebuffer *fb,
+                               GLenum attachment, struct gl_renderbuffer *rb)
+{
+   struct gl_renderbuffer_attachment *att;
+
+   att = _mesa_get_attachment(ctx, fb, attachment);
+   ASSERT(att);
+
+   if (rb) {
+      _mesa_set_renderbuffer_attachment(ctx, att, rb);
+   }
+   else {
+      _mesa_remove_attachment(ctx, att);
+   }
+}
+
+
 /**
  * Test if an attachment point is complete and update its Complete field.
  * \param format if GL_COLOR, this is a color attachment point,
@@ -326,15 +272,15 @@ test_attachment_completeness(const GLcontext *ctx, GLenum format,
    /* assume complete */
    att->Complete = GL_TRUE;
 
-   if (att->Type == GL_NONE)
-      return; /* complete */
-
    /* Look for reasons why the attachment might be incomplete */
    if (att->Type == GL_TEXTURE) {
-      struct gl_texture_object *texObj = att->Texture;
+      const struct gl_texture_object *texObj = att->Texture;
       struct gl_texture_image *texImage;
 
-      assert(texObj);
+      if (!texObj) {
+         att->Complete = GL_FALSE;
+         return;
+      }
 
       texImage = texObj->Image[att->CubeMapFace][att->TextureLevel];
       if (!texImage) {
@@ -351,13 +297,21 @@ test_attachment_completeness(const GLcontext *ctx, GLenum format,
       }
 
       if (format == GL_COLOR) {
-         if (texImage->Format != GL_RGB && texImage->Format != GL_RGBA) {
+         if (texImage->TexFormat->BaseFormat != GL_RGB &&
+             texImage->TexFormat->BaseFormat != GL_RGBA) {
             att->Complete = GL_FALSE;
             return;
          }
       }
       else if (format == GL_DEPTH) {
-         if (texImage->Format != GL_DEPTH_COMPONENT) {
+         if (texImage->TexFormat->BaseFormat == GL_DEPTH_COMPONENT) {
+            /* OK */
+         }
+         else if (ctx->Extensions.EXT_packed_depth_stencil &&
+                  att->Renderbuffer->_BaseFormat == GL_DEPTH_STENCIL_EXT) {
+            /* OK */
+         }
+         else {
             att->Complete = GL_FALSE;
             return;
          }
@@ -368,9 +322,7 @@ test_attachment_completeness(const GLcontext *ctx, GLenum format,
          return;
       }
    }
-   else {
-      assert(att->Type == GL_RENDERBUFFER_EXT);
-
+   else if (att->Type == GL_RENDERBUFFER_EXT) {
       if (att->Renderbuffer->Width < 1 || att->Renderbuffer->Height < 1) {
          att->Complete = GL_FALSE;
          return;
@@ -383,75 +335,103 @@ test_attachment_completeness(const GLcontext *ctx, GLenum format,
          }
       }
       else if (format == GL_DEPTH) {
-         if (att->Renderbuffer->_BaseFormat != GL_DEPTH_COMPONENT) {
+         if (att->Renderbuffer->_BaseFormat == GL_DEPTH_COMPONENT) {
+            /* OK */
+         }
+         else if (ctx->Extensions.EXT_packed_depth_stencil &&
+                  att->Renderbuffer->_BaseFormat == GL_DEPTH_STENCIL_EXT) {
+            /* OK */
+         }
+         else {
             att->Complete = GL_FALSE;
             return;
          }
       }
       else {
          assert(format == GL_STENCIL);
-         if (att->Renderbuffer->_BaseFormat != GL_STENCIL_INDEX) {
+         if (att->Renderbuffer->_BaseFormat == GL_STENCIL_INDEX) {
+            /* OK */
+         }
+         else if (ctx->Extensions.EXT_packed_depth_stencil &&
+                  att->Renderbuffer->_BaseFormat == GL_DEPTH_STENCIL_EXT) {
+            /* OK */
+         }
+         else {
             att->Complete = GL_FALSE;
             return;
          }
       }
    }
+   else {
+      ASSERT(att->Type == GL_NONE);
+      /* complete */
+      return;
+   }
 }
 
 
 /**
  * Test if the given framebuffer object is complete and update its
  * Status field with the results.
+ * Also update the framebuffer's Width and Height fields if the
+ * framebuffer is complete.
  */
-static void
-test_framebuffer_completeness(GLcontext *ctx,
-                              struct gl_framebuffer *fb)
+void
+_mesa_test_framebuffer_completeness(GLcontext *ctx, struct gl_framebuffer *fb)
 {
+   GLuint numImages, width = 0, height = 0;
+   GLenum intFormat = GL_NONE;
+   GLuint w = 0, h = 0;
    GLint i;
-   GLuint numImages, width, height;
-   GLenum intFormat;
 
-   /* Set to COMPLETE status, then try to find reasons for being incomplete */
-   fb->Status = GL_FRAMEBUFFER_COMPLETE_EXT;
+   assert(fb->Name != 0);
 
    numImages = 0;
+   fb->Width = 0;
+   fb->Height = 0;
 
    /* Start at -2 to more easily loop over all attachment points */
-   for (i = -2; i < ctx->Const.MaxColorAttachments; i++) {
+   for (i = -2; i < (GLint) ctx->Const.MaxColorAttachments; i++) {
       struct gl_renderbuffer_attachment *att;
-      GLuint w, h;
       GLenum f;
 
       if (i == -2) {
-         att = &fb->DepthAttachment;
+         att = &fb->Attachment[BUFFER_DEPTH];
          test_attachment_completeness(ctx, GL_DEPTH, att);
          if (!att->Complete) {
-            fb->Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
+            fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
             return;
          }
       }
       else if (i == -1) {
-         att = &fb->StencilAttachment;
+         att = &fb->Attachment[BUFFER_STENCIL];
          test_attachment_completeness(ctx, GL_STENCIL, att);
          if (!att->Complete) {
-            fb->Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
+            fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
             return;
          }
       }
       else {
-         att = &fb->ColorAttachment[i];
+         att = &fb->Attachment[BUFFER_COLOR0 + i];
          test_attachment_completeness(ctx, GL_COLOR, att);
          if (!att->Complete) {
-            fb->Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
+            fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
             return;
          }
       }
 
       if (att->Type == GL_TEXTURE) {
-         w = att->Texture->Image[att->CubeMapFace][att->TextureLevel]->Width;
-         h = att->Texture->Image[att->CubeMapFace][att->TextureLevel]->Height;
-         f = att->Texture->Image[att->CubeMapFace][att->TextureLevel]->Format;
+         const struct gl_texture_image *texImg
+            = att->Texture->Image[att->CubeMapFace][att->TextureLevel];
+         w = texImg->Width;
+         h = texImg->Height;
+         f = texImg->_BaseFormat;
          numImages++;
+         if (f != GL_RGB && f != GL_RGBA && f != GL_DEPTH_COMPONENT) {
+            /* XXX need GL_DEPTH_STENCIL_EXT test? */
+            fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT;
+            return;
+         }
       }
       else if (att->Type == GL_RENDERBUFFER_EXT) {
          w = att->Renderbuffer->Width;
@@ -474,57 +454,84 @@ test_framebuffer_completeness(GLcontext *ctx,
       else {
          /* check that width, height, format are same */
          if (w != width || h != height) {
-            fb->Status = GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT;
+            fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT;
             return;
          }
-         if (i >= 0 && f != intFormat) {
-            fb->Status = GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT;
+         if (intFormat != GL_NONE && f != intFormat) {
+            fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT;
             return;
          }
-
       }
    }
 
    /* Check that all DrawBuffers are present */
    for (i = 0; i < ctx->Const.MaxDrawBuffers; i++) {
-      if (fb->DrawBuffer[i] != GL_NONE) {
-         struct gl_renderbuffer_attachment *att
-            = get_attachment(ctx, fb, fb->DrawBuffer[i]);
+      if (fb->ColorDrawBuffer[i] != GL_NONE) {
+         const struct gl_renderbuffer_attachment *att
+            = _mesa_get_attachment(ctx, fb, fb->ColorDrawBuffer[i]);
+         assert(att);
          if (att->Type == GL_NONE) {
-            fb->Status = GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT;
+            fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT;
             return;
          }
       }
    }
 
    /* Check that the ReadBuffer is present */
-   if (fb->ReadBuffer != GL_NONE) {
-      struct gl_renderbuffer_attachment *att
-         = get_attachment(ctx, fb, fb->ReadBuffer);
+   if (fb->ColorReadBuffer != GL_NONE) {
+      const struct gl_renderbuffer_attachment *att
+         = _mesa_get_attachment(ctx, fb, fb->ColorReadBuffer);
+      assert(att);
       if (att->Type == GL_NONE) {
-         fb->Status = GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT;
+         fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT;
          return;
       }
    }
 
+   /* Check if any renderbuffer is attached more than once.
+    * Note that there's one exception: a GL_DEPTH_STENCIL renderbuffer can be
+    * bound to both the stencil and depth attachment points at the same time.
+    */
+   for (i = 0; i < BUFFER_COUNT - 1; i++) {
+      struct gl_renderbuffer *rb_i = fb->Attachment[i].Renderbuffer;
+      if (rb_i) {
+         GLint j;
+         for (j = i + 1; j < BUFFER_COUNT; j++) {
+            struct gl_renderbuffer *rb_j = fb->Attachment[j].Renderbuffer;
+            if (rb_i == rb_j && rb_i->_BaseFormat != GL_DEPTH_STENCIL_EXT) {
+               fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_DUPLICATE_ATTACHMENT_EXT;
+               return;
+            }
+         }
+      }
+   }
+
+
    if (numImages == 0) {
-      fb->Status = GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT;
+      fb->_Status = GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT;
       return;
    }
-}
 
+   /*
+    * If we get here, the framebuffer is complete!
+    */
+   fb->_Status = GL_FRAMEBUFFER_COMPLETE_EXT;
+   fb->Width = w;
+   fb->Height = h;
+}
 
 
 GLboolean GLAPIENTRY
 _mesa_IsRenderbufferEXT(GLuint renderbuffer)
 {
-   const struct gl_renderbuffer *rb;
    GET_CURRENT_CONTEXT(ctx);
-
    ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
-
-   rb = lookup_renderbuffer(ctx, renderbuffer);
-   return rb ? GL_TRUE : GL_FALSE;
+   if (renderbuffer) {
+      struct gl_renderbuffer *rb = lookup_renderbuffer(ctx, renderbuffer);
+      if (rb != NULL && rb != &DummyRenderbuffer)
+         return GL_TRUE;
+   }
+   return GL_FALSE;
 }
 
 
@@ -542,6 +549,8 @@ _mesa_BindRenderbufferEXT(GLenum target, GLuint renderbuffer)
       return;
    }
 
+   FLUSH_VERTICES(ctx, _NEW_BUFFERS);
+
    if (renderbuffer) {
       newRb = lookup_renderbuffer(ctx, renderbuffer);
       if (newRb == &DummyRenderbuffer) {
@@ -568,7 +577,7 @@ _mesa_BindRenderbufferEXT(GLenum target, GLuint renderbuffer)
    if (oldRb) {
       oldRb->RefCount--;
       if (oldRb->RefCount == 0) {
-         oldRb->Delete(ctx, oldRb);
+         oldRb->Delete(oldRb);
       }
    }
 
@@ -585,12 +594,20 @@ _mesa_DeleteRenderbuffersEXT(GLsizei n, const GLuint *renderbuffers)
    GET_CURRENT_CONTEXT(ctx);
 
    ASSERT_OUTSIDE_BEGIN_END(ctx);
+   FLUSH_VERTICES(ctx, _NEW_BUFFERS);
 
    for (i = 0; i < n; i++) {
       if (renderbuffers[i] > 0) {
         struct gl_renderbuffer *rb;
         rb = lookup_renderbuffer(ctx, renderbuffers[i]);
         if (rb) {
+            /* check if deleting currently bound renderbuffer object */
+            if (rb == ctx->CurrentRenderbuffer) {
+               /* bind default */
+               ASSERT(rb->RefCount >= 2);
+               _mesa_BindRenderbufferEXT(GL_RENDERBUFFER_EXT, 0);
+            }
+
            /* remove from hash table immediately, to free the ID */
            _mesa_HashRemove(ctx->Shared->RenderBuffers, renderbuffers[i]);
 
@@ -600,7 +617,7 @@ _mesa_DeleteRenderbuffersEXT(GLsizei n, const GLuint *renderbuffers)
                 */
                rb->RefCount--;
                if (rb->RefCount == 0) {
-                  rb->Delete(ctx, rb);
+                  rb->Delete(rb);
                }
            }
         }
@@ -643,7 +660,7 @@ _mesa_GenRenderbuffersEXT(GLsizei n, GLuint *renderbuffers)
  * Given an internal format token for a render buffer, return the
  * corresponding base format.
  * \return one of GL_RGB, GL_RGBA, GL_STENCIL_INDEX, GL_DEPTH_COMPONENT
- *  or zero if error.
+ *  GL_DEPTH_STENCIL_EXT or zero if error.
  */
 static GLenum
 base_internal_format(GLcontext *ctx, GLenum internalFormat)
@@ -678,6 +695,12 @@ base_internal_format(GLcontext *ctx, GLenum internalFormat)
    case GL_DEPTH_COMPONENT24:
    case GL_DEPTH_COMPONENT32:
       return GL_DEPTH_COMPONENT;
+   case GL_DEPTH_STENCIL_EXT:
+   case GL_DEPTH24_STENCIL8_EXT:
+      if (ctx->Extensions.EXT_packed_depth_stencil)
+         return GL_DEPTH_STENCIL_EXT;
+      else
+         return 0;
    /* XXX add floating point formats eventually */
    default:
       return 0;
@@ -724,6 +747,15 @@ _mesa_RenderbufferStorageEXT(GLenum target, GLenum internalFormat,
       return;
    }
 
+   FLUSH_VERTICES(ctx, _NEW_BUFFERS);
+
+   if (rb->InternalFormat == internalFormat &&
+       rb->Width == width &&
+       rb->Height == height) {
+      /* no change in allocation needed */
+      return;
+   }
+
    /* Now allocate the storage */
    ASSERT(rb->AllocStorage);
    if (rb->AllocStorage(ctx, rb, internalFormat, width, height)) {
@@ -731,6 +763,8 @@ _mesa_RenderbufferStorageEXT(GLenum target, GLenum internalFormat,
       assert(rb->Width == width);
       assert(rb->Height == height);
       assert(rb->InternalFormat);
+      assert(rb->RedBits || rb->GreenBits || rb->BlueBits || rb->AlphaBits ||
+             rb->DepthBits || rb->StencilBits || rb->IndexBits);
       rb->_BaseFormat = baseFormat;
    }
    else {
@@ -739,8 +773,18 @@ _mesa_RenderbufferStorageEXT(GLenum target, GLenum internalFormat,
       rb->Height = 0;
       rb->InternalFormat = GL_NONE;
       rb->_BaseFormat = GL_NONE;
-   }
-
+      rb->RedBits =
+      rb->GreenBits =
+      rb->BlueBits =
+      rb->AlphaBits =
+      rb->IndexBits =
+      rb->DepthBits =
+      rb->StencilBits = 0;
+   }
+
+   /*
+   test_framebuffer_completeness(ctx, fb);
+   */
    /* XXX if this renderbuffer is attached anywhere, invalidate attachment
     * points???
     */
@@ -766,6 +810,8 @@ _mesa_GetRenderbufferParameterivEXT(GLenum target, GLenum pname, GLint *params)
       return;
    }
 
+   FLUSH_VERTICES(ctx, _NEW_BUFFERS);
+
    switch (pname) {
    case GL_RENDERBUFFER_WIDTH_EXT:
       *params = ctx->CurrentRenderbuffer->Width;
@@ -776,6 +822,24 @@ _mesa_GetRenderbufferParameterivEXT(GLenum target, GLenum pname, GLint *params)
    case GL_RENDERBUFFER_INTERNAL_FORMAT_EXT:
       *params = ctx->CurrentRenderbuffer->InternalFormat;
       return;
+   case GL_RENDERBUFFER_RED_SIZE_EXT:
+      *params = ctx->CurrentRenderbuffer->RedBits;
+      break;
+   case GL_RENDERBUFFER_GREEN_SIZE_EXT:
+      *params = ctx->CurrentRenderbuffer->GreenBits;
+      break;
+   case GL_RENDERBUFFER_BLUE_SIZE_EXT:
+      *params = ctx->CurrentRenderbuffer->BlueBits;
+      break;
+   case GL_RENDERBUFFER_ALPHA_SIZE_EXT:
+      *params = ctx->CurrentRenderbuffer->AlphaBits;
+      break;
+   case GL_RENDERBUFFER_DEPTH_SIZE_EXT:
+      *params = ctx->CurrentRenderbuffer->DepthBits;
+      break;
+   case GL_RENDERBUFFER_STENCIL_SIZE_EXT:
+      *params = ctx->CurrentRenderbuffer->StencilBits;
+      break;
    default:
       _mesa_error(ctx, GL_INVALID_ENUM,
                   "glGetRenderbufferParameterivEXT(target)");
@@ -787,13 +851,36 @@ _mesa_GetRenderbufferParameterivEXT(GLenum target, GLenum pname, GLint *params)
 GLboolean GLAPIENTRY
 _mesa_IsFramebufferEXT(GLuint framebuffer)
 {
-   const struct gl_framebuffer *fb;
    GET_CURRENT_CONTEXT(ctx);
-
    ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
+   if (framebuffer) {
+      struct gl_framebuffer *rb = lookup_framebuffer(ctx, framebuffer);
+      if (rb != NULL && rb != &DummyFramebuffer)
+         return GL_TRUE;
+   }
+   return GL_FALSE;
+}
 
-   fb = lookup_framebuffer(ctx, framebuffer);
-   return fb ? GL_TRUE : GL_FALSE;
+
+/**
+ * Examine all the framebuffer's attachments to see if any are textures.
+ * If so, call ctx->Driver.FinishRenderTexture() for each texture to
+ * notify the device driver that the texture image may have changed.
+ */
+static void
+check_texture_render(GLcontext *ctx, struct gl_framebuffer *fb)
+{
+   if (ctx->Driver.FinishRenderTexture) {
+      GLuint i;
+      for (i = 0; i < BUFFER_COUNT; i++) {
+         struct gl_renderbuffer_attachment *att = fb->Attachment + i;
+         struct gl_texture_object *texObj = att->Texture;
+         if (texObj) {
+            ctx->Driver.FinishRenderTexture(ctx, texObj, att->CubeMapFace,
+                                            att->TextureLevel);
+         }
+      }
+   }
 }
 
 
@@ -801,17 +888,43 @@ void GLAPIENTRY
 _mesa_BindFramebufferEXT(GLenum target, GLuint framebuffer)
 {
    struct gl_framebuffer *newFb, *oldFb;
+   GLboolean bindReadBuf, bindDrawBuf;
    GET_CURRENT_CONTEXT(ctx);
 
    ASSERT_OUTSIDE_BEGIN_END(ctx);
 
-   if (target != GL_FRAMEBUFFER_EXT) {
-         _mesa_error(ctx, GL_INVALID_ENUM,
-                  "glBindFramebufferEXT(target)");
+   switch (target) {
+#if FEATURE_EXT_framebuffer_blit
+   case GL_DRAW_FRAMEBUFFER_EXT:
+      if (!ctx->Extensions.EXT_framebuffer_blit) {
+         _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
+         return;
+      }
+      bindDrawBuf = GL_TRUE;
+      bindReadBuf = GL_FALSE;
+      break;
+   case GL_READ_FRAMEBUFFER_EXT:
+      if (!ctx->Extensions.EXT_framebuffer_blit) {
+         _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
+         return;
+      }
+      bindDrawBuf = GL_FALSE;
+      bindReadBuf = GL_TRUE;
+      break;
+#endif
+   case GL_FRAMEBUFFER_EXT:
+      bindDrawBuf = GL_TRUE;
+      bindReadBuf = GL_TRUE;
+      break;
+   default:
+      _mesa_error(ctx, GL_INVALID_ENUM, "glBindFramebufferEXT(target)");
       return;
    }
 
+   FLUSH_VERTICES(ctx, _NEW_BUFFERS);
+
    if (framebuffer) {
+      /* Binding a user-created framebuffer object */
       newFb = lookup_framebuffer(ctx, framebuffer);
       if (newFb == &DummyFramebuffer) {
          /* ID was reserved, but no real framebuffer object made yet */
@@ -826,23 +939,46 @@ _mesa_BindFramebufferEXT(GLenum target, GLuint framebuffer)
         }
          _mesa_HashInsert(ctx->Shared->FrameBuffers, framebuffer, newFb);
       }
-      newFb->RefCount++;
+      if (bindReadBuf)
+         newFb->RefCount++;
+      if (bindDrawBuf)
+         newFb->RefCount++;
    }
    else {
-      newFb = NULL;
+      /* Binding the window system framebuffer (which was originally set
+       * with MakeCurrent).
+       */
+      newFb = ctx->WinSysDrawBuffer;
    }
 
-   oldFb = ctx->CurrentFramebuffer;
-   if (oldFb) {
-      oldFb->RefCount--;
-      if (oldFb->RefCount == 0) {
-         oldFb->Delete(ctx, oldFb);
+   ASSERT(newFb != &DummyFramebuffer);
+
+   if (bindReadBuf) {
+      oldFb = ctx->ReadBuffer;
+      if (oldFb && oldFb->Name != 0) {
+         oldFb->RefCount--;
+         if (oldFb->RefCount == 0) {
+            oldFb->Delete(oldFb);
+         }
       }
+      ctx->ReadBuffer = newFb;
    }
 
-   ASSERT(newFb != &DummyFramebuffer);
-
-   ctx->CurrentFramebuffer = newFb;
+   if (bindDrawBuf) {
+      oldFb = ctx->DrawBuffer;
+      if (oldFb && oldFb->Name != 0) {
+         /* check if old FB had any texture attachments */
+         if (ctx->Driver.FinishRenderTexture) {
+            check_texture_render(ctx, oldFb);
+         }
+         /* check if time to delete this framebuffer */
+         oldFb->RefCount--;
+         if (oldFb->RefCount == 0) {
+            oldFb->Delete(oldFb);
+         }
+      }
+      ctx->DrawBuffer = newFb;
+   }
 }
 
 
@@ -853,12 +989,22 @@ _mesa_DeleteFramebuffersEXT(GLsizei n, const GLuint *framebuffers)
    GET_CURRENT_CONTEXT(ctx);
 
    ASSERT_OUTSIDE_BEGIN_END(ctx);
+   FLUSH_VERTICES(ctx, _NEW_BUFFERS);
 
    for (i = 0; i < n; i++) {
       if (framebuffers[i] > 0) {
         struct gl_framebuffer *fb;
         fb = lookup_framebuffer(ctx, framebuffers[i]);
         if (fb) {
+            ASSERT(fb == &DummyFramebuffer || fb->Name == framebuffers[i]);
+
+            /* check if deleting currently bound framebuffer object */
+            if (fb == ctx->DrawBuffer) {
+               /* bind default */
+               ASSERT(fb->RefCount >= 2);
+               _mesa_BindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
+            }
+
            /* remove from hash table immediately, to free the ID */
            _mesa_HashRemove(ctx->Shared->FrameBuffers, framebuffers[i]);
 
@@ -868,7 +1014,7 @@ _mesa_DeleteFramebuffersEXT(GLsizei n, const GLuint *framebuffers)
                 */
                fb->RefCount--;
                if (fb->RefCount == 0) {
-                  fb->Delete(ctx, fb);
+                  fb->Delete(fb);
                }
            }
         }
@@ -911,22 +1057,45 @@ _mesa_GenFramebuffersEXT(GLsizei n, GLuint *framebuffers)
 GLenum GLAPIENTRY
 _mesa_CheckFramebufferStatusEXT(GLenum target)
 {
+   struct gl_framebuffer *buffer;
    GET_CURRENT_CONTEXT(ctx);
 
-   ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FRAMEBUFFER_STATUS_ERROR_EXT);
+   ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, 0);
 
-   if (target != GL_FRAMEBUFFER_EXT) {
+   switch (target) {
+#if FEATURE_EXT_framebuffer_blit
+   case GL_DRAW_FRAMEBUFFER_EXT:
+      if (!ctx->Extensions.EXT_framebuffer_blit) {
+         _mesa_error(ctx, GL_INVALID_ENUM, "glCheckFramebufferStatus(target)");
+         return 0;
+      }
+      buffer = ctx->DrawBuffer;
+      break;
+   case GL_READ_FRAMEBUFFER_EXT:
+      if (!ctx->Extensions.EXT_framebuffer_blit) {
+         _mesa_error(ctx, GL_INVALID_ENUM, "glCheckFramebufferStatus(target)");
+         return 0;
+      }
+      buffer = ctx->ReadBuffer;
+      break;
+#endif
+   case GL_FRAMEBUFFER_EXT:
+      buffer = ctx->DrawBuffer;
+      break;
+   default:
       _mesa_error(ctx, GL_INVALID_ENUM, "glCheckFramebufferStatus(target)");
-      return GL_FRAMEBUFFER_STATUS_ERROR_EXT;
+      return 0; /* formerly GL_FRAMEBUFFER_STATUS_ERROR_EXT */
    }
 
-   if (!ctx->CurrentFramebuffer) {
+   if (buffer->Name == 0) {
       /* The window system / default framebuffer is always complete */
       return GL_FRAMEBUFFER_COMPLETE_EXT;
    }
 
-   test_framebuffer_completeness(ctx, ctx->CurrentFramebuffer);
-   return ctx->CurrentFramebuffer->Status;
+   FLUSH_VERTICES(ctx, _NEW_BUFFERS);
+
+   _mesa_test_framebuffer_completeness(ctx, buffer);
+   return buffer->_Status;
 }
 
 
@@ -948,13 +1117,14 @@ error_check_framebuffer_texture(GLcontext *ctx, GLuint dims,
       return GL_TRUE;
    }
 
-   if (ctx->CurrentFramebuffer == NULL) {
+   /* check framebuffer binding */
+   if (ctx->DrawBuffer->Name == 0) {
       _mesa_error(ctx, GL_INVALID_OPERATION,
                   "glFramebufferTexture%dDEXT", dims);
       return GL_TRUE;
    }
 
-   /* only check textarget, level if texture ID is non-zero*/
+   /* only check textarget, level if texture ID is non-zero */
    if (texture) {
       if ((dims == 1 && textarget != GL_TEXTURE_1D) ||
          (dims == 3 && textarget != GL_TEXTURE_3D) ||
@@ -982,6 +1152,7 @@ _mesa_FramebufferTexture1DEXT(GLenum target, GLenum attachment,
                               GLenum textarget, GLuint texture, GLint level)
 {
    struct gl_renderbuffer_attachment *att;
+   struct gl_texture_object *texObj;
    GET_CURRENT_CONTEXT(ctx);
 
    ASSERT_OUTSIDE_BEGIN_END(ctx);
@@ -992,15 +1163,18 @@ _mesa_FramebufferTexture1DEXT(GLenum target, GLenum attachment,
 
    ASSERT(textarget == GL_TEXTURE_1D);
 
-   att = get_attachment(ctx, ctx->CurrentFramebuffer, attachment);
+   /* XXX read blit */
+   att = _mesa_get_attachment(ctx, ctx->DrawBuffer, attachment);
    if (att == NULL) {
       _mesa_error(ctx, GL_INVALID_ENUM,
                  "glFramebufferTexture1DEXT(attachment)");
       return;
    }
 
+   FLUSH_VERTICES(ctx, _NEW_BUFFERS);
+
    if (texture) {
-      struct gl_texture_object *texObj = (struct gl_texture_object *)
+      texObj = (struct gl_texture_object *)
         _mesa_HashLookup(ctx->Shared->TexObjects, texture);
       if (!texObj) {
         _mesa_error(ctx, GL_INVALID_VALUE,
@@ -1012,13 +1186,12 @@ _mesa_FramebufferTexture1DEXT(GLenum target, GLenum attachment,
                     "glFramebufferTexture1DEXT(texture target)");
         return;
       }
-      set_texture_attachment(ctx, att, texObj, textarget, level, 0);
    }
    else {
-      remove_attachment(ctx, att);
+      /* remove texture attachment */
+      texObj = NULL;
    }
-
-   /* XXX call a driver function to signal new attachment? */
+   ctx->Driver.RenderbufferTexture(ctx, att, texObj, textarget, level, 0);
 }
 
 
@@ -1027,6 +1200,7 @@ _mesa_FramebufferTexture2DEXT(GLenum target, GLenum attachment,
                               GLenum textarget, GLuint texture, GLint level)
 {
    struct gl_renderbuffer_attachment *att;
+   struct gl_texture_object *texObj;
    GET_CURRENT_CONTEXT(ctx);
 
    ASSERT_OUTSIDE_BEGIN_END(ctx);
@@ -1039,15 +1213,17 @@ _mesa_FramebufferTexture2DEXT(GLenum target, GLenum attachment,
          textarget == GL_TEXTURE_RECTANGLE_ARB ||
          IS_CUBE_FACE(textarget));
 
-   att = get_attachment(ctx, ctx->CurrentFramebuffer, attachment);
+   att = _mesa_get_attachment(ctx, ctx->DrawBuffer, attachment);
    if (att == NULL) {
       _mesa_error(ctx, GL_INVALID_ENUM,
                  "glFramebufferTexture2DEXT(attachment)");
       return;
    }
 
+   FLUSH_VERTICES(ctx, _NEW_BUFFERS);
+
    if (texture) {
-      struct gl_texture_object *texObj = (struct gl_texture_object *)
+      texObj = (struct gl_texture_object *)
         _mesa_HashLookup(ctx->Shared->TexObjects, texture);
       if (!texObj) {
         _mesa_error(ctx, GL_INVALID_VALUE,
@@ -1063,12 +1239,12 @@ _mesa_FramebufferTexture2DEXT(GLenum target, GLenum attachment,
                     "glFramebufferTexture2DEXT(texture target)");
         return;
       }
-      set_texture_attachment(ctx, att, texObj, textarget, level, 0);
    }
    else {
-      remove_attachment(ctx, att);
+      /* remove texture attachment */
+      texObj = NULL;
    }
-
+   ctx->Driver.RenderbufferTexture(ctx, att, texObj, textarget, level, 0);
 }
 
 
@@ -1078,6 +1254,7 @@ _mesa_FramebufferTexture3DEXT(GLenum target, GLenum attachment,
                               GLint level, GLint zoffset)
 {
    struct gl_renderbuffer_attachment *att;
+   struct gl_texture_object *texObj;
    GET_CURRENT_CONTEXT(ctx);
 
    ASSERT_OUTSIDE_BEGIN_END(ctx);
@@ -1088,16 +1265,18 @@ _mesa_FramebufferTexture3DEXT(GLenum target, GLenum attachment,
 
    ASSERT(textarget == GL_TEXTURE_3D);
 
-   att = get_attachment(ctx, ctx->CurrentFramebuffer, attachment);
+   att = _mesa_get_attachment(ctx, ctx->DrawBuffer, attachment);
    if (att == NULL) {
       _mesa_error(ctx, GL_INVALID_ENUM,
                  "glFramebufferTexture1DEXT(attachment)");
       return;
    }
 
+   FLUSH_VERTICES(ctx, _NEW_BUFFERS);
+
    if (texture) {
       const GLint maxSize = 1 << (ctx->Const.Max3DTextureLevels - 1);
-      struct gl_texture_object *texObj = (struct gl_texture_object *)
+      texObj = (struct gl_texture_object *)
         _mesa_HashLookup(ctx->Shared->TexObjects, texture);
       if (!texObj) {
         _mesa_error(ctx, GL_INVALID_VALUE,
@@ -1114,11 +1293,13 @@ _mesa_FramebufferTexture3DEXT(GLenum target, GLenum attachment,
                     "glFramebufferTexture3DEXT(zoffset)");
         return;
       }
-      set_texture_attachment(ctx, att, texObj, textarget, level, zoffset);
    }
    else {
-      remove_attachment(ctx, att);
+      /* remove texture attachment */
+      texObj = NULL;
    }
+   ctx->Driver.RenderbufferTexture(ctx, att, texObj, textarget,
+                                   level, zoffset);
 }
 
 
@@ -1128,28 +1309,53 @@ _mesa_FramebufferRenderbufferEXT(GLenum target, GLenum attachment,
                                  GLuint renderbuffer)
 {
    struct gl_renderbuffer_attachment *att;
+   struct gl_framebuffer *fb;
+   struct gl_renderbuffer *rb;
    GET_CURRENT_CONTEXT(ctx);
 
    ASSERT_OUTSIDE_BEGIN_END(ctx);
 
-   if (target != GL_FRAMEBUFFER_EXT) {
+   switch (target) {
+#if FEATURE_EXT_framebuffer_blit
+   case GL_DRAW_FRAMEBUFFER_EXT:
+      if (!ctx->Extensions.EXT_framebuffer_blit) {
+         _mesa_error(ctx, GL_INVALID_ENUM,
+                     "glFramebufferRenderbufferEXT(target)");
+         return;
+      }
+      fb = ctx->DrawBuffer;
+      break;
+   case GL_READ_FRAMEBUFFER_EXT:
+      if (!ctx->Extensions.EXT_framebuffer_blit) {
+         _mesa_error(ctx, GL_INVALID_ENUM,
+                     "glFramebufferRenderbufferEXT(target)");
+         return;
+      }
+      fb = ctx->ReadBuffer;
+      break;
+#endif
+   case GL_FRAMEBUFFER_EXT:
+      fb = ctx->DrawBuffer;
+      break;
+   default:
       _mesa_error(ctx, GL_INVALID_ENUM,
                   "glFramebufferRenderbufferEXT(target)");
       return;
    }
 
    if (renderbufferTarget != GL_RENDERBUFFER_EXT) {
-         _mesa_error(ctx, GL_INVALID_ENUM,
-                 "glFramebufferRenderbufferEXT(renderbufferTarget)");
+      _mesa_error(ctx, GL_INVALID_ENUM,
+                  "glFramebufferRenderbufferEXT(renderbufferTarget)");
       return;
    }
 
-   if (ctx->CurrentFramebuffer == NULL) {
+   if (fb->Name == 0) {
+      /* Can't attach new renderbuffers to a window system framebuffer */
       _mesa_error(ctx, GL_INVALID_OPERATION, "glFramebufferRenderbufferEXT");
       return;
    }
 
-   att = get_attachment(ctx, ctx->CurrentFramebuffer, attachment);
+   att = _mesa_get_attachment(ctx, fb, attachment);
    if (att == NULL) {
       _mesa_error(ctx, GL_INVALID_ENUM,
                  "glFramebufferRenderbufferEXT(attachment)");
@@ -1157,18 +1363,22 @@ _mesa_FramebufferRenderbufferEXT(GLenum target, GLenum attachment,
    }
 
    if (renderbuffer) {
-      struct gl_renderbuffer *rb;
       rb = lookup_renderbuffer(ctx, renderbuffer);
       if (!rb) {
-        _mesa_error(ctx, GL_INVALID_VALUE,
+        _mesa_error(ctx, GL_INVALID_OPERATION,
                     "glFramebufferRenderbufferEXT(renderbuffer)");
         return;
       }
-      set_renderbuffer_attachment(ctx, att, rb);
    }
    else {
-      remove_attachment(ctx, att);
+      /* remove renderbuffer attachment */
+      rb = NULL;
    }
+
+   FLUSH_VERTICES(ctx, _NEW_BUFFERS);
+
+   assert(ctx->Driver.FramebufferRenderbuffer);
+   ctx->Driver.FramebufferRenderbuffer(ctx, fb, attachment, rb);
 }
 
 
@@ -1177,29 +1387,54 @@ _mesa_GetFramebufferAttachmentParameterivEXT(GLenum target, GLenum attachment,
                                              GLenum pname, GLint *params)
 {
    const struct gl_renderbuffer_attachment *att;
+   struct gl_framebuffer *buffer;
    GET_CURRENT_CONTEXT(ctx);
 
    ASSERT_OUTSIDE_BEGIN_END(ctx);
 
-   if (target != GL_FRAMEBUFFER_EXT) {
+   switch (target) {
+#if FEATURE_EXT_framebuffer_blit
+   case GL_DRAW_FRAMEBUFFER_EXT:
+      if (!ctx->Extensions.EXT_framebuffer_blit) {
+         _mesa_error(ctx, GL_INVALID_ENUM,
+                     "glGetFramebufferAttachmentParameterivEXT(target)");
+         return;
+      }
+      buffer = ctx->DrawBuffer;
+      break;
+   case GL_READ_FRAMEBUFFER_EXT:
+      if (!ctx->Extensions.EXT_framebuffer_blit) {
+         _mesa_error(ctx, GL_INVALID_ENUM,
+                     "glGetFramebufferAttachmentParameterivEXT(target)");
+         return;
+      }
+      buffer = ctx->ReadBuffer;
+      break;
+#endif
+   case GL_FRAMEBUFFER_EXT:
+      buffer = ctx->DrawBuffer;
+      break;
+   default:
       _mesa_error(ctx, GL_INVALID_ENUM,
                   "glGetFramebufferAttachmentParameterivEXT(target)");
       return;
    }
 
-   if (ctx->CurrentFramebuffer == NULL) {
+   if (buffer->Name == 0) {
       _mesa_error(ctx, GL_INVALID_OPERATION,
                   "glGetFramebufferAttachmentParameterivEXT");
       return;
    }
 
-   att = get_attachment(ctx, ctx->CurrentFramebuffer, attachment);
+   att = _mesa_get_attachment(ctx, buffer, attachment);
    if (att == NULL) {
       _mesa_error(ctx, GL_INVALID_ENUM,
                   "glGetFramebufferAttachmentParameterivEXT(attachment)");
       return;
    }
 
+   FLUSH_VERTICES(ctx, _NEW_BUFFERS);
+
    switch (pname) {
    case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE_EXT:
       *params = att->Type;
@@ -1259,12 +1494,14 @@ _mesa_GenerateMipmapEXT(GLenum target)
    GET_CURRENT_CONTEXT(ctx);
 
    ASSERT_OUTSIDE_BEGIN_END(ctx);
+   FLUSH_VERTICES(ctx, _NEW_BUFFERS);
 
    switch (target) {
    case GL_TEXTURE_1D:
    case GL_TEXTURE_2D:
    case GL_TEXTURE_3D:
    case GL_TEXTURE_CUBE_MAP:
+      /* OK, legal value */
       break;
    default:
       _mesa_error(ctx, GL_INVALID_ENUM, "glGenerateMipmapEXT(target)");
@@ -1277,3 +1514,51 @@ _mesa_GenerateMipmapEXT(GLenum target)
    /* XXX this might not handle cube maps correctly */
    _mesa_generate_mipmap(ctx, target, texUnit, texObj);
 }
+
+
+#if FEATURE_EXT_framebuffer_blit
+void GLAPIENTRY
+_mesa_BlitFramebufferEXT(GLint srcX0, GLint srcY0, GLint srcX1, GLint srcY1,
+                         GLint dstX0, GLint dstY0, GLint dstX1, GLint dstY1,
+                         GLbitfield mask, GLenum filter)
+{
+   GET_CURRENT_CONTEXT(ctx);
+
+   ASSERT_OUTSIDE_BEGIN_END(ctx);
+   FLUSH_VERTICES(ctx, _NEW_BUFFERS);
+
+   /* check for complete framebuffers */
+   if (ctx->DrawBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT ||
+       ctx->ReadBuffer->_Status != GL_FRAMEBUFFER_COMPLETE_EXT) {
+      _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION_EXT,
+                  "glBlitFramebufferEXT(incomplete draw/read buffers)");
+      return;
+   }
+
+   /* depth/stencil must be blitted with nearest filtering */
+   if ((mask & (GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT))
+        && filter != GL_NEAREST) {
+      _mesa_error(ctx, GL_INVALID_OPERATION,
+             "glBlitFramebufferEXT(depth/stencil requires GL_NEAREST filter");
+      return;
+   }
+
+   if (mask & ~(GL_COLOR_BUFFER_BIT |
+                GL_DEPTH_BUFFER_BIT |
+                GL_STENCIL_BUFFER_BIT)) {
+      _mesa_error( ctx, GL_INVALID_VALUE, "glBlitFramebufferEXT(mask)");
+      return;
+   }
+
+   if (!ctx->Extensions.EXT_framebuffer_blit) {
+      _mesa_error(ctx, GL_INVALID_OPERATION, "glBlitFramebufferEXT");
+      return;
+   }
+
+   ASSERT(ctx->Driver.BlitFramebuffer);
+   ctx->Driver.BlitFramebuffer(ctx,
+                               srcX0, srcY0, srcX1, srcY1,
+                               dstX0, dstY0, dstX1, dstY1,
+                               mask, filter);
+}
+#endif /* FEATURE_EXT_framebuffer_blit */