mesa: Fix pack_uint_Z_FLOAT32()
[mesa.git] / src / mesa / swrast / s_renderbuffer.c
index 7622e38fd3c4fc608b527d84d186c905ce1c38bb..8c97e4e11b480fd83a249254a8ea1a0a074e5866 100644 (file)
@@ -1,6 +1,5 @@
 /*
  * Mesa 3-D graphics library
- * Version:  6.5
  *
  * Copyright (C) 1999-2006  Brian Paul   All Rights Reserved.
  *
  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
- * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
- * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
- * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
+ * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+ * OTHER DEALINGS IN THE SOFTWARE.
  */
 
 
 #include "main/formats.h"
 #include "main/mtypes.h"
 #include "main/renderbuffer.h"
+#include "swrast/s_context.h"
 #include "swrast/s_renderbuffer.h"
 
 
-/*
- * Routines for get/put values in common buffer formats follow.
- */
-
-/* Returns a bytes per pixel of the DataType in the get/put span
- * functions for at least a subset of the available combinations a
- * renderbuffer can have.
- *
- * It would be nice to see gl_renderbuffer start talking about a
- * gl_format instead of a GLenum DataType.
- */
-static int
-get_datatype_bytes(struct gl_renderbuffer *rb)
-{
-   int component_size;
-
-   switch (rb->DataType) {
-   case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
-      component_size = 8;
-      break;
-   case GL_FLOAT:
-   case GL_UNSIGNED_INT:
-   case GL_UNSIGNED_INT_24_8_EXT:
-      component_size = 4;
-      break;
-   case GL_UNSIGNED_SHORT:
-      component_size = 2;
-      break;
-   case GL_UNSIGNED_BYTE:
-      component_size = 1;
-      break;
-   default:
-      component_size = 1;
-      assert(0);
-   }
-
-   switch (rb->_BaseFormat) {
-   case GL_DEPTH_COMPONENT:
-   case GL_DEPTH_STENCIL:
-      return component_size;
-   default:
-      return 4 * component_size;
-   }
-}
-
-/* This is commonly used by most of the accessors. */
-static void *
-get_pointer_generic(struct gl_context *ctx, struct gl_renderbuffer *rb,
-                   GLint x, GLint y)
-{
-   if (!rb->Data)
-      return NULL;
-
-   return ((char *) rb->Data +
-          (y * rb->RowStride + x) * _mesa_get_format_bytes(rb->Format));
-}
-
-/* GetRow() implementation for formats where DataType matches the rb->Format.
- */
-static void
-get_row_generic(struct gl_context *ctx, struct gl_renderbuffer *rb,
-               GLuint count, GLint x, GLint y, void *values)
-{
-   void *src = rb->GetPointer(ctx, rb, x, y);
-   memcpy(values, src, count * _mesa_get_format_bytes(rb->Format));
-}
-
-/* Only used for float textures currently, but might also be used for
- * RGBA8888, RGBA16, etc.
- */
-static void
-get_values_generic(struct gl_context *ctx, struct gl_renderbuffer *rb,
-                  GLuint count, const GLint x[], const GLint y[], void *values)
-{
-   int format_bytes = _mesa_get_format_bytes(rb->Format) / sizeof(GLfloat);
-   GLuint i;
-
-   for (i = 0; i < count; i++) {
-      const void *src = rb->GetPointer(ctx, rb, x[i], y[i]);
-      char *dst = (char *) values + i * format_bytes;
-      memcpy(dst, src, format_bytes);
-   }
-}
-
-/* For the GL_RED/GL_RG/GL_RGB format/DataType combinations (and
- * GL_LUMINANCE/GL_INTENSITY?), the Put functions are a matter of
- * storing those initial components of the value per pixel into the
- * destination.
- */
-static void
-put_row_generic(struct gl_context *ctx, struct gl_renderbuffer *rb,
-               GLuint count, GLint x, GLint y,
-               const void *values, const GLubyte *mask)
-{
-   void *row = rb->GetPointer(ctx, rb, x, y);
-   int format_bytes = _mesa_get_format_bytes(rb->Format) / sizeof(GLfloat);
-   int datatype_bytes = get_datatype_bytes(rb);
-   unsigned int i;
-
-   if (mask) {
-      for (i = 0; i < count; i++) {
-        char *dst = (char *) row + i * format_bytes;
-        const char *src = (const char *) values + i * datatype_bytes;
-
-         if (mask[i]) {
-           memcpy(dst, src, format_bytes);
-         }
-      }
-   }
-   else {
-      for (i = 0; i < count; i++) {
-        char *dst = (char *) row + i * format_bytes;
-        const char *src = (const char *) values + i * datatype_bytes;
-        memcpy(dst, src, format_bytes);
-      }
-   }
-}
-
-static void
-put_mono_row_generic(struct gl_context *ctx, struct gl_renderbuffer *rb,
-                    GLuint count, GLint x, GLint y,
-                    const void *value, const GLubyte *mask)
-{
-   void *row = rb->GetPointer(ctx, rb, x, y);
-   int format_bytes = _mesa_get_format_bytes(rb->Format) / sizeof(GLfloat);
-   unsigned int i;
-
-   if (mask) {
-      for (i = 0; i < count; i++) {
-        char *dst = (char *) row + i * format_bytes;
-         if (mask[i]) {
-           memcpy(dst, value, format_bytes);
-         }
-      }
-   }
-   else {
-      for (i = 0; i < count; i++) {
-        char *dst = (char *) row + i * format_bytes;
-        memcpy(dst, value, format_bytes);
-      }
-   }
-}
-
-
-static void
-put_values_generic(struct gl_context *ctx, struct gl_renderbuffer *rb,
-                  GLuint count, const GLint x[], const GLint y[],
-                  const void *values, const GLubyte *mask)
-{
-   int format_bytes = _mesa_get_format_bytes(rb->Format) / sizeof(GLfloat);
-   int datatype_bytes = get_datatype_bytes(rb);
-   unsigned int i;
-
-   for (i = 0; i < count; i++) {
-      if (!mask || mask[i]) {
-        void *dst = rb->GetPointer(ctx, rb, x[i], y[i]);
-        const char *src = (const char *) values + i * datatype_bytes;
-        memcpy(dst, src, format_bytes);
-      }
-   }
-}
-
-
-static void
-put_mono_values_generic(struct gl_context *ctx,
-                       struct gl_renderbuffer *rb,
-                       GLuint count, const GLint x[], const GLint y[],
-                       const void *value, const GLubyte *mask)
-{
-   int format_bytes = _mesa_get_format_bytes(rb->Format) / sizeof(GLfloat);
-   unsigned int i;
-
-   for (i = 0; i < count; i++) {
-      if (!mask || mask[i]) {
-        void *dst = rb->GetPointer(ctx, rb, x[i], y[i]);
-        memcpy(dst, value, format_bytes);
-      }
-   }
-}
-
-/**********************************************************************
- * Functions for buffers of 1 X GLubyte values.
- * Typically stencil.
- */
-
-static void
-get_values_ubyte(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
-                 const GLint x[], const GLint y[], void *values)
-{
-   GLubyte *dst = (GLubyte *) values;
-   GLuint i;
-   ASSERT(rb->DataType == GL_UNSIGNED_BYTE);
-   for (i = 0; i < count; i++) {
-      const GLubyte *src = (GLubyte *) rb->Data + y[i] * rb->RowStride + x[i];
-      dst[i] = *src;
-   }
-}
-
-
-static void
-put_row_ubyte(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
-              GLint x, GLint y, const void *values, const GLubyte *mask)
-{
-   const GLubyte *src = (const GLubyte *) values;
-   GLubyte *dst = (GLubyte *) rb->Data + y * rb->RowStride + x;
-   ASSERT(rb->DataType == GL_UNSIGNED_BYTE);
-   if (mask) {
-      GLuint i;
-      for (i = 0; i < count; i++) {
-         if (mask[i]) {
-            dst[i] = src[i];
-         }
-      }
-   }
-   else {
-      memcpy(dst, values, count * sizeof(GLubyte));
-   }
-}
-
-
-static void
-put_mono_row_ubyte(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
-                   GLint x, GLint y, const void *value, const GLubyte *mask)
-{
-   const GLubyte val = *((const GLubyte *) value);
-   GLubyte *dst = (GLubyte *) rb->Data + y * rb->RowStride + x;
-   ASSERT(rb->DataType == GL_UNSIGNED_BYTE);
-   if (mask) {
-      GLuint i;
-      for (i = 0; i < count; i++) {
-         if (mask[i]) {
-            dst[i] = val;
-         }
-      }
-   }
-   else {
-      GLuint i;
-      for (i = 0; i < count; i++) {
-         dst[i] = val;
-      }
-   }
-}
-
-
-static void
-put_values_ubyte(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
-                 const GLint x[], const GLint y[],
-                 const void *values, const GLubyte *mask)
-{
-   const GLubyte *src = (const GLubyte *) values;
-   GLuint i;
-   ASSERT(rb->DataType == GL_UNSIGNED_BYTE);
-   for (i = 0; i < count; i++) {
-      if (!mask || mask[i]) {
-         GLubyte *dst = (GLubyte *) rb->Data + y[i] * rb->RowStride + x[i];
-         *dst = src[i];
-      }
-   }
-}
-
-
-static void
-put_mono_values_ubyte(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
-                      const GLint x[], const GLint y[],
-                      const void *value, const GLubyte *mask)
-{
-   const GLubyte val = *((const GLubyte *) value);
-   GLuint i;
-   ASSERT(rb->DataType == GL_UNSIGNED_BYTE);
-   for (i = 0; i < count; i++) {
-      if (!mask || mask[i]) {
-         GLubyte *dst = (GLubyte *) rb->Data + y[i] * rb->RowStride + x[i];
-         *dst = val;
-      }
-   }
-}
-
-
-/**********************************************************************
- * Functions for buffers of 1 X GLushort values.
- * Typically depth/Z.
- */
-
-static void
-get_values_ushort(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
-                  const GLint x[], const GLint y[], void *values)
-{
-   GLushort *dst = (GLushort *) values;
-   GLuint i;
-   ASSERT(rb->DataType == GL_UNSIGNED_SHORT);
-   for (i = 0; i < count; i++) {
-      const GLushort *src = (GLushort *) rb->Data + y[i] * rb->RowStride + x[i];
-      dst[i] = *src;
-   }
-}
-
-
-static void
-put_row_ushort(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
-               GLint x, GLint y, const void *values, const GLubyte *mask)
-{
-   const GLushort *src = (const GLushort *) values;
-   GLushort *dst = (GLushort *) rb->Data + y * rb->RowStride + x;
-   ASSERT(rb->DataType == GL_UNSIGNED_SHORT);
-   if (mask) {
-      GLuint i;
-      for (i = 0; i < count; i++) {
-         if (mask[i]) {
-            dst[i] = src[i];
-         }
-      }
-   }
-   else {
-      memcpy(dst, src, count * sizeof(GLushort));
-   }
-}
-
-
-static void
-put_mono_row_ushort(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
-                    GLint x, GLint y, const void *value, const GLubyte *mask)
-{
-   const GLushort val = *((const GLushort *) value);
-   GLushort *dst = (GLushort *) rb->Data + y * rb->RowStride + x;
-   ASSERT(rb->DataType == GL_UNSIGNED_SHORT);
-   if (mask) {
-      GLuint i;
-      for (i = 0; i < count; i++) {
-         if (mask[i]) {
-            dst[i] = val;
-         }
-      }
-   }
-   else {
-      GLuint i;
-      for (i = 0; i < count; i++) {
-         dst[i] = val;
-      }
-   }
-}
-
-
-static void
-put_values_ushort(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
-                  const GLint x[], const GLint y[], const void *values,
-                  const GLubyte *mask)
-{
-   const GLushort *src = (const GLushort *) values;
-   GLuint i;
-   ASSERT(rb->DataType == GL_UNSIGNED_SHORT);
-   for (i = 0; i < count; i++) {
-      if (!mask || mask[i]) {
-         GLushort *dst = (GLushort *) rb->Data + y[i] * rb->RowStride + x[i];
-         *dst = src[i];
-      }
-   }
-}
-
-static void
-put_mono_values_ushort(struct gl_context *ctx, struct gl_renderbuffer *rb,
-                       GLuint count, const GLint x[], const GLint y[],
-                       const void *value, const GLubyte *mask)
-{
-   const GLushort val = *((const GLushort *) value);
-   ASSERT(rb->DataType == GL_UNSIGNED_SHORT);
-   if (mask) {
-      GLuint i;
-      for (i = 0; i < count; i++) {
-         if (mask[i]) {
-            GLushort *dst = (GLushort *) rb->Data + y[i] * rb->RowStride + x[i];
-            *dst = val;
-         }
-      }
-   }
-   else {
-      GLuint i;
-      for (i = 0; i < count; i++) {
-         GLushort *dst = (GLushort *) rb->Data + y[i] * rb->RowStride + x[i];
-         *dst = val;
-      }
-   }
-}
-
-/**********************************************************************
- * Functions for buffers of 1 X GLuint values.
- * Typically depth/Z or color index.
- */
-
-static void
-get_values_uint(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
-                const GLint x[], const GLint y[], void *values)
-{
-   GLuint *dst = (GLuint *) values;
-   GLuint i;
-   ASSERT(rb->DataType == GL_UNSIGNED_INT ||
-          rb->DataType == GL_UNSIGNED_INT_24_8_EXT);
-   for (i = 0; i < count; i++) {
-      const GLuint *src = (GLuint *) rb->Data + y[i] * rb->RowStride + x[i];
-      dst[i] = *src;
-   }
-}
-
-
-static void
-put_row_uint(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
-             GLint x, GLint y, const void *values, const GLubyte *mask)
-{
-   const GLuint *src = (const GLuint *) values;
-   GLuint *dst = (GLuint *) rb->Data + y * rb->RowStride + x;
-   ASSERT(rb->DataType == GL_UNSIGNED_INT ||
-          rb->DataType == GL_UNSIGNED_INT_24_8_EXT);
-   if (mask) {
-      GLuint i;
-      for (i = 0; i < count; i++) {
-         if (mask[i]) {
-            dst[i] = src[i];
-         }
-      }
-   }
-   else {
-      memcpy(dst, src, count * sizeof(GLuint));
-   }
-}
-
-
-static void
-put_mono_row_uint(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
-                  GLint x, GLint y, const void *value, const GLubyte *mask)
-{
-   const GLuint val = *((const GLuint *) value);
-   GLuint *dst = (GLuint *) rb->Data + y * rb->RowStride + x;
-   ASSERT(rb->DataType == GL_UNSIGNED_INT ||
-          rb->DataType == GL_UNSIGNED_INT_24_8_EXT);
-   if (mask) {
-      GLuint i;
-      for (i = 0; i < count; i++) {
-         if (mask[i]) {
-            dst[i] = val;
-         }
-      }
-   }
-   else {
-      GLuint i;
-      for (i = 0; i < count; i++) {
-         dst[i] = val;
-      }
-   }
-}
-
-
-static void
-put_values_uint(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
-                const GLint x[], const GLint y[], const void *values,
-                const GLubyte *mask)
-{
-   const GLuint *src = (const GLuint *) values;
-   GLuint i;
-   ASSERT(rb->DataType == GL_UNSIGNED_INT ||
-          rb->DataType == GL_UNSIGNED_INT_24_8_EXT);
-   for (i = 0; i < count; i++) {
-      if (!mask || mask[i]) {
-         GLuint *dst = (GLuint *) rb->Data + y[i] * rb->RowStride + x[i];
-         *dst = src[i];
-      }
-   }
-}
-
-
-static void
-put_mono_values_uint(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
-                     const GLint x[], const GLint y[], const void *value,
-                     const GLubyte *mask)
-{
-   const GLuint val = *((const GLuint *) value);
-   GLuint i;
-   ASSERT(rb->DataType == GL_UNSIGNED_INT ||
-          rb->DataType == GL_UNSIGNED_INT_24_8_EXT);
-   for (i = 0; i < count; i++) {
-      if (!mask || mask[i]) {
-         GLuint *dst = (GLuint *) rb->Data + y[i] * rb->RowStride + x[i];
-         *dst = val;
-      }
-   }
-}
-
-
-/**********************************************************************
- * Functions for buffers of 3 X GLubyte (or GLbyte) values.
- * Typically color buffers.
- * NOTE: the incoming and outgoing colors are RGBA!  We ignore incoming
- * alpha values and return 255 for outgoing alpha values.
- */
-
-static void *
-get_pointer_ubyte3(struct gl_context *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.
-    */
-   return NULL;
-}
-
-
-static void
-get_row_ubyte3(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
-               GLint x, GLint y, void *values)
-{
-   const GLubyte *src = ((const GLubyte *) rb->Data) +
-                                          3 * (y * rb->RowStride + 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];
-      dst[i * 4 + 1] = src[i * 3 + 1];
-      dst[i * 4 + 2] = src[i * 3 + 2];
-      dst[i * 4 + 3] = 255;
-   }
-}
-
-
-static void
-get_values_ubyte3(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
-                  const GLint x[], const GLint y[], void *values)
-{
-   GLubyte *dst = (GLubyte *) values;
-   GLuint i;
-   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->RowStride + x[i]);
-      dst[i * 4 + 0] = src[0];
-      dst[i * 4 + 1] = src[1];
-      dst[i * 4 + 2] = src[2];
-      dst[i * 4 + 3] = 255;
-   }
-}
-
-
-static void
-put_row_ubyte3(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
-               GLint x, GLint y, const void *values, const GLubyte *mask)
-{
-   /* note: incoming values are RGB+A! */
-   const GLubyte *src = (const GLubyte *) values;
-   GLubyte *dst = (GLubyte *) rb->Data + 3 * (y * rb->RowStride + x);
-   GLuint i;
-   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];
-         dst[i * 3 + 1] = src[i * 4 + 1];
-         dst[i * 3 + 2] = src[i * 4 + 2];
-      }
-   }
-}
-
-
-static void
-put_row_rgb_ubyte3(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
-                   GLint x, GLint y, const void *values, const GLubyte *mask)
-{
-   /* note: incoming values are RGB+A! */
-   const GLubyte *src = (const GLubyte *) values;
-   GLubyte *dst = (GLubyte *) rb->Data + 3 * (y * rb->RowStride + x);
-   GLuint i;
-   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];
-         dst[i * 3 + 1] = src[i * 3 + 1];
-         dst[i * 3 + 2] = src[i * 3 + 2];
-      }
-   }
-}
-
-
-static void
-put_mono_row_ubyte3(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
-                    GLint x, GLint y, const void *value, const GLubyte *mask)
-{
-   /* note: incoming value is RGB+A! */
-   const GLubyte val0 = ((const GLubyte *) value)[0];
-   const GLubyte val1 = ((const GLubyte *) value)[1];
-   const GLubyte val2 = ((const GLubyte *) value)[2];
-   GLubyte *dst = (GLubyte *) rb->Data + 3 * (y * rb->RowStride + x);
-   ASSERT(rb->Format == MESA_FORMAT_RGB888);
-   ASSERT(rb->DataType == GL_UNSIGNED_BYTE);
-   if (!mask && val0 == val1 && val1 == val2) {
-      /* optimized case */
-      memset(dst, val0, 3 * count);
-   }
-   else {
-      GLuint i;
-      for (i = 0; i < count; i++) {
-         if (!mask || mask[i]) {
-            dst[i * 3 + 0] = val0;
-            dst[i * 3 + 1] = val1;
-            dst[i * 3 + 2] = val2;
-         }
-      }
-   }
-}
-
-
-static void
-put_values_ubyte3(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
-                  const GLint x[], const GLint y[], const void *values,
-                  const GLubyte *mask)
-{
-   /* note: incoming values are RGB+A! */
-   const GLubyte *src = (const GLubyte *) values;
-   GLuint i;
-   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->RowStride + x[i]);
-         dst[0] = src[i * 4 + 0];
-         dst[1] = src[i * 4 + 1];
-         dst[2] = src[i * 4 + 2];
-      }
-   }
-}
-
-
-static void
-put_mono_values_ubyte3(struct gl_context *ctx, struct gl_renderbuffer *rb,
-                       GLuint count, const GLint x[], const GLint y[],
-                       const void *value, const GLubyte *mask)
-{
-   /* note: incoming value is RGB+A! */
-   const GLubyte val0 = ((const GLubyte *) value)[0];
-   const GLubyte val1 = ((const GLubyte *) value)[1];
-   const GLubyte val2 = ((const GLubyte *) value)[2];
-   GLuint i;
-   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->RowStride + x[i]);
-         dst[0] = val0;
-         dst[1] = val1;
-         dst[2] = val2;
-      }
-   }
-}
-
-
-/**********************************************************************
- * Functions for buffers of 4 X GLubyte (or GLbyte) values.
- * Typically color buffers.
- */
-
-static void
-get_values_ubyte4(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
-                  const GLint x[], const GLint y[], void *values)
-{
-   /* treat 4*GLubyte as 1*GLuint */
-   GLuint *dst = (GLuint *) values;
-   GLuint i;
-   ASSERT(rb->DataType == GL_UNSIGNED_BYTE);
-   ASSERT(rb->Format == MESA_FORMAT_RGBA8888 ||
-          rb->Format == MESA_FORMAT_RGBA8888_REV);
-   for (i = 0; i < count; i++) {
-      const GLuint *src = (GLuint *) rb->Data + (y[i] * rb->RowStride + x[i]);
-      dst[i] = *src;
-   }
-}
-
-
-static void
-put_row_ubyte4(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
-               GLint x, GLint y, const void *values, const GLubyte *mask)
-{
-   /* treat 4*GLubyte as 1*GLuint */
-   const GLuint *src = (const GLuint *) values;
-   GLuint *dst = (GLuint *) rb->Data + (y * rb->RowStride + x);
-   ASSERT(rb->DataType == GL_UNSIGNED_BYTE);
-   ASSERT(rb->Format == MESA_FORMAT_RGBA8888 ||
-          rb->Format == MESA_FORMAT_RGBA8888_REV);
-   if (mask) {
-      GLuint i;
-      for (i = 0; i < count; i++) {
-         if (mask[i]) {
-            dst[i] = src[i];
-         }
-      }
-   }
-   else {
-      memcpy(dst, src, 4 * count * sizeof(GLubyte));
-   }
-}
-
-
-static void
-put_row_rgb_ubyte4(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
-                   GLint x, GLint y, const void *values, const GLubyte *mask)
-{
-   /* Store RGB values in RGBA buffer */
-   const GLubyte *src = (const GLubyte *) values;
-   GLubyte *dst = (GLubyte *) rb->Data + 4 * (y * rb->RowStride + x);
-   GLuint i;
-   ASSERT(rb->DataType == GL_UNSIGNED_BYTE);
-   ASSERT(rb->Format == MESA_FORMAT_RGBA8888 ||
-          rb->Format == MESA_FORMAT_RGBA8888_REV);
-   for (i = 0; i < count; i++) {
-      if (!mask || mask[i]) {
-         dst[i * 4 + 0] = src[i * 3 + 0];
-         dst[i * 4 + 1] = src[i * 3 + 1];
-         dst[i * 4 + 2] = src[i * 3 + 2];
-         dst[i * 4 + 3] = 0xff;
-      }
-   }
-}
-
-
-static void
-put_mono_row_ubyte4(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
-                    GLint x, GLint y, const void *value, const GLubyte *mask)
-{
-   /* treat 4*GLubyte as 1*GLuint */
-   const GLuint val = *((const GLuint *) value);
-   GLuint *dst = (GLuint *) rb->Data + (y * rb->RowStride + x);
-   ASSERT(rb->DataType == GL_UNSIGNED_BYTE);
-   ASSERT(rb->Format == MESA_FORMAT_RGBA8888 ||
-          rb->Format == MESA_FORMAT_RGBA8888_REV);
-   if (!mask && val == 0) {
-      /* common case */
-      memset(dst, 0, count * 4 * sizeof(GLubyte));
-   }
-   else {
-      /* general case */
-      if (mask) {
-         GLuint i;
-         for (i = 0; i < count; i++) {
-            if (mask[i]) {
-               dst[i] = val;
-            }
-         }
-      }
-      else {
-         GLuint i;
-         for (i = 0; i < count; i++) {
-            dst[i] = val;
-         }
-      }
-   }
-}
-
-
-static void
-put_values_ubyte4(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
-                  const GLint x[], const GLint y[], const void *values,
-                  const GLubyte *mask)
-{
-   /* treat 4*GLubyte as 1*GLuint */
-   const GLuint *src = (const GLuint *) values;
-   GLuint i;
-   ASSERT(rb->DataType == GL_UNSIGNED_BYTE);
-   ASSERT(rb->Format == MESA_FORMAT_RGBA8888 ||
-          rb->Format == MESA_FORMAT_RGBA8888_REV);
-   for (i = 0; i < count; i++) {
-      if (!mask || mask[i]) {
-         GLuint *dst = (GLuint *) rb->Data + (y[i] * rb->RowStride + x[i]);
-         *dst = src[i];
-      }
-   }
-}
-
-
-static void
-put_mono_values_ubyte4(struct gl_context *ctx, struct gl_renderbuffer *rb,
-                       GLuint count, const GLint x[], const GLint y[],
-                       const void *value, const GLubyte *mask)
-{
-   /* treat 4*GLubyte as 1*GLuint */
-   const GLuint val = *((const GLuint *) value);
-   GLuint i;
-   ASSERT(rb->DataType == GL_UNSIGNED_BYTE);
-   ASSERT(rb->Format == MESA_FORMAT_RGBA8888 ||
-          rb->Format == MESA_FORMAT_RGBA8888_REV);
-   for (i = 0; i < count; i++) {
-      if (!mask || mask[i]) {
-         GLuint *dst = (GLuint *) rb->Data + (y[i] * rb->RowStride + x[i]);
-         *dst = val;
-      }
-   }
-}
-
-
-/**********************************************************************
- * Functions for buffers of 4 X GLushort (or GLshort) values.
- * Typically accum buffer.
- */
-
-static void
-get_values_ushort4(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
-                   const GLint x[], const GLint y[], void *values)
-{
-   GLushort *dst = (GLushort *) values;
-   GLuint i;
-   ASSERT(rb->DataType == GL_UNSIGNED_SHORT || rb->DataType == GL_SHORT);
-   for (i = 0; i < count; i++) {
-      const GLushort *src
-         = (GLushort *) rb->Data + 4 * (y[i] * rb->RowStride + x[i]);
-      dst[i] = *src;
-   }
-}
-
-
-static void
-put_row_ushort4(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
-                GLint x, GLint y, const void *values, const GLubyte *mask)
-{
-   const GLushort *src = (const GLushort *) values;
-   GLushort *dst = (GLushort *) rb->Data + 4 * (y * rb->RowStride + x);
-   ASSERT(rb->DataType == GL_UNSIGNED_SHORT || rb->DataType == GL_SHORT);
-   if (mask) {
-      GLuint i;
-      for (i = 0; i < count; i++) {
-         if (mask[i]) {
-            dst[i * 4 + 0] = src[i * 4 + 0];
-            dst[i * 4 + 1] = src[i * 4 + 1];
-            dst[i * 4 + 2] = src[i * 4 + 2];
-            dst[i * 4 + 3] = src[i * 4 + 3];
-         }
-      }
-   }
-   else {
-      memcpy(dst, src, 4 * count * sizeof(GLushort));
-   }
-}
-
-
-static void
-put_row_rgb_ushort4(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
-                    GLint x, GLint y, const void *values, const GLubyte *mask)
-{
-   /* Put RGB values in RGBA buffer */
-   const GLushort *src = (const GLushort *) values;
-   GLushort *dst = (GLushort *) rb->Data + 4 * (y * rb->RowStride + x);
-   ASSERT(rb->DataType == GL_UNSIGNED_SHORT || rb->DataType == GL_SHORT);
-   if (mask) {
-      GLuint i;
-      for (i = 0; i < count; i++) {
-         if (mask[i]) {
-            dst[i * 4 + 0] = src[i * 3 + 0];
-            dst[i * 4 + 1] = src[i * 3 + 1];
-            dst[i * 4 + 2] = src[i * 3 + 2];
-            dst[i * 4 + 3] = 0xffff;
-         }
-      }
-   }
-   else {
-      memcpy(dst, src, 4 * count * sizeof(GLushort));
-   }
-}
-
-
-static void
-put_mono_row_ushort4(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
-                     GLint x, GLint y, const void *value, const GLubyte *mask)
-{
-   const GLushort val0 = ((const GLushort *) value)[0];
-   const GLushort val1 = ((const GLushort *) value)[1];
-   const GLushort val2 = ((const GLushort *) value)[2];
-   const GLushort val3 = ((const GLushort *) value)[3];
-   GLushort *dst = (GLushort *) rb->Data + 4 * (y * rb->RowStride + x);
-   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 */
-      memset(dst, 0, count * 4 * sizeof(GLushort));
-   }
-   else {
-      GLuint i;
-      for (i = 0; i < count; i++) {
-         if (!mask || mask[i]) {
-            dst[i * 4 + 0] = val0;
-            dst[i * 4 + 1] = val1;
-            dst[i * 4 + 2] = val2;
-            dst[i * 4 + 3] = val3;
-         }
-      }
-   }
-}
-
-
-static void
-put_values_ushort4(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
-                   const GLint x[], const GLint y[], const void *values,
-                   const GLubyte *mask)
-{
-   const GLushort *src = (const GLushort *) values;
-   GLuint i;
-   ASSERT(rb->DataType == GL_UNSIGNED_SHORT || rb->DataType == GL_SHORT);
-   for (i = 0; i < count; i++) {
-      if (!mask || mask[i]) {
-         GLushort *dst =
-            ((GLushort *) rb->Data) + 4 * (y[i] * rb->RowStride + x[i]);
-         dst[0] = src[i * 4 + 0];
-         dst[1] = src[i * 4 + 1];
-         dst[2] = src[i * 4 + 2];
-         dst[3] = src[i * 4 + 3];
-      }
-   }
-}
-
-
-static void
-put_mono_values_ushort4(struct gl_context *ctx, struct gl_renderbuffer *rb,
-                        GLuint count, const GLint x[], const GLint y[],
-                        const void *value, const GLubyte *mask)
-{
-   const GLushort val0 = ((const GLushort *) value)[0];
-   const GLushort val1 = ((const GLushort *) value)[1];
-   const GLushort val2 = ((const GLushort *) value)[2];
-   const GLushort val3 = ((const GLushort *) value)[3];
-   GLuint i;
-   ASSERT(rb->DataType == GL_UNSIGNED_SHORT || rb->DataType == GL_SHORT);
-   for (i = 0; i < count; i++) {
-      if (!mask || mask[i]) {
-         GLushort *dst = ((GLushort *) rb->Data) +
-                                      4 * (y[i] * rb->RowStride + x[i]);
-         dst[0] = val0;
-         dst[1] = val1;
-         dst[2] = val2;
-         dst[3] = val3;
-      }
-   }
-}
-
-/**********************************************************************
- * Functions for MESA_FORMAT_R8.
- */
-static void
-get_row_r8(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
-          GLint x, GLint y, void *values)
-{
-   const GLubyte *src = rb->GetPointer(ctx, rb, x, y);
-   GLuint *dst = values;
-   GLuint i;
-
-   for (i = 0; i < count; i++) {
-      dst[i] = 0xff000000 | src[i];
-   }
-}
-
-static void
-get_values_r8(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
-             const GLint x[], const GLint y[], void *values)
-{
-   GLuint *dst = (GLuint *) values;
-   GLuint i;
-
-   for (i = 0; i < count; i++) {
-      const GLubyte *src = rb->GetPointer(ctx, rb, x[i], y[i]);
-      dst[i] = 0xff000000 | *src;
-   }
-}
-
-/**********************************************************************
- * Functions for MESA_FORMAT_GR88.
- */
-static void
-get_row_rg88(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
-            GLint x, GLint y, void *values)
-{
-   const GLushort *src = rb->GetPointer(ctx, rb, x, y);
-   GLuint *dst = values;
-   GLuint i;
-
-   for (i = 0; i < count; i++) {
-      dst[i] = 0xff000000 | src[i];
-   }
-}
-
-static void
-get_values_rg88(struct gl_context *ctx, struct gl_renderbuffer *rb,
-               GLuint count, const GLint x[], const GLint y[], void *values)
-{
-   GLuint *dst = (GLuint *) values;
-   GLuint i;
-
-   for (i = 0; i < count; i++) {
-      const GLshort *src = rb->GetPointer(ctx, rb, x[i], y[i]);
-      dst[i] = 0xff000000 | *src;
-   }
-}
-
-/**********************************************************************
- * Functions for MESA_FORMAT_R16.
- */
-static void
-get_row_r16(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
-           GLint x, GLint y, void *values)
-{
-   const GLushort *src = rb->GetPointer(ctx, rb, x, y);
-   GLushort *dst = values;
-   GLuint i;
-
-   for (i = 0; i < count; i++) {
-      dst[i * 4 + RCOMP] = src[i];
-      dst[i * 4 + GCOMP] = 0;
-      dst[i * 4 + BCOMP] = 0;
-      dst[i * 4 + ACOMP] = 0xffff;
-   }
-}
-
-static void
-get_values_r16(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
-              const GLint x[], const GLint y[], void *values)
-{
-   GLushort *dst = values;
-   GLuint i;
-
-   for (i = 0; i < count; i++) {
-      const GLushort *src = rb->GetPointer(ctx, rb, x[i], y[i]);
-      dst[i * 4 + RCOMP] = *src;
-      dst[i * 4 + GCOMP] = 0;
-      dst[i * 4 + BCOMP] = 0;
-      dst[i * 4 + ACOMP] = 0xffff;
-   }
-}
-
-/**********************************************************************
- * Functions for MESA_FORMAT_RG1616.
- */
-static void
-get_row_rg1616(struct gl_context *ctx, struct gl_renderbuffer *rb, GLuint count,
-              GLint x, GLint y, void *values)
-{
-   const GLushort *src = rb->GetPointer(ctx, rb, x, y);
-   GLushort *dst = values;
-   GLuint i;
-
-   for (i = 0; i < count; i++) {
-      dst[i * 4 + RCOMP] = src[i * 2];
-      dst[i * 4 + GCOMP] = src[i * 2 + 1];
-      dst[i * 4 + BCOMP] = 0;
-      dst[i * 4 + ACOMP] = 0xffff;
-   }
-}
-
-static void
-get_values_rg1616(struct gl_context *ctx, struct gl_renderbuffer *rb,
-                 GLuint count, const GLint x[], const GLint y[], void *values)
-{
-   GLushort *dst = values;
-   GLuint i;
-
-   for (i = 0; i < count; i++) {
-      const GLshort *src = rb->GetPointer(ctx, rb, x[i], y[i]);
-      dst[i * 4 + RCOMP] = src[0];
-      dst[i * 4 + GCOMP] = src[1];
-      dst[i * 4 + BCOMP] = 0;
-      dst[i * 4 + ACOMP] = 0xffff;
-   }
-}
-
-/**********************************************************************
- * Functions for MESA_FORMAT_INTENSITY_FLOAT32.
- */
-static void
-get_row_i_float32(struct gl_context *ctx, struct gl_renderbuffer *rb,
-                 GLuint count, GLint x, GLint y, void *values)
-{
-   const GLfloat *src = rb->GetPointer(ctx, rb, x, y);
-   GLfloat *dst = values;
-   GLuint i;
-
-   for (i = 0; i < count; i++) {
-      dst[i * 4 + RCOMP] =
-      dst[i * 4 + GCOMP] =
-      dst[i * 4 + BCOMP] =
-      dst[i * 4 + ACOMP] = src[i];
-   }
-}
-
-static void
-get_values_i_float32(struct gl_context *ctx, struct gl_renderbuffer *rb,
-                    GLuint count, const GLint x[], const GLint y[],
-                    void *values)
-{
-   GLfloat *dst = values;
-   GLuint i;
-
-   for (i = 0; i < count; i++) {
-      const GLfloat *src = rb->GetPointer(ctx, rb, x[i], y[i]);
-      dst[i * 4 + RCOMP] =
-      dst[i * 4 + GCOMP] =
-      dst[i * 4 + BCOMP] =
-      dst[i * 4 + ACOMP] = src[0];
-   }
-}
-
-/**********************************************************************
- * Functions for MESA_FORMAT_LUMINANCE_FLOAT32.
- */
-static void
-get_row_l_float32(struct gl_context *ctx, struct gl_renderbuffer *rb,
-                 GLuint count, GLint x, GLint y, void *values)
-{
-   const GLfloat *src = rb->GetPointer(ctx, rb, x, y);
-   GLfloat *dst = values;
-   GLuint i;
-
-   for (i = 0; i < count; i++) {
-      dst[i * 4 + RCOMP] =
-      dst[i * 4 + GCOMP] =
-      dst[i * 4 + BCOMP] = src[i];
-      dst[i * 4 + ACOMP] = 1.0;
-   }
-}
-
-static void
-get_values_l_float32(struct gl_context *ctx, struct gl_renderbuffer *rb,
-                    GLuint count, const GLint x[], const GLint y[],
-                    void *values)
-{
-   GLfloat *dst = values;
-   GLuint i;
-
-   for (i = 0; i < count; i++) {
-      const GLfloat *src = rb->GetPointer(ctx, rb, x[i], y[i]);
-      dst[i * 4 + RCOMP] =
-      dst[i * 4 + GCOMP] =
-      dst[i * 4 + BCOMP] = src[0];
-      dst[i * 4 + ACOMP] = 1.0;
-   }
-}
-
-/**********************************************************************
- * Functions for MESA_FORMAT_ALPHA_FLOAT32.
- */
-static void
-get_row_a_float32(struct gl_context *ctx, struct gl_renderbuffer *rb,
-                 GLuint count, GLint x, GLint y, void *values)
-{
-   const GLfloat *src = rb->GetPointer(ctx, rb, x, y);
-   GLfloat *dst = values;
-   GLuint i;
-
-   for (i = 0; i < count; i++) {
-      dst[i * 4 + RCOMP] = 0.0;
-      dst[i * 4 + GCOMP] = 0.0;
-      dst[i * 4 + BCOMP] = 0.0;
-      dst[i * 4 + ACOMP] = src[i];
-   }
-}
-
-static void
-get_values_a_float32(struct gl_context *ctx, struct gl_renderbuffer *rb,
-                    GLuint count, const GLint x[], const GLint y[],
-                    void *values)
-{
-   GLfloat *dst = values;
-   GLuint i;
-
-   for (i = 0; i < count; i++) {
-      const GLfloat *src = rb->GetPointer(ctx, rb, x[i], y[i]);
-      dst[i * 4 + RCOMP] = 0.0;
-      dst[i * 4 + GCOMP] = 0.0;
-      dst[i * 4 + BCOMP] = 0.0;
-      dst[i * 4 + ACOMP] = src[0];
-   }
-}
-
-static void
-put_row_a_float32(struct gl_context *ctx, struct gl_renderbuffer *rb,
-                 GLuint count, GLint x, GLint y,
-                 const void *values, const GLubyte *mask)
-{
-   float *dst = rb->GetPointer(ctx, rb, x, y);
-   const float *src = values;
-   unsigned int i;
-
-   if (mask) {
-      for (i = 0; i < count; i++) {
-         if (mask[i]) {
-           dst[i] = src[i * 4 + ACOMP];
-         }
-      }
-   }
-   else {
-      for (i = 0; i < count; i++) {
-        dst[i] = src[i * 4 + ACOMP];
-      }
-   }
-}
-
-static void
-put_mono_row_a_float32(struct gl_context *ctx, struct gl_renderbuffer *rb,
-                      GLuint count, GLint x, GLint y,
-                      const void *value, const GLubyte *mask)
-{
-   float *dst = rb->GetPointer(ctx, rb, x, y);
-   const float *src = value;
-   unsigned int i;
-
-   if (mask) {
-      for (i = 0; i < count; i++) {
-         if (mask[i]) {
-           dst[i] = src[ACOMP];
-         }
-      }
-   }
-   else {
-      for (i = 0; i < count; i++) {
-        dst[i] = src[ACOMP];
-      }
-   }
-}
-
-static void
-put_values_a_float32(struct gl_context *ctx, struct gl_renderbuffer *rb,
-                    GLuint count, const GLint x[], const GLint y[],
-                    const void *values, const GLubyte *mask)
-{
-   const float *src = values;
-   unsigned int i;
-
-   for (i = 0; i < count; i++) {
-      if (!mask || mask[i]) {
-        float *dst = rb->GetPointer(ctx, rb, x[i], y[i]);
-
-        *dst = src[i * 4 + ACOMP];
-      }
-   }
-}
-
-static void
-put_mono_values_a_float32(struct gl_context *ctx,
-                         struct gl_renderbuffer *rb,
-                         GLuint count, const GLint x[], const GLint y[],
-                         const void *value, const GLubyte *mask)
-{
-   const float *src = value;
-   unsigned int i;
-
-   for (i = 0; i < count; i++) {
-      if (!mask || mask[i]) {
-        float *dst = rb->GetPointer(ctx, rb, x[i], y[i]);
-        *dst = src[ACOMP];
-      }
-   }
-}
-
-/**********************************************************************
- * Functions for MESA_FORMAT_R_FLOAT32.
- */
-static void
-get_row_r_float32(struct gl_context *ctx, struct gl_renderbuffer *rb,
-                 GLuint count, GLint x, GLint y, void *values)
-{
-   const GLfloat *src = rb->GetPointer(ctx, rb, x, y);
-   GLfloat *dst = values;
-   GLuint i;
-
-   for (i = 0; i < count; i++) {
-      dst[i * 4 + RCOMP] = src[i];
-      dst[i * 4 + GCOMP] = 0.0;
-      dst[i * 4 + BCOMP] = 0.0;
-      dst[i * 4 + ACOMP] = 1.0;
-   }
-}
-
-static void
-get_values_r_float32(struct gl_context *ctx, struct gl_renderbuffer *rb,
-                    GLuint count, const GLint x[], const GLint y[],
-                    void *values)
-{
-   GLfloat *dst = values;
-   GLuint i;
-
-   for (i = 0; i < count; i++) {
-      const GLfloat *src = rb->GetPointer(ctx, rb, x[i], y[i]);
-      dst[i * 4 + RCOMP] = src[0];
-      dst[i * 4 + GCOMP] = 0.0;
-      dst[i * 4 + BCOMP] = 0.0;
-      dst[i * 4 + ACOMP] = 1.0;
-   }
-}
-
-/**********************************************************************
- * Functions for MESA_FORMAT_RG_FLOAT32.
- */
-static void
-get_row_rg_float32(struct gl_context *ctx, struct gl_renderbuffer *rb,
-                  GLuint count, GLint x, GLint y, void *values)
-{
-   const GLfloat *src = rb->GetPointer(ctx, rb, x, y);
-   GLfloat *dst = values;
-   GLuint i;
-
-   for (i = 0; i < count; i++) {
-      dst[i * 4 + RCOMP] = src[i * 2 + 0];
-      dst[i * 4 + GCOMP] = src[i * 2 + 1];
-      dst[i * 4 + BCOMP] = 0.0;
-      dst[i * 4 + ACOMP] = 1.0;
-   }
-}
-
-static void
-get_values_rg_float32(struct gl_context *ctx, struct gl_renderbuffer *rb,
-                     GLuint count, const GLint x[], const GLint y[],
-                     void *values)
-{
-   GLfloat *dst = values;
-   GLuint i;
-
-   for (i = 0; i < count; i++) {
-      const GLfloat *src = rb->GetPointer(ctx, rb, x[i], y[i]);
-      dst[i * 4 + RCOMP] = src[0];
-      dst[i * 4 + GCOMP] = src[1];
-      dst[i * 4 + BCOMP] = 0.0;
-      dst[i * 4 + ACOMP] = 1.0;
-   }
-}
-
-/**
- * This is the default software fallback for gl_renderbuffer's span
- * access functions.
- *
- * The assumptions are that rb->Data will be a pointer to (0,0), that pixels
- * are packed in the type of rb->Format, and that subsequent rows appear
- * rb->RowStride pixels later.
- */
-void
-_swrast_set_renderbuffer_accessors(struct gl_renderbuffer *rb)
-{
-   rb->GetPointer = get_pointer_generic;
-   rb->GetRow = get_row_generic;
-
-   switch (rb->Format) {
-   case MESA_FORMAT_RGB888:
-      rb->DataType = GL_UNSIGNED_BYTE;
-      rb->GetPointer = get_pointer_ubyte3;
-      rb->GetRow = get_row_ubyte3;
-      rb->GetValues = get_values_ubyte3;
-      rb->PutRow = put_row_ubyte3;
-      rb->PutRowRGB = put_row_rgb_ubyte3;
-      rb->PutMonoRow = put_mono_row_ubyte3;
-      rb->PutValues = put_values_ubyte3;
-      rb->PutMonoValues = put_mono_values_ubyte3;
-      break;
-
-   case MESA_FORMAT_RGBA8888:
-   case MESA_FORMAT_RGBA8888_REV:
-      rb->DataType = GL_UNSIGNED_BYTE;
-      rb->GetValues = get_values_ubyte4;
-      rb->PutRow = put_row_ubyte4;
-      rb->PutRowRGB = put_row_rgb_ubyte4;
-      rb->PutMonoRow = put_mono_row_ubyte4;
-      rb->PutValues = put_values_ubyte4;
-      rb->PutMonoValues = put_mono_values_ubyte4;
-      break;
-
-   case MESA_FORMAT_R8:
-      rb->DataType = GL_UNSIGNED_BYTE;
-      rb->GetValues = get_values_r8;
-      rb->GetRow = get_row_r8;
-      rb->PutRow = put_row_generic;
-      rb->PutRowRGB = put_row_generic;
-      rb->PutMonoRow = put_mono_row_generic;
-      rb->PutValues = put_values_generic;
-      rb->PutMonoValues = put_mono_values_generic;
-      break;
-
-   case MESA_FORMAT_GR88:
-      rb->DataType = GL_UNSIGNED_BYTE;
-      rb->GetValues = get_values_rg88;
-      rb->GetRow = get_row_rg88;
-      rb->PutRow = put_row_generic;
-      rb->PutRowRGB = put_row_generic;
-      rb->PutMonoRow = put_mono_row_generic;
-      rb->PutValues = put_values_generic;
-      rb->PutMonoValues = put_mono_values_generic;
-      break;
-
-   case MESA_FORMAT_R16:
-      rb->DataType = GL_UNSIGNED_SHORT;
-      rb->GetValues = get_values_r16;
-      rb->GetRow = get_row_r16;
-      rb->PutRow = put_row_generic;
-      rb->PutRowRGB = put_row_generic;
-      rb->PutMonoRow = put_mono_row_generic;
-      rb->PutValues = put_values_generic;
-      rb->PutMonoValues = put_mono_values_generic;
-      break;
-
-   case MESA_FORMAT_RG1616:
-      rb->DataType = GL_UNSIGNED_SHORT;
-      rb->GetValues = get_values_rg1616;
-      rb->GetRow = get_row_rg1616;
-      rb->PutRow = put_row_generic;
-      rb->PutRowRGB = put_row_generic;
-      rb->PutMonoRow = put_mono_row_generic;
-      rb->PutValues = put_values_generic;
-      rb->PutMonoValues = put_mono_values_generic;
-      break;
-
-   case MESA_FORMAT_SIGNED_RGBA_16:
-      rb->DataType = GL_SHORT;
-      rb->GetValues = get_values_ushort4;
-      rb->PutRow = put_row_ushort4;
-      rb->PutRowRGB = put_row_rgb_ushort4;
-      rb->PutMonoRow = put_mono_row_ushort4;
-      rb->PutValues = put_values_ushort4;
-      rb->PutMonoValues = put_mono_values_ushort4;
-      break;
-
-   case MESA_FORMAT_S8:
-      rb->DataType = GL_UNSIGNED_BYTE;
-      rb->GetValues = get_values_ubyte;
-      rb->PutRow = put_row_ubyte;
-      rb->PutRowRGB = NULL;
-      rb->PutMonoRow = put_mono_row_ubyte;
-      rb->PutValues = put_values_ubyte;
-      rb->PutMonoValues = put_mono_values_ubyte;
-      break;
-
-   case MESA_FORMAT_Z16:
-      rb->DataType = GL_UNSIGNED_SHORT;
-      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;
-      break;
-
-   case MESA_FORMAT_Z32:
-   case MESA_FORMAT_X8_Z24:
-   case MESA_FORMAT_Z24_X8:
-      rb->DataType = GL_UNSIGNED_INT;
-      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;
-      break;
-
-   case MESA_FORMAT_Z24_S8:
-   case MESA_FORMAT_S8_Z24:
-      rb->DataType = GL_UNSIGNED_INT_24_8_EXT;
-      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;
-      break;
-
-   case MESA_FORMAT_RGBA_FLOAT32:
-      rb->GetRow = get_row_generic;
-      rb->GetValues = get_values_generic;
-      rb->PutRow = put_row_generic;
-      rb->PutRowRGB = NULL;
-      rb->PutMonoRow = put_mono_row_generic;
-      rb->PutValues = put_values_generic;
-      rb->PutMonoValues = put_mono_values_generic;
-      break;
-
-   case MESA_FORMAT_INTENSITY_FLOAT32:
-      rb->GetRow = get_row_i_float32;
-      rb->GetValues = get_values_i_float32;
-      rb->PutRow = put_row_generic;
-      rb->PutRowRGB = NULL;
-      rb->PutMonoRow = put_mono_row_generic;
-      rb->PutValues = put_values_generic;
-      rb->PutMonoValues = put_mono_values_generic;
-      break;
-
-   case MESA_FORMAT_LUMINANCE_FLOAT32:
-      rb->GetRow = get_row_l_float32;
-      rb->GetValues = get_values_l_float32;
-      rb->PutRow = put_row_generic;
-      rb->PutRowRGB = NULL;
-      rb->PutMonoRow = put_mono_row_generic;
-      rb->PutValues = put_values_generic;
-      rb->PutMonoValues = put_mono_values_generic;
-      break;
-
-   case MESA_FORMAT_ALPHA_FLOAT32:
-      rb->GetRow = get_row_a_float32;
-      rb->GetValues = get_values_a_float32;
-      rb->PutRow = put_row_a_float32;
-      rb->PutRowRGB = NULL;
-      rb->PutMonoRow = put_mono_row_a_float32;
-      rb->PutValues = put_values_a_float32;
-      rb->PutMonoValues = put_mono_values_a_float32;
-      break;
-
-   case MESA_FORMAT_RG_FLOAT32:
-      rb->GetRow = get_row_rg_float32;
-      rb->GetValues = get_values_rg_float32;
-      rb->PutRow = put_row_generic;
-      rb->PutRowRGB = NULL;
-      rb->PutMonoRow = put_mono_row_generic;
-      rb->PutValues = put_values_generic;
-      rb->PutMonoValues = put_mono_values_generic;
-      break;
-
-   case MESA_FORMAT_R_FLOAT32:
-      rb->GetRow = get_row_r_float32;
-      rb->GetValues = get_values_r_float32;
-      rb->PutRow = put_row_generic;
-      rb->PutRowRGB = NULL;
-      rb->PutMonoRow = put_mono_row_generic;
-      rb->PutValues = put_values_generic;
-      rb->PutMonoValues = put_mono_values_generic;
-      break;
-
-   default:
-      break;
-   }
-}
-
 /**
  * This is a software fallback for the gl_renderbuffer->AllocStorage
  * function.
@@ -1579,15 +51,15 @@ _swrast_set_renderbuffer_accessors(struct gl_renderbuffer *rb)
  *
  * This one multi-purpose function can allocate stencil, depth, accum, color
  * or color-index buffers!
- *
- * This function also plugs in the appropriate GetPointer, Get/PutRow and
- * Get/PutValues functions.
  */
 static GLboolean
 soft_renderbuffer_storage(struct gl_context *ctx, struct gl_renderbuffer *rb,
                           GLenum internalFormat,
                           GLuint width, GLuint height)
 {
+   struct swrast_renderbuffer *srb = swrast_renderbuffer(rb);
+   GLuint bpp;
+
    switch (internalFormat) {
    case GL_RGB:
    case GL_R3_G3_B2:
@@ -1597,7 +69,7 @@ soft_renderbuffer_storage(struct gl_context *ctx, struct gl_renderbuffer *rb,
    case GL_RGB10:
    case GL_RGB12:
    case GL_RGB16:
-      rb->Format = MESA_FORMAT_RGB888;
+      rb->Format = MESA_FORMAT_BGR_UNORM8;
       break;
    case GL_RGBA:
    case GL_RGBA2:
@@ -1609,71 +81,59 @@ soft_renderbuffer_storage(struct gl_context *ctx, struct gl_renderbuffer *rb,
    case GL_RGBA12:
 #endif
       if (_mesa_little_endian())
-         rb->Format = MESA_FORMAT_RGBA8888_REV;
+         rb->Format = MESA_FORMAT_R8G8B8A8_UNORM;
       else
-         rb->Format = MESA_FORMAT_RGBA8888;
+         rb->Format = MESA_FORMAT_A8B8G8R8_UNORM;
       break;
    case GL_RGBA16:
    case GL_RGBA16_SNORM:
       /* for accum buffer */
-      rb->Format = MESA_FORMAT_SIGNED_RGBA_16;
+      rb->Format = MESA_FORMAT_RGBA_SNORM16;
       break;
    case GL_STENCIL_INDEX:
    case GL_STENCIL_INDEX1_EXT:
    case GL_STENCIL_INDEX4_EXT:
    case GL_STENCIL_INDEX8_EXT:
    case GL_STENCIL_INDEX16_EXT:
-      rb->Format = MESA_FORMAT_S8;
+      rb->Format = MESA_FORMAT_S_UINT8;
       break;
    case GL_DEPTH_COMPONENT:
    case GL_DEPTH_COMPONENT16:
-      rb->Format = MESA_FORMAT_Z16;
+      rb->Format = MESA_FORMAT_Z_UNORM16;
       break;
    case GL_DEPTH_COMPONENT24:
-      rb->Format = MESA_FORMAT_X8_Z24;
+      rb->Format = MESA_FORMAT_Z24_UNORM_X8_UINT;
       break;
    case GL_DEPTH_COMPONENT32:
-      rb->Format = MESA_FORMAT_Z32;
+      rb->Format = MESA_FORMAT_Z_UNORM32;
       break;
    case GL_DEPTH_STENCIL_EXT:
    case GL_DEPTH24_STENCIL8_EXT:
-      rb->Format = MESA_FORMAT_Z24_S8;
+      rb->Format = MESA_FORMAT_S8_UINT_Z24_UNORM;
       break;
    default:
       /* unsupported format */
       return GL_FALSE;
    }
 
-   _swrast_set_renderbuffer_accessors(rb);
-
-   ASSERT(rb->DataType);
-   ASSERT(rb->GetPointer);
-   ASSERT(rb->GetRow);
-   ASSERT(rb->GetValues);
-   ASSERT(rb->PutRow);
-   ASSERT(rb->PutMonoRow);
-   ASSERT(rb->PutValues);
-   ASSERT(rb->PutMonoValues);
+   bpp = _mesa_get_format_bytes(rb->Format);
 
    /* free old buffer storage */
-   if (rb->Data) {
-      free(rb->Data);
-      rb->Data = NULL;
-   }
+   free(srb->Buffer);
+   srb->Buffer = NULL;
 
-   rb->RowStride = width;
+   srb->RowStride = width * bpp;
 
    if (width > 0 && height > 0) {
       /* allocate new buffer storage */
-      rb->Data = malloc(width * height * _mesa_get_format_bytes(rb->Format));
+      srb->Buffer = malloc(srb->RowStride * height);
 
-      if (rb->Data == NULL) {
+      if (srb->Buffer == NULL) {
          rb->Width = 0;
          rb->Height = 0;
-        rb->RowStride = 0;
          _mesa_error(ctx, GL_OUT_OF_MEMORY,
                      "software renderbuffer allocation (%d x %d x %d)",
-                     width, height, _mesa_get_format_bytes(rb->Format));
+                     width, height, bpp);
          return GL_FALSE;
       }
    }
@@ -1693,26 +153,45 @@ soft_renderbuffer_storage(struct gl_context *ctx, struct gl_renderbuffer *rb,
    }
    else {
       /* the internalFormat should have been error checked long ago */
-      ASSERT(rb->_BaseFormat);
+      assert(rb->_BaseFormat);
    }
 
    return GL_TRUE;
 }
 
 
+/**
+ * Called via gl_renderbuffer::Delete()
+ */
+static void
+soft_renderbuffer_delete(struct gl_context *ctx, struct gl_renderbuffer *rb)
+{
+   struct swrast_renderbuffer *srb = swrast_renderbuffer(rb);
+
+   free(srb->Buffer);
+   srb->Buffer = NULL;
+   _mesa_delete_renderbuffer(ctx, rb);
+}
+
+
 void
 _swrast_map_soft_renderbuffer(struct gl_context *ctx,
                               struct gl_renderbuffer *rb,
                               GLuint x, GLuint y, GLuint w, GLuint h,
                               GLbitfield mode,
                               GLubyte **out_map,
-                              GLint *out_stride)
+                              GLint *out_stride,
+                              bool flip_y)
 {
-   GLubyte *map = rb->Data;
+   struct swrast_renderbuffer *srb = swrast_renderbuffer(rb);
+   GLubyte *map = srb->Buffer;
    int cpp = _mesa_get_format_bytes(rb->Format);
-   int stride = rb->RowStride * cpp;
+   int stride = rb->Width * cpp;
 
-   ASSERT(rb->Data);
+   if (!map) {
+      *out_map = NULL;
+      *out_stride = 0;
+   }
 
    map += y * stride;
    map += x * cpp;
@@ -1739,15 +218,13 @@ _swrast_unmap_soft_renderbuffer(struct gl_context *ctx,
 struct gl_renderbuffer *
 _swrast_new_soft_renderbuffer(struct gl_context *ctx, GLuint name)
 {
-   struct gl_renderbuffer *rb = _mesa_new_renderbuffer(ctx, name);
-   if (rb) {
-      rb->AllocStorage = soft_renderbuffer_storage;
-      /* Normally, one would setup the PutRow, GetRow, etc functions here.
-       * But we're doing that in the soft_renderbuffer_storage() function
-       * instead.
-       */
+   struct swrast_renderbuffer *srb = CALLOC_STRUCT(swrast_renderbuffer);
+   if (srb) {
+      _mesa_init_renderbuffer(&srb->Base, name);
+      srb->Base.AllocStorage = soft_renderbuffer_storage;
+      srb->Base.Delete = soft_renderbuffer_delete;
    }
-   return rb;
+   return &srb->Base;
 }
 
 
@@ -1789,7 +266,7 @@ add_color_renderbuffers(struct gl_context *ctx, struct gl_framebuffer *fb,
 
       assert(fb->Attachment[b].Renderbuffer == NULL);
 
-      rb = _mesa_new_renderbuffer(ctx, 0);
+      rb = ctx->Driver.NewRenderbuffer(ctx, 0);
       if (!rb) {
          _mesa_error(ctx, GL_OUT_OF_MEMORY, "Allocating color buffer");
          return GL_FALSE;
@@ -1798,7 +275,7 @@ add_color_renderbuffers(struct gl_context *ctx, struct gl_framebuffer *fb,
       rb->InternalFormat = GL_RGBA;
 
       rb->AllocStorage = soft_renderbuffer_storage;
-      _mesa_add_renderbuffer(fb, b, rb);
+      _mesa_attach_and_own_rb(fb, b, rb);
    }
 
    return GL_TRUE;
@@ -1827,7 +304,7 @@ add_depth_renderbuffer(struct gl_context *ctx, struct gl_framebuffer *fb,
 
    assert(fb->Attachment[BUFFER_DEPTH].Renderbuffer == NULL);
 
-   rb = _mesa_new_renderbuffer(ctx, 0);
+   rb = _swrast_new_soft_renderbuffer(ctx, 0);
    if (!rb) {
       _mesa_error(ctx, GL_OUT_OF_MEMORY, "Allocating depth buffer");
       return GL_FALSE;
@@ -1844,7 +321,7 @@ add_depth_renderbuffer(struct gl_context *ctx, struct gl_framebuffer *fb,
    }
 
    rb->AllocStorage = soft_renderbuffer_storage;
-   _mesa_add_renderbuffer(fb, BUFFER_DEPTH, rb);
+   _mesa_attach_and_own_rb(fb, BUFFER_DEPTH, rb);
 
    return GL_TRUE;
 }
@@ -1872,7 +349,7 @@ add_stencil_renderbuffer(struct gl_context *ctx, struct gl_framebuffer *fb,
 
    assert(fb->Attachment[BUFFER_STENCIL].Renderbuffer == NULL);
 
-   rb = _mesa_new_renderbuffer(ctx, 0);
+   rb = _swrast_new_soft_renderbuffer(ctx, 0);
    if (!rb) {
       _mesa_error(ctx, GL_OUT_OF_MEMORY, "Allocating stencil buffer");
       return GL_FALSE;
@@ -1882,7 +359,32 @@ add_stencil_renderbuffer(struct gl_context *ctx, struct gl_framebuffer *fb,
    rb->InternalFormat = GL_STENCIL_INDEX8;
 
    rb->AllocStorage = soft_renderbuffer_storage;
-   _mesa_add_renderbuffer(fb, BUFFER_STENCIL, rb);
+   _mesa_attach_and_own_rb(fb, BUFFER_STENCIL, rb);
+
+   return GL_TRUE;
+}
+
+
+static GLboolean
+add_depth_stencil_renderbuffer(struct gl_context *ctx,
+                               struct gl_framebuffer *fb)
+{
+   struct gl_renderbuffer *rb;
+
+   assert(fb->Attachment[BUFFER_DEPTH].Renderbuffer == NULL);
+   assert(fb->Attachment[BUFFER_STENCIL].Renderbuffer == NULL);
+
+   rb = _swrast_new_soft_renderbuffer(ctx, 0);
+   if (!rb) {
+      _mesa_error(ctx, GL_OUT_OF_MEMORY, "Allocating depth+stencil buffer");
+      return GL_FALSE;
+   }
+
+   rb->InternalFormat = GL_DEPTH_STENCIL;
+
+   rb->AllocStorage = soft_renderbuffer_storage;
+   _mesa_attach_and_own_rb(fb, BUFFER_DEPTH, rb);
+   _mesa_attach_and_reference_rb(fb, BUFFER_STENCIL, rb);
 
    return GL_TRUE;
 }
@@ -1911,7 +413,7 @@ add_accum_renderbuffer(struct gl_context *ctx, struct gl_framebuffer *fb,
 
    assert(fb->Attachment[BUFFER_ACCUM].Renderbuffer == NULL);
 
-   rb = _mesa_new_renderbuffer(ctx, 0);
+   rb = _swrast_new_soft_renderbuffer(ctx, 0);
    if (!rb) {
       _mesa_error(ctx, GL_OUT_OF_MEMORY, "Allocating accum buffer");
       return GL_FALSE;
@@ -1919,7 +421,7 @@ add_accum_renderbuffer(struct gl_context *ctx, struct gl_framebuffer *fb,
 
    rb->InternalFormat = GL_RGBA16_SNORM;
    rb->AllocStorage = soft_renderbuffer_storage;
-   _mesa_add_renderbuffer(fb, BUFFER_ACCUM, rb);
+   _mesa_attach_and_own_rb(fb, BUFFER_ACCUM, rb);
 
    return GL_TRUE;
 }
@@ -1951,7 +453,7 @@ add_aux_renderbuffers(struct gl_context *ctx, struct gl_framebuffer *fb,
    assert(numBuffers <= MAX_AUX_BUFFERS);
 
    for (i = 0; i < numBuffers; i++) {
-      struct gl_renderbuffer *rb = _mesa_new_renderbuffer(ctx, 0);
+      struct gl_renderbuffer *rb = _swrast_new_soft_renderbuffer(ctx, 0);
 
       assert(fb->Attachment[BUFFER_AUX0 + i].Renderbuffer == NULL);
 
@@ -1964,7 +466,7 @@ add_aux_renderbuffers(struct gl_context *ctx, struct gl_framebuffer *fb,
       rb->InternalFormat = GL_RGBA;
 
       rb->AllocStorage = soft_renderbuffer_storage;
-      _mesa_add_renderbuffer(fb, BUFFER_AUX0 + i, rb);
+      _mesa_attach_and_own_rb(fb, BUFFER_AUX0 + i, rb);
    }
    return GL_TRUE;
 }
@@ -1999,14 +501,29 @@ _swrast_add_soft_renderbuffers(struct gl_framebuffer *fb,
                               frontRight, backRight);
    }
 
-   if (depth) {
-      assert(fb->Visual.depthBits > 0);
-      add_depth_renderbuffer(NULL, fb, fb->Visual.depthBits);
-   }
+#if 0
+   /* This is pretty much for debugging purposes only since there's a perf
+    * hit for using combined depth/stencil in swrast.
+    */
+   if (depth && fb->Visual.depthBits == 24 &&
+       stencil && fb->Visual.stencilBits == 8) {
+      /* use combined depth/stencil buffer */
+      add_depth_stencil_renderbuffer(NULL, fb);
+   }
+   else
+#else
+   (void) add_depth_stencil_renderbuffer;
+#endif
+   {
+      if (depth) {
+         assert(fb->Visual.depthBits > 0);
+         add_depth_renderbuffer(NULL, fb, fb->Visual.depthBits);
+      }
 
-   if (stencil) {
-      assert(fb->Visual.stencilBits > 0);
-      add_stencil_renderbuffer(NULL, fb, fb->Visual.stencilBits);
+      if (stencil) {
+         assert(fb->Visual.stencilBits > 0);
+         add_stencil_renderbuffer(NULL, fb, fb->Visual.stencilBits);
+      }
    }
 
    if (accum) {
@@ -2032,3 +549,151 @@ _swrast_add_soft_renderbuffers(struct gl_framebuffer *fb,
    }
 #endif
 }
+
+
+
+static void
+map_attachment(struct gl_context *ctx,
+                 struct gl_framebuffer *fb,
+                 gl_buffer_index buffer)
+{
+   struct gl_texture_object *texObj = fb->Attachment[buffer].Texture;
+   struct gl_renderbuffer *rb = fb->Attachment[buffer].Renderbuffer;
+   struct swrast_renderbuffer *srb = swrast_renderbuffer(rb);
+
+   if (texObj) {
+      /* map texture image (render to texture) */
+      const GLuint level = fb->Attachment[buffer].TextureLevel;
+      const GLuint face = fb->Attachment[buffer].CubeMapFace;
+      const GLuint slice = fb->Attachment[buffer].Zoffset;
+      struct gl_texture_image *texImage = texObj->Image[face][level];
+      if (texImage) {
+         ctx->Driver.MapTextureImage(ctx, texImage, slice,
+                                     0, 0, texImage->Width, texImage->Height,
+                                     GL_MAP_READ_BIT | GL_MAP_WRITE_BIT,
+                                     &srb->Map, &srb->RowStride);
+      }
+   }
+   else if (rb) {
+      /* Map ordinary renderbuffer */
+      ctx->Driver.MapRenderbuffer(ctx, rb,
+                                  0, 0, rb->Width, rb->Height,
+                                  GL_MAP_READ_BIT | GL_MAP_WRITE_BIT,
+                                  &srb->Map, &srb->RowStride,
+                                  fb->FlipY);
+   }
+
+   assert(srb->Map);
+}
+
+static void
+unmap_attachment(struct gl_context *ctx,
+                   struct gl_framebuffer *fb,
+                   gl_buffer_index buffer)
+{
+   struct gl_texture_object *texObj = fb->Attachment[buffer].Texture;
+   struct gl_renderbuffer *rb = fb->Attachment[buffer].Renderbuffer;
+   struct swrast_renderbuffer *srb = swrast_renderbuffer(rb);
+
+   if (texObj) {
+      /* unmap texture image (render to texture) */
+      const GLuint level = fb->Attachment[buffer].TextureLevel;
+      const GLuint face = fb->Attachment[buffer].CubeMapFace;
+      const GLuint slice = fb->Attachment[buffer].Zoffset;
+      struct gl_texture_image *texImage = texObj->Image[face][level];
+      if (texImage) {
+         ctx->Driver.UnmapTextureImage(ctx, texImage, slice);
+      }
+   }
+   else if (rb) {
+      /* unmap ordinary renderbuffer */
+      ctx->Driver.UnmapRenderbuffer(ctx, rb);
+   }
+
+   srb->Map = NULL;
+}
+
+
+/**
+ * Determine what type to use (ubyte vs. float) for span colors for the
+ * given renderbuffer.
+ * See also _swrast_write_rgba_span().
+ */
+static void
+find_renderbuffer_colortype(struct gl_renderbuffer *rb)
+{
+   struct swrast_renderbuffer *srb = swrast_renderbuffer(rb);
+   GLuint rbMaxBits = _mesa_get_format_max_bits(rb->Format);
+   GLenum rbDatatype = _mesa_get_format_datatype(rb->Format);
+
+   if (rbDatatype == GL_UNSIGNED_NORMALIZED && rbMaxBits <= 8) {
+      /* the buffer's values fit in GLubyte values */
+      srb->ColorType = GL_UNSIGNED_BYTE;
+   }
+   else {
+      /* use floats otherwise */
+      srb->ColorType = GL_FLOAT;
+   }
+}
+
+
+/**
+ * Map the renderbuffers we'll use for tri/line/point rendering.
+ */
+void
+_swrast_map_renderbuffers(struct gl_context *ctx)
+{
+   struct gl_framebuffer *fb = ctx->DrawBuffer;
+   struct gl_renderbuffer *depthRb, *stencilRb;
+   GLuint buf;
+
+   depthRb = fb->Attachment[BUFFER_DEPTH].Renderbuffer;
+   if (depthRb) {
+      /* map depth buffer */
+      map_attachment(ctx, fb, BUFFER_DEPTH);
+   }
+
+   stencilRb = fb->Attachment[BUFFER_STENCIL].Renderbuffer;
+   if (stencilRb && stencilRb != depthRb) {
+      /* map stencil buffer */
+      map_attachment(ctx, fb, BUFFER_STENCIL);
+   }
+
+   for (buf = 0; buf < fb->_NumColorDrawBuffers; buf++) {
+      if (fb->_ColorDrawBufferIndexes[buf] != BUFFER_NONE) {
+         map_attachment(ctx, fb, fb->_ColorDrawBufferIndexes[buf]);
+         find_renderbuffer_colortype(fb->_ColorDrawBuffers[buf]);
+      }
+   }
+}
+/**
+ * Unmap renderbuffers after rendering.
+ */
+void
+_swrast_unmap_renderbuffers(struct gl_context *ctx)
+{
+   struct gl_framebuffer *fb = ctx->DrawBuffer;
+   struct gl_renderbuffer *depthRb, *stencilRb;
+   GLuint buf;
+
+   depthRb = fb->Attachment[BUFFER_DEPTH].Renderbuffer;
+   if (depthRb) {
+      /* map depth buffer */
+      unmap_attachment(ctx, fb, BUFFER_DEPTH);
+   }
+
+   stencilRb = fb->Attachment[BUFFER_STENCIL].Renderbuffer;
+   if (stencilRb && stencilRb != depthRb) {
+      /* map stencil buffer */
+      unmap_attachment(ctx, fb, BUFFER_STENCIL);
+   }
+
+   for (buf = 0; buf < fb->_NumColorDrawBuffers; buf++) {
+      if (fb->_ColorDrawBufferIndexes[buf] != BUFFER_NONE) {
+         unmap_attachment(ctx, fb, fb->_ColorDrawBufferIndexes[buf]);
+      }
+   }
+}