mesa: Fix valid texture target test in _mesa_GetTexLevelParameteriv()
[mesa.git] / src / mesa / main / teximage.c
index ad7a1ea0c8d0bc891830fff74af8126575e6fefc..c0e5b9bec14df5486d191ca4e0bef1b3351fad64 100644 (file)
@@ -69,8 +69,6 @@
  *
  * This is the format which is used during texture application (i.e. the
  * texture format and env mode determine the arithmetic used.
- *
- * XXX this could be static
  */
 GLint
 _mesa_base_tex_format( struct gl_context *ctx, GLint internalFormat )
@@ -323,7 +321,8 @@ _mesa_base_tex_format( struct gl_context *ctx, GLint internalFormat )
    }
 #endif /* FEATURE_EXT_texture_sRGB */
 
-   if (ctx->Extensions.EXT_texture_integer) {
+   if (ctx->VersionMajor >= 3 ||
+       ctx->Extensions.EXT_texture_integer) {
       switch (internalFormat) {
       case GL_RGBA8UI_EXT:
       case GL_RGBA16UI_EXT:
@@ -340,6 +339,11 @@ _mesa_base_tex_format( struct gl_context *ctx, GLint internalFormat )
       case GL_RGB16I_EXT:
       case GL_RGB32I_EXT:
          return GL_RGB;
+      }
+   }
+
+   if (ctx->Extensions.EXT_texture_integer) {
+      switch (internalFormat) {
       case GL_ALPHA8UI_EXT:
       case GL_ALPHA16UI_EXT:
       case GL_ALPHA32UI_EXT:
@@ -391,7 +395,7 @@ _mesa_base_tex_format( struct gl_context *ctx, GLint internalFormat )
       case GL_R16UI:
       case GL_R32I:
       case GL_R32UI:
-        if (!ctx->Extensions.EXT_texture_integer)
+        if (ctx->VersionMajor < 3 && !ctx->Extensions.EXT_texture_integer)
            break;
         /* FALLTHROUGH */
       case GL_R8:
@@ -416,7 +420,7 @@ _mesa_base_tex_format( struct gl_context *ctx, GLint internalFormat )
       case GL_RG16UI:
       case GL_RG32I:
       case GL_RG32UI:
-        if (!ctx->Extensions.EXT_texture_integer)
+        if (ctx->VersionMajor < 3 && !ctx->Extensions.EXT_texture_integer)
            break;
         /* FALLTHROUGH */
       case GL_RG:
@@ -824,7 +828,7 @@ _mesa_get_proxy_tex_image(struct gl_context *ctx, GLenum target, GLint level)
    struct gl_texture_image *texImage;
    GLuint texIndex;
 
-   if (level < 0 )
+   if (level < 0)
       return NULL;
 
    switch (target) {
@@ -911,7 +915,6 @@ _mesa_max_texture_levels(struct gl_context *ctx, GLenum target)
    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_ARB:
    case GL_PROXY_TEXTURE_CUBE_MAP_ARB:
       return ctx->Extensions.ARB_texture_cube_map
          ? ctx->Const.MaxCubeTextureLevels : 0;
@@ -1059,7 +1062,6 @@ clear_teximage_fields(struct gl_texture_image *img)
  * Initialize basic fields of the gl_texture_image struct.
  *
  * \param ctx GL context.
- * \param target texture target (GL_TEXTURE_1D, GL_TEXTURE_RECTANGLE, etc).
  * \param img texture image structure to be initialized.
  * \param width image width.
  * \param height image height.
@@ -1072,17 +1074,19 @@ clear_teximage_fields(struct gl_texture_image *img)
  * Note: width, height and depth include the border.
  */
 void
-_mesa_init_teximage_fields(struct gl_context *ctx, GLenum target,
+_mesa_init_teximage_fields(struct gl_context *ctx,
                            struct gl_texture_image *img,
                            GLsizei width, GLsizei height, GLsizei depth,
                            GLint border, GLenum internalFormat,
                            gl_format format)
 {
+   GLenum target;
    ASSERT(img);
    ASSERT(width >= 0);
    ASSERT(height >= 0);
    ASSERT(depth >= 0);
 
+   target = img->TexObject->Target;
    img->_BaseFormat = _mesa_base_tex_format( ctx, internalFormat );
    ASSERT(img->_BaseFormat > 0);
    img->InternalFormat = internalFormat;
@@ -1094,26 +1098,72 @@ _mesa_init_teximage_fields(struct gl_context *ctx, GLenum target,
    img->Width2 = width - 2 * border;   /* == 1 << img->WidthLog2; */
    img->WidthLog2 = _mesa_logbase2(img->Width2);
 
-   if (height == 1) { /* 1-D texture */
-      img->Height2 = 1;
+   switch(target) {
+   case GL_TEXTURE_1D:
+   case GL_TEXTURE_BUFFER:
+   case GL_PROXY_TEXTURE_1D:
+      if (height == 0)
+         img->Height2 = 0;
+      else
+         img->Height2 = 1;
       img->HeightLog2 = 0;
-   }
-   else {
+      if (depth == 0)
+         img->Depth2 = 0;
+      else
+         img->Depth2 = 1;
+      img->DepthLog2 = 0;
+      break;
+   case GL_TEXTURE_1D_ARRAY:
+   case GL_PROXY_TEXTURE_1D_ARRAY:
+      img->Height2 = height; /* no border */
+      img->HeightLog2 = 0; /* not used */
+      if (depth == 0)
+         img->Depth2 = 0;
+      else
+         img->Depth2 = 1;
+      img->DepthLog2 = 0;
+      break;
+   case GL_TEXTURE_2D:
+   case GL_TEXTURE_RECTANGLE:
+   case GL_TEXTURE_CUBE_MAP:
+   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:
+   case GL_TEXTURE_EXTERNAL_OES:
+   case GL_PROXY_TEXTURE_2D:
+   case GL_PROXY_TEXTURE_RECTANGLE:
+   case GL_PROXY_TEXTURE_CUBE_MAP:
       img->Height2 = height - 2 * border; /* == 1 << img->HeightLog2; */
       img->HeightLog2 = _mesa_logbase2(img->Height2);
-   }
-
-   if (depth == 1) {  /* 2-D texture */
-      img->Depth2 = 1;
+      if (depth == 0)
+         img->Depth2 = 0;
+      else
+         img->Depth2 = 1;
       img->DepthLog2 = 0;
-   }
-   else {
+      break;
+   case GL_TEXTURE_2D_ARRAY:
+   case GL_PROXY_TEXTURE_2D_ARRAY:
+      img->Height2 = height - 2 * border; /* == 1 << img->HeightLog2; */
+      img->HeightLog2 = _mesa_logbase2(img->Height2);
+      img->Depth2 = depth; /* no border */
+      img->DepthLog2 = 0; /* not used */
+      break;
+   case GL_TEXTURE_3D:
+   case GL_PROXY_TEXTURE_3D:
+      img->Height2 = height - 2 * border; /* == 1 << img->HeightLog2; */
+      img->HeightLog2 = _mesa_logbase2(img->Height2);
       img->Depth2 = depth - 2 * border;   /* == 1 << img->DepthLog2; */
       img->DepthLog2 = _mesa_logbase2(img->Depth2);
+      break;
+   default:
+      _mesa_problem(NULL, "invalid target 0x%x in _mesa_init_teximage_fields()",
+                    target);
    }
 
    img->MaxLog2 = MAX2(img->WidthLog2, img->HeightLog2);
-
    img->TexFormat = format;
 }
 
@@ -1174,7 +1224,7 @@ _mesa_test_proxy_teximage(struct gl_context *ctx, GLenum target, GLint level,
    switch (target) {
    case GL_PROXY_TEXTURE_1D:
       maxSize = 1 << (ctx->Const.MaxTextureLevels - 1);
-      if (width < 2 * border || width > 2 + maxSize)
+      if (width < 2 * border || width > 2 * border + maxSize)
          return GL_FALSE;
       if (level >= ctx->Const.MaxTextureLevels)
          return GL_FALSE;
@@ -1186,9 +1236,9 @@ _mesa_test_proxy_teximage(struct gl_context *ctx, GLenum target, GLint level,
 
    case GL_PROXY_TEXTURE_2D:
       maxSize = 1 << (ctx->Const.MaxTextureLevels - 1);
-      if (width < 2 * border || width > 2 + maxSize)
+      if (width < 2 * border || width > 2 * border + maxSize)
          return GL_FALSE;
-      if (height < 2 * border || height > 2 + maxSize)
+      if (height < 2 * border || height > 2 * border + maxSize)
          return GL_FALSE;
       if (level >= ctx->Const.MaxTextureLevels)
          return GL_FALSE;
@@ -1202,11 +1252,11 @@ _mesa_test_proxy_teximage(struct gl_context *ctx, GLenum target, GLint level,
 
    case GL_PROXY_TEXTURE_3D:
       maxSize = 1 << (ctx->Const.Max3DTextureLevels - 1);
-      if (width < 2 * border || width > 2 + maxSize)
+      if (width < 2 * border || width > 2 * border + maxSize)
          return GL_FALSE;
-      if (height < 2 * border || height > 2 + maxSize)
+      if (height < 2 * border || height > 2 * border + maxSize)
          return GL_FALSE;
-      if (depth < 2 * border || depth > 2 + maxSize)
+      if (depth < 2 * border || depth > 2 * border + maxSize)
          return GL_FALSE;
       if (level >= ctx->Const.Max3DTextureLevels)
          return GL_FALSE;
@@ -1232,9 +1282,9 @@ _mesa_test_proxy_teximage(struct gl_context *ctx, GLenum target, GLint level,
 
    case GL_PROXY_TEXTURE_CUBE_MAP_ARB:
       maxSize = 1 << (ctx->Const.MaxCubeTextureLevels - 1);
-      if (width < 2 * border || width > 2 + maxSize)
+      if (width < 2 * border || width > 2 * border + maxSize)
          return GL_FALSE;
-      if (height < 2 * border || height > 2 + maxSize)
+      if (height < 2 * border || height > 2 * border + maxSize)
          return GL_FALSE;
       if (level >= ctx->Const.MaxCubeTextureLevels)
          return GL_FALSE;
@@ -1248,7 +1298,7 @@ _mesa_test_proxy_teximage(struct gl_context *ctx, GLenum target, GLint level,
 
    case GL_PROXY_TEXTURE_1D_ARRAY_EXT:
       maxSize = 1 << (ctx->Const.MaxTextureLevels - 1);
-      if (width < 2 * border || width > 2 + maxSize)
+      if (width < 2 * border || width > 2 * border + maxSize)
          return GL_FALSE;
       if (height < 1 || height > ctx->Const.MaxArrayTextureLayers)
          return GL_FALSE;
@@ -1262,9 +1312,9 @@ _mesa_test_proxy_teximage(struct gl_context *ctx, GLenum target, GLint level,
 
    case GL_PROXY_TEXTURE_2D_ARRAY_EXT:
       maxSize = 1 << (ctx->Const.MaxTextureLevels - 1);
-      if (width < 2 * border || width > 2 + maxSize)
+      if (width < 2 * border || width > 2 * border + maxSize)
          return GL_FALSE;
-      if (height < 2 * border || height > 2 + maxSize)
+      if (height < 2 * border || height > 2 * border + maxSize)
          return GL_FALSE;
       if (depth < 1 || depth > ctx->Const.MaxArrayTextureLayers)
          return GL_FALSE;
@@ -1506,6 +1556,7 @@ texture_error_check( struct gl_context *ctx,
    const GLboolean isProxy = target == proxyTarget;
    GLboolean sizeOK = GL_TRUE;
    GLboolean colorFormat;
+   GLenum err;
 
    /* Even though there are no color-index textures, we still have to support
     * uploading color-index data and remapping it to RGB via the
@@ -1574,16 +1625,10 @@ texture_error_check( struct gl_context *ctx,
    }
 
    /* Check incoming image format and type */
-   if (!_mesa_is_legal_format_and_type(ctx, format, type)) {
-      /* Normally, GL_INVALID_OPERATION is generated by a format/type
-       * mismatch (see the 1.2 spec page 94, sec 3.6.4.).  But with the
-       * GL_EXT_texture_integer extension, some combinations should generate
-       * GL_INVALID_ENUM instead (grr!).
-       */
+   err = _mesa_error_check_format_and_type(ctx, format, type);
+   if (err != GL_NO_ERROR) {
       if (!isProxy) {
-         GLenum error = _mesa_is_integer_format(format)
-            ? GL_INVALID_ENUM : GL_INVALID_OPERATION;
-         _mesa_error(ctx, error,
+         _mesa_error(ctx, err,
                      "glTexImage%dD(incompatible format 0x%x, type 0x%x)",
                      dimensions, format, type);
       }
@@ -1681,7 +1726,7 @@ texture_error_check( struct gl_context *ctx,
    }
 
    /* additional checks for integer textures */
-   if (ctx->Extensions.EXT_texture_integer &&
+   if ((ctx->VersionMajor >= 3 || ctx->Extensions.EXT_texture_integer) &&
        (_mesa_is_integer_format(format) !=
         _mesa_is_integer_format(internalFormat))) {
       if (!isProxy) {
@@ -1732,6 +1777,8 @@ subtexture_error_check( struct gl_context *ctx, GLuint dimensions,
                         GLint width, GLint height, GLint depth,
                         GLenum format, GLenum type )
 {
+   GLenum err;
+
    /* Basic level check */
    if (level < 0 || level >= MAX_TEXTURE_LEVELS) {
       _mesa_error(ctx, GL_INVALID_ENUM, "glTexSubImage2D(level=%d)", level);
@@ -1755,13 +1802,9 @@ subtexture_error_check( struct gl_context *ctx, GLuint dimensions,
       return GL_TRUE;
    }
 
-   if (!_mesa_is_legal_format_and_type(ctx, format, type)) {
-      /* As with the glTexImage2D check above, the error code here
-       * depends on texture integer.
-       */
-      GLenum error = _mesa_is_integer_format(format)
-         ? GL_INVALID_OPERATION : GL_INVALID_ENUM;
-      _mesa_error(ctx, error,
+   err = _mesa_error_check_format_and_type(ctx, format, type);
+   if (err != GL_NO_ERROR) {
+      _mesa_error(ctx, err,
                   "glTexSubImage%dD(incompatible format 0x%x, type 0x%x)",
                   dimensions, format, type);
       return GL_TRUE;
@@ -1800,23 +1843,25 @@ subtexture_error_check2( struct gl_context *ctx, GLuint dimensions,
       return GL_TRUE;
    }
    if (dimensions > 1) {
-      if (yoffset < -((GLint)destTex->Border)) {
+      GLint yBorder = (target == GL_TEXTURE_1D_ARRAY) ? 0 : destTex->Border;
+      if (yoffset < -yBorder) {
          _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage%dD(yoffset)",
                      dimensions);
          return GL_TRUE;
       }
-      if (yoffset + height > (GLint) (destTex->Height + destTex->Border)) {
+      if (yoffset + height > (GLint) destTex->Height + yBorder) {
          _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage%dD(yoffset+height)",
                      dimensions);
          return GL_TRUE;
       }
    }
    if (dimensions > 2) {
-      if (zoffset < -((GLint)destTex->Border)) {
+      GLint zBorder = (target == GL_TEXTURE_2D_ARRAY) ? 0 : destTex->Border;
+      if (zoffset < -zBorder) {
          _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage3D(zoffset)");
          return GL_TRUE;
       }
-      if (zoffset + depth  > (GLint) (destTex->Depth + destTex->Border)) {
+      if (zoffset + depth  > (GLint) destTex->Depth + zBorder) {
          _mesa_error(ctx, GL_INVALID_VALUE, "glTexSubImage3D(zoffset+depth)");
          return GL_TRUE;
       }
@@ -1854,6 +1899,17 @@ subtexture_error_check2( struct gl_context *ctx, GLuint dimensions,
       }         
    }
 
+   if (ctx->VersionMajor >= 3 || ctx->Extensions.EXT_texture_integer) {
+      /* both source and dest must be integer-valued, or neither */
+      if (_mesa_is_format_integer_color(destTex->TexFormat) !=
+          _mesa_is_integer_format(format)) {
+         _mesa_error(ctx, GL_INVALID_OPERATION,
+                     "glTexSubImage%dD(integer/non-integer format mismatch)",
+                     dimensions);
+         return GL_TRUE;
+      }
+   }
+
    return GL_FALSE;
 }
 
@@ -1901,7 +1957,7 @@ copytexture_error_check( struct gl_context *ctx, GLuint dimensions,
    }
 
    /* Check that the source buffer is complete */
-   if (ctx->ReadBuffer->Name) {
+   if (_mesa_is_user_fbo(ctx->ReadBuffer)) {
       if (ctx->ReadBuffer->_Status == 0) {
          _mesa_test_framebuffer_completeness(ctx, ctx->ReadBuffer);
       }
@@ -1910,6 +1966,13 @@ copytexture_error_check( struct gl_context *ctx, GLuint dimensions,
                      "glCopyTexImage%dD(invalid readbuffer)", dimensions);
          return GL_TRUE;
       }
+
+      if (ctx->ReadBuffer->Visual.samples > 0) {
+        _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION,
+                    "glCopyTexImage%dD(multisample FBO)",
+                    dimensions);
+        return GL_TRUE;
+      }
    }
 
    /* Check border */
@@ -1932,6 +1995,24 @@ copytexture_error_check( struct gl_context *ctx, GLuint dimensions,
       return GL_TRUE;
    }
 
+   /* From the EXT_texture_integer spec:
+    *
+    *     "INVALID_OPERATION is generated by CopyTexImage* and CopyTexSubImage*
+    *      if the texture internalformat is an integer format and the read color
+    *      buffer is not an integer format, or if the internalformat is not an
+    *      integer format and the read color buffer is an integer format."
+    */
+   if (_mesa_is_color_format(internalFormat)) {
+      struct gl_renderbuffer *rb = ctx->ReadBuffer->_ColorReadBuffer;
+
+      if (_mesa_is_integer_format(rb->InternalFormat) !=
+         _mesa_is_integer_format(internalFormat)) {
+        _mesa_error(ctx, GL_INVALID_OPERATION,
+                    "glCopyTexImage%dD(integer vs non-integer)", dimensions);
+        return GL_TRUE;
+      }
+   }
+
    /* Do size, level checking */
    sizeOK = (proxyTarget == GL_PROXY_TEXTURE_CUBE_MAP_ARB)
       ? (width == height) : 1;
@@ -2000,7 +2081,7 @@ copytexsubimage_error_check1( struct gl_context *ctx, GLuint dimensions,
                               GLenum target, GLint level)
 {
    /* Check that the source buffer is complete */
-   if (ctx->ReadBuffer->Name) {
+   if (_mesa_is_user_fbo(ctx->ReadBuffer)) {
       if (ctx->ReadBuffer->_Status == 0) {
          _mesa_test_framebuffer_completeness(ctx, ctx->ReadBuffer);
       }
@@ -2009,6 +2090,13 @@ copytexsubimage_error_check1( struct gl_context *ctx, GLuint dimensions,
                      "glCopyTexImage%dD(invalid readbuffer)", dimensions);
          return GL_TRUE;
       }
+
+      if (ctx->ReadBuffer->Visual.samples > 0) {
+        _mesa_error(ctx, GL_INVALID_FRAMEBUFFER_OPERATION,
+                    "glCopyTexSubImage%dD(multisample FBO)",
+                    dimensions);
+        return GL_TRUE;
+      }
    }
 
    /* check target (proxies not allowed) */
@@ -2076,13 +2164,14 @@ copytexsubimage_error_check2( struct gl_context *ctx, GLuint dimensions,
       return GL_TRUE;
    }
    if (dimensions > 1) {
-      if (yoffset < -((GLint)teximage->Border)) {
+      GLint yBorder = (target == GL_TEXTURE_1D_ARRAY) ? 0 : teximage->Border;
+      if (yoffset < -yBorder) {
          _mesa_error(ctx, GL_INVALID_VALUE,
                      "glCopyTexSubImage%dD(yoffset=%d)", dimensions, yoffset);
          return GL_TRUE;
       }
       /* NOTE: we're adding the border here, not subtracting! */
-      if (yoffset + height > (GLint) (teximage->Height + teximage->Border)) {
+      if (yoffset + height > (GLint) teximage->Height + yBorder) {
          _mesa_error(ctx, GL_INVALID_VALUE,
                      "glCopyTexSubImage%dD(yoffset+height)", dimensions);
          return GL_TRUE;
@@ -2091,12 +2180,13 @@ copytexsubimage_error_check2( struct gl_context *ctx, GLuint dimensions,
 
    /* check z offset */
    if (dimensions > 2) {
-      if (zoffset < -((GLint)teximage->Border)) {
+      GLint zBorder = (target == GL_TEXTURE_2D_ARRAY) ? 0 : teximage->Border;
+      if (zoffset < -zBorder) {
          _mesa_error(ctx, GL_INVALID_VALUE,
                      "glCopyTexSubImage%dD(zoffset)", dimensions);
          return GL_TRUE;
       }
-      if (zoffset > (GLint) (teximage->Depth + teximage->Border)) {
+      if (zoffset > (GLint) teximage->Depth + zBorder) {
          _mesa_error(ctx, GL_INVALID_VALUE,
                      "glCopyTexSubImage%dD(zoffset+depth)", dimensions);
          return GL_TRUE;
@@ -2140,16 +2230,21 @@ copytexsubimage_error_check2( struct gl_context *ctx, GLuint dimensions,
       return GL_TRUE;
    }
 
-   /* If copying into an integer texture, the source buffer must also be
-    * integer-valued.
+   /* From the EXT_texture_integer spec:
+    *
+    *     "INVALID_OPERATION is generated by CopyTexImage* and CopyTexSubImage*
+    *      if the texture internalformat is an integer format and the read color
+    *      buffer is not an integer format, or if the internalformat is not an
+    *      integer format and the read color buffer is an integer format."
     */
-   if (_mesa_is_format_integer_color(teximage->TexFormat)) {
+   if (_mesa_is_color_format(teximage->InternalFormat)) {
       struct gl_renderbuffer *rb = ctx->ReadBuffer->_ColorReadBuffer;
-      if (!_mesa_is_format_integer_color(rb->Format)) {
-         _mesa_error(ctx, GL_INVALID_OPERATION,
-                  "glCopyTexSubImage%dD(source buffer is not integer format)",
-                  dimensions);
-         return GL_TRUE;
+
+      if (_mesa_is_format_integer_color(rb->Format) !=
+         _mesa_is_format_integer_color(teximage->TexFormat)) {
+        _mesa_error(ctx, GL_INVALID_OPERATION,
+                    "glCopyTexImage%dD(integer vs non-integer)", dimensions);
+        return GL_TRUE;
       }
    }
 
@@ -2180,7 +2275,7 @@ check_rtt_cb(GLuint key, void *data, void *userData)
    const GLuint level = info->level, face = info->face;
 
    /* If this is a user-created FBO */
-   if (fb->Name) {
+   if (_mesa_is_user_fbo(fb)) {
       GLuint i;
       /* check if any of the FBO's attachments point to 'texObj' */
       for (i = 0; i < BUFFER_COUNT; i++) {
@@ -2422,7 +2517,7 @@ teximage(struct gl_context *ctx, GLuint dims,
                                                            format, type);
 
          if (legal_texture_size(ctx, texFormat, width, height, depth)) {
-            _mesa_init_teximage_fields(ctx, target, texImage, width, height,
+            _mesa_init_teximage_fields(ctx, texImage, width, height,
                                        depth, border, internalFormat,
                                        texFormat);
          }
@@ -2473,7 +2568,7 @@ teximage(struct gl_context *ctx, GLuint dims,
                                                     type);
 
             if (legal_texture_size(ctx, texFormat, width, height, depth)) {
-               _mesa_init_teximage_fields(ctx, target, texImage,
+               _mesa_init_teximage_fields(ctx, texImage,
                                           width, height, depth,
                                           border, internalFormat, texFormat);
 
@@ -2481,22 +2576,19 @@ teximage(struct gl_context *ctx, GLuint dims,
                ASSERT(ctx->Driver.TexImage3D);
                switch (dims) {
                case 1:
-                  ctx->Driver.TexImage1D(ctx, target, level, internalFormat,
+                  ctx->Driver.TexImage1D(ctx, texImage, internalFormat,
                                          width, border, format,
-                                         type, pixels, unpack, texObj,
-                                         texImage);
+                                         type, pixels, unpack);
                   break;
                case 2:
-                  ctx->Driver.TexImage2D(ctx, target, level, internalFormat,
+                  ctx->Driver.TexImage2D(ctx, texImage, internalFormat,
                                          width, height, border, format,
-                                         type, pixels, unpack, texObj,
-                                         texImage);
+                                         type, pixels, unpack);
                   break;
                case 3:
-                  ctx->Driver.TexImage3D(ctx, target, level, internalFormat,
+                  ctx->Driver.TexImage3D(ctx, texImage, internalFormat,
                                          width, height, depth, border, format,
-                                         type, pixels, unpack, texObj,
-                                         texImage);
+                                         type, pixels, unpack);
                   break;
                default:
                   _mesa_problem(ctx, "invalid dims=%u in teximage()", dims);
@@ -2670,10 +2762,12 @@ texsubimage(struct gl_context *ctx, GLuint dims, GLenum target, GLint level,
          /* If we have a border, offset=-1 is legal.  Bias by border width. */
          switch (dims) {
          case 3:
-            zoffset += texImage->Border;
+            if (target != GL_TEXTURE_2D_ARRAY)
+               zoffset += texImage->Border;
             /* fall-through */
          case 2:
-            yoffset += texImage->Border;
+            if (target != GL_TEXTURE_1D_ARRAY)
+               yoffset += texImage->Border;
             /* fall-through */
          case 1:
             xoffset += texImage->Border;
@@ -2755,6 +2849,25 @@ _mesa_TexSubImage3D( GLenum target, GLint level,
 
 
 
+/**
+ * For glCopyTexSubImage, return the source renderbuffer to copy texel data
+ * from.  This depends on whether the texture contains color or depth values.
+ */
+static struct gl_renderbuffer *
+get_copy_tex_image_source(struct gl_context *ctx, gl_format texFormat)
+{
+   if (_mesa_get_format_bits(texFormat, GL_DEPTH_BITS) > 0) {
+      /* reading from depth/stencil buffer */
+      return ctx->ReadBuffer->Attachment[BUFFER_DEPTH].Renderbuffer;
+   }
+   else {
+      /* copying from color buffer */
+      return ctx->ReadBuffer->_ColorReadBuffer;
+   }
+}
+
+
+
 /**
  * Implement the glCopyTexImage1/2D() functions.
  */
@@ -2815,30 +2928,33 @@ copyteximage(struct gl_context *ctx, GLuint dims,
             /* Free old texture image */
             ctx->Driver.FreeTextureImageBuffer(ctx, texImage);
 
-            _mesa_init_teximage_fields(ctx, target, texImage, width, height, 1,
+            _mesa_init_teximage_fields(ctx, texImage, width, height, 1,
                                        border, internalFormat, texFormat);
 
             /* Allocate texture memory (no pixel data yet) */
             if (dims == 1) {
-               ctx->Driver.TexImage1D(ctx, target, level, internalFormat,
+               ctx->Driver.TexImage1D(ctx, texImage, internalFormat,
                                       width, border, GL_NONE, GL_NONE, NULL,
-                                      &ctx->Unpack, texObj, texImage);
+                                      &ctx->Unpack);
             }
             else {
-               ctx->Driver.TexImage2D(ctx, target, level, internalFormat,
+               ctx->Driver.TexImage2D(ctx, texImage, internalFormat,
                                       width, height, border, GL_NONE, GL_NONE,
-                                      NULL, &ctx->Unpack, texObj, texImage);
+                                      NULL, &ctx->Unpack);
             }
 
             if (_mesa_clip_copytexsubimage(ctx, &dstX, &dstY, &srcX, &srcY,
                                            &width, &height)) {
+               struct gl_renderbuffer *srcRb =
+                  get_copy_tex_image_source(ctx, texImage->TexFormat);
+
                if (dims == 1)
-                  ctx->Driver.CopyTexSubImage1D(ctx, target, level, dstX,
-                                                srcX, srcY, width);
+                  ctx->Driver.CopyTexSubImage1D(ctx, texImage, dstX,
+                                                srcRb, srcX, srcY, width);
                                                 
                else
-                  ctx->Driver.CopyTexSubImage2D(ctx, target, level, dstX, dstY,
-                                                srcX, srcY, width, height);
+                  ctx->Driver.CopyTexSubImage2D(ctx, texImage, dstX, dstY,
+                                                srcRb, srcX, srcY, width, height);
             }
 
             check_gen_mipmap(ctx, target, texObj, level);
@@ -2923,10 +3039,12 @@ copytexsubimage(struct gl_context *ctx, GLuint dims, GLenum target, GLint level,
          /* If we have a border, offset=-1 is legal.  Bias by border width. */
          switch (dims) {
          case 3:
-            zoffset += texImage->Border;
+            if (target != GL_TEXTURE_2D_ARRAY)
+               zoffset += texImage->Border;
             /* fall-through */
          case 2:
-            yoffset += texImage->Border;
+            if (target != GL_TEXTURE_1D_ARRAY)
+               yoffset += texImage->Border;
             /* fall-through */
          case 1:
             xoffset += texImage->Border;
@@ -2934,20 +3052,22 @@ copytexsubimage(struct gl_context *ctx, GLuint dims, GLenum target, GLint level,
 
          if (_mesa_clip_copytexsubimage(ctx, &xoffset, &yoffset, &x, &y,
                                         &width, &height)) {
+            struct gl_renderbuffer *srcRb =
+               get_copy_tex_image_source(ctx, texImage->TexFormat);
+
             switch (dims) {
             case 1:
-               ctx->Driver.CopyTexSubImage1D(ctx, target, level,
-                                             xoffset, x, y, width);
+               ctx->Driver.CopyTexSubImage1D(ctx, texImage, xoffset,
+                                             srcRb, x, y, width);
                break;
             case 2:
-               ctx->Driver.CopyTexSubImage2D(ctx, target, level,
-                                             xoffset, yoffset,
-                                             x, y, width, height);
+               ctx->Driver.CopyTexSubImage2D(ctx, texImage, xoffset, yoffset,
+                                             srcRb, x, y, width, height);
                break;
             case 3:
-               ctx->Driver.CopyTexSubImage3D(ctx, target, level,
+               ctx->Driver.CopyTexSubImage3D(ctx, texImage,
                                              xoffset, yoffset, zoffset,
-                                             x, y, width, height);
+                                             srcRb, x, y, width, height);
                break;
             default:
                _mesa_problem(ctx, "bad dims in copytexsubimage()");
@@ -3407,7 +3527,7 @@ compressedteximage(struct gl_context *ctx, GLuint dims,
          }
          else {
             /* no error: store the teximage parameters */
-            _mesa_init_teximage_fields(ctx, target, texImage, width, height,
+            _mesa_init_teximage_fields(ctx, texImage, width, height,
                                        depth, border, internalFormat,
                                        MESA_FORMAT_NONE);
          }
@@ -3442,34 +3562,31 @@ compressedteximage(struct gl_context *ctx, GLuint dims,
                                                     GL_NONE);
 
             if (legal_texture_size(ctx, texFormat, width, height, depth)) {
-               _mesa_init_teximage_fields(ctx, target, texImage,
+               _mesa_init_teximage_fields(ctx, texImage,
                                           width, height, depth,
                                           border, internalFormat, texFormat);
 
                switch (dims) {
                case 1:
                   ASSERT(ctx->Driver.CompressedTexImage1D);
-                  ctx->Driver.CompressedTexImage1D(ctx, target, level,
+                  ctx->Driver.CompressedTexImage1D(ctx, texImage,
                                                    internalFormat,
                                                    width,
-                                                   border, imageSize, data,
-                                                   texObj, texImage);
+                                                   border, imageSize, data);
                   break;
                case 2:
                   ASSERT(ctx->Driver.CompressedTexImage2D);
-                  ctx->Driver.CompressedTexImage2D(ctx, target, level,
+                  ctx->Driver.CompressedTexImage2D(ctx, texImage,
                                                    internalFormat,
                                                    width, height,
-                                                   border, imageSize, data,
-                                                   texObj, texImage);
+                                                   border, imageSize, data);
                   break;
                case 3:
                   ASSERT(ctx->Driver.CompressedTexImage3D);
-                  ctx->Driver.CompressedTexImage3D(ctx, target, level,
+                  ctx->Driver.CompressedTexImage3D(ctx, texImage,
                                                    internalFormat,
                                                    width, height, depth,
-                                                   border, imageSize, data,
-                                                   texObj, texImage);
+                                                   border, imageSize, data);
                   break;
                default:
                   _mesa_problem(ctx, "bad dims in compressedteximage");
@@ -3567,28 +3684,25 @@ compressed_tex_sub_image(GLuint dims, GLenum target, GLint level,
          switch (dims) {
          case 1:
             if (ctx->Driver.CompressedTexSubImage1D) {
-               ctx->Driver.CompressedTexSubImage1D(ctx, target, level,
+               ctx->Driver.CompressedTexSubImage1D(ctx, texImage,
                                                    xoffset, width,
-                                                   format, imageSize, data,
-                                                   texObj, texImage);
+                                                   format, imageSize, data);
             }
             break;
          case 2:
             if (ctx->Driver.CompressedTexSubImage2D) {
-               ctx->Driver.CompressedTexSubImage2D(ctx, target, level,
+               ctx->Driver.CompressedTexSubImage2D(ctx, texImage,
                                                    xoffset, yoffset,
                                                    width, height,
-                                                   format, imageSize, data,
-                                                   texObj, texImage);
+                                                   format, imageSize, data);
             }
             break;
          case 3:
             if (ctx->Driver.CompressedTexSubImage3D) {
-               ctx->Driver.CompressedTexSubImage3D(ctx, target, level,
+               ctx->Driver.CompressedTexSubImage3D(ctx, texImage,
                                                    xoffset, yoffset, zoffset,
                                                    width, height, depth,
-                                                   format, imageSize, data,
-                                                   texObj, texImage);
+                                                   format, imageSize, data);
             }
             break;
          default: