mesa: Add missing include guards
[mesa.git] / src / mesa / main / texgetimage.c
index cdbd61877085c83fb010c4ca25d399e966650627..0ab9ed445d63172707c2841e2bc32a77b48701bc 100644 (file)
@@ -88,12 +88,6 @@ get_tex_depth(struct gl_context *ctx, GLuint dimensions,
       return;
    }
 
-   if (texImage->TexObject->Target == GL_TEXTURE_1D_ARRAY) {
-      depth = height;
-      height = 1;
-   }
-
-   assert(zoffset + depth <= texImage->Depth);
    for (img = 0; img < depth; img++) {
       GLubyte *srcMap;
       GLint srcRowStride;
@@ -141,7 +135,6 @@ get_tex_depth_stencil(struct gl_context *ctx, GLuint dimensions,
    assert(type == GL_UNSIGNED_INT_24_8 ||
           type == GL_FLOAT_32_UNSIGNED_INT_24_8_REV);
 
-   assert(zoffset + depth <= texImage->Depth);
    for (img = 0; img < depth; img++) {
       GLubyte *srcMap;
       GLint rowstride;
@@ -233,7 +226,6 @@ get_tex_ycbcr(struct gl_context *ctx, GLuint dimensions,
 {
    GLint img, row;
 
-   assert(zoffset + depth <= texImage->Depth);
    for (img = 0; img < depth; img++) {
       GLubyte *srcMap;
       GLint rowstride;
@@ -273,6 +265,40 @@ get_tex_ycbcr(struct gl_context *ctx, GLuint dimensions,
    }
 }
 
+/**
+ * Depending on the base format involved we may need to apply a rebase
+ * transform (for example: if we download to a Luminance format we want
+ * G=0 and B=0).
+ */
+static bool
+teximage_needs_rebase(mesa_format texFormat, GLenum baseFormat,
+                      bool is_compressed, uint8_t *rebaseSwizzle)
+{
+   bool needsRebase = false;
+
+   if (baseFormat == GL_LUMINANCE ||
+       baseFormat == GL_INTENSITY) {
+      needsRebase = true;
+      rebaseSwizzle[0] = MESA_FORMAT_SWIZZLE_X;
+      rebaseSwizzle[1] = MESA_FORMAT_SWIZZLE_ZERO;
+      rebaseSwizzle[2] = MESA_FORMAT_SWIZZLE_ZERO;
+      rebaseSwizzle[3] = MESA_FORMAT_SWIZZLE_ONE;
+   } else if (baseFormat == GL_LUMINANCE_ALPHA) {
+      needsRebase = true;
+      rebaseSwizzle[0] = MESA_FORMAT_SWIZZLE_X;
+      rebaseSwizzle[1] = MESA_FORMAT_SWIZZLE_ZERO;
+      rebaseSwizzle[2] = MESA_FORMAT_SWIZZLE_ZERO;
+      rebaseSwizzle[3] = MESA_FORMAT_SWIZZLE_W;
+   } else if (!is_compressed &&
+              (baseFormat != _mesa_get_format_base_format(texFormat))) {
+      needsRebase =
+         _mesa_compute_rgba2base2rgba_component_mapping(baseFormat,
+                                                        rebaseSwizzle);
+   }
+
+   return needsRebase;
+}
+
 
 /**
  * Get a color texture image with decompression.
@@ -297,8 +323,7 @@ get_tex_rgba_compressed(struct gl_context *ctx, GLuint dimensions,
    uint8_t rebaseSwizzle[4];
 
    /* Decompress into temp float buffer, then pack into user buffer */
-   tempImage = malloc(width * height * depth
-                                  * 4 * sizeof(GLfloat));
+   tempImage = malloc(width * height * depth * 4 * sizeof(GLfloat));
    if (!tempImage) {
       _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage()");
       return;
@@ -328,26 +353,8 @@ get_tex_rgba_compressed(struct gl_context *ctx, GLuint dimensions,
       }
    }
 
-   /* Depending on the base format involved we may need to apply a rebase
-    * transform (for example: if we download to a Luminance format we want
-    * G=0 and B=0).
-    */
-   if (baseFormat == GL_LUMINANCE ||
-       baseFormat == GL_INTENSITY) {
-      needsRebase = true;
-      rebaseSwizzle[0] = MESA_FORMAT_SWIZZLE_X;
-      rebaseSwizzle[1] = MESA_FORMAT_SWIZZLE_ZERO;
-      rebaseSwizzle[2] = MESA_FORMAT_SWIZZLE_ZERO;
-      rebaseSwizzle[3] = MESA_FORMAT_SWIZZLE_ONE;
-   } else if (baseFormat == GL_LUMINANCE_ALPHA) {
-      needsRebase = true;
-      rebaseSwizzle[0] = MESA_FORMAT_SWIZZLE_X;
-      rebaseSwizzle[1] = MESA_FORMAT_SWIZZLE_ZERO;
-      rebaseSwizzle[2] = MESA_FORMAT_SWIZZLE_ZERO;
-      rebaseSwizzle[3] = MESA_FORMAT_SWIZZLE_W;
-   } else {
-      needsRebase = false;
-   }
+   needsRebase = teximage_needs_rebase(texFormat, baseFormat, true,
+                                       rebaseSwizzle);
 
    srcStride = 4 * width * sizeof(GLfloat);
    dstStride = _mesa_image_row_stride(&ctx->Pack, width, format, type);
@@ -361,6 +368,13 @@ get_tex_rgba_compressed(struct gl_context *ctx, GLuint dimensions,
                            tempSlice, RGBA32_FLOAT, srcStride,
                            width, height,
                            needsRebase ? rebaseSwizzle : NULL);
+
+      /* Handle byte swapping if required */
+      if (ctx->Pack.SwapBytes) {
+         _mesa_swap_bytes_2d_image(format, type, &ctx->Pack,
+                                   width, height, dest, dest);
+      }
+
       tempSlice += 4 * width * height;
    }
 
@@ -425,38 +439,8 @@ get_tex_rgba_uncompressed(struct gl_context *ctx, GLuint dimensions,
    bool needsRebase;
    void *rgba = NULL;
 
-   if (texImage->TexObject->Target == GL_TEXTURE_1D_ARRAY) {
-      depth = height;
-      height = 1;
-      zoffset = yoffset;
-      yoffset = 0;
-   }
-
-   /* Depending on the base format involved we may need to apply a rebase
-    * transform (for example: if we download to a Luminance format we want
-    * G=0 and B=0).
-    */
-   if (texImage->_BaseFormat == GL_LUMINANCE ||
-       texImage->_BaseFormat == GL_INTENSITY) {
-      needsRebase = true;
-      rebaseSwizzle[0] = MESA_FORMAT_SWIZZLE_X;
-      rebaseSwizzle[1] = MESA_FORMAT_SWIZZLE_ZERO;
-      rebaseSwizzle[2] = MESA_FORMAT_SWIZZLE_ZERO;
-      rebaseSwizzle[3] = MESA_FORMAT_SWIZZLE_ONE;
-   } else if (texImage->_BaseFormat == GL_LUMINANCE_ALPHA) {
-      needsRebase = true;
-      rebaseSwizzle[0] = MESA_FORMAT_SWIZZLE_X;
-      rebaseSwizzle[1] = MESA_FORMAT_SWIZZLE_ZERO;
-      rebaseSwizzle[2] = MESA_FORMAT_SWIZZLE_ZERO;
-      rebaseSwizzle[3] = MESA_FORMAT_SWIZZLE_W;
-    } else if (texImage->_BaseFormat !=
-               _mesa_get_format_base_format(texFormat)) {
-      needsRebase =
-         _mesa_compute_rgba2base2rgba_component_mapping(texImage->_BaseFormat,
-                                                        rebaseSwizzle);
-    } else {
-      needsRebase = false;
-    }
+   needsRebase = teximage_needs_rebase(texFormat, texImage->_BaseFormat, false,
+                                       rebaseSwizzle);
 
    /* Describe the dst format */
    dst_is_integer = _mesa_is_enum_format_integer(format);
@@ -511,13 +495,15 @@ get_tex_rgba_uncompressed(struct gl_context *ctx, GLuint dimensions,
           */
          if (format == rgba_format) {
             rgba = dest;
-         } else if (rgba == NULL) { /* Allocate the RGBA buffer only once */
+         } else {
             need_convert = true;
-            rgba = malloc(height * rgba_stride);
-            if (!rgba) {
-               _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage()");
-               ctx->Driver.UnmapTextureImage(ctx, texImage, img);
-               return;
+            if (rgba == NULL) { /* Allocate the RGBA buffer only once */
+               rgba = malloc(height * rgba_stride);
+               if (!rgba) {
+                  _mesa_error(ctx, GL_OUT_OF_MEMORY, "glGetTexImage()");
+                  ctx->Driver.UnmapTextureImage(ctx, texImage, img);
+                  return;
+               }
             }
          }
 
@@ -557,25 +543,16 @@ get_tex_rgba_uncompressed(struct gl_context *ctx, GLuint dimensions,
 
    do_swap:
       /* Handle byte swapping if required */
-      if (ctx->Pack.SwapBytes) {
-         GLint swapSize = _mesa_sizeof_packed_type(type);
-         if (swapSize == 2 || swapSize == 4) {
-            int swapsPerPixel = _mesa_bytes_per_pixel(format, type) / swapSize;
-            assert(_mesa_bytes_per_pixel(format, type) % swapSize == 0);
-            if (swapSize == 2)
-               _mesa_swap2((GLushort *) dest, width * height * swapsPerPixel);
-            else if (swapSize == 4)
-               _mesa_swap4((GLuint *) dest, width * height * swapsPerPixel);
-         }
-      }
+      if (ctx->Pack.SwapBytes)
+         _mesa_swap_bytes_2d_image(format, type, &ctx->Pack,
+                                   width, height, dest, dest);
 
       /* Unmap the src texture buffer */
       ctx->Driver.UnmapTextureImage(ctx, texImage, zoffset + img);
    }
 
 done:
-   if (rgba)
-      free(rgba);
+   free(rgba);
 }
 
 
@@ -651,7 +628,7 @@ get_tex_memcpy(struct gl_context *ctx,
        texBaseFormat == texImage->_BaseFormat) {
       memCopy = _mesa_format_matches_format_and_type(texImage->TexFormat,
                                                      format, type,
-                                                     ctx->Pack.SwapBytes);
+                                                     ctx->Pack.SwapBytes, NULL);
    }
 
    if (depth > 1) {
@@ -677,7 +654,7 @@ get_tex_memcpy(struct gl_context *ctx,
 
       if (src) {
          if (bytesPerRow == dstRowStride && bytesPerRow == srcRowStride) {
-            memcpy(dst, src, bytesPerRow * texImage->Height);
+            memcpy(dst, src, bytesPerRow * height);
          }
          else {
             GLuint row;
@@ -739,6 +716,17 @@ _mesa_GetTexSubImage_sw(struct gl_context *ctx,
       pixels = ADD_POINTERS(buf, pixels);
    }
 
+   /* for all array textures, the Z axis selects the layer */
+   if (texImage->TexObject->Target == GL_TEXTURE_1D_ARRAY) {
+      depth = height;
+      height = 1;
+      zoffset = yoffset;
+      yoffset = 0;
+      assert(zoffset + depth <= texImage->Height);
+   } else {
+      assert(zoffset + depth <= texImage->Depth);
+   }
+
    if (get_tex_memcpy(ctx, xoffset, yoffset, zoffset, width, height, depth,
                       format, type, pixels, texImage)) {
       /* all done */
@@ -773,16 +761,15 @@ _mesa_GetTexSubImage_sw(struct gl_context *ctx,
 
 
 /**
- * This is the software fallback for Driver.GetCompressedTexSubImage().
- * All error checking will have been done before this routine is called.
+ * This function assumes that all error checking has been done.
  */
-void
-_mesa_GetCompressedTexSubImage_sw(struct gl_context *ctx,
-                                  struct gl_texture_image *texImage,
-                                  GLint xoffset, GLint yoffset,
-                                  GLint zoffset, GLsizei width,
-                                  GLint height, GLint depth,
-                                  GLvoid *img)
+static void
+get_compressed_texsubimage_sw(struct gl_context *ctx,
+                              struct gl_texture_image *texImage,
+                              GLint xoffset, GLint yoffset,
+                              GLint zoffset, GLsizei width,
+                              GLint height, GLint depth,
+                              GLvoid *img)
 {
    const GLuint dimensions =
       _mesa_get_texture_dimensions(texImage->TexObject->Target);
@@ -875,12 +862,12 @@ legal_getteximage_target(struct gl_context *ctx, GLenum target, bool dsa)
     *    the targets from table 8.19 (for GetTexImage and GetnTexImage *only*),
     *    or TEXTURE_CUBE_MAP (for GetTextureImage *only*)." (Emphasis added.)
     */
-   case GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB:
-   case GL_TEXTURE_CUBE_MAP_NEGATIVE_X_ARB:
-   case GL_TEXTURE_CUBE_MAP_POSITIVE_Y_ARB:
-   case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y_ARB:
-   case GL_TEXTURE_CUBE_MAP_POSITIVE_Z_ARB:
-   case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z_ARB:
+   case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
+   case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
+   case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
+   case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
+   case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
+   case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
       return dsa ? GL_FALSE : ctx->Extensions.ARB_texture_cube_map;
    case GL_TEXTURE_CUBE_MAP:
       return dsa ? GL_TRUE : GL_FALSE;
@@ -892,7 +879,7 @@ legal_getteximage_target(struct gl_context *ctx, GLenum target, bool dsa)
 
 /**
  * Wrapper for _mesa_select_tex_image() which can handle target being
- * GL_TEXTURE_CUBE_MAP_ARB in which case we use zoffset to select a cube face.
+ * GL_TEXTURE_CUBE_MAP in which case we use zoffset to select a cube face.
  * This can happen for glGetTextureImage and glGetTextureSubImage (DSA
  * functions).
  */
@@ -926,14 +913,6 @@ dimensions_error_check(struct gl_context *ctx,
                        const char *caller)
 {
    const struct gl_texture_image *texImage;
-   int i;
-
-   if (width == 0 || height == 0 || depth == 0) {
-      /* Not an error, but nothing to do.  Return 'true' so that the
-       * caller simply returns.
-       */
-      return true;
-   }
 
    if (xoffset < 0) {
       _mesa_error(ctx, GL_INVALID_VALUE, "%s(xoffset = %d)", caller, xoffset);
@@ -1002,15 +981,20 @@ dimensions_error_check(struct gl_context *ctx,
                      "%s(zoffset + depth = %d)", caller, zoffset + depth);
          return true;
       }
-      /* check that the range of faces exist */
-      for (i = 0; i < depth; i++) {
-         GLenum face = GL_TEXTURE_CUBE_MAP_POSITIVE_X + zoffset + i;
-         if (!_mesa_select_tex_image(texObj, face, level)) {
-            /* non-existant face */
-            _mesa_error(ctx, GL_INVALID_OPERATION,
-                        "%s(missing cube face)", caller);
-            return true;
-         }
+      /* According to OpenGL 4.6 spec, section 8.11.4 ("Texture Image Queries"):
+       *
+       *   "An INVALID_OPERATION error is generated by GetTextureImage if the
+       *   effective target is TEXTURE_CUBE_MAP or TEXTURE_CUBE_MAP_ARRAY ,
+       *   and the texture object is not cube complete or cube array complete,
+       *   respectively."
+       *
+       * This applies also to GetTextureSubImage, GetCompressedTexImage,
+       * GetCompressedTextureImage, and GetnCompressedTexImage.
+       */
+      if (!_mesa_cube_complete(texObj)) {
+         _mesa_error(ctx, GL_INVALID_OPERATION,
+                     "%s(cube incomplete)", caller);
+         return true;
       }
       break;
    default:
@@ -1019,8 +1003,31 @@ dimensions_error_check(struct gl_context *ctx,
 
    texImage = select_tex_image(texObj, target, level, zoffset);
    if (!texImage) {
-      /* missing texture image */
-      _mesa_error(ctx, GL_INVALID_OPERATION, "%s(missing image)", caller);
+      /* Trying to return a non-defined level is a valid operation per se, as
+       * OpenGL 4.6 spec, section 8.11.4 ("Texture Image Queries") does not
+       * handle this case as an error.
+       *
+       * Rather, we need to look at section 8.22 ("Texture State and Proxy
+       * State"):
+       *
+       *   "Each initial texture image is null. It has zero width, height, and
+       *    depth, internal format RGBA, or R8 for buffer textures, component
+       *    sizes set to zero and component types set to NONE, the compressed
+       *    flag set to FALSE, a zero compressed size, and the bound buffer
+       *    object name is zero."
+       *
+       * This means we need to assume the image for the non-defined level is
+       * an empty image. With this assumption, we can go back to section
+       * 8.11.4 and checking again the errors:
+       *
+       *   "An INVALID_VALUE error is generated if xoffset + width is greater
+       *    than the texture’s width, yoffset + height is greater than the
+       *    texture’s height, or zoffset + depth is greater than the texture’s
+       *    depth."
+       *
+       * Thus why we return INVALID_VALUE.
+       */
+      _mesa_error(ctx, GL_INVALID_VALUE, "%s(missing image)", caller);
       return true;
    }
 
@@ -1050,9 +1057,9 @@ dimensions_error_check(struct gl_context *ctx,
 
    /* Extra checks for compressed textures */
    {
-      GLuint bw, bh;
-      _mesa_get_format_block_size(texImage->TexFormat, &bw, &bh);
-      if (bw > 1 || bh > 1) {
+      GLuint bw, bh, bd;
+      _mesa_get_format_block_size_3d(texImage->TexFormat, &bw, &bh, &bd);
+      if (bw > 1 || bh > 1 || bd > 1) {
          /* offset must be multiple of block size */
          if (xoffset % bw != 0) {
             _mesa_error(ctx, GL_INVALID_VALUE,
@@ -1067,7 +1074,13 @@ dimensions_error_check(struct gl_context *ctx,
             }
          }
 
-         /* The size must be a multiple of bw x bh, or we must be using a
+         if (zoffset % bd != 0) {
+            _mesa_error(ctx, GL_INVALID_VALUE,
+                        "%s(zoffset = %d)", caller, zoffset);
+            return true;
+         }
+
+         /* The size must be a multiple of bw x bh x bd, or we must be using a
           * offset+size that exactly hits the edge of the image.
           */
          if ((width % bw != 0) &&
@@ -1083,9 +1096,23 @@ dimensions_error_check(struct gl_context *ctx,
                         "%s(height = %d)", caller, height);
             return true;
          }
+
+         if ((depth % bd != 0) &&
+             (zoffset + depth != (GLint) texImage->Depth)) {
+            _mesa_error(ctx, GL_INVALID_VALUE,
+                        "%s(depth = %d)", caller, depth);
+            return true;
+         }
       }
    }
 
+   if (width == 0 || height == 0 || depth == 0) {
+      /* Not an error, but nothing to do.  Return 'true' so that the
+       * caller simply returns.
+       */
+      return true;
+   }
+
    return false;
 }
 
@@ -1213,6 +1240,13 @@ getteximage_error_check(struct gl_context *ctx,
                   "%s(format=GL_STENCIL_INDEX)", caller);
       return true;
    }
+   else if (_mesa_is_stencil_format(format)
+           && !_mesa_is_depthstencil_format(baseFormat)
+           && !_mesa_is_stencil_format(baseFormat)) {
+      _mesa_error(ctx, GL_INVALID_OPERATION,
+                  "%s(format mismatch)", caller);
+      return true;
+   }
    else if (_mesa_is_ycbcr_format(format)
             && !_mesa_is_ycbcr_format(baseFormat)) {
       _mesa_error(ctx, GL_INVALID_OPERATION,
@@ -1421,6 +1455,11 @@ _mesa_GetTextureImage(GLuint texture, GLint level, GLenum format, GLenum type,
       return;
    }
 
+   if (!legal_getteximage_target(ctx, texObj->Target, true)) {
+      _mesa_error(ctx, GL_INVALID_OPERATION, "%s", caller);
+      return;
+   }
+
    get_texture_image_dims(texObj, texObj->Target, level,
                           &width, &height, &depth);
 
@@ -1452,6 +1491,12 @@ _mesa_GetTextureSubImage(GLuint texture, GLint level,
       return;
    }
 
+   if (!legal_getteximage_target(ctx, texObj->Target, true)) {
+      _mesa_error(ctx, GL_INVALID_OPERATION,
+                  "%s(buffer/multisample texture)", caller);
+      return;
+   }
+
    if (getteximage_error_check(ctx, texObj, texObj->Target, level,
                                xoffset, yoffset, zoffset, width, height, depth,
                                format, type, bufSize, pixels, caller)) {
@@ -1642,9 +1687,9 @@ get_compressed_texture_image(struct gl_context *ctx,
       texImage = texObj->Image[firstFace + i][level];
       assert(texImage);
 
-      ctx->Driver.GetCompressedTexSubImage(ctx, texImage,
-                                           xoffset, yoffset, zoffset,
-                                           width, height, depth, pixels);
+      get_compressed_texsubimage_sw(ctx, texImage,
+                                    xoffset, yoffset, zoffset,
+                                    width, height, depth, pixels);
 
       /* next cube face */
       pixels = (GLubyte *) pixels + imageStride;
@@ -1754,7 +1799,7 @@ _mesa_GetCompressedTextureSubImage(GLuint texture, GLint level,
 {
    GET_CURRENT_CONTEXT(ctx);
    static const char *caller = "glGetCompressedTextureImage";
-   struct gl_texture_object *texObj;
+   struct gl_texture_object *texObj = NULL;
 
    texObj = _mesa_lookup_texture_err(ctx, texture, caller);
    if (!texObj) {