swrast: Factor out texture slice counting.
authorEric Anholt <eric@anholt.net>
Fri, 19 Apr 2013 20:30:34 +0000 (13:30 -0700)
committerEric Anholt <eric@anholt.net>
Tue, 30 Apr 2013 17:40:42 +0000 (10:40 -0700)
This function going to get used a lot more in upcoming patches.

Reviewed-by: Kenneth Graunke <kenneth@whitecape.org>
Reviewed-by: Brian Paul <brianp@vmware.com>
src/mesa/swrast/s_texture.c

index 2e2e860d72f6a1c634f92a5aa5043d8e48da3e04..4e11b0a18280b1c32c1d7279fcab84b80a9d941e 100644 (file)
@@ -59,6 +59,14 @@ _swrast_delete_texture_image(struct gl_context *ctx,
    _mesa_delete_texture_image(ctx, texImage);
 }
 
+static unsigned int
+texture_slices(struct gl_texture_image *texImage)
+{
+   if (texImage->TexObject->Target == GL_TEXTURE_1D_ARRAY)
+      return texImage->Height;
+   else
+      return texImage->Depth;
+}
 
 /**
  * Called via ctx->Driver.AllocTextureImageBuffer()
@@ -84,11 +92,11 @@ _swrast_alloc_texture_image_buffer(struct gl_context *ctx,
     * We allocate the array for 1D/2D textures too in order to avoid special-
     * case code in the texstore routines.
     */
-   swImg->ImageOffsets = malloc(texImage->Depth * sizeof(GLuint));
+   swImg->ImageOffsets = malloc(texture_slices(texImage) * sizeof(GLuint));
    if (!swImg->ImageOffsets)
       return GL_FALSE;
 
-   for (i = 0; i < texImage->Depth; i++) {
+   for (i = 0; i < texture_slices(texImage); i++) {
       swImg->ImageOffsets[i] = i * texImage->Width * texImage->Height;
    }
 
@@ -210,20 +218,20 @@ _swrast_map_teximage(struct gl_context *ctx,
       
    map = swImage->Buffer;
 
+   assert(slice < texture_slices(texImage));
+
    if (texImage->TexObject->Target == GL_TEXTURE_3D ||
        texImage->TexObject->Target == GL_TEXTURE_2D_ARRAY) {
       GLuint sliceSize = _mesa_format_image_size(texImage->TexFormat,
                                                  texImage->Width,
                                                  texImage->Height,
                                                  1);
-      assert(slice < texImage->Depth);
       map += slice * sliceSize;
    } else if (texImage->TexObject->Target == GL_TEXTURE_1D_ARRAY) {
       GLuint sliceSize = _mesa_format_image_size(texImage->TexFormat,
                                                  texImage->Width,
                                                  1,
                                                  1);
-      assert(slice < texImage->Height);
       map += slice * sliceSize;
    }