swrast: always call _swrast_choose_texture_sample_func()
[mesa.git] / src / mesa / main / texgetimage.c
index b23317c46a06d79058ca371f07e6efa4f8e0a27c..2830dda867f207922b4c81d715df07c182a14167 100644 (file)
 #include "enums.h"
 #include "context.h"
 #include "formats.h"
+#include "format_unpack.h"
 #include "image.h"
 #include "mfeatures.h"
 #include "mtypes.h"
 #include "pack.h"
 #include "pbo.h"
+#include "texcompress.h"
 #include "texgetimage.h"
 #include "teximage.h"
 
@@ -63,55 +65,6 @@ type_with_negative_values(GLenum type)
 }
 
 
-/**
- * glGetTexImage for color index pixels.
- */
-static void
-get_tex_color_index(struct gl_context *ctx, GLuint dimensions,
-                    GLenum format, GLenum type, GLvoid *pixels,
-                    const struct gl_texture_image *texImage)
-{
-   const GLint width = texImage->Width;
-   const GLint height = texImage->Height;
-   const GLint depth = texImage->Depth;
-   const GLint rowstride = texImage->RowStride;
-   const GLuint indexBits =
-      _mesa_get_format_bits(texImage->TexFormat, GL_TEXTURE_INDEX_SIZE_EXT);
-   const GLbitfield transferOps = 0x0;
-   GLint img, row, col;
-
-   for (img = 0; img < depth; img++) {
-      for (row = 0; row < height; row++) {
-         GLuint indexRow[MAX_WIDTH] = { 0 };
-         void *dest = _mesa_image_address(dimensions, &ctx->Pack, pixels,
-                                          width, height, format, type,
-                                          img, row, 0);
-         assert(dest);
-
-         if (indexBits == 8) {
-            const GLubyte *src = (const GLubyte *) texImage->Data;
-            src += rowstride * (img * height + row);
-            for (col = 0; col < width; col++) {
-               indexRow[col] = src[col];
-            }
-         }
-         else if (indexBits == 16) {
-            const GLushort *src = (const GLushort *) texImage->Data;
-            src += rowstride * (img * height + row);
-            for (col = 0; col < width; col++) {
-               indexRow[col] = src[col];
-            }
-         }
-         else {
-            _mesa_problem(ctx, "Color index problem in _mesa_GetTexImage");
-         }
-         _mesa_pack_index_span(ctx, width, type, dest,
-                               indexRow, &ctx->Pack, transferOps);
-      }
-   }
-}
-
-
 /**
  * glGetTexImage for depth/Z pixels.
  */
@@ -123,8 +76,9 @@ get_tex_depth(struct gl_context *ctx, GLuint dimensions,
    const GLint width = texImage->Width;
    const GLint height = texImage->Height;
    const GLint depth = texImage->Depth;
-   GLint img, row, col;
+   GLint img, row;
    GLfloat *depthRow = (GLfloat *) malloc(width * sizeof(GLfloat));
+   const GLint texelSize = _mesa_get_format_bytes(texImage->TexFormat);
 
    if (!depthRow) {
       _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
@@ -136,11 +90,12 @@ get_tex_depth(struct gl_context *ctx, GLuint dimensions,
          void *dest = _mesa_image_address(dimensions, &ctx->Pack, pixels,
                                           width, height, format, type,
                                           img, row, 0);
-         assert(dest);
+         const GLubyte *src = (GLubyte *) texImage->Data +
+            (texImage->ImageOffsets[img] +
+             texImage->RowStride * row) * texelSize;
+
+         _mesa_unpack_float_z_row(texImage->TexFormat, width, src, depthRow);
 
-         for (col = 0; col < width; col++) {
-            texImage->FetchTexelf(texImage, col, row, img, depthRow + col);
-         }
          _mesa_pack_depth_span(ctx, width, dest, type, depthRow, &ctx->Pack);
       }
    }
@@ -220,176 +175,142 @@ get_tex_ycbcr(struct gl_context *ctx, GLuint dimensions,
 }
 
 
-#if FEATURE_EXT_texture_sRGB
-
-
 /**
- * Convert a float value from linear space to a
- * non-linear sRGB value in [0, 255].
- * Not terribly efficient.
- */
-static INLINE GLfloat
-linear_to_nonlinear(GLfloat cl)
-{
-   /* can't have values outside [0, 1] */
-   GLfloat cs;
-   if (cl < 0.0031308f) {
-      cs = 12.92f * cl;
-   }
-   else {
-      cs = (GLfloat)(1.055 * pow(cl, 0.41666) - 0.055);
-   }
-   return cs;
-}
-
-
-/**
- * glGetTexImagefor sRGB pixels;
+ * glGetTexImage for color formats (RGBA, RGB, alpha, LA, etc).
+ * Compressed textures are handled here as well.
  */
 static void
-get_tex_srgb(struct gl_context *ctx, GLuint dimensions,
+get_tex_rgba(struct gl_context *ctx, GLuint dimensions,
              GLenum format, GLenum type, GLvoid *pixels,
-             const struct gl_texture_image *texImage)
+             struct gl_texture_image *texImage)
 {
-   const GLint width = texImage->Width;
-   const GLint height = texImage->Height;
-   const GLint depth = texImage->Depth;
-   const GLbitfield transferOps = 0x0;
-   GLint img, row;
-   GLfloat (*rgba)[4] = (GLfloat (*)[4]) malloc(4 * width * sizeof(GLfloat));
-
-   if (!rgba) {
-      _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
-      return;
-   }
-
-   for (img = 0; img < depth; img++) {
-      for (row = 0; row < height; row++) {
-         void *dest = _mesa_image_address(dimensions, &ctx->Pack, pixels,
-                                          width, height, format, type,
-                                          img, row, 0);
-
-         GLint col;
+   /* don't want to apply sRGB -> RGB conversion here so override the format */
+   const gl_format texFormat = _mesa_get_srgb_format_linear(texImage->TexFormat);
+   const GLuint width = texImage->Width;
+   const GLuint height = texImage->Height;
+   const GLuint depth = texImage->Depth;
+   const GLenum dataType = _mesa_get_format_datatype(texFormat);
+   const GLenum baseFormat = _mesa_get_format_base_format(texFormat);
+   /* Normally, no pixel transfer ops are performed during glGetTexImage.
+    * The only possible exception is component clamping to [0,1].
+    */
+   GLbitfield transferOps = 0x0;
 
-         /* convert row to RGBA format */
-         for (col = 0; col < width; col++) {
-            texImage->FetchTexelf(texImage, col, row, img, rgba[col]);
-            if (texImage->_BaseFormat == GL_LUMINANCE) {
-               rgba[col][RCOMP] = linear_to_nonlinear(rgba[col][RCOMP]);
-               rgba[col][GCOMP] = 0.0;
-               rgba[col][BCOMP] = 0.0;
-            }
-            else if (texImage->_BaseFormat == GL_LUMINANCE_ALPHA) {
-               rgba[col][RCOMP] = linear_to_nonlinear(rgba[col][RCOMP]);
-               rgba[col][GCOMP] = 0.0;
-               rgba[col][BCOMP] = 0.0;
-            }
-            else if (texImage->_BaseFormat == GL_RGB ||
-                     texImage->_BaseFormat == GL_RGBA) {
-               rgba[col][RCOMP] = linear_to_nonlinear(rgba[col][RCOMP]);
-               rgba[col][GCOMP] = linear_to_nonlinear(rgba[col][GCOMP]);
-               rgba[col][BCOMP] = linear_to_nonlinear(rgba[col][BCOMP]);
-            }
-         }
-         _mesa_pack_rgba_span_float(ctx, width, (GLfloat (*)[4]) rgba,
-                                    format, type, dest,
-                                    &ctx->Pack, transferOps);
+   /* In general, clamping does not apply to glGetTexImage, except when
+    * the returned type of the image can't hold negative values.
+    */
+   if (!type_with_negative_values(type)) {
+      /* the returned image type can't have negative values */
+      if (dataType == GL_FLOAT ||
+          dataType == GL_SIGNED_NORMALIZED ||
+          format == GL_LUMINANCE ||
+          format == GL_LUMINANCE_ALPHA) {
+         transferOps |= IMAGE_CLAMP_BIT;
       }
    }
 
-   free(rgba);
-}
-
-
-#else /* FEATURE_EXT_texture_sRGB */
-
+   if (_mesa_is_format_compressed(texFormat)) {
+      /* Decompress into temp buffer, then pack into user buffer */
+      GLfloat *tempImage, *srcRow;
+      GLuint row;
 
-static INLINE void
-get_tex_srgb(struct gl_context *ctx, GLuint dimensions,
-             GLenum format, GLenum type, GLvoid *pixels,
-             const struct gl_texture_image *texImage)
-{
-   ASSERT_NO_FEATURE();
-}
+      tempImage = (GLfloat *) malloc(texImage->Width * texImage->Height *
+                                     texImage->Depth * 4 * sizeof(GLfloat));
+      if (!tempImage) {
+         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage()");
+         return;
+      }
 
+      _mesa_decompress_image(texFormat, texImage->Width, texImage->Height,
+                             texImage->Data, texImage->RowStride, tempImage);
 
-#endif /* FEATURE_EXT_texture_sRGB */
+      if (baseFormat == GL_LUMINANCE ||
+          baseFormat == GL_LUMINANCE_ALPHA) {
+         /* Set green and blue to zero since the pack function here will
+          * compute L=R+G+B.
+          */
+         GLuint i;
+         for (i = 0; i < width * height; i++) {
+            tempImage[i * 4 + GCOMP] = tempImage[i * 4 + BCOMP] = 0.0f;
+         }
+      }
 
+      srcRow = tempImage;
+      for (row = 0; row < height; row++) {
+         void *dest = _mesa_image_address(dimensions, &ctx->Pack, pixels,
+                                          width, height, format, type,
+                                          0, row, 0);
 
-/**
- * glGetTexImagefor RGBA, Luminance, etc. pixels.
- * This is the slow way since we use texture sampling.
- */
-static void
-get_tex_rgba(struct gl_context *ctx, GLuint dimensions,
-             GLenum format, GLenum type, GLvoid *pixels,
-             const struct gl_texture_image *texImage)
-{
-   const GLint width = texImage->Width;
-   const GLint height = texImage->Height;
-   const GLint depth = texImage->Depth;
-   /* Normally, no pixel transfer ops are performed during glGetTexImage.
-    * The only possible exception is component clamping to [0,1].
-    */
-   GLbitfield transferOps = 0x0;
-   GLint img, row;
-   GLfloat (*rgba)[4] = (GLfloat (*)[4]) malloc(4 * width * sizeof(GLfloat));
+         _mesa_pack_rgba_span_float(ctx, width, (GLfloat (*)[4]) srcRow,
+                                    format, type, dest, &ctx->Pack, transferOps);
+         srcRow += width * 4;
+      }
 
-   if (!rgba) {
-      _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage");
-      return;
+      free(tempImage);
    }
+   else {
+      /* No decompression needed */
+      const GLint texelSize = _mesa_get_format_bytes(texFormat);
+      GLuint img, row;
+      GLfloat (*rgba)[4];
+
+      rgba = (GLfloat (*)[4]) malloc(4 * width * sizeof(GLfloat));
+      if (!rgba) {
+         _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage()");
+         return;
+      }
 
-   for (img = 0; img < depth; img++) {
-      for (row = 0; row < height; row++) {
-         void *dest = _mesa_image_address(dimensions, &ctx->Pack, pixels,
-                                          width, height, format, type,
-                                          img, row, 0);
-         GLint col;
-         GLenum dataType = _mesa_get_format_datatype(texImage->TexFormat);
+      for (img = 0; img < depth; img++) {
+         for (row = 0; row < height; row++) {
+            void *dest = _mesa_image_address(dimensions, &ctx->Pack, pixels,
+                                             width, height, format, type,
+                                             img, row, 0);
+            const GLubyte *src = (const GLubyte *) texImage->Data +
+               (texImage->ImageOffsets[img] +
+                texImage->RowStride * row) * texelSize;
 
-         /* clamp does not apply to GetTexImage (final conversion)?
-          * Looks like we need clamp though when going from format
-          * containing negative values to unsigned format.
-          */
-         if (format == GL_LUMINANCE || format == GL_LUMINANCE_ALPHA) {
-            transferOps |= IMAGE_CLAMP_BIT;
-         }
-         else if (!type_with_negative_values(type) &&
-                  (dataType == GL_FLOAT ||
-                   dataType == GL_SIGNED_NORMALIZED)) {
-            transferOps |= IMAGE_CLAMP_BIT;
-         }
+            _mesa_unpack_rgba_row(texFormat, width, src, rgba);
 
-         for (col = 0; col < width; col++) {
-            texImage->FetchTexelf(texImage, col, row, img, rgba[col]);
             if (texImage->_BaseFormat == GL_ALPHA) {
-               rgba[col][RCOMP] = 0.0F;
-               rgba[col][GCOMP] = 0.0F;
-               rgba[col][BCOMP] = 0.0F;
+               GLint col;
+               for (col = 0; col < width; col++) {
+                  rgba[col][RCOMP] = 0.0F;
+                  rgba[col][GCOMP] = 0.0F;
+                  rgba[col][BCOMP] = 0.0F;
+               }
             }
             else if (texImage->_BaseFormat == GL_LUMINANCE) {
-               rgba[col][GCOMP] = 0.0F;
-               rgba[col][BCOMP] = 0.0F;
-               rgba[col][ACOMP] = 1.0F;
+               GLint col;
+               for (col = 0; col < width; col++) {
+                  rgba[col][GCOMP] = 0.0F;
+                  rgba[col][BCOMP] = 0.0F;
+                  rgba[col][ACOMP] = 1.0F;
+               }
             }
             else if (texImage->_BaseFormat == GL_LUMINANCE_ALPHA) {
-               rgba[col][GCOMP] = 0.0F;
-               rgba[col][BCOMP] = 0.0F;
+               GLint col;
+               for (col = 0; col < width; col++) {
+                  rgba[col][GCOMP] = 0.0F;
+                  rgba[col][BCOMP] = 0.0F;
+               }
             }
             else if (texImage->_BaseFormat == GL_INTENSITY) {
-               rgba[col][GCOMP] = 0.0F;
-               rgba[col][BCOMP] = 0.0F;
-               rgba[col][ACOMP] = 1.0F;
+               GLint col;
+               for (col = 0; col < width; col++) {
+                  rgba[col][GCOMP] = 0.0F;
+                  rgba[col][BCOMP] = 0.0F;
+                  rgba[col][ACOMP] = 1.0F;
+               }
             }
+
+            _mesa_pack_rgba_span_float(ctx, width, (GLfloat (*)[4]) rgba,
+                                       format, type, dest,
+                                       &ctx->Pack, transferOps);
          }
-         _mesa_pack_rgba_span_float(ctx, width, (GLfloat (*)[4]) rgba,
-                                    format, type, dest,
-                                    &ctx->Pack, transferOps);
       }
-   }
 
-   free(rgba);
+      free(rgba);
+   }
 }
 
 
@@ -520,8 +441,8 @@ _mesa_get_teximage(struct gl_context *ctx, GLenum target, GLint level,
        * texture data to the PBO if the PBO is in VRAM along with the texture.
        */
       GLubyte *buf = (GLubyte *)
-         ctx->Driver.MapBuffer(ctx, GL_PIXEL_PACK_BUFFER_EXT,
-                               GL_WRITE_ONLY_ARB, ctx->Pack.BufferObj);
+         ctx->Driver.MapBufferRange(ctx, 0, ctx->Pack.BufferObj->Size,
+                                   GL_MAP_WRITE_BIT, ctx->Pack.BufferObj);
       if (!buf) {
          /* out of memory or other unexpected error */
          _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage(map PBO failed)");
@@ -536,9 +457,6 @@ _mesa_get_teximage(struct gl_context *ctx, GLenum target, GLint level,
    if (get_tex_memcpy(ctx, format, type, pixels, texObj, texImage)) {
       /* all done */
    }
-   else if (format == GL_COLOR_INDEX) {
-      get_tex_color_index(ctx, dimensions, format, type, pixels, texImage);
-   }
    else if (format == GL_DEPTH_COMPONENT) {
       get_tex_depth(ctx, dimensions, format, type, pixels, texImage);
    }
@@ -548,16 +466,12 @@ _mesa_get_teximage(struct gl_context *ctx, GLenum target, GLint level,
    else if (format == GL_YCBCR_MESA) {
       get_tex_ycbcr(ctx, dimensions, format, type, pixels, texImage);
    }
-   else if (_mesa_get_format_color_encoding(texImage->TexFormat) == GL_SRGB) {
-      get_tex_srgb(ctx, dimensions, format, type, pixels, texImage);
-   }
    else {
       get_tex_rgba(ctx, dimensions, format, type, pixels, texImage);
    }
 
    if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
-      ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_PACK_BUFFER_EXT,
-                              ctx->Pack.BufferObj);
+      ctx->Driver.UnmapBuffer(ctx, ctx->Pack.BufferObj);
    }
 }
 
@@ -582,8 +496,8 @@ _mesa_get_compressed_teximage(struct gl_context *ctx, GLenum target, GLint level
    if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
       /* pack texture image into a PBO */
       GLubyte *buf = (GLubyte *)
-         ctx->Driver.MapBuffer(ctx, GL_PIXEL_PACK_BUFFER_EXT,
-                               GL_WRITE_ONLY_ARB, ctx->Pack.BufferObj);
+         ctx->Driver.MapBufferRange(ctx, 0, ctx->Pack.BufferObj->Size,
+                                   GL_MAP_WRITE_BIT, ctx->Pack.BufferObj);
       if (!buf) {
          /* out of memory or other unexpected error */
          _mesa_error(ctx, GL_OUT_OF_MEMORY,
@@ -613,8 +527,7 @@ _mesa_get_compressed_teximage(struct gl_context *ctx, GLenum target, GLint level
    }
 
    if (_mesa_is_bufferobj(ctx->Pack.BufferObj)) {
-      ctx->Driver.UnmapBuffer(ctx, GL_PIXEL_PACK_BUFFER_EXT,
-                              ctx->Pack.BufferObj);
+      ctx->Driver.UnmapBuffer(ctx, ctx->Pack.BufferObj);
    }
 }
 
@@ -651,16 +564,12 @@ getteximage_error_check(struct gl_context *ctx, GLenum target, GLint level,
    }
 
    if (_mesa_components_in_format(format) <= 0 ||
-       format == GL_STENCIL_INDEX) {
+       format == GL_STENCIL_INDEX ||
+       format == GL_COLOR_INDEX) {
       _mesa_error( ctx, GL_INVALID_ENUM, "glGetTexImage(format)" );
       return GL_TRUE;
    }
 
-   if (!ctx->Extensions.EXT_paletted_texture && _mesa_is_index_format(format)) {
-      _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(format)");
-      return GL_TRUE;
-   }
-
    if (!ctx->Extensions.ARB_depth_texture && _mesa_is_depth_format(format)) {
       _mesa_error(ctx, GL_INVALID_ENUM, "glGetTexImage(format)");
       return GL_TRUE;
@@ -699,17 +608,10 @@ getteximage_error_check(struct gl_context *ctx, GLenum target, GLint level,
    baseFormat = _mesa_get_format_base_format(texImage->TexFormat);
       
    /* Make sure the requested image format is compatible with the
-    * texture's format.  Note that a color index texture can be converted
-    * to RGBA so that combo is allowed.
+    * texture's format.
     */
    if (_mesa_is_color_format(format)
-       && !_mesa_is_color_format(baseFormat)
-       && !_mesa_is_index_format(baseFormat)) {
-      _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
-      return GL_TRUE;
-   }
-   else if (_mesa_is_index_format(format)
-            && !_mesa_is_index_format(baseFormat)) {
+       && !_mesa_is_color_format(baseFormat)) {
       _mesa_error(ctx, GL_INVALID_OPERATION, "glGetTexImage(format mismatch)");
       return GL_TRUE;
    }