st/mesa: fix glCopyPixels bugs/crashes when src region need clipping
[mesa.git] / src / mesa / main / renderbuffer.c
index 1db5dce4fe49bcd041a722a5f24c614c6293a785..4d7b0aff046097d947067321f729e316b16a9c4e 100644 (file)
 #include "glheader.h"
 #include "imports.h"
 #include "context.h"
+#include "fbobject.h"
+#include "formats.h"
 #include "mtypes.h"
 #include "fbobject.h"
 #include "renderbuffer.h"
 
+#include "rbadaptors.h"
+
 
 /* 32-bit color index format.  Not a public format. */
 #define COLOR_INDEX32 0x424243
@@ -70,6 +74,9 @@ get_pointer_ubyte(GLcontext *ctx, struct gl_renderbuffer *rb,
    if (!rb->Data)
       return NULL;
    ASSERT(rb->DataType == GL_UNSIGNED_BYTE);
+   /* Can't assert rb->Format since these funcs may be used for serveral
+    * different formats (GL_ALPHA8, GL_STENCIL_INDEX8, etc).
+    */
    return (GLubyte *) rb->Data + y * rb->Width + x;
 }
 
@@ -80,7 +87,7 @@ get_row_ubyte(GLcontext *ctx, struct gl_renderbuffer *rb, GLuint count,
 {
    const GLubyte *src = (const GLubyte *) rb->Data + y * rb->Width + x;
    ASSERT(rb->DataType == GL_UNSIGNED_BYTE);
-   _mesa_memcpy(values, src, count * sizeof(GLubyte));
+   memcpy(values, src, count * sizeof(GLubyte));
 }
 
 
@@ -90,7 +97,7 @@ get_values_ubyte(GLcontext *ctx, struct gl_renderbuffer *rb, GLuint count,
 {
    GLubyte *dst = (GLubyte *) values;
    GLuint i;
-   assert(rb->DataType == GL_UNSIGNED_BYTE);
+   ASSERT(rb->DataType == GL_UNSIGNED_BYTE);
    for (i = 0; i < count; i++) {
       const GLubyte *src = (GLubyte *) rb->Data + y[i] * rb->Width + x[i];
       dst[i] = *src;
@@ -104,7 +111,7 @@ put_row_ubyte(GLcontext *ctx, struct gl_renderbuffer *rb, GLuint count,
 {
    const GLubyte *src = (const GLubyte *) values;
    GLubyte *dst = (GLubyte *) rb->Data + y * rb->Width + x;
-   assert(rb->DataType == GL_UNSIGNED_BYTE);
+   ASSERT(rb->DataType == GL_UNSIGNED_BYTE);
    if (mask) {
       GLuint i;
       for (i = 0; i < count; i++) {
@@ -114,7 +121,7 @@ put_row_ubyte(GLcontext *ctx, struct gl_renderbuffer *rb, GLuint count,
       }
    }
    else {
-      _mesa_memcpy(dst, values, count * sizeof(GLubyte));
+      memcpy(dst, values, count * sizeof(GLubyte));
    }
 }
 
@@ -125,7 +132,7 @@ put_mono_row_ubyte(GLcontext *ctx, struct gl_renderbuffer *rb, GLuint count,
 {
    const GLubyte val = *((const GLubyte *) value);
    GLubyte *dst = (GLubyte *) rb->Data + y * rb->Width + x;
-   assert(rb->DataType == GL_UNSIGNED_BYTE);
+   ASSERT(rb->DataType == GL_UNSIGNED_BYTE);
    if (mask) {
       GLuint i;
       for (i = 0; i < count; i++) {
@@ -150,7 +157,7 @@ put_values_ubyte(GLcontext *ctx, struct gl_renderbuffer *rb, GLuint count,
 {
    const GLubyte *src = (const GLubyte *) values;
    GLuint i;
-   assert(rb->DataType == GL_UNSIGNED_BYTE);
+   ASSERT(rb->DataType == GL_UNSIGNED_BYTE);
    for (i = 0; i < count; i++) {
       if (!mask || mask[i]) {
          GLubyte *dst = (GLubyte *) rb->Data + y[i] * rb->Width + x[i];
@@ -167,7 +174,7 @@ put_mono_values_ubyte(GLcontext *ctx, struct gl_renderbuffer *rb, GLuint count,
 {
    const GLubyte val = *((const GLubyte *) value);
    GLuint i;
-   assert(rb->DataType == GL_UNSIGNED_BYTE);
+   ASSERT(rb->DataType == GL_UNSIGNED_BYTE);
    for (i = 0; i < count; i++) {
       if (!mask || mask[i]) {
          GLubyte *dst = (GLubyte *) rb->Data + y[i] * rb->Width + x[i];
@@ -200,7 +207,7 @@ get_row_ushort(GLcontext *ctx, struct gl_renderbuffer *rb, GLuint count,
 {
    const void *src = rb->GetPointer(ctx, rb, x, y);
    ASSERT(rb->DataType == GL_UNSIGNED_SHORT);
-   _mesa_memcpy(values, src, count * sizeof(GLushort));
+   memcpy(values, src, count * sizeof(GLushort));
 }
 
 
@@ -234,7 +241,7 @@ put_row_ushort(GLcontext *ctx, struct gl_renderbuffer *rb, GLuint count,
       }
    }
    else {
-      _mesa_memcpy(dst, src, count * sizeof(GLushort));
+      memcpy(dst, src, count * sizeof(GLushort));
    }
 }
 
@@ -330,7 +337,7 @@ get_row_uint(GLcontext *ctx, struct gl_renderbuffer *rb, GLuint count,
    const void *src = rb->GetPointer(ctx, rb, x, y);
    ASSERT(rb->DataType == GL_UNSIGNED_INT ||
           rb->DataType == GL_UNSIGNED_INT_24_8_EXT);
-   _mesa_memcpy(values, src, count * sizeof(GLuint));
+   memcpy(values, src, count * sizeof(GLuint));
 }
 
 
@@ -366,7 +373,7 @@ put_row_uint(GLcontext *ctx, struct gl_renderbuffer *rb, GLuint count,
       }
    }
    else {
-      _mesa_memcpy(dst, src, count * sizeof(GLuint));
+      memcpy(dst, src, count * sizeof(GLuint));
    }
 }
 
@@ -443,6 +450,7 @@ static void *
 get_pointer_ubyte3(GLcontext *ctx, struct gl_renderbuffer *rb,
                    GLint x, GLint y)
 {
+   ASSERT(rb->Format == MESA_FORMAT_RGB888);
    /* No direct access since this buffer is RGB but caller will be
     * treating it as if it were RGBA.
     */
@@ -457,6 +465,7 @@ get_row_ubyte3(GLcontext *ctx, struct gl_renderbuffer *rb, GLuint count,
    const GLubyte *src = (const GLubyte *) rb->Data + 3 * (y * rb->Width + x);
    GLubyte *dst = (GLubyte *) values;
    GLuint i;
+   ASSERT(rb->Format == MESA_FORMAT_RGB888);
    ASSERT(rb->DataType == GL_UNSIGNED_BYTE);
    for (i = 0; i < count; i++) {
       dst[i * 4 + 0] = src[i * 3 + 0];
@@ -473,7 +482,8 @@ get_values_ubyte3(GLcontext *ctx, struct gl_renderbuffer *rb, GLuint count,
 {
    GLubyte *dst = (GLubyte *) values;
    GLuint i;
-   assert(rb->DataType == GL_UNSIGNED_BYTE);
+   ASSERT(rb->Format == MESA_FORMAT_RGB888);
+   ASSERT(rb->DataType == GL_UNSIGNED_BYTE);
    for (i = 0; i < count; i++) {
       const GLubyte *src
          = (GLubyte *) rb->Data + 3 * (y[i] * rb->Width + x[i]);
@@ -493,7 +503,8 @@ put_row_ubyte3(GLcontext *ctx, struct gl_renderbuffer *rb, GLuint count,
    const GLubyte *src = (const GLubyte *) values;
    GLubyte *dst = (GLubyte *) rb->Data + 3 * (y * rb->Width + x);
    GLuint i;
-   assert(rb->DataType == GL_UNSIGNED_BYTE);
+   ASSERT(rb->Format == MESA_FORMAT_RGB888);
+   ASSERT(rb->DataType == GL_UNSIGNED_BYTE);
    for (i = 0; i < count; i++) {
       if (!mask || mask[i]) {
          dst[i * 3 + 0] = src[i * 4 + 0];
@@ -512,7 +523,8 @@ put_row_rgb_ubyte3(GLcontext *ctx, struct gl_renderbuffer *rb, GLuint count,
    const GLubyte *src = (const GLubyte *) values;
    GLubyte *dst = (GLubyte *) rb->Data + 3 * (y * rb->Width + x);
    GLuint i;
-   assert(rb->DataType == GL_UNSIGNED_BYTE);
+   ASSERT(rb->Format == MESA_FORMAT_RGB888);
+   ASSERT(rb->DataType == GL_UNSIGNED_BYTE);
    for (i = 0; i < count; i++) {
       if (!mask || mask[i]) {
          dst[i * 3 + 0] = src[i * 3 + 0];
@@ -532,10 +544,11 @@ put_mono_row_ubyte3(GLcontext *ctx, struct gl_renderbuffer *rb, GLuint count,
    const GLubyte val1 = ((const GLubyte *) value)[1];
    const GLubyte val2 = ((const GLubyte *) value)[2];
    GLubyte *dst = (GLubyte *) rb->Data + 3 * (y * rb->Width + x);
-   assert(rb->DataType == GL_UNSIGNED_BYTE);
+   ASSERT(rb->Format == MESA_FORMAT_RGB888);
+   ASSERT(rb->DataType == GL_UNSIGNED_BYTE);
    if (!mask && val0 == val1 && val1 == val2) {
       /* optimized case */
-      _mesa_memset(dst, val0, 3 * count);
+      memset(dst, val0, 3 * count);
    }
    else {
       GLuint i;
@@ -558,7 +571,8 @@ put_values_ubyte3(GLcontext *ctx, struct gl_renderbuffer *rb, GLuint count,
    /* note: incoming values are RGB+A! */
    const GLubyte *src = (const GLubyte *) values;
    GLuint i;
-   assert(rb->DataType == GL_UNSIGNED_BYTE);
+   ASSERT(rb->Format == MESA_FORMAT_RGB888);
+   ASSERT(rb->DataType == GL_UNSIGNED_BYTE);
    for (i = 0; i < count; i++) {
       if (!mask || mask[i]) {
          GLubyte *dst = (GLubyte *) rb->Data + 3 * (y[i] * rb->Width + x[i]);
@@ -580,7 +594,8 @@ put_mono_values_ubyte3(GLcontext *ctx, struct gl_renderbuffer *rb,
    const GLubyte val1 = ((const GLubyte *) value)[1];
    const GLubyte val2 = ((const GLubyte *) value)[2];
    GLuint i;
-   assert(rb->DataType == GL_UNSIGNED_BYTE);
+   ASSERT(rb->Format == MESA_FORMAT_RGB888);
+   ASSERT(rb->DataType == GL_UNSIGNED_BYTE);
    for (i = 0; i < count; i++) {
       if (!mask || mask[i]) {
          GLubyte *dst = (GLubyte *) rb->Data + 3 * (y[i] * rb->Width + x[i]);
@@ -604,6 +619,7 @@ get_pointer_ubyte4(GLcontext *ctx, struct gl_renderbuffer *rb,
    if (!rb->Data)
       return NULL;
    ASSERT(rb->DataType == GL_UNSIGNED_BYTE);
+   ASSERT(rb->Format == MESA_FORMAT_RGBA8888);
    return (GLubyte *) rb->Data + 4 * (y * rb->Width + x);
 }
 
@@ -612,9 +628,10 @@ static void
 get_row_ubyte4(GLcontext *ctx, struct gl_renderbuffer *rb, GLuint count,
                GLint x, GLint y, void *values)
 {
-   const GLbyte *src = (const GLbyte *) rb->Data + 4 * (y * rb->Width + x);
+   const GLubyte *src = (const GLubyte *) rb->Data + 4 * (y * rb->Width + x);
    ASSERT(rb->DataType == GL_UNSIGNED_BYTE);
-   _mesa_memcpy(values, src, 4 * count * sizeof(GLbyte));
+   ASSERT(rb->Format == MESA_FORMAT_RGBA8888);
+   memcpy(values, src, 4 * count * sizeof(GLubyte));
 }
 
 
@@ -625,7 +642,8 @@ get_values_ubyte4(GLcontext *ctx, struct gl_renderbuffer *rb, GLuint count,
    /* treat 4*GLubyte as 1*GLuint */
    GLuint *dst = (GLuint *) values;
    GLuint i;
-   assert(rb->DataType == GL_UNSIGNED_BYTE);
+   ASSERT(rb->DataType == GL_UNSIGNED_BYTE);
+   ASSERT(rb->Format == MESA_FORMAT_RGBA8888);
    for (i = 0; i < count; i++) {
       const GLuint *src = (GLuint *) rb->Data + (y[i] * rb->Width + x[i]);
       dst[i] = *src;
@@ -640,7 +658,8 @@ put_row_ubyte4(GLcontext *ctx, struct gl_renderbuffer *rb, GLuint count,
    /* treat 4*GLubyte as 1*GLuint */
    const GLuint *src = (const GLuint *) values;
    GLuint *dst = (GLuint *) rb->Data + (y * rb->Width + x);
-   assert(rb->DataType == GL_UNSIGNED_BYTE);
+   ASSERT(rb->DataType == GL_UNSIGNED_BYTE);
+   ASSERT(rb->Format == MESA_FORMAT_RGBA8888);
    if (mask) {
       GLuint i;
       for (i = 0; i < count; i++) {
@@ -650,7 +669,7 @@ put_row_ubyte4(GLcontext *ctx, struct gl_renderbuffer *rb, GLuint count,
       }
    }
    else {
-      _mesa_memcpy(dst, src, 4 * count * sizeof(GLubyte));
+      memcpy(dst, src, 4 * count * sizeof(GLubyte));
    }
 }
 
@@ -663,7 +682,8 @@ put_row_rgb_ubyte4(GLcontext *ctx, struct gl_renderbuffer *rb, GLuint count,
    const GLubyte *src = (const GLubyte *) values;
    GLubyte *dst = (GLubyte *) rb->Data + 4 * (y * rb->Width + x);
    GLuint i;
-   assert(rb->DataType == GL_UNSIGNED_BYTE);
+   ASSERT(rb->DataType == GL_UNSIGNED_BYTE);
+   ASSERT(rb->Format == MESA_FORMAT_RGBA8888);
    for (i = 0; i < count; i++) {
       if (!mask || mask[i]) {
          dst[i * 4 + 0] = src[i * 3 + 0];
@@ -682,10 +702,11 @@ put_mono_row_ubyte4(GLcontext *ctx, struct gl_renderbuffer *rb, GLuint count,
    /* treat 4*GLubyte as 1*GLuint */
    const GLuint val = *((const GLuint *) value);
    GLuint *dst = (GLuint *) rb->Data + (y * rb->Width + x);
-   assert(rb->DataType == GL_UNSIGNED_BYTE);
+   ASSERT(rb->DataType == GL_UNSIGNED_BYTE);
+   ASSERT(rb->Format == MESA_FORMAT_RGBA8888);
    if (!mask && val == 0) {
       /* common case */
-      _mesa_bzero(dst, count * 4 * sizeof(GLubyte));
+      memset(dst, 0, count * 4 * sizeof(GLubyte));
    }
    else {
       /* general case */
@@ -715,7 +736,8 @@ put_values_ubyte4(GLcontext *ctx, struct gl_renderbuffer *rb, GLuint count,
    /* treat 4*GLubyte as 1*GLuint */
    const GLuint *src = (const GLuint *) values;
    GLuint i;
-   assert(rb->DataType == GL_UNSIGNED_BYTE);
+   ASSERT(rb->DataType == GL_UNSIGNED_BYTE);
+   ASSERT(rb->Format == MESA_FORMAT_RGBA8888);
    for (i = 0; i < count; i++) {
       if (!mask || mask[i]) {
          GLuint *dst = (GLuint *) rb->Data + (y[i] * rb->Width + x[i]);
@@ -733,7 +755,8 @@ put_mono_values_ubyte4(GLcontext *ctx, struct gl_renderbuffer *rb,
    /* treat 4*GLubyte as 1*GLuint */
    const GLuint val = *((const GLuint *) value);
    GLuint i;
-   assert(rb->DataType == GL_UNSIGNED_BYTE);
+   ASSERT(rb->DataType == GL_UNSIGNED_BYTE);
+   ASSERT(rb->Format == MESA_FORMAT_RGBA8888);
    for (i = 0; i < count; i++) {
       if (!mask || mask[i]) {
          GLuint *dst = (GLuint *) rb->Data + (y[i] * rb->Width + x[i]);
@@ -765,7 +788,7 @@ get_row_ushort4(GLcontext *ctx, struct gl_renderbuffer *rb, GLuint count,
 {
    const GLshort *src = (const GLshort *) rb->Data + 4 * (y * rb->Width + x);
    ASSERT(rb->DataType == GL_UNSIGNED_SHORT || rb->DataType == GL_SHORT);
-   _mesa_memcpy(values, src, 4 * count * sizeof(GLshort));
+   memcpy(values, src, 4 * count * sizeof(GLshort));
 }
 
 
@@ -803,7 +826,7 @@ put_row_ushort4(GLcontext *ctx, struct gl_renderbuffer *rb, GLuint count,
       }
    }
    else {
-      _mesa_memcpy(dst, src, 4 * count * sizeof(GLushort));
+      memcpy(dst, src, 4 * count * sizeof(GLushort));
    }
 }
 
@@ -828,7 +851,7 @@ put_row_rgb_ushort4(GLcontext *ctx, struct gl_renderbuffer *rb, GLuint count,
       }
    }
    else {
-      _mesa_memcpy(dst, src, 4 * count * sizeof(GLushort));
+      memcpy(dst, src, 4 * count * sizeof(GLushort));
    }
 }
 
@@ -845,7 +868,7 @@ put_mono_row_ushort4(GLcontext *ctx, struct gl_renderbuffer *rb, GLuint count,
    ASSERT(rb->DataType == GL_UNSIGNED_SHORT || rb->DataType == GL_SHORT);
    if (!mask && val0 == 0 && val1 == 0 && val2 == 0 && val3 == 0) {
       /* common case for clearing accum buffer */
-      _mesa_bzero(dst, count * 4 * sizeof(GLushort));
+      memset(dst, 0, count * 4 * sizeof(GLushort));
    }
    else {
       GLuint i;
@@ -919,21 +942,13 @@ put_mono_values_ushort4(GLcontext *ctx, struct gl_renderbuffer *rb,
  * This function also plugs in the appropriate GetPointer, Get/PutRow and
  * Get/PutValues functions.
  */
-static GLboolean
-soft_renderbuffer_storage(GLcontext *ctx, struct gl_renderbuffer *rb,
-                          GLenum internalFormat, GLuint width, GLuint height)
+GLboolean
+_mesa_soft_renderbuffer_storage(GLcontext *ctx, struct gl_renderbuffer *rb,
+                                GLenum internalFormat,
+                                GLuint width, GLuint height)
 {
    GLuint pixelSize;
 
-   /* first clear these fields */
-   rb->RedBits =
-   rb->GreenBits =
-   rb->BlueBits =
-   rb->AlphaBits =
-   rb->IndexBits =
-   rb->DepthBits =
-   rb->StencilBits = 0;
-
    switch (internalFormat) {
    case GL_RGB:
    case GL_R3_G3_B2:
@@ -943,7 +958,7 @@ soft_renderbuffer_storage(GLcontext *ctx, struct gl_renderbuffer *rb,
    case GL_RGB10:
    case GL_RGB12:
    case GL_RGB16:
-      rb->_BaseFormat = GL_RGB;
+      rb->Format = MESA_FORMAT_RGB888;
       rb->DataType = GL_UNSIGNED_BYTE;
       rb->GetPointer = get_pointer_ubyte3;
       rb->GetRow = get_row_ubyte3;
@@ -953,10 +968,6 @@ soft_renderbuffer_storage(GLcontext *ctx, struct gl_renderbuffer *rb,
       rb->PutMonoRow = put_mono_row_ubyte3;
       rb->PutValues = put_values_ubyte3;
       rb->PutMonoValues = put_mono_values_ubyte3;
-      rb->RedBits   = 8 * sizeof(GLubyte);
-      rb->GreenBits = 8 * sizeof(GLubyte);
-      rb->BlueBits  = 8 * sizeof(GLubyte);
-      rb->AlphaBits = 0;
       pixelSize = 3 * sizeof(GLubyte);
       break;
    case GL_RGBA:
@@ -964,7 +975,11 @@ soft_renderbuffer_storage(GLcontext *ctx, struct gl_renderbuffer *rb,
    case GL_RGBA4:
    case GL_RGB5_A1:
    case GL_RGBA8:
-      rb->_BaseFormat = GL_RGBA;
+#if 1
+   case GL_RGB10_A2:
+   case GL_RGBA12:
+#endif
+      rb->Format = MESA_FORMAT_RGBA8888;
       rb->DataType = GL_UNSIGNED_BYTE;
       rb->GetPointer = get_pointer_ubyte4;
       rb->GetRow = get_row_ubyte4;
@@ -974,17 +989,12 @@ soft_renderbuffer_storage(GLcontext *ctx, struct gl_renderbuffer *rb,
       rb->PutMonoRow = put_mono_row_ubyte4;
       rb->PutValues = put_values_ubyte4;
       rb->PutMonoValues = put_mono_values_ubyte4;
-      rb->RedBits   = 8 * sizeof(GLubyte);
-      rb->GreenBits = 8 * sizeof(GLubyte);
-      rb->BlueBits  = 8 * sizeof(GLubyte);
-      rb->AlphaBits = 8 * sizeof(GLubyte);
       pixelSize = 4 * sizeof(GLubyte);
       break;
-   case GL_RGB10_A2:
-   case GL_RGBA12:
    case GL_RGBA16:
-      rb->_BaseFormat = GL_RGBA;
-      rb->DataType = GL_UNSIGNED_SHORT;
+      /* for accum buffer */
+      rb->Format = MESA_FORMAT_SIGNED_RGBA_16;
+      rb->DataType = GL_SHORT;
       rb->GetPointer = get_pointer_ushort4;
       rb->GetRow = get_row_ushort4;
       rb->GetValues = get_values_ushort4;
@@ -993,15 +1003,11 @@ soft_renderbuffer_storage(GLcontext *ctx, struct gl_renderbuffer *rb,
       rb->PutMonoRow = put_mono_row_ushort4;
       rb->PutValues = put_values_ushort4;
       rb->PutMonoValues = put_mono_values_ushort4;
-      rb->RedBits   = 8 * sizeof(GLushort);
-      rb->GreenBits = 8 * sizeof(GLushort);
-      rb->BlueBits  = 8 * sizeof(GLushort);
-      rb->AlphaBits = 8 * sizeof(GLushort);
       pixelSize = 4 * sizeof(GLushort);
       break;
-#if 00
-   case ALPHA8:
-      rb->_BaseFormat = GL_RGBA; /* Yes, not GL_ALPHA! */
+#if 0
+   case GL_ALPHA8:
+      rb->Format = MESA_FORMAT_A8;
       rb->DataType = GL_UNSIGNED_BYTE;
       rb->GetPointer = get_pointer_alpha8;
       rb->GetRow = get_row_alpha8;
@@ -1011,10 +1017,6 @@ soft_renderbuffer_storage(GLcontext *ctx, struct gl_renderbuffer *rb,
       rb->PutMonoRow = put_mono_row_alpha8;
       rb->PutValues = put_values_alpha8;
       rb->PutMonoValues = put_mono_values_alpha8;
-      rb->RedBits   = 0; /*red*/
-      rb->GreenBits = 0; /*green*/
-      rb->BlueBits  = 0; /*blue*/
-      rb->AlphaBits = 8 * sizeof(GLubyte);
       pixelSize = sizeof(GLubyte);
       break;
 #endif
@@ -1022,7 +1024,8 @@ soft_renderbuffer_storage(GLcontext *ctx, struct gl_renderbuffer *rb,
    case GL_STENCIL_INDEX1_EXT:
    case GL_STENCIL_INDEX4_EXT:
    case GL_STENCIL_INDEX8_EXT:
-      rb->_BaseFormat = GL_STENCIL_INDEX;
+   case GL_STENCIL_INDEX16_EXT:
+      rb->Format = MESA_FORMAT_S8;
       rb->DataType = GL_UNSIGNED_BYTE;
       rb->GetPointer = get_pointer_ubyte;
       rb->GetRow = get_row_ubyte;
@@ -1032,26 +1035,11 @@ soft_renderbuffer_storage(GLcontext *ctx, struct gl_renderbuffer *rb,
       rb->PutMonoRow = put_mono_row_ubyte;
       rb->PutValues = put_values_ubyte;
       rb->PutMonoValues = put_mono_values_ubyte;
-      rb->StencilBits = 8 * sizeof(GLubyte);
       pixelSize = sizeof(GLubyte);
       break;
-   case GL_STENCIL_INDEX16_EXT:
-      rb->_BaseFormat = GL_STENCIL_INDEX;
-      rb->DataType = GL_UNSIGNED_SHORT;
-      rb->GetPointer = get_pointer_ushort;
-      rb->GetRow = get_row_ushort;
-      rb->GetValues = get_values_ushort;
-      rb->PutRow = put_row_ushort;
-      rb->PutRowRGB = NULL;
-      rb->PutMonoRow = put_mono_row_ushort;
-      rb->PutValues = put_values_ushort;
-      rb->PutMonoValues = put_mono_values_ushort;
-      rb->StencilBits = 8 * sizeof(GLushort);
-      pixelSize = sizeof(GLushort);
-      break;
    case GL_DEPTH_COMPONENT:
    case GL_DEPTH_COMPONENT16:
-      rb->_BaseFormat = GL_DEPTH_COMPONENT;
+      rb->Format = MESA_FORMAT_Z16;
       rb->DataType = GL_UNSIGNED_SHORT;
       rb->GetPointer = get_pointer_ushort;
       rb->GetRow = get_row_ushort;
@@ -1061,12 +1049,22 @@ soft_renderbuffer_storage(GLcontext *ctx, struct gl_renderbuffer *rb,
       rb->PutMonoRow = put_mono_row_ushort;
       rb->PutValues = put_values_ushort;
       rb->PutMonoValues = put_mono_values_ushort;
-      rb->DepthBits = 8 * sizeof(GLushort);
       pixelSize = sizeof(GLushort);
       break;
    case GL_DEPTH_COMPONENT24:
+      rb->DataType = GL_UNSIGNED_INT;
+      rb->GetPointer = get_pointer_uint;
+      rb->GetRow = get_row_uint;
+      rb->GetValues = get_values_uint;
+      rb->PutRow = put_row_uint;
+      rb->PutRowRGB = NULL;
+      rb->PutMonoRow = put_mono_row_uint;
+      rb->PutValues = put_values_uint;
+      rb->PutMonoValues = put_mono_values_uint;
+      rb->Format = MESA_FORMAT_X8_Z24;
+      pixelSize = sizeof(GLuint);
+      break;
    case GL_DEPTH_COMPONENT32:
-      rb->_BaseFormat = GL_DEPTH_COMPONENT;
       rb->DataType = GL_UNSIGNED_INT;
       rb->GetPointer = get_pointer_uint;
       rb->GetRow = get_row_uint;
@@ -1076,15 +1074,12 @@ soft_renderbuffer_storage(GLcontext *ctx, struct gl_renderbuffer *rb,
       rb->PutMonoRow = put_mono_row_uint;
       rb->PutValues = put_values_uint;
       rb->PutMonoValues = put_mono_values_uint;
-      if (internalFormat == GL_DEPTH_COMPONENT24)
-         rb->DepthBits = 24;
-      else
-         rb->DepthBits = 32;
+      rb->Format = MESA_FORMAT_Z32;
       pixelSize = sizeof(GLuint);
       break;
    case GL_DEPTH_STENCIL_EXT:
    case GL_DEPTH24_STENCIL8_EXT:
-      rb->_BaseFormat = GL_DEPTH_STENCIL_EXT;
+      rb->Format = MESA_FORMAT_Z24_S8;
       rb->DataType = GL_UNSIGNED_INT_24_8_EXT;
       rb->GetPointer = get_pointer_uint;
       rb->GetRow = get_row_uint;
@@ -1094,12 +1089,12 @@ soft_renderbuffer_storage(GLcontext *ctx, struct gl_renderbuffer *rb,
       rb->PutMonoRow = put_mono_row_uint;
       rb->PutValues = put_values_uint;
       rb->PutMonoValues = put_mono_values_uint;
-      rb->DepthBits = 24;
-      rb->StencilBits = 8;
       pixelSize = sizeof(GLuint);
       break;
    case GL_COLOR_INDEX8_EXT:
-      rb->_BaseFormat = GL_COLOR_INDEX;
+   case GL_COLOR_INDEX16_EXT:
+   case COLOR_INDEX32:
+      rb->Format = MESA_FORMAT_CI8;
       rb->DataType = GL_UNSIGNED_BYTE;
       rb->GetPointer = get_pointer_ubyte;
       rb->GetRow = get_row_ubyte;
@@ -1109,39 +1104,10 @@ soft_renderbuffer_storage(GLcontext *ctx, struct gl_renderbuffer *rb,
       rb->PutMonoRow = put_mono_row_ubyte;
       rb->PutValues = put_values_ubyte;
       rb->PutMonoValues = put_mono_values_ubyte;
-      rb->IndexBits = 8 * sizeof(GLubyte);
       pixelSize = sizeof(GLubyte);
       break;
-   case GL_COLOR_INDEX16_EXT:
-      rb->_BaseFormat = GL_COLOR_INDEX;
-      rb->DataType = GL_UNSIGNED_SHORT;
-      rb->GetPointer = get_pointer_ushort;
-      rb->GetRow = get_row_ushort;
-      rb->GetValues = get_values_ushort;
-      rb->PutRow = put_row_ushort;
-      rb->PutRowRGB = NULL;
-      rb->PutMonoRow = put_mono_row_ushort;
-      rb->PutValues = put_values_ushort;
-      rb->PutMonoValues = put_mono_values_ushort;
-      rb->IndexBits = 8 * sizeof(GLushort);
-      pixelSize = sizeof(GLushort);
-      break;
-   case COLOR_INDEX32:
-      rb->_BaseFormat = GL_COLOR_INDEX;
-      rb->DataType = GL_UNSIGNED_INT;
-      rb->GetPointer = get_pointer_uint;
-      rb->GetRow = get_row_uint;
-      rb->GetValues = get_values_uint;
-      rb->PutRow = put_row_uint;
-      rb->PutRowRGB = NULL;
-      rb->PutMonoRow = put_mono_row_uint;
-      rb->PutValues = put_values_uint;
-      rb->PutMonoValues = put_mono_values_uint;
-      rb->IndexBits = 8 * sizeof(GLuint);
-      pixelSize = sizeof(GLuint);
-      break;
    default:
-      _mesa_problem(ctx, "Bad internalFormat in soft_renderbuffer_storage");
+      _mesa_problem(ctx, "Bad internalFormat in _mesa_soft_renderbuffer_storage");
       return GL_FALSE;
    }
 
@@ -1155,23 +1121,29 @@ soft_renderbuffer_storage(GLcontext *ctx, struct gl_renderbuffer *rb,
    ASSERT(rb->PutMonoValues);
 
    /* free old buffer storage */
-   if (rb->Data)
-      _mesa_free(rb->Data);
-
-   /* allocate new buffer storage */
-   rb->Data = _mesa_malloc(width * height * pixelSize);
-   if (rb->Data == NULL) {
-      rb->Width = 0;
-      rb->Height = 0;
-      _mesa_error(ctx, GL_OUT_OF_MEMORY,
-                  "software renderbuffer allocation (%d x %d x %d)",
-                  width, height, pixelSize);
-      return GL_FALSE;
+   if (rb->Data) {
+      free(rb->Data);
+      rb->Data = NULL;
+   }
+
+   if (width > 0 && height > 0) {
+      /* allocate new buffer storage */
+      rb->Data = malloc(width * height * pixelSize);
+
+      if (rb->Data == NULL) {
+         rb->Width = 0;
+         rb->Height = 0;
+         _mesa_error(ctx, GL_OUT_OF_MEMORY,
+                     "software renderbuffer allocation (%d x %d x %d)",
+                     width, height, pixelSize);
+         return GL_FALSE;
+      }
    }
 
    rb->Width = width;
    rb->Height = height;
-   rb->InternalFormat = internalFormat;
+   rb->_BaseFormat = _mesa_base_fbo_format(ctx, internalFormat);
+   ASSERT(rb->_BaseFormat);
 
    return GL_TRUE;
 }
@@ -1198,6 +1170,7 @@ alloc_storage_alpha8(GLcontext *ctx, struct gl_renderbuffer *arb,
                      GLenum internalFormat, GLuint width, GLuint height)
 {
    ASSERT(arb != arb->Wrapped);
+   ASSERT(arb->Format == MESA_FORMAT_A8);
 
    /* first, pass the call to the wrapped RGB buffer */
    if (!arb->Wrapped->AllocStorage(ctx, arb->Wrapped, internalFormat,
@@ -1207,10 +1180,10 @@ alloc_storage_alpha8(GLcontext *ctx, struct gl_renderbuffer *arb,
 
    /* next, resize my alpha buffer */
    if (arb->Data) {
-      _mesa_free(arb->Data);
+      free(arb->Data);
    }
 
-   arb->Data = _mesa_malloc(width * height * sizeof(GLubyte));
+   arb->Data = malloc(width * height * sizeof(GLubyte));
    if (arb->Data == NULL) {
       arb->Width = 0;
       arb->Height = 0;
@@ -1220,7 +1193,6 @@ alloc_storage_alpha8(GLcontext *ctx, struct gl_renderbuffer *arb,
 
    arb->Width = width;
    arb->Height = height;
-   arb->InternalFormat = internalFormat;
 
    return GL_TRUE;
 }
@@ -1233,13 +1205,13 @@ static void
 delete_renderbuffer_alpha8(struct gl_renderbuffer *arb)
 {
    if (arb->Data) {
-      _mesa_free(arb->Data);
+      free(arb->Data);
    }
    ASSERT(arb->Wrapped);
    ASSERT(arb != arb->Wrapped);
    arb->Wrapped->Delete(arb->Wrapped);
    arb->Wrapped = NULL;
-   _mesa_free(arb);
+   free(arb);
 }
 
 
@@ -1348,7 +1320,7 @@ put_mono_row_alpha8(GLcontext *ctx, struct gl_renderbuffer *arb, GLuint count,
       }
    }
    else {
-      _mesa_memset(dst, val, count);
+      memset(dst, val, count);
    }
 }
 
@@ -1395,6 +1367,17 @@ put_mono_values_alpha8(GLcontext *ctx, struct gl_renderbuffer *arb,
 }
 
 
+static void
+copy_buffer_alpha8(struct gl_renderbuffer* dst, struct gl_renderbuffer* src)
+{
+   ASSERT(dst->Format == MESA_FORMAT_A8);
+   ASSERT(src->Format == MESA_FORMAT_A8);
+   ASSERT(dst->Width == src->Width);
+   ASSERT(dst->Height == src->Height);
+
+   memcpy(dst->Data, src->Data, dst->Width * dst->Height * sizeof(GLubyte));
+}
+
 
 /**********************************************************************/
 /**********************************************************************/
@@ -1418,8 +1401,12 @@ nop_get_pointer(GLcontext *ctx, struct gl_renderbuffer *rb, GLint x, GLint y)
 void
 _mesa_init_renderbuffer(struct gl_renderbuffer *rb, GLuint name)
 {
+   _glthread_INIT_MUTEX(rb->Mutex);
+
+   rb->Magic = RB_MAGIC;
+   rb->ClassID = 0;
    rb->Name = name;
-   rb->RefCount = 1;
+   rb->RefCount = 0;
    rb->Delete = _mesa_delete_renderbuffer;
 
    /* The rest of these should be set later by the caller of this function or
@@ -1430,12 +1417,9 @@ _mesa_init_renderbuffer(struct gl_renderbuffer *rb, GLuint name)
    rb->Width = 0;
    rb->Height = 0;
    rb->InternalFormat = GL_NONE;
-   rb->_BaseFormat = GL_NONE;
+   rb->Format = MESA_FORMAT_NONE;
+
    rb->DataType = GL_NONE;
-   rb->RedBits = rb->GreenBits = rb->BlueBits = rb->AlphaBits = 0;
-   rb->IndexBits = 0;
-   rb->DepthBits = 0;
-   rb->StencilBits = 0;
    rb->Data = NULL;
 
    /* Point back to ourself so that we don't have to check for Wrapped==NULL
@@ -1471,15 +1455,15 @@ _mesa_new_renderbuffer(GLcontext *ctx, GLuint name)
 
 /**
  * Delete a gl_framebuffer.
- * This is the default function for framebuffer->Delete().
+ * This is the default function for renderbuffer->Delete().
  */
 void
 _mesa_delete_renderbuffer(struct gl_renderbuffer *rb)
 {
    if (rb->Data) {
-      _mesa_free(rb->Data);
+      free(rb->Data);
    }
-   _mesa_free(rb);
+   free(rb);
 }
 
 
@@ -1494,9 +1478,9 @@ _mesa_new_soft_renderbuffer(GLcontext *ctx, GLuint name)
 {
    struct gl_renderbuffer *rb = _mesa_new_renderbuffer(ctx, name);
    if (rb) {
-      rb->AllocStorage = soft_renderbuffer_storage;
+      rb->AllocStorage = _mesa_soft_renderbuffer_storage;
       /* Normally, one would setup the PutRow, GetRow, etc functions here.
-       * But we're doing that in the soft_renderbuffer_storage() function
+       * But we're doing that in the _mesa_soft_renderbuffer_storage() function
        * instead.
        */
    }
@@ -1550,79 +1534,17 @@ _mesa_add_color_renderbuffers(GLcontext *ctx, struct gl_framebuffer *fb,
 
       if (rgbBits <= 8) {
          if (alphaBits)
-            rb->InternalFormat = GL_RGBA8;
+            rb->Format = MESA_FORMAT_RGBA8888;
          else
-            rb->InternalFormat = GL_RGB8;
+            rb->Format = MESA_FORMAT_RGB888;
       }
       else {
          assert(rgbBits <= 16);
-         if (alphaBits)
-            rb->InternalFormat = GL_RGBA16;
-         else
-            rb->InternalFormat = GL_RGBA16; /* don't really have RGB16 yet */
+         rb->Format = MESA_FORMAT_NONE; /*XXX RGBA16;*/
       }
+      rb->InternalFormat = GL_RGBA;
 
-      rb->AllocStorage = soft_renderbuffer_storage;
-      _mesa_add_renderbuffer(fb, b, rb);
-   }
-
-   return GL_TRUE;
-}
-
-
-/**
- * Add software-based color index renderbuffers to the given framebuffer.
- * This is a helper routine for device drivers when creating a
- * window system framebuffer (not a user-created render/framebuffer).
- * Once this function is called, you can basically forget about this
- * renderbuffer; core Mesa will handle all the buffer management and
- * rendering!
- */
-GLboolean
-_mesa_add_color_index_renderbuffers(GLcontext *ctx, struct gl_framebuffer *fb,
-                                    GLuint indexBits,
-                                    GLboolean frontLeft, GLboolean backLeft,
-                                    GLboolean frontRight, GLboolean backRight)
-{
-   GLuint b;
-
-   if (indexBits > 8) {
-      _mesa_problem(ctx,
-                "Unsupported bit depth in _mesa_add_color_index_renderbuffers");
-      return GL_FALSE;
-   }
-
-   assert(MAX_COLOR_ATTACHMENTS >= 4);
-
-   for (b = BUFFER_FRONT_LEFT; b <= BUFFER_BACK_RIGHT; b++) {
-      struct gl_renderbuffer *rb;
-
-      if (b == BUFFER_FRONT_LEFT && !frontLeft)
-         continue;
-      else if (b == BUFFER_BACK_LEFT && !backLeft)
-         continue;
-      else if (b == BUFFER_FRONT_RIGHT && !frontRight)
-         continue;
-      else if (b == BUFFER_BACK_RIGHT && !backRight)
-         continue;
-
-      assert(fb->Attachment[b].Renderbuffer == NULL);
-
-      rb = _mesa_new_renderbuffer(ctx, 0);
-      if (!rb) {
-         _mesa_error(ctx, GL_OUT_OF_MEMORY, "Allocating color buffer");
-         return GL_FALSE;
-      }
-
-      if (indexBits <= 8) {
-         /* only support GLuint for now */
-         /*rb->InternalFormat = GL_COLOR_INDEX8_EXT;*/
-         rb->InternalFormat = COLOR_INDEX32;
-      }
-      else {
-         rb->InternalFormat = COLOR_INDEX32;
-      }
-      rb->AllocStorage = soft_renderbuffer_storage;
+      rb->AllocStorage = _mesa_soft_renderbuffer_storage;
       _mesa_add_renderbuffer(fb, b, rb);
    }
 
@@ -1693,7 +1615,7 @@ _mesa_add_alpha_renderbuffers(GLcontext *ctx, struct gl_framebuffer *fb,
        * values.
        */
       arb->InternalFormat = arb->Wrapped->InternalFormat;
-      arb->_BaseFormat    = arb->Wrapped->_BaseFormat;
+      arb->Format         = MESA_FORMAT_A8;
       arb->DataType       = arb->Wrapped->DataType;
       arb->AllocStorage   = alloc_storage_alpha8;
       arb->Delete         = delete_renderbuffer_alpha8;
@@ -1717,6 +1639,27 @@ _mesa_add_alpha_renderbuffers(GLcontext *ctx, struct gl_framebuffer *fb,
 }
 
 
+/**
+ * For framebuffers that use a software alpha channel wrapper
+ * created by _mesa_add_alpha_renderbuffer or _mesa_add_soft_renderbuffers,
+ * copy the back buffer alpha channel into the front buffer alpha channel.
+ */
+void
+_mesa_copy_soft_alpha_renderbuffers(GLcontext *ctx, struct gl_framebuffer *fb)
+{
+   if (fb->Attachment[BUFFER_FRONT_LEFT].Renderbuffer &&
+       fb->Attachment[BUFFER_BACK_LEFT].Renderbuffer)
+      copy_buffer_alpha8(fb->Attachment[BUFFER_FRONT_LEFT].Renderbuffer,
+                         fb->Attachment[BUFFER_BACK_LEFT].Renderbuffer);
+
+
+   if (fb->Attachment[BUFFER_FRONT_RIGHT].Renderbuffer &&
+       fb->Attachment[BUFFER_BACK_RIGHT].Renderbuffer)
+      copy_buffer_alpha8(fb->Attachment[BUFFER_FRONT_RIGHT].Renderbuffer,
+                         fb->Attachment[BUFFER_BACK_RIGHT].Renderbuffer);
+}
+
+
 /**
  * Add a software-based depth renderbuffer to the given framebuffer.
  * This is a helper routine for device drivers when creating a
@@ -1746,16 +1689,19 @@ _mesa_add_depth_renderbuffer(GLcontext *ctx, struct gl_framebuffer *fb,
    }
 
    if (depthBits <= 16) {
+      rb->Format = MESA_FORMAT_Z16;
       rb->InternalFormat = GL_DEPTH_COMPONENT16;
    }
    else if (depthBits <= 24) {
+      rb->Format = MESA_FORMAT_X8_Z24;
       rb->InternalFormat = GL_DEPTH_COMPONENT24;
    }
    else {
+      rb->Format = MESA_FORMAT_Z32;
       rb->InternalFormat = GL_DEPTH_COMPONENT32;
    }
 
-   rb->AllocStorage = soft_renderbuffer_storage;
+   rb->AllocStorage = _mesa_soft_renderbuffer_storage;
    _mesa_add_renderbuffer(fb, BUFFER_DEPTH, rb);
 
    return GL_TRUE;
@@ -1790,15 +1736,11 @@ _mesa_add_stencil_renderbuffer(GLcontext *ctx, struct gl_framebuffer *fb,
       return GL_FALSE;
    }
 
-   if (stencilBits <= 8) {
-      rb->InternalFormat = GL_STENCIL_INDEX8_EXT;
-   }
-   else {
-      /* not really supported (see s_stencil.c code) */
-      rb->InternalFormat = GL_STENCIL_INDEX16_EXT;
-   }
+   assert(stencilBits <= 8);
+   rb->Format = MESA_FORMAT_S8;
+   rb->InternalFormat = GL_STENCIL_INDEX8;
 
-   rb->AllocStorage = soft_renderbuffer_storage;
+   rb->AllocStorage = _mesa_soft_renderbuffer_storage;
    _mesa_add_renderbuffer(fb, BUFFER_STENCIL, rb);
 
    return GL_TRUE;
@@ -1834,8 +1776,9 @@ _mesa_add_accum_renderbuffer(GLcontext *ctx, struct gl_framebuffer *fb,
       return GL_FALSE;
    }
 
+   rb->Format = MESA_FORMAT_SIGNED_RGBA_16;
    rb->InternalFormat = GL_RGBA16;
-   rb->AllocStorage = soft_renderbuffer_storage;
+   rb->AllocStorage = _mesa_soft_renderbuffer_storage;
    _mesa_add_renderbuffer(fb, BUFFER_ACCUM, rb);
 
    return GL_TRUE;
@@ -1865,7 +1808,7 @@ _mesa_add_aux_renderbuffers(GLcontext *ctx, struct gl_framebuffer *fb,
       return GL_FALSE;
    }
 
-   assert(numBuffers < MAX_AUX_BUFFERS);
+   assert(numBuffers <= MAX_AUX_BUFFERS);
 
    for (i = 0; i < numBuffers; i++) {
       struct gl_renderbuffer *rb = _mesa_new_renderbuffer(ctx, 0);
@@ -1877,14 +1820,11 @@ _mesa_add_aux_renderbuffers(GLcontext *ctx, struct gl_framebuffer *fb,
          return GL_FALSE;
       }
 
-      if (colorBits <= 8) {
-         rb->InternalFormat = GL_RGBA8;
-      }
-      else {
-         rb->InternalFormat = GL_RGBA16;
-      }
+      assert (colorBits <= 8);
+      rb->Format = MESA_FORMAT_RGBA8888;
+      rb->InternalFormat = GL_RGBA;
 
-      rb->AllocStorage = soft_renderbuffer_storage;
+      rb->AllocStorage = _mesa_soft_renderbuffer_storage;
       _mesa_add_renderbuffer(fb, BUFFER_AUX0 + i, rb);
    }
    return GL_TRUE;
@@ -1911,21 +1851,13 @@ _mesa_add_soft_renderbuffers(struct gl_framebuffer *fb,
    GLboolean backRight = fb->Visual.stereoMode && fb->Visual.doubleBufferMode;
 
    if (color) {
-      if (fb->Visual.rgbMode) {
-         assert(fb->Visual.redBits == fb->Visual.greenBits);
-         assert(fb->Visual.redBits == fb->Visual.blueBits);
-         _mesa_add_color_renderbuffers(NULL, fb,
-                                       fb->Visual.redBits,
-                                       fb->Visual.alphaBits,
-                                       frontLeft, backLeft,
-                                       frontRight, backRight);
-      }
-      else {
-         _mesa_add_color_index_renderbuffers(NULL, fb,
-                                             fb->Visual.indexBits,
-                                             frontLeft, backLeft,
-                                             frontRight, backRight);
-      }
+      assert(fb->Visual.redBits == fb->Visual.greenBits);
+      assert(fb->Visual.redBits == fb->Visual.blueBits);
+      _mesa_add_color_renderbuffers(NULL, fb,
+                                   fb->Visual.redBits,
+                                   fb->Visual.alphaBits,
+                                   frontLeft, backLeft,
+                                   frontRight, backRight);
    }
 
    if (depth) {
@@ -1939,7 +1871,6 @@ _mesa_add_soft_renderbuffers(struct gl_framebuffer *fb,
    }
 
    if (accum) {
-      assert(fb->Visual.rgbMode);
       assert(fb->Visual.accumRedBits > 0);
       assert(fb->Visual.accumGreenBits > 0);
       assert(fb->Visual.accumBlueBits > 0);
@@ -1951,14 +1882,12 @@ _mesa_add_soft_renderbuffers(struct gl_framebuffer *fb,
    }
 
    if (aux) {
-      assert(fb->Visual.rgbMode);
       assert(fb->Visual.numAuxBuffers > 0);
       _mesa_add_aux_renderbuffers(NULL, fb, fb->Visual.redBits,
                                   fb->Visual.numAuxBuffers);
    }
 
    if (alpha) {
-      assert(fb->Visual.rgbMode);
       assert(fb->Visual.alphaBits > 0);
       _mesa_add_alpha_renderbuffers(NULL, fb, fb->Visual.alphaBits,
                                     frontLeft, backLeft,
@@ -1980,14 +1909,20 @@ void
 _mesa_add_renderbuffer(struct gl_framebuffer *fb,
                        GLuint bufferName, struct gl_renderbuffer *rb)
 {
+   GLenum baseFormat;
+
    assert(fb);
    assert(rb);
-#if 00
-   /* there should be no previous renderbuffer on this attachment point! */
-   assert(fb->Attachment[bufferName].Renderbuffer == NULL);
-#endif
    assert(bufferName < BUFFER_COUNT);
 
+   /* There should be no previous renderbuffer on this attachment point,
+    * with the exception of depth/stencil since the same renderbuffer may
+    * be used for both.
+    */
+   assert(bufferName == BUFFER_DEPTH ||
+          bufferName == BUFFER_STENCIL ||
+          fb->Attachment[bufferName].Renderbuffer == NULL);
+
    /* winsys vs. user-created buffer cross check */
    if (fb->Name) {
       assert(rb->Name);
@@ -1996,9 +1931,99 @@ _mesa_add_renderbuffer(struct gl_framebuffer *fb,
       assert(!rb->Name);
    }
 
+   /* If Mesa's compiled with deep color channels (16 or 32 bits / channel)
+    * and the device driver is expecting 8-bit values (GLubyte), we can
+    * use a "renderbuffer adaptor/wrapper" to do the necessary conversions.
+    */
+   baseFormat = _mesa_get_format_base_format(rb->Format);
+   if (baseFormat == GL_RGBA) {
+      if (CHAN_BITS == 16 && rb->DataType == GL_UNSIGNED_BYTE) {
+         GET_CURRENT_CONTEXT(ctx);
+         rb = _mesa_new_renderbuffer_16wrap8(ctx, rb);
+      }
+      else if (CHAN_BITS == 32 && rb->DataType == GL_UNSIGNED_BYTE) {
+         GET_CURRENT_CONTEXT(ctx);
+         rb = _mesa_new_renderbuffer_32wrap8(ctx, rb);
+      }
+      else if (CHAN_BITS == 32 && rb->DataType == GL_UNSIGNED_SHORT) {
+         GET_CURRENT_CONTEXT(ctx);
+         rb = _mesa_new_renderbuffer_32wrap16(ctx, rb);
+      }
+   }
+
    fb->Attachment[bufferName].Type = GL_RENDERBUFFER_EXT;
    fb->Attachment[bufferName].Complete = GL_TRUE;
-   fb->Attachment[bufferName].Renderbuffer = rb;
+   _mesa_reference_renderbuffer(&fb->Attachment[bufferName].Renderbuffer, rb);
+}
+
+
+/**
+ * Remove the named renderbuffer from the given framebuffer.
+ */
+void
+_mesa_remove_renderbuffer(struct gl_framebuffer *fb, GLuint bufferName)
+{
+   struct gl_renderbuffer *rb;
+
+   assert(bufferName < BUFFER_COUNT);
+
+   rb = fb->Attachment[bufferName].Renderbuffer;
+   if (!rb)
+      return;
+
+   _mesa_reference_renderbuffer(&rb, NULL);
+
+   fb->Attachment[bufferName].Renderbuffer = NULL;
+}
+
+
+/**
+ * Set *ptr to point to rb.  If *ptr points to another renderbuffer,
+ * dereference that buffer first.  The new renderbuffer's refcount will
+ * be incremented.  The old renderbuffer's refcount will be decremented.
+ */
+void
+_mesa_reference_renderbuffer(struct gl_renderbuffer **ptr,
+                             struct gl_renderbuffer *rb)
+{
+   assert(ptr);
+   if (*ptr == rb) {
+      /* no change */
+      return;
+   }
+
+   if (*ptr) {
+      /* Unreference the old renderbuffer */
+      GLboolean deleteFlag = GL_FALSE;
+      struct gl_renderbuffer *oldRb = *ptr;
+
+      assert(oldRb->Magic == RB_MAGIC);
+      _glthread_LOCK_MUTEX(oldRb->Mutex);
+      assert(oldRb->Magic == RB_MAGIC);
+      ASSERT(oldRb->RefCount > 0);
+      oldRb->RefCount--;
+      /*printf("RB DECR %p (%d) to %d\n", (void*) oldRb, oldRb->Name, oldRb->RefCount);*/
+      deleteFlag = (oldRb->RefCount == 0);
+      _glthread_UNLOCK_MUTEX(oldRb->Mutex);
+
+      if (deleteFlag) {
+         oldRb->Magic = 0; /* now invalid memory! */
+         oldRb->Delete(oldRb);
+      }
+
+      *ptr = NULL;
+   }
+   assert(!*ptr);
+
+   if (rb) {
+      assert(rb->Magic == RB_MAGIC);
+      /* reference new renderbuffer */
+      _glthread_LOCK_MUTEX(rb->Mutex);
+      rb->RefCount++;
+      /*printf("RB INCR %p (%d) to %d\n", (void*) rb, rb->Name, rb->RefCount);*/
+      _glthread_UNLOCK_MUTEX(rb->Mutex);
+      *ptr = rb;
+   }
 }
 
 
@@ -2018,8 +2043,8 @@ _mesa_new_depthstencil_renderbuffer(GLcontext *ctx, GLuint name)
 
    /* init fields not covered by _mesa_new_renderbuffer() */
    dsrb->InternalFormat = GL_DEPTH24_STENCIL8_EXT;
-   dsrb->AllocStorage = soft_renderbuffer_storage;
+   dsrb->Format = MESA_FORMAT_Z24_S8;
+   dsrb->AllocStorage = _mesa_soft_renderbuffer_storage;
 
    return dsrb;
 }
-