Mesa (main): mesa: Split tx_compress_dxtn into per-format functions

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Fri May 27 02:12:24 UTC 2022


Module: Mesa
Branch: main
Commit: 5602f424c3561e63fc249d1e9eaf5e5b4ae225f0
URL:    http://cgit.freedesktop.org/mesa/mesa/commit/?id=5602f424c3561e63fc249d1e9eaf5e5b4ae225f0

Author: Kenneth Graunke <kenneth at whitecape.org>
Date:   Mon May  2 12:06:38 2022 -0700

mesa: Split tx_compress_dxtn into per-format functions

This avoids an unnecessary switch statement in many cases.

Reviewed-by: Marek Olšák <marek.olsak at amd.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/16631>

---

 src/mesa/main/texcompress_s3tc.c     |  16 +--
 src/mesa/main/texcompress_s3tc_tmp.h | 186 ++++++++++++++++++++++-------------
 2 files changed, 119 insertions(+), 83 deletions(-)

diff --git a/src/mesa/main/texcompress_s3tc.c b/src/mesa/main/texcompress_s3tc.c
index 06546a2a590..c16e6e93da7 100644
--- a/src/mesa/main/texcompress_s3tc.c
+++ b/src/mesa/main/texcompress_s3tc.c
@@ -84,9 +84,7 @@ _mesa_texstore_rgb_dxt1(TEXSTORE_PARAMS)
 
    dst = dstSlices[0];
 
-   tx_compress_dxtn(3, srcWidth, srcHeight, pixels,
-                    GL_COMPRESSED_RGB_S3TC_DXT1_EXT,
-                    dst, dstRowStride);
+   tx_compress_dxt1(3, srcWidth, srcHeight, pixels, dst, dstRowStride, 3);
 
    free((void *) tempImage);
 
@@ -140,9 +138,7 @@ _mesa_texstore_rgba_dxt1(TEXSTORE_PARAMS)
 
    dst = dstSlices[0];
 
-   tx_compress_dxtn(4, srcWidth, srcHeight, pixels,
-                    GL_COMPRESSED_RGBA_S3TC_DXT1_EXT,
-                    dst, dstRowStride);
+   tx_compress_dxt1(4, srcWidth, srcHeight, pixels, dst, dstRowStride, 4);
 
    free((void*) tempImage);
 
@@ -195,9 +191,7 @@ _mesa_texstore_rgba_dxt3(TEXSTORE_PARAMS)
 
    dst = dstSlices[0];
 
-   tx_compress_dxtn(4, srcWidth, srcHeight, pixels,
-                    GL_COMPRESSED_RGBA_S3TC_DXT3_EXT,
-                    dst, dstRowStride);
+   tx_compress_dxt3(4, srcWidth, srcHeight, pixels, dst, dstRowStride);
 
    free((void *) tempImage);
 
@@ -250,9 +244,7 @@ _mesa_texstore_rgba_dxt5(TEXSTORE_PARAMS)
 
    dst = dstSlices[0];
 
-   tx_compress_dxtn(4, srcWidth, srcHeight, pixels,
-                    GL_COMPRESSED_RGBA_S3TC_DXT5_EXT,
-                    dst, dstRowStride);
+   tx_compress_dxt5(4, srcWidth, srcHeight, pixels, dst, dstRowStride);
 
    free((void *) tempImage);
 
diff --git a/src/mesa/main/texcompress_s3tc_tmp.h b/src/mesa/main/texcompress_s3tc_tmp.h
index 5dd25222f15..5c2c0ef7fc4 100644
--- a/src/mesa/main/texcompress_s3tc_tmp.h
+++ b/src/mesa/main/texcompress_s3tc_tmp.h
@@ -906,88 +906,132 @@ static void extractsrccolors( GLubyte srcpixels[4][4][4], const GLchan *srcaddr,
 }
 
 
-static void tx_compress_dxtn(GLint srccomps, GLint width, GLint height, const GLubyte *srcPixData,
-                     GLenum destFormat, GLubyte *dest, GLint dstRowStride)
+static void
+tx_compress_dxt1(int srccomps, int width, int height,
+                 const GLubyte *srcPixData, GLubyte *dest, int dstRowStride,
+                 unsigned dstComps)
 {
-      GLubyte *blkaddr = dest;
-      GLubyte srcpixels[4][4][4];
-      const GLchan *srcaddr = srcPixData;
-      GLint numxpixels, numypixels;
-      GLint i, j;
-      GLint dstRowDiff;
+   GLenum destFormat = dstComps == 3 ? GL_COMPRESSED_RGB_S3TC_DXT1_EXT
+                                     : GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
+   GLubyte *blkaddr = dest;
+   GLubyte srcpixels[4][4][4];
+   const GLchan *srcaddr = srcPixData;
+   int numxpixels, numypixels;
+
+   /* hmm we used to get called without dstRowStride... */
+   int dstRowDiff = dstRowStride >= (width * 2) ?
+                    dstRowStride - (((width + 3) & ~3) * 2) : 0;
+   /* fprintf(stderr, "dxt1 tex width %d tex height %d dstRowStride %d\n",
+              width, height, dstRowStride); */
+   for (int j = 0; j < height; j += 4) {
+      if (height > j + 3) numypixels = 4;
+      else numypixels = height - j;
+      srcaddr = srcPixData + j * width * srccomps;
+      for (int i = 0; i < width; i += 4) {
+         if (width > i + 3) numxpixels = 4;
+         else numxpixels = width - i;
+         extractsrccolors(srcpixels, srcaddr, width, numxpixels, numypixels, srccomps);
+         encodedxtcolorblockfaster(blkaddr, srcpixels, numxpixels, numypixels, destFormat);
+         srcaddr += srccomps * numxpixels;
+         blkaddr += 8;
+      }
+      blkaddr += dstRowDiff;
+   }
+}
 
+static void
+tx_compress_dxt3(int srccomps, int width, int height,
+                 const GLubyte *srcPixData, GLubyte *dest, int dstRowStride)
+{
+   GLenum destFormat = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT;
+   GLubyte *blkaddr = dest;
+   GLubyte srcpixels[4][4][4];
+   const GLchan *srcaddr = srcPixData;
+   int numxpixels, numypixels;
+
+   int dstRowDiff = dstRowStride >= (width * 4) ?
+                    dstRowStride - (((width + 3) & ~3) * 4) : 0;
+   /* fprintf(stderr, "dxt3 tex width %d tex height %d dstRowStride %d\n",
+              width, height, dstRowStride); */
+   for (int j = 0; j < height; j += 4) {
+      if (height > j + 3) numypixels = 4;
+      else numypixels = height - j;
+      srcaddr = srcPixData + j * width * srccomps;
+      for (int i = 0; i < width; i += 4) {
+         if (width > i + 3) numxpixels = 4;
+         else numxpixels = width - i;
+         extractsrccolors(srcpixels, srcaddr, width, numxpixels, numypixels, srccomps);
+         *blkaddr++ = (srcpixels[0][0][3] >> 4) | (srcpixels[0][1][3] & 0xf0);
+         *blkaddr++ = (srcpixels[0][2][3] >> 4) | (srcpixels[0][3][3] & 0xf0);
+         *blkaddr++ = (srcpixels[1][0][3] >> 4) | (srcpixels[1][1][3] & 0xf0);
+         *blkaddr++ = (srcpixels[1][2][3] >> 4) | (srcpixels[1][3][3] & 0xf0);
+         *blkaddr++ = (srcpixels[2][0][3] >> 4) | (srcpixels[2][1][3] & 0xf0);
+         *blkaddr++ = (srcpixels[2][2][3] >> 4) | (srcpixels[2][3][3] & 0xf0);
+         *blkaddr++ = (srcpixels[3][0][3] >> 4) | (srcpixels[3][1][3] & 0xf0);
+         *blkaddr++ = (srcpixels[3][2][3] >> 4) | (srcpixels[3][3][3] & 0xf0);
+         encodedxtcolorblockfaster(blkaddr, srcpixels, numxpixels, numypixels, destFormat);
+         srcaddr += srccomps * numxpixels;
+         blkaddr += 8;
+      }
+      blkaddr += dstRowDiff;
+   }
+}
+
+static void
+tx_compress_dxt5(int srccomps, int width, int height,
+                 const GLubyte *srcPixData, GLubyte *dest, int dstRowStride)
+{
+   GLenum destFormat = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT;
+   GLubyte *blkaddr = dest;
+   GLubyte srcpixels[4][4][4];
+   const GLchan *srcaddr = srcPixData;
+   int numxpixels, numypixels;
+
+   int dstRowDiff = dstRowStride >= (width * 4) ?
+                    dstRowStride - (((width + 3) & ~3) * 4) : 0;
+   /* fprintf(stderr, "dxt5 tex width %d tex height %d dstRowStride %d\n",
+              width, height, dstRowStride); */
+   for (int j = 0; j < height; j += 4) {
+      if (height > j + 3) numypixels = 4;
+      else numypixels = height - j;
+      srcaddr = srcPixData + j * width * srccomps;
+      for (int i = 0; i < width; i += 4) {
+         if (width > i + 3) numxpixels = 4;
+         else numxpixels = width - i;
+         extractsrccolors(srcpixels, srcaddr, width, numxpixels, numypixels, srccomps);
+         encodedxt5alpha(blkaddr, srcpixels, numxpixels, numypixels);
+         encodedxtcolorblockfaster(blkaddr + 8, srcpixels, numxpixels, numypixels, destFormat);
+         srcaddr += srccomps * numxpixels;
+         blkaddr += 16;
+      }
+      blkaddr += dstRowDiff;
+   }
+}
+
+static void
+tx_compress_dxtn(GLint srccomps, GLint width, GLint height,
+                 const GLubyte *srcPixData, GLenum destFormat,
+                 GLubyte *dest, GLint dstRowStride)
+{
    switch (destFormat) {
    case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
+      tx_compress_dxt1(srccomps, width, height, srcPixData,
+                       dest, dstRowStride, 3);
+      break;
    case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
-      /* hmm we used to get called without dstRowStride... */
-      dstRowDiff = dstRowStride >= (width * 2) ? dstRowStride - (((width + 3) & ~3) * 2) : 0;
-/*      fprintf(stderr, "dxt1 tex width %d tex height %d dstRowStride %d\n",
-              width, height, dstRowStride); */
-      for (j = 0; j < height; j += 4) {
-         if (height > j + 3) numypixels = 4;
-         else numypixels = height - j;
-         srcaddr = srcPixData + j * width * srccomps;
-         for (i = 0; i < width; i += 4) {
-            if (width > i + 3) numxpixels = 4;
-            else numxpixels = width - i;
-            extractsrccolors(srcpixels, srcaddr, width, numxpixels, numypixels, srccomps);
-            encodedxtcolorblockfaster(blkaddr, srcpixels, numxpixels, numypixels, destFormat);
-            srcaddr += srccomps * numxpixels;
-            blkaddr += 8;
-         }
-         blkaddr += dstRowDiff;
-      }
+      tx_compress_dxt1(srccomps, width, height, srcPixData,
+                       dest, dstRowStride, 4);
       break;
    case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
-      dstRowDiff = dstRowStride >= (width * 4) ? dstRowStride - (((width + 3) & ~3) * 4) : 0;
-/*      fprintf(stderr, "dxt3 tex width %d tex height %d dstRowStride %d\n",
-              width, height, dstRowStride); */
-      for (j = 0; j < height; j += 4) {
-         if (height > j + 3) numypixels = 4;
-         else numypixels = height - j;
-         srcaddr = srcPixData + j * width * srccomps;
-         for (i = 0; i < width; i += 4) {
-            if (width > i + 3) numxpixels = 4;
-            else numxpixels = width - i;
-            extractsrccolors(srcpixels, srcaddr, width, numxpixels, numypixels, srccomps);
-            *blkaddr++ = (srcpixels[0][0][3] >> 4) | (srcpixels[0][1][3] & 0xf0);
-            *blkaddr++ = (srcpixels[0][2][3] >> 4) | (srcpixels[0][3][3] & 0xf0);
-            *blkaddr++ = (srcpixels[1][0][3] >> 4) | (srcpixels[1][1][3] & 0xf0);
-            *blkaddr++ = (srcpixels[1][2][3] >> 4) | (srcpixels[1][3][3] & 0xf0);
-            *blkaddr++ = (srcpixels[2][0][3] >> 4) | (srcpixels[2][1][3] & 0xf0);
-            *blkaddr++ = (srcpixels[2][2][3] >> 4) | (srcpixels[2][3][3] & 0xf0);
-            *blkaddr++ = (srcpixels[3][0][3] >> 4) | (srcpixels[3][1][3] & 0xf0);
-            *blkaddr++ = (srcpixels[3][2][3] >> 4) | (srcpixels[3][3][3] & 0xf0);
-            encodedxtcolorblockfaster(blkaddr, srcpixels, numxpixels, numypixels, destFormat);
-            srcaddr += srccomps * numxpixels;
-            blkaddr += 8;
-         }
-         blkaddr += dstRowDiff;
-      }
+      tx_compress_dxt3(srccomps, width, height, srcPixData,
+                       dest, dstRowStride);
       break;
    case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
-      dstRowDiff = dstRowStride >= (width * 4) ? dstRowStride - (((width + 3) & ~3) * 4) : 0;
-/*      fprintf(stderr, "dxt5 tex width %d tex height %d dstRowStride %d\n",
-              width, height, dstRowStride); */
-      for (j = 0; j < height; j += 4) {
-         if (height > j + 3) numypixels = 4;
-         else numypixels = height - j;
-         srcaddr = srcPixData + j * width * srccomps;
-         for (i = 0; i < width; i += 4) {
-            if (width > i + 3) numxpixels = 4;
-            else numxpixels = width - i;
-            extractsrccolors(srcpixels, srcaddr, width, numxpixels, numypixels, srccomps);
-            encodedxt5alpha(blkaddr, srcpixels, numxpixels, numypixels);
-            encodedxtcolorblockfaster(blkaddr + 8, srcpixels, numxpixels, numypixels, destFormat);
-            srcaddr += srccomps * numxpixels;
-            blkaddr += 16;
-         }
-         blkaddr += dstRowDiff;
-      }
+      tx_compress_dxt5(srccomps, width, height, srcPixData,
+                       dest, dstRowStride);
       break;
    default:
-      assert(false);
-      return;
+      unreachable("unknown DXTn format");
    }
 }
 



More information about the mesa-commit mailing list