[Mesa-dev] [PATCH 1/6] mesa: fix SwapBytes handling in texstore.

Dave Airlie airlied at gmail.com
Tue Aug 25 18:14:50 PDT 2015


From: Dave Airlie <airlied at redhat.com>

This code doesn't handle the the user setting
GL_UNPACK_ALIGNMENT at all well since we have
the cases where 7 byte wide (GL_RED/GL_UNSIGNED_BYTE)
still has it's rows aligned to 8, and the old
code failed in that case.

Just iterate the swaps over rows and images
to handle this instead.

Signed-off-by: Dave Airlie <airlied at redhat.com>
---
 src/mesa/main/texstore.c | 34 ++++++++++++++++++++++++++--------
 1 file changed, 26 insertions(+), 8 deletions(-)

diff --git a/src/mesa/main/texstore.c b/src/mesa/main/texstore.c
index fc83310..d07acca 100644
--- a/src/mesa/main/texstore.c
+++ b/src/mesa/main/texstore.c
@@ -729,17 +729,35 @@ texstore_rgba(TEXSTORE_PARAMS)
       if (swapSize == 2 || swapSize == 4) {
          int bytesPerPixel = _mesa_bytes_per_pixel(srcFormat, srcType);
          int swapsPerPixel = bytesPerPixel / swapSize;
-         int elementCount = srcWidth * srcHeight * srcDepth;
+         int srcStride = _mesa_image_row_stride(srcPacking, srcWidth, srcFormat, srcType);
+         int imageStride = _mesa_image_image_stride(srcPacking, srcWidth, srcHeight, srcFormat, srcType);
+         int bufferSize = imageStride * srcDepth;
+         int row, layer;
+         const uint8_t *src, *srcrow;
+         uint8_t *dst, *dstrow;
+
          assert(bytesPerPixel % swapSize == 0);
-         tempImage = malloc(elementCount * bytesPerPixel);
+         tempImage = malloc(bufferSize);
          if (!tempImage)
             return GL_FALSE;
-         if (swapSize == 2)
-            _mesa_swap2_copy(tempImage, (GLushort *) srcAddr,
-                             elementCount * swapsPerPixel);
-         else
-            _mesa_swap4_copy(tempImage, (GLuint *) srcAddr,
-                             elementCount * swapsPerPixel);
+         src = srcAddr;
+         dst = tempImage;
+         for (layer = 0; layer < srcDepth; layer++) {
+            srcrow = src;
+            dstrow = dst;
+            for (row = 0; row < srcHeight; row++) {
+               if (swapSize == 2)
+                  _mesa_swap2_copy((GLushort *)dstrow, (GLushort *)srcrow,
+                                   srcWidth * swapsPerPixel);
+               else
+                  _mesa_swap4_copy((GLuint *)dstrow, (GLuint *)srcrow,
+                                   srcWidth * swapsPerPixel);
+               srcrow += srcStride;
+               dstrow += srcStride;
+            }
+            src += imageStride;
+            dst += imageStride;
+         }
          srcAddr = tempImage;
       }
    }
-- 
2.4.3



More information about the mesa-dev mailing list