radeon: minor refactoring of texture code
authorMaciej Cencora <m.cencora@gmail.com>
Sat, 14 Nov 2009 14:03:31 +0000 (15:03 +0100)
committerMaciej Cencora <m.cencora@gmail.com>
Sat, 14 Nov 2009 15:55:38 +0000 (16:55 +0100)
Also properly set dstImageOffsets for TexSubImage case.

src/mesa/drivers/dri/radeon/radeon_texture.c

index 607ce7864e8ec9d9e294a6d2099e6c050b69d3f6..8fc686581b5bfee9a9f1803f56a99dfbe2803ba1 100644 (file)
@@ -508,6 +508,27 @@ gl_format radeonChooseTextureFormat(GLcontext * ctx,
        return MESA_FORMAT_NONE;                /* never get here */
 }
 
+static GLuint * allocate_image_offsets(GLcontext *ctx,
+       unsigned alignedWidth,
+       unsigned height,
+       unsigned depth)
+{
+       int i;
+       GLuint *offsets;
+
+       offsets = _mesa_malloc(depth * sizeof(GLuint)) ;
+       if (!offsets) {
+               _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTex[Sub]Image");
+               return NULL;
+       }
+
+       for (i = 0; i < depth; ++i) {
+               offsets[i] = alignedWidth * height * i;
+       }
+
+       return offsets;
+}
+
 /**
  * All glTexImage calls go through this function.
  */
@@ -605,8 +626,8 @@ static void radeon_teximage(
        }
 
        if (pixels) {
-               radeon_teximage_map(image, GL_TRUE);
                if (compressed) {
+                       radeon_teximage_map(image, GL_TRUE);
                        if (image->mt) {
                                uint32_t srcRowStride, bytesPerRow, rows;
                                srcRowStride = _mesa_format_row_stride(texImage->TexFormat, width);
@@ -629,19 +650,17 @@ static void radeon_teximage(
                        }
 
                        if (dims == 3) {
-                               int i;
-
-                               dstImageOffsets = _mesa_malloc(depth * sizeof(GLuint)) ;
-                               if (!dstImageOffsets)
-                                       _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexImage");
-
-                               for (i = 0; i < depth; ++i) {
-                                       dstImageOffsets[i] = dstRowStride/_mesa_get_format_bytes(texImage->TexFormat) * height * i;
+                               unsigned alignedWidth = dstRowStride/_mesa_get_format_bytes(texImage->TexFormat);
+                               dstImageOffsets = allocate_image_offsets(ctx, alignedWidth, height, depth);
+                               if (!dstImageOffsets) {
+                                       return;
                                }
                        } else {
                                dstImageOffsets = texImage->ImageOffsets;
                        }
 
+                       radeon_teximage_map(image, GL_TRUE);
+
                        if (!_mesa_texstore(ctx, dims,
                                            texImage->_BaseFormat,
                                            texImage->TexFormat,
@@ -752,7 +771,7 @@ static void radeon_texsubimage(GLcontext* ctx, int dims, GLenum target, int leve
 
        if (pixels) {
                GLint dstRowStride;
-               radeon_teximage_map(image, GL_TRUE);
+               GLuint *dstImageOffsets;
 
                if (image->mt) {
                        radeon_mipmap_level *lvl = &image->mt->levels[image->mtlevel];
@@ -761,6 +780,18 @@ static void radeon_texsubimage(GLcontext* ctx, int dims, GLenum target, int leve
                        dstRowStride = texImage->RowStride * _mesa_get_format_bytes(texImage->TexFormat);
                }
 
+               if (dims == 3) {
+                       unsigned alignedWidth = dstRowStride/_mesa_get_format_bytes(texImage->TexFormat);
+                       dstImageOffsets = allocate_image_offsets(ctx, alignedWidth, height, depth);
+                       if (!dstImageOffsets) {
+                               return;
+                       }
+               } else {
+                       dstImageOffsets = texImage->ImageOffsets;
+               }
+
+               radeon_teximage_map(image, GL_TRUE);
+
                if (compressed) {
                        uint32_t srcRowStride, bytesPerRow, rows;
                        GLubyte *img_start;
@@ -786,12 +817,16 @@ static void radeon_texsubimage(GLcontext* ctx, int dims, GLenum target, int leve
                                            texImage->TexFormat, texImage->Data,
                                            xoffset, yoffset, zoffset,
                                            dstRowStride,
-                                           texImage->ImageOffsets,
+                                           dstImageOffsets,
                                            width, height, depth,
                                            format, type, pixels, packing)) {
                                _mesa_error(ctx, GL_OUT_OF_MEMORY, "glTexSubImage");
                        }
                }
+
+               if (dims == 3) {
+                       _mesa_free(dstImageOffsets);
+               }
        }
 
        radeon_teximage_unmap(image);