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@amd.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/16631>
This commit is contained in:
Kenneth Graunke 2022-05-02 12:06:38 -07:00 committed by Marge Bot
parent eb673c55f1
commit 5602f424c3
2 changed files with 119 additions and 83 deletions

View File

@ -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);

View File

@ -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");
}
}