add FreeTexImageData hook to help single-copy texturing in drivers
[mesa.git] / src / mesa / main / fbobject.c
index 1b605e7a836d02b91bc5f39e99721bebdb11151d..3480beaa47d5f1ada35341cf282b2fe908256245 100644 (file)
 #include "texstore.h"
 
 
+/**
+ * Notes:
+ *
+ * None of the GL_EXT_framebuffer_object functions are compiled into
+ * display lists.
+ */
+
+
+
+/*
+ * When glGenRender/FramebuffersEXT() is called we insert pointers to
+ * these placeholder objects into the hash table.
+ * Later, when the object ID is first bound, we replace the placeholder
+ * with the real frame/renderbuffer.
+ */
+static struct gl_framebuffer DummyFramebuffer;
+static struct gl_renderbuffer DummyRenderbuffer;
+
+
 #define IS_CUBE_FACE(TARGET) \
    ((TARGET) >= GL_TEXTURE_CUBE_MAP_POSITIVE_X && \
     (TARGET) <= GL_TEXTURE_CUBE_MAP_NEGATIVE_Z)
 
 
 /**
- * Helper routine for getting a gl_render_buffer_object.
+ * Helper routine for getting a gl_renderbuffer.
  */
-static struct gl_render_buffer_object *
+static struct gl_renderbuffer *
 lookup_renderbuffer(GLcontext *ctx, GLuint id)
 {
-   struct gl_render_buffer_object *rb;
+   struct gl_renderbuffer *rb;
 
-   if (!id == 0)
+   if (id == 0)
       return NULL;
 
-   rb = (struct gl_render_buffer_object *)
+   rb = (struct gl_renderbuffer *)
       _mesa_HashLookup(ctx->Shared->RenderBuffers, id);
    return rb;
 }
 
 
 /**
- * Helper routine for getting a gl_frame_buffer_object.
+ * Helper routine for getting a gl_framebuffer.
  */
-static struct gl_frame_buffer_object *
+static struct gl_framebuffer *
 lookup_framebuffer(GLcontext *ctx, GLuint id)
 {
-   struct gl_frame_buffer_object *fb;
+   struct gl_framebuffer *fb;
 
-   if (!id == 0)
+   if (id == 0)
       return NULL;
 
-   fb = (struct gl_frame_buffer_object *)
+   fb = (struct gl_framebuffer *)
       _mesa_HashLookup(ctx->Shared->FrameBuffers, id);
    return fb;
 }
 
 
 /**
- * Allocate a new gl_render_buffer_object.
- * XXX make this a device driver function.
+ * 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().
  */
-static struct gl_render_buffer_object *
-new_renderbuffer(GLcontext *ctx, GLuint name)
+void
+_mesa_delete_framebuffer(GLcontext *ctx, struct gl_framebuffer *fb)
 {
-   struct gl_render_buffer_object *rb = CALLOC_STRUCT(gl_render_buffer_object);
+   (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;
@@ -93,23 +177,26 @@ new_renderbuffer(GLcontext *ctx, GLuint name)
 
 
 /**
- * Allocate a new gl_frame_buffer_object.
- * XXX make this a device driver function.
+ * Delete a gl_framebuffer.
+ * This is the default function for framebuffer->Delete().
  */
-static struct gl_frame_buffer_object *
-new_framebuffer(GLcontext *ctx, GLuint name)
+void
+_mesa_delete_renderbuffer(GLcontext *ctx, struct gl_renderbuffer *rb)
 {
-   struct gl_frame_buffer_object *fb = CALLOC_STRUCT(gl_frame_buffer_object);
-   if (fb) {
-      fb->Name = name;
-      fb->RefCount = 1;
+   (void) ctx;
+   if (rb->Data) {
+      _mesa_free(rb->Data);
    }
-   return fb;
+   _mesa_free(rb);
 }
 
 
-static struct gl_render_buffer_attachment *
-get_attachment(GLcontext *ctx, GLenum attachment)
+/**
+ * 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)
 {
    GLuint i;
 
@@ -134,19 +221,23 @@ get_attachment(GLcontext *ctx, GLenum attachment)
       if (i >= ctx->Const.MaxColorAttachments) {
         return NULL;
       }
-      return &ctx->CurrentFramebuffer->ColorAttachment[i];
+      return &fb->ColorAttachment[i];
    case GL_DEPTH_ATTACHMENT_EXT:
-      return &ctx->CurrentFramebuffer->DepthAttachment;
+      return &fb->DepthAttachment;
    case GL_STENCIL_ATTACHMENT_EXT:
-      return &ctx->CurrentFramebuffer->StencilAttachment;
+      return &fb->StencilAttachment;
    default:
       return NULL;
    }
 }
 
 
+/**
+ * Remove any texture or renderbuffer attached to the given attachment
+ * point.  Update reference counts, etc.
+ */
 static void
-remove_attachment(GLcontext *ctx, struct gl_render_buffer_attachment *att)
+remove_attachment(GLcontext *ctx, struct gl_renderbuffer_attachment *att)
 {
    if (att->Type == GL_TEXTURE) {
       ASSERT(att->Texture);
@@ -162,17 +253,22 @@ remove_attachment(GLcontext *ctx, struct gl_render_buffer_attachment *att)
       ASSERT(!att->Texture);
       att->Renderbuffer->RefCount--;
       if (att->Renderbuffer->RefCount == 0) {
-        _mesa_free(att->Renderbuffer); /* XXX driver free */
+         att->Renderbuffer->Delete(ctx, att->Renderbuffer);
       }
       att->Renderbuffer = NULL;
    }
    att->Type = GL_NONE;
+   att->Complete = GL_TRUE;
 }
 
 
+/**
+ * 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_render_buffer_attachment *att,
+                    struct gl_renderbuffer_attachment *att,
                     struct gl_texture_object *texObj,
                     GLenum texTarget, GLuint level, GLuint zoffset)
 {
@@ -187,26 +283,242 @@ set_texture_attachment(GLcontext *ctx,
       att->CubeMapFace = 0;
    }
    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.
+    */
 }
 
 
+/**
+ * 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_render_buffer_attachment *att,
-                         struct gl_render_buffer_object *rb)
+                            struct gl_renderbuffer_attachment *att,
+                            struct gl_renderbuffer *rb)
 {
    remove_attachment(ctx, att);
    att->Type = GL_RENDERBUFFER_EXT;
    att->Renderbuffer = rb;
+   att->Texture = NULL; /* just to be safe */
+   att->Complete = GL_FALSE;
    rb->RefCount++;
 }
 
 
-GLboolean
+/**
+ * Test if an attachment point is complete and update its Complete field.
+ * \param format if GL_COLOR, this is a color attachment point,
+ *               if GL_DEPTH, this is a depth component attachment point,
+ *               if GL_STENCIL, this is a stencil component attachment point.
+ */
+static void
+test_attachment_completeness(const GLcontext *ctx, GLenum format,
+                             struct gl_renderbuffer_attachment *att)
+{
+   assert(format == GL_COLOR || format == GL_DEPTH || format == GL_STENCIL);
+
+   /* 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;
+      struct gl_texture_image *texImage;
+
+      assert(texObj);
+
+      texImage = texObj->Image[att->CubeMapFace][att->TextureLevel];
+      if (!texImage) {
+         att->Complete = GL_FALSE;
+         return;
+      }
+      if (texImage->Width < 1 || texImage->Height < 1) {
+         att->Complete = GL_FALSE;
+         return;
+      }
+      if (texObj->Target == GL_TEXTURE_3D && att->Zoffset >= texImage->Depth) {
+         att->Complete = GL_FALSE;
+         return;
+      }
+
+      if (format == GL_COLOR) {
+         if (texImage->Format != GL_RGB && texImage->Format != GL_RGBA) {
+            att->Complete = GL_FALSE;
+            return;
+         }
+      }
+      else if (format == GL_DEPTH) {
+         if (texImage->Format != GL_DEPTH_COMPONENT) {
+            att->Complete = GL_FALSE;
+            return;
+         }
+      }
+      else {
+         /* no such thing as stencil textures */
+         att->Complete = GL_FALSE;
+         return;
+      }
+   }
+   else {
+      assert(att->Type == GL_RENDERBUFFER_EXT);
+
+      if (att->Renderbuffer->Width < 1 || att->Renderbuffer->Height < 1) {
+         att->Complete = GL_FALSE;
+         return;
+      }
+      if (format == GL_COLOR) {
+         if (att->Renderbuffer->_BaseFormat != GL_RGB &&
+             att->Renderbuffer->_BaseFormat != GL_RGBA) {
+            att->Complete = GL_FALSE;
+            return;
+         }
+      }
+      else if (format == GL_DEPTH) {
+         if (att->Renderbuffer->_BaseFormat != GL_DEPTH_COMPONENT) {
+            att->Complete = GL_FALSE;
+            return;
+         }
+      }
+      else {
+         assert(format == GL_STENCIL);
+         if (att->Renderbuffer->_BaseFormat != GL_STENCIL_INDEX) {
+            att->Complete = GL_FALSE;
+            return;
+         }
+      }
+   }
+}
+
+
+/**
+ * Test if the given framebuffer object is complete and update its
+ * Status field with the results.
+ */
+static void
+test_framebuffer_completeness(GLcontext *ctx,
+                              struct gl_framebuffer *fb)
+{
+   GLint i;
+   GLuint numImages, width = 0, height = 0;
+   GLenum intFormat = GL_NONE;
+
+   /* Set to COMPLETE status, then try to find reasons for being incomplete */
+   fb->Status = GL_FRAMEBUFFER_COMPLETE_EXT;
+
+   numImages = 0;
+
+   /* Start at -2 to more easily loop over all attachment points */
+   for (i = -2; i < ctx->Const.MaxColorAttachments; i++) {
+      struct gl_renderbuffer_attachment *att;
+      GLuint w, h;
+      GLenum f;
+
+      if (i == -2) {
+         att = &fb->DepthAttachment;
+         test_attachment_completeness(ctx, GL_DEPTH, att);
+         if (!att->Complete) {
+            fb->Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
+            return;
+         }
+      }
+      else if (i == -1) {
+         att = &fb->StencilAttachment;
+         test_attachment_completeness(ctx, GL_STENCIL, att);
+         if (!att->Complete) {
+            fb->Status = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT;
+            return;
+         }
+      }
+      else {
+         att = &fb->ColorAttachment[i];
+         test_attachment_completeness(ctx, GL_COLOR, att);
+         if (!att->Complete) {
+            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;
+         numImages++;
+      }
+      else if (att->Type == GL_RENDERBUFFER_EXT) {
+         w = att->Renderbuffer->Width;
+         h = att->Renderbuffer->Height;
+         f = att->Renderbuffer->InternalFormat;
+         numImages++;
+      }
+      else {
+         assert(att->Type == GL_NONE);
+         continue;
+      }
+
+      if (numImages == 1) {
+         /* set required width, height and format */
+         width = w;
+         height = h;
+         if (i >= 0)
+            intFormat = f;
+      }
+      else {
+         /* check that width, height, format are same */
+         if (w != width || h != height) {
+            fb->Status = GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT;
+            return;
+         }
+         if (i >= 0 && 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 (att->Type == GL_NONE) {
+            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 (att->Type == GL_NONE) {
+         fb->Status = GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT;
+         return;
+      }
+   }
+
+   if (numImages == 0) {
+      fb->Status = GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT;
+      return;
+   }
+}
+
+
+
+GLboolean GLAPIENTRY
 _mesa_IsRenderbufferEXT(GLuint renderbuffer)
 {
-   struct gl_render_buffer_object *rb;
+   const struct gl_renderbuffer *rb;
    GET_CURRENT_CONTEXT(ctx);
 
    ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
@@ -216,10 +528,10 @@ _mesa_IsRenderbufferEXT(GLuint renderbuffer)
 }
 
 
-void
+void GLAPIENTRY
 _mesa_BindRenderbufferEXT(GLenum target, GLuint renderbuffer)
 {
-   struct gl_render_buffer_object *newRb, *oldRb;
+   struct gl_renderbuffer *newRb, *oldRb;
    GET_CURRENT_CONTEXT(ctx);
 
    ASSERT_OUTSIDE_BEGIN_END(ctx);
@@ -232,13 +544,19 @@ _mesa_BindRenderbufferEXT(GLenum target, GLuint renderbuffer)
 
    if (renderbuffer) {
       newRb = lookup_renderbuffer(ctx, renderbuffer);
+      if (newRb == &DummyRenderbuffer) {
+         /* ID was reserved, but no real renderbuffer object made yet */
+         newRb = NULL;
+      }
       if (!newRb) {
         /* create new renderbuffer object */
-        newRb = new_renderbuffer(ctx, renderbuffer);
+        newRb = ctx->Driver.NewRenderbuffer(ctx, renderbuffer);
         if (!newRb) {
            _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindRenderbufferEXT");
            return;
         }
+         ASSERT(newRb->AllocStorage);
+         _mesa_HashInsert(ctx->Shared->RenderBuffers, renderbuffer, newRb);
       }
       newRb->RefCount++;
    }
@@ -250,15 +568,17 @@ _mesa_BindRenderbufferEXT(GLenum target, GLuint renderbuffer)
    if (oldRb) {
       oldRb->RefCount--;
       if (oldRb->RefCount == 0) {
-        _mesa_free(oldRb);  /* XXX device driver function */
+         oldRb->Delete(ctx, oldRb);
       }
    }
 
+   ASSERT(newRb != &DummyRenderbuffer);
+
    ctx->CurrentRenderbuffer = newRb;
 }
 
 
-void
+void GLAPIENTRY
 _mesa_DeleteRenderbuffersEXT(GLsizei n, const GLuint *renderbuffers)
 {
    GLint i;
@@ -267,19 +587,21 @@ _mesa_DeleteRenderbuffersEXT(GLsizei n, const GLuint *renderbuffers)
    ASSERT_OUTSIDE_BEGIN_END(ctx);
 
    for (i = 0; i < n; i++) {
-      if (renderbuffers[i]) {
-        struct gl_render_buffer_object *rb;
+      if (renderbuffers[i] > 0) {
+        struct gl_renderbuffer *rb;
         rb = lookup_renderbuffer(ctx, renderbuffers[i]);
         if (rb) {
            /* remove from hash table immediately, to free the ID */
            _mesa_HashRemove(ctx->Shared->RenderBuffers, renderbuffers[i]);
 
-           /* But the object will not be freed until it's no longer bound in
-            * any context.
-            */
-           rb->RefCount--;
-           if (rb->RefCount == 0) {
-              _mesa_free(rb); /* XXX call device driver function */
+            if (rb != &DummyRenderbuffer) {
+               /* But the object will not be freed until it's no longer
+                * bound in any context.
+                */
+               rb->RefCount--;
+               if (rb->RefCount == 0) {
+                  rb->Delete(ctx, rb);
+               }
            }
         }
       }
@@ -287,7 +609,7 @@ _mesa_DeleteRenderbuffersEXT(GLsizei n, const GLuint *renderbuffers)
 }
 
 
-void
+void GLAPIENTRY
 _mesa_GenRenderbuffersEXT(GLsizei n, GLuint *renderbuffers)
 {
    GET_CURRENT_CONTEXT(ctx);
@@ -307,101 +629,67 @@ _mesa_GenRenderbuffersEXT(GLsizei n, GLuint *renderbuffers)
    first = _mesa_HashFindFreeKeyBlock(ctx->Shared->RenderBuffers, n);
 
    for (i = 0; i < n; i++) {
-      struct gl_render_buffer_object *rb;
       GLuint name = first + i;
-      rb = new_renderbuffer(ctx, name);
-      if (!rb) {
-         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGenRenderbuffersEXT");
-         return;
-      }
-
-      /* insert into hash table */
+      renderbuffers[i] = name;
+      /* insert dummy placeholder into hash table */
       _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
-      _mesa_HashInsert(ctx->Shared->RenderBuffers, name, rb);
+      _mesa_HashInsert(ctx->Shared->RenderBuffers, name, &DummyRenderbuffer);
       _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
-
-      renderbuffers[i] = name;
    }
 }
 
 
+/**
+ * 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.
+ */
 static GLenum
 base_internal_format(GLcontext *ctx, GLenum internalFormat)
 {
    switch (internalFormat) {
-      /*case GL_ALPHA:*/
-      case GL_ALPHA4:
-      case GL_ALPHA8:
-      case GL_ALPHA12:
-      case GL_ALPHA16:
-         return GL_ALPHA;
-      /*case GL_LUMINANCE:*/
-      case GL_LUMINANCE4:
-      case GL_LUMINANCE8:
-      case GL_LUMINANCE12:
-      case GL_LUMINANCE16:
-         return GL_LUMINANCE;
-      /*case GL_LUMINANCE_ALPHA:*/
-      case GL_LUMINANCE4_ALPHA4:
-      case GL_LUMINANCE6_ALPHA2:
-      case GL_LUMINANCE8_ALPHA8:
-      case GL_LUMINANCE12_ALPHA4:
-      case GL_LUMINANCE12_ALPHA12:
-      case GL_LUMINANCE16_ALPHA16:
-         return GL_LUMINANCE_ALPHA;
-      /*case GL_INTENSITY:*/
-      case GL_INTENSITY4:
-      case GL_INTENSITY8:
-      case GL_INTENSITY12:
-      case GL_INTENSITY16:
-         return GL_INTENSITY;
-      /*case GL_RGB:*/
-      case GL_R3_G3_B2:
-      case GL_RGB4:
-      case GL_RGB5:
-      case GL_RGB8:
-      case GL_RGB10:
-      case GL_RGB12:
-      case GL_RGB16:
-         return GL_RGB;
-      /*case GL_RGBA:*/
-      case GL_RGBA2:
-      case GL_RGBA4:
-      case GL_RGB5_A1:
-      case GL_RGBA8:
-      case GL_RGB10_A2:
-      case GL_RGBA12:
-      case GL_RGBA16:
-         return GL_RGBA;
-      case GL_STENCIL_INDEX1_EXT:
-      case GL_STENCIL_INDEX4_EXT:
-      case GL_STENCIL_INDEX8_EXT:
-      case GL_STENCIL_INDEX16_EXT:
-         return GL_STENCIL_INDEX;
-      default:
-         ; /* fallthrough */
-   }
-
-   if (ctx->Extensions.SGIX_depth_texture) {
-      switch (internalFormat) {
-         /*case GL_DEPTH_COMPONENT:*/
-         case GL_DEPTH_COMPONENT16_SGIX:
-         case GL_DEPTH_COMPONENT24_SGIX:
-         case GL_DEPTH_COMPONENT32_SGIX:
-            return GL_DEPTH_COMPONENT;
-         default:
-            ; /* fallthrough */
-      }
-   }
-
-   return 0;
+   case GL_RGB:
+   case GL_R3_G3_B2:
+   case GL_RGB4:
+   case GL_RGB5:
+   case GL_RGB8:
+   case GL_RGB10:
+   case GL_RGB12:
+   case GL_RGB16:
+      return GL_RGB;
+   case GL_RGBA:
+   case GL_RGBA2:
+   case GL_RGBA4:
+   case GL_RGB5_A1:
+   case GL_RGBA8:
+   case GL_RGB10_A2:
+   case GL_RGBA12:
+   case GL_RGBA16:
+      return GL_RGBA;
+   case GL_STENCIL_INDEX:
+   case GL_STENCIL_INDEX1_EXT:
+   case GL_STENCIL_INDEX4_EXT:
+   case GL_STENCIL_INDEX8_EXT:
+   case GL_STENCIL_INDEX16_EXT:
+      return GL_STENCIL_INDEX;
+   case GL_DEPTH_COMPONENT:
+   case GL_DEPTH_COMPONENT16:
+   case GL_DEPTH_COMPONENT24:
+   case GL_DEPTH_COMPONENT32:
+      return GL_DEPTH_COMPONENT;
+   /* XXX add floating point formats eventually */
+   default:
+      return 0;
+   }
 }
 
 
-void
+void GLAPIENTRY
 _mesa_RenderbufferStorageEXT(GLenum target, GLenum internalFormat,
                              GLsizei width, GLsizei height)
 {
+   struct gl_renderbuffer *rb;
    GLenum baseFormat;
    GET_CURRENT_CONTEXT(ctx);
 
@@ -429,30 +717,37 @@ _mesa_RenderbufferStorageEXT(GLenum target, GLenum internalFormat,
       return;
    }
 
-   /* XXX this check isn't in the spec, but seems necessary */
-   if (!ctx->CurrentRenderbuffer) {
+   rb = ctx->CurrentRenderbuffer;
+
+   if (!rb) {
       _mesa_error(ctx, GL_INVALID_OPERATION, "glRenderbufferStorageEXT");
       return;
    }
 
-   if (ctx->CurrentRenderbuffer->Data) {
-      /* XXX device driver free */
-      _mesa_free(ctx->CurrentRenderbuffer->Data);
+   /* Now allocate the storage */
+   ASSERT(rb->AllocStorage);
+   if (rb->AllocStorage(ctx, rb, internalFormat, width, height)) {
+      /* No error - check/set fields now */
+      assert(rb->Width == width);
+      assert(rb->Height == height);
+      assert(rb->InternalFormat);
+      rb->_BaseFormat = baseFormat;
    }
-
-   /* XXX device driver allocate, fix size */
-   ctx->CurrentRenderbuffer->Data = _mesa_malloc(width * height * 4);
-   if (ctx->CurrentRenderbuffer->Data == NULL) {
-      _mesa_error(ctx, GL_OUT_OF_MEMORY, "glRenderbufferStorageEXT");
-      return;
+   else {
+      /* Probably ran out of memory - clear the fields */
+      rb->Width = 0;
+      rb->Height = 0;
+      rb->InternalFormat = GL_NONE;
+      rb->_BaseFormat = GL_NONE;
    }
-   ctx->CurrentRenderbuffer->InternalFormat = internalFormat;
-   ctx->CurrentRenderbuffer->Width = width;
-   ctx->CurrentRenderbuffer->Height = height;
+
+   /* XXX if this renderbuffer is attached anywhere, invalidate attachment
+    * points???
+    */
 }
 
 
-void
+void GLAPIENTRY
 _mesa_GetRenderbufferParameterivEXT(GLenum target, GLenum pname, GLint *params)
 {
    GET_CURRENT_CONTEXT(ctx);
@@ -489,10 +784,10 @@ _mesa_GetRenderbufferParameterivEXT(GLenum target, GLenum pname, GLint *params)
 }
 
 
-GLboolean
+GLboolean GLAPIENTRY
 _mesa_IsFramebufferEXT(GLuint framebuffer)
 {
-   struct gl_frame_buffer_object *fb;
+   const struct gl_framebuffer *fb;
    GET_CURRENT_CONTEXT(ctx);
 
    ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FALSE);
@@ -502,10 +797,10 @@ _mesa_IsFramebufferEXT(GLuint framebuffer)
 }
 
 
-void
+void GLAPIENTRY
 _mesa_BindFramebufferEXT(GLenum target, GLuint framebuffer)
 {
-   struct gl_frame_buffer_object *newFb, *oldFb;
+   struct gl_framebuffer *newFb, *oldFb;
    GET_CURRENT_CONTEXT(ctx);
 
    ASSERT_OUTSIDE_BEGIN_END(ctx);
@@ -518,13 +813,18 @@ _mesa_BindFramebufferEXT(GLenum target, GLuint framebuffer)
 
    if (framebuffer) {
       newFb = lookup_framebuffer(ctx, framebuffer);
+      if (newFb == &DummyFramebuffer) {
+         /* ID was reserved, but no real framebuffer object made yet */
+         newFb = NULL;
+      }
       if (!newFb) {
         /* create new framebuffer object */
-        newFb = new_framebuffer(ctx, framebuffer);
+        newFb = ctx->Driver.NewFramebuffer(ctx, framebuffer);
         if (!newFb) {
            _mesa_error(ctx, GL_OUT_OF_MEMORY, "glBindFramebufferEXT");
            return;
         }
+         _mesa_HashInsert(ctx->Shared->FrameBuffers, framebuffer, newFb);
       }
       newFb->RefCount++;
    }
@@ -536,15 +836,17 @@ _mesa_BindFramebufferEXT(GLenum target, GLuint framebuffer)
    if (oldFb) {
       oldFb->RefCount--;
       if (oldFb->RefCount == 0) {
-        _mesa_free(oldFb);  /* XXX device driver function */
+         oldFb->Delete(ctx, oldFb);
       }
    }
 
+   ASSERT(newFb != &DummyFramebuffer);
+
    ctx->CurrentFramebuffer = newFb;
 }
 
 
-void
+void GLAPIENTRY
 _mesa_DeleteFramebuffersEXT(GLsizei n, const GLuint *framebuffers)
 {
    GLint i;
@@ -553,19 +855,21 @@ _mesa_DeleteFramebuffersEXT(GLsizei n, const GLuint *framebuffers)
    ASSERT_OUTSIDE_BEGIN_END(ctx);
 
    for (i = 0; i < n; i++) {
-      if (framebuffers[i]) {
-        struct gl_frame_buffer_object *fb;
+      if (framebuffers[i] > 0) {
+        struct gl_framebuffer *fb;
         fb = lookup_framebuffer(ctx, framebuffers[i]);
         if (fb) {
            /* remove from hash table immediately, to free the ID */
            _mesa_HashRemove(ctx->Shared->FrameBuffers, framebuffers[i]);
 
-           /* But the object will not be freed until it's no longer bound in
-            * any context.
-            */
-           fb->RefCount--;
-           if (fb->RefCount == 0) {
-              _mesa_free(fb); /* XXX call device driver function */
+            if (fb != &DummyFramebuffer) {
+               /* But the object will not be freed until it's no longer
+                * bound in any context.
+                */
+               fb->RefCount--;
+               if (fb->RefCount == 0) {
+                  fb->Delete(ctx, fb);
+               }
            }
         }
       }
@@ -573,7 +877,7 @@ _mesa_DeleteFramebuffersEXT(GLsizei n, const GLuint *framebuffers)
 }
 
 
-void
+void GLAPIENTRY
 _mesa_GenFramebuffersEXT(GLsizei n, GLuint *framebuffers)
 {
    GET_CURRENT_CONTEXT(ctx);
@@ -593,26 +897,18 @@ _mesa_GenFramebuffersEXT(GLsizei n, GLuint *framebuffers)
    first = _mesa_HashFindFreeKeyBlock(ctx->Shared->FrameBuffers, n);
 
    for (i = 0; i < n; i++) {
-      struct gl_frame_buffer_object *fb;
       GLuint name = first + i;
-      fb = new_framebuffer(ctx, name);
-      if (!fb) {
-         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGenFramebuffersEXT");
-         return;
-      }
-
-      /* insert into hash table */
+      framebuffers[i] = name;
+      /* insert dummy placeholder into hash table */
       _glthread_LOCK_MUTEX(ctx->Shared->Mutex);
-      _mesa_HashInsert(ctx->Shared->FrameBuffers, name, fb);
+      _mesa_HashInsert(ctx->Shared->FrameBuffers, name, &DummyFramebuffer);
       _glthread_UNLOCK_MUTEX(ctx->Shared->Mutex);
-
-      framebuffers[i] = name;
    }
 }
 
 
 
-GLenum
+GLenum GLAPIENTRY
 _mesa_CheckFramebufferStatusEXT(GLenum target)
 {
    GET_CURRENT_CONTEXT(ctx);
@@ -620,24 +916,17 @@ _mesa_CheckFramebufferStatusEXT(GLenum target)
    ASSERT_OUTSIDE_BEGIN_END_WITH_RETVAL(ctx, GL_FRAMEBUFFER_STATUS_ERROR_EXT);
 
    if (target != GL_FRAMEBUFFER_EXT) {
-      _mesa_error(ctx, GL_INVALID_ENUM,
-                  "glCheckFramebufferStatus(target)");
+      _mesa_error(ctx, GL_INVALID_ENUM, "glCheckFramebufferStatus(target)");
       return GL_FRAMEBUFFER_STATUS_ERROR_EXT;
    }
 
-   /* return one of:
-      GL_FRAMEBUFFER_COMPLETE_EXT
-      GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT
-      GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT
-      GL_FRAMEBUFFER_INCOMPLETE_DUPLICATE_ATTACHMENT_EXT
-      GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT
-      GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT
-      GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT
-      GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT
-      GL_FRAMEBUFFER_UNSUPPORTED_EXT
-      GL_FRAMEBUFFER_STATUS_ERROR_EXT
-   */
-   return GL_FRAMEBUFFER_STATUS_ERROR_EXT;
+   if (!ctx->CurrentFramebuffer) {
+      /* The window system / default framebuffer is always complete */
+      return GL_FRAMEBUFFER_COMPLETE_EXT;
+   }
+
+   test_framebuffer_completeness(ctx, ctx->CurrentFramebuffer);
+   return ctx->CurrentFramebuffer->Status;
 }
 
 
@@ -688,11 +977,11 @@ error_check_framebuffer_texture(GLcontext *ctx, GLuint dims,
 }
 
 
-void
+void GLAPIENTRY
 _mesa_FramebufferTexture1DEXT(GLenum target, GLenum attachment,
                               GLenum textarget, GLuint texture, GLint level)
 {
-   struct gl_render_buffer_attachment *att;
+   struct gl_renderbuffer_attachment *att;
    GET_CURRENT_CONTEXT(ctx);
 
    ASSERT_OUTSIDE_BEGIN_END(ctx);
@@ -703,7 +992,7 @@ _mesa_FramebufferTexture1DEXT(GLenum target, GLenum attachment,
 
    ASSERT(textarget == GL_TEXTURE_1D);
 
-   att = get_attachment(ctx, attachment);
+   att = get_attachment(ctx, ctx->CurrentFramebuffer, attachment);
    if (att == NULL) {
       _mesa_error(ctx, GL_INVALID_ENUM,
                  "glFramebufferTexture1DEXT(attachment)");
@@ -733,11 +1022,11 @@ _mesa_FramebufferTexture1DEXT(GLenum target, GLenum attachment,
 }
 
 
-void
+void GLAPIENTRY
 _mesa_FramebufferTexture2DEXT(GLenum target, GLenum attachment,
                               GLenum textarget, GLuint texture, GLint level)
 {
-   struct gl_render_buffer_attachment *att;
+   struct gl_renderbuffer_attachment *att;
    GET_CURRENT_CONTEXT(ctx);
 
    ASSERT_OUTSIDE_BEGIN_END(ctx);
@@ -750,7 +1039,7 @@ _mesa_FramebufferTexture2DEXT(GLenum target, GLenum attachment,
          textarget == GL_TEXTURE_RECTANGLE_ARB ||
          IS_CUBE_FACE(textarget));
 
-   att = get_attachment(ctx, attachment);
+   att = get_attachment(ctx, ctx->CurrentFramebuffer, attachment);
    if (att == NULL) {
       _mesa_error(ctx, GL_INVALID_ENUM,
                  "glFramebufferTexture2DEXT(attachment)");
@@ -783,12 +1072,12 @@ _mesa_FramebufferTexture2DEXT(GLenum target, GLenum attachment,
 }
 
 
-void
+void GLAPIENTRY
 _mesa_FramebufferTexture3DEXT(GLenum target, GLenum attachment,
                               GLenum textarget, GLuint texture,
                               GLint level, GLint zoffset)
 {
-   struct gl_render_buffer_attachment *att;
+   struct gl_renderbuffer_attachment *att;
    GET_CURRENT_CONTEXT(ctx);
 
    ASSERT_OUTSIDE_BEGIN_END(ctx);
@@ -799,7 +1088,7 @@ _mesa_FramebufferTexture3DEXT(GLenum target, GLenum attachment,
 
    ASSERT(textarget == GL_TEXTURE_3D);
 
-   att = get_attachment(ctx, attachment);
+   att = get_attachment(ctx, ctx->CurrentFramebuffer, attachment);
    if (att == NULL) {
       _mesa_error(ctx, GL_INVALID_ENUM,
                  "glFramebufferTexture1DEXT(attachment)");
@@ -807,6 +1096,7 @@ _mesa_FramebufferTexture3DEXT(GLenum target, GLenum attachment,
    }
 
    if (texture) {
+      const GLint maxSize = 1 << (ctx->Const.Max3DTextureLevels - 1);
       struct gl_texture_object *texObj = (struct gl_texture_object *)
         _mesa_HashLookup(ctx->Shared->TexObjects, texture);
       if (!texObj) {
@@ -819,7 +1109,7 @@ _mesa_FramebufferTexture3DEXT(GLenum target, GLenum attachment,
                     "glFramebufferTexture3DEXT(texture target)");
         return;
       }
-      if (zoffset >= texObj->Image[0][level]->Depth) {
+      if (zoffset < 0 || zoffset >= maxSize) {
         _mesa_error(ctx, GL_INVALID_VALUE,
                     "glFramebufferTexture3DEXT(zoffset)");
         return;
@@ -832,14 +1122,12 @@ _mesa_FramebufferTexture3DEXT(GLenum target, GLenum attachment,
 }
 
 
-
-void
+void GLAPIENTRY
 _mesa_FramebufferRenderbufferEXT(GLenum target, GLenum attachment,
                                  GLenum renderbufferTarget,
                                  GLuint renderbuffer)
 {
-   struct gl_render_buffer_object *rb;
-   struct gl_render_buffer_attachment *att;
+   struct gl_renderbuffer_attachment *att;
    GET_CURRENT_CONTEXT(ctx);
 
    ASSERT_OUTSIDE_BEGIN_END(ctx);
@@ -861,36 +1149,34 @@ _mesa_FramebufferRenderbufferEXT(GLenum target, GLenum attachment,
       return;
    }
 
+   att = get_attachment(ctx, ctx->CurrentFramebuffer, attachment);
+   if (att == NULL) {
+      _mesa_error(ctx, GL_INVALID_ENUM,
+                 "glFramebufferRenderbufferEXT(attachment)");
+      return;
+   }
+
    if (renderbuffer) {
+      struct gl_renderbuffer *rb;
       rb = lookup_renderbuffer(ctx, renderbuffer);
       if (!rb) {
         _mesa_error(ctx, GL_INVALID_VALUE,
                     "glFramebufferRenderbufferEXT(renderbuffer)");
         return;
       }
+      set_renderbuffer_attachment(ctx, att, rb);
    }
    else {
-      rb = NULL; /* default renderbuffer */
-   }
-
-   att = get_attachment(ctx, attachment);
-   if (att == NULL) {
-      _mesa_error(ctx, GL_INVALID_ENUM,
-                 "glFramebufferRenderbufferEXT(attachment)");
-      return;
+      remove_attachment(ctx, att);
    }
-
-   remove_attachment(ctx, att);
-   set_renderbuffer_attachment(ctx, att, rb);
 }
 
 
-
-void
+void GLAPIENTRY
 _mesa_GetFramebufferAttachmentParameterivEXT(GLenum target, GLenum attachment,
                                              GLenum pname, GLint *params)
 {
-   const struct gl_render_buffer_attachment *att;
+   const struct gl_renderbuffer_attachment *att;
    GET_CURRENT_CONTEXT(ctx);
 
    ASSERT_OUTSIDE_BEGIN_END(ctx);
@@ -907,7 +1193,7 @@ _mesa_GetFramebufferAttachmentParameterivEXT(GLenum target, GLenum attachment,
       return;
    }
 
-   att = get_attachment(ctx, attachment);
+   att = get_attachment(ctx, ctx->CurrentFramebuffer, attachment);
    if (att == NULL) {
       _mesa_error(ctx, GL_INVALID_ENUM,
                   "glGetFramebufferAttachmentParameterivEXT(attachment)");
@@ -965,8 +1251,7 @@ _mesa_GetFramebufferAttachmentParameterivEXT(GLenum target, GLenum attachment,
 }
 
 
-
-void
+void GLAPIENTRY
 _mesa_GenerateMipmapEXT(GLenum target)
 {
    struct gl_texture_unit *texUnit;