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]; dst = dstSlices[0];
tx_compress_dxtn(3, srcWidth, srcHeight, pixels, tx_compress_dxt1(3, srcWidth, srcHeight, pixels, dst, dstRowStride, 3);
GL_COMPRESSED_RGB_S3TC_DXT1_EXT,
dst, dstRowStride);
free((void *) tempImage); free((void *) tempImage);
@ -140,9 +138,7 @@ _mesa_texstore_rgba_dxt1(TEXSTORE_PARAMS)
dst = dstSlices[0]; dst = dstSlices[0];
tx_compress_dxtn(4, srcWidth, srcHeight, pixels, tx_compress_dxt1(4, srcWidth, srcHeight, pixels, dst, dstRowStride, 4);
GL_COMPRESSED_RGBA_S3TC_DXT1_EXT,
dst, dstRowStride);
free((void*) tempImage); free((void*) tempImage);
@ -195,9 +191,7 @@ _mesa_texstore_rgba_dxt3(TEXSTORE_PARAMS)
dst = dstSlices[0]; dst = dstSlices[0];
tx_compress_dxtn(4, srcWidth, srcHeight, pixels, tx_compress_dxt3(4, srcWidth, srcHeight, pixels, dst, dstRowStride);
GL_COMPRESSED_RGBA_S3TC_DXT3_EXT,
dst, dstRowStride);
free((void *) tempImage); free((void *) tempImage);
@ -250,9 +244,7 @@ _mesa_texstore_rgba_dxt5(TEXSTORE_PARAMS)
dst = dstSlices[0]; dst = dstSlices[0];
tx_compress_dxtn(4, srcWidth, srcHeight, pixels, tx_compress_dxt5(4, srcWidth, srcHeight, pixels, dst, dstRowStride);
GL_COMPRESSED_RGBA_S3TC_DXT5_EXT,
dst, dstRowStride);
free((void *) tempImage); free((void *) tempImage);

View File

@ -906,28 +906,28 @@ 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, static void
GLenum destFormat, GLubyte *dest, GLint dstRowStride) tx_compress_dxt1(int srccomps, int width, int height,
const GLubyte *srcPixData, GLubyte *dest, int dstRowStride,
unsigned dstComps)
{ {
GLenum destFormat = dstComps == 3 ? GL_COMPRESSED_RGB_S3TC_DXT1_EXT
: GL_COMPRESSED_RGBA_S3TC_DXT1_EXT;
GLubyte *blkaddr = dest; GLubyte *blkaddr = dest;
GLubyte srcpixels[4][4][4]; GLubyte srcpixels[4][4][4];
const GLchan *srcaddr = srcPixData; const GLchan *srcaddr = srcPixData;
GLint numxpixels, numypixels; int numxpixels, numypixels;
GLint i, j;
GLint dstRowDiff;
switch (destFormat) {
case GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
case GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
/* hmm we used to get called without dstRowStride... */ /* hmm we used to get called without dstRowStride... */
dstRowDiff = dstRowStride >= (width * 2) ? dstRowStride - (((width + 3) & ~3) * 2) : 0; int dstRowDiff = dstRowStride >= (width * 2) ?
dstRowStride - (((width + 3) & ~3) * 2) : 0;
/* fprintf(stderr, "dxt1 tex width %d tex height %d dstRowStride %d\n", /* fprintf(stderr, "dxt1 tex width %d tex height %d dstRowStride %d\n",
width, height, dstRowStride); */ width, height, dstRowStride); */
for (j = 0; j < height; j += 4) { for (int j = 0; j < height; j += 4) {
if (height > j + 3) numypixels = 4; if (height > j + 3) numypixels = 4;
else numypixels = height - j; else numypixels = height - j;
srcaddr = srcPixData + j * width * srccomps; srcaddr = srcPixData + j * width * srccomps;
for (i = 0; i < width; i += 4) { for (int i = 0; i < width; i += 4) {
if (width > i + 3) numxpixels = 4; if (width > i + 3) numxpixels = 4;
else numxpixels = width - i; else numxpixels = width - i;
extractsrccolors(srcpixels, srcaddr, width, numxpixels, numypixels, srccomps); extractsrccolors(srcpixels, srcaddr, width, numxpixels, numypixels, srccomps);
@ -937,16 +937,27 @@ static void tx_compress_dxtn(GLint srccomps, GLint width, GLint height, const GL
} }
blkaddr += dstRowDiff; blkaddr += dstRowDiff;
} }
break; }
case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
dstRowDiff = dstRowStride >= (width * 4) ? dstRowStride - (((width + 3) & ~3) * 4) : 0; 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", /* fprintf(stderr, "dxt3 tex width %d tex height %d dstRowStride %d\n",
width, height, dstRowStride); */ width, height, dstRowStride); */
for (j = 0; j < height; j += 4) { for (int j = 0; j < height; j += 4) {
if (height > j + 3) numypixels = 4; if (height > j + 3) numypixels = 4;
else numypixels = height - j; else numypixels = height - j;
srcaddr = srcPixData + j * width * srccomps; srcaddr = srcPixData + j * width * srccomps;
for (i = 0; i < width; i += 4) { for (int i = 0; i < width; i += 4) {
if (width > i + 3) numxpixels = 4; if (width > i + 3) numxpixels = 4;
else numxpixels = width - i; else numxpixels = width - i;
extractsrccolors(srcpixels, srcaddr, width, numxpixels, numypixels, srccomps); extractsrccolors(srcpixels, srcaddr, width, numxpixels, numypixels, srccomps);
@ -964,16 +975,27 @@ static void tx_compress_dxtn(GLint srccomps, GLint width, GLint height, const GL
} }
blkaddr += dstRowDiff; blkaddr += dstRowDiff;
} }
break; }
case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
dstRowDiff = dstRowStride >= (width * 4) ? dstRowStride - (((width + 3) & ~3) * 4) : 0; 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", /* fprintf(stderr, "dxt5 tex width %d tex height %d dstRowStride %d\n",
width, height, dstRowStride); */ width, height, dstRowStride); */
for (j = 0; j < height; j += 4) { for (int j = 0; j < height; j += 4) {
if (height > j + 3) numypixels = 4; if (height > j + 3) numypixels = 4;
else numypixels = height - j; else numypixels = height - j;
srcaddr = srcPixData + j * width * srccomps; srcaddr = srcPixData + j * width * srccomps;
for (i = 0; i < width; i += 4) { for (int i = 0; i < width; i += 4) {
if (width > i + 3) numxpixels = 4; if (width > i + 3) numxpixels = 4;
else numxpixels = width - i; else numxpixels = width - i;
extractsrccolors(srcpixels, srcaddr, width, numxpixels, numypixels, srccomps); extractsrccolors(srcpixels, srcaddr, width, numxpixels, numypixels, srccomps);
@ -984,10 +1006,32 @@ static void tx_compress_dxtn(GLint srccomps, GLint width, GLint height, const GL
} }
blkaddr += dstRowDiff; 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:
tx_compress_dxt1(srccomps, width, height, srcPixData,
dest, dstRowStride, 4);
break;
case GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
tx_compress_dxt3(srccomps, width, height, srcPixData,
dest, dstRowStride);
break;
case GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
tx_compress_dxt5(srccomps, width, height, srcPixData,
dest, dstRowStride);
break; break;
default: default:
assert(false); unreachable("unknown DXTn format");
return;
} }
} }