From 7c802fe4f8f74675d41af31bb37c0817b299e50d Mon Sep 17 00:00:00 2001 From: Matthias Bentrup Date: Tue, 31 Jan 2012 13:24:47 +0100 Subject: [PATCH 1/5] Change type of image data to color4ub_t * The image data is always in 4-byte RGBA form, but the engine used a mix of unsigned * or byte * pointers, which meant that casts and pointer arithmetic had to be used in many places. Changing this to color4ub_t * (which actually matches the underlying data) makes a lot of this unnecessary and also works if we ever compile ioquake3 on a system having sizeof(unsigned) != 4. --- code/renderer/tr_image.c | 240 ++++++++++++++++++++++------------------------ 1 files changed, 116 insertions(+), 124 deletions(-) diff --git a/code/renderer/tr_image.c b/code/renderer/tr_image.c index f511201..6b74bc0 100644 --- a/code/renderer/tr_image.c +++ b/code/renderer/tr_image.c @@ -229,13 +229,13 @@ If a larger shrinking is needed, use the mipmap function before or after. ================ */ -static void ResampleTexture( unsigned *in, int inwidth, int inheight, unsigned *out, - int outwidth, int outheight ) { +static void ResampleTexture( color4ub_t *in, int inwidth, int inheight, + color4ub_t *out, int outwidth, int outheight ) { int i, j; - unsigned *inrow, *inrow2; + color4ub_t *inrow, *inrow2; unsigned frac, fracstep; unsigned p1[2048], p2[2048]; - byte *pix1, *pix2, *pix3, *pix4; + color4ub_t *pix1, *pix2, *pix3, *pix4; if (outwidth>2048) ri.Error(ERR_DROP, "ResampleTexture: max width"); @@ -244,12 +244,12 @@ static void ResampleTexture( unsigned *in, int inwidth, int inheight, unsigned * frac = fracstep>>2; for ( i=0 ; i>16); + p1[i] = frac>>16; frac += fracstep; } frac = 3*(fracstep>>2); for ( i=0 ; i>16); + p2[i] = frac>>16; frac += fracstep; } @@ -258,14 +258,14 @@ static void ResampleTexture( unsigned *in, int inwidth, int inheight, unsigned * inrow2 = in + inwidth*(int)((i+0.75)*inheight/outheight); frac = fracstep >> 1; for (j=0 ; j>2; - ((byte *)(out+j))[1] = (pix1[1] + pix2[1] + pix3[1] + pix4[1])>>2; - ((byte *)(out+j))[2] = (pix1[2] + pix2[2] + pix3[2] + pix4[2])>>2; - ((byte *)(out+j))[3] = (pix1[3] + pix2[3] + pix3[3] + pix4[3])>>2; + pix1 = inrow + p1[j]; + pix2 = inrow + p2[j]; + pix3 = inrow2 + p1[j]; + pix4 = inrow2 + p2[j]; + out[j][0] = (pix1[0][0] + pix2[0][0] + pix3[0][0] + pix4[0][0])>>2; + out[j][1] = (pix1[0][1] + pix2[0][1] + pix3[0][1] + pix4[0][1])>>2; + out[j][2] = (pix1[0][2] + pix2[0][2] + pix3[0][2] + pix4[0][2])>>2; + out[j][3] = (pix1[0][3] + pix2[0][3] + pix3[0][3] + pix4[0][3])>>2; } } } @@ -278,51 +278,45 @@ Scale up the pixel values in a texture to increase the lighting range ================ */ -void R_LightScaleTexture (unsigned *in, int inwidth, int inheight, qboolean only_gamma ) +void R_LightScaleTexture (color4ub_t *in, int inwidth, int inheight, qboolean only_gamma ) { if ( only_gamma ) { if ( !glConfig.deviceSupportsGamma ) { int i, c; - byte *p; - - p = (byte *)in; c = inwidth*inheight; - for (i=0 ; i> 1; outHeight = inHeight >> 1; - temp = ri.Hunk_AllocateTempMemory( outWidth * outHeight * 4 ); + temp = ri.Hunk_AllocateTempMemory( outWidth * outHeight * sizeof(color4ub_t) ); inWidthMask = inWidth - 1; inHeightMask = inHeight - 1; for ( i = 0 ; i < outHeight ; i++ ) { for ( j = 0 ; j < outWidth ; j++ ) { - outpix = (byte *) ( temp + i * outWidth + j ); for ( k = 0 ; k < 4 ; k++ ) { total = - 1 * ((byte *)&in[ ((i*2-1)&inHeightMask)*inWidth + ((j*2-1)&inWidthMask) ])[k] + - 2 * ((byte *)&in[ ((i*2-1)&inHeightMask)*inWidth + ((j*2)&inWidthMask) ])[k] + - 2 * ((byte *)&in[ ((i*2-1)&inHeightMask)*inWidth + ((j*2+1)&inWidthMask) ])[k] + - 1 * ((byte *)&in[ ((i*2-1)&inHeightMask)*inWidth + ((j*2+2)&inWidthMask) ])[k] + - - 2 * ((byte *)&in[ ((i*2)&inHeightMask)*inWidth + ((j*2-1)&inWidthMask) ])[k] + - 4 * ((byte *)&in[ ((i*2)&inHeightMask)*inWidth + ((j*2)&inWidthMask) ])[k] + - 4 * ((byte *)&in[ ((i*2)&inHeightMask)*inWidth + ((j*2+1)&inWidthMask) ])[k] + - 2 * ((byte *)&in[ ((i*2)&inHeightMask)*inWidth + ((j*2+2)&inWidthMask) ])[k] + - - 2 * ((byte *)&in[ ((i*2+1)&inHeightMask)*inWidth + ((j*2-1)&inWidthMask) ])[k] + - 4 * ((byte *)&in[ ((i*2+1)&inHeightMask)*inWidth + ((j*2)&inWidthMask) ])[k] + - 4 * ((byte *)&in[ ((i*2+1)&inHeightMask)*inWidth + ((j*2+1)&inWidthMask) ])[k] + - 2 * ((byte *)&in[ ((i*2+1)&inHeightMask)*inWidth + ((j*2+2)&inWidthMask) ])[k] + - - 1 * ((byte *)&in[ ((i*2+2)&inHeightMask)*inWidth + ((j*2-1)&inWidthMask) ])[k] + - 2 * ((byte *)&in[ ((i*2+2)&inHeightMask)*inWidth + ((j*2)&inWidthMask) ])[k] + - 2 * ((byte *)&in[ ((i*2+2)&inHeightMask)*inWidth + ((j*2+1)&inWidthMask) ])[k] + - 1 * ((byte *)&in[ ((i*2+2)&inHeightMask)*inWidth + ((j*2+2)&inWidthMask) ])[k]; - outpix[k] = total / 36; + 1 * in[ ((i*2-1)&inHeightMask)*inWidth + ((j*2-1)&inWidthMask) ][k] + + 2 * in[ ((i*2-1)&inHeightMask)*inWidth + ((j*2)&inWidthMask) ][k] + + 2 * in[ ((i*2-1)&inHeightMask)*inWidth + ((j*2+1)&inWidthMask) ][k] + + 1 * in[ ((i*2-1)&inHeightMask)*inWidth + ((j*2+2)&inWidthMask) ][k] + + + 2 * in[ ((i*2)&inHeightMask)*inWidth + ((j*2-1)&inWidthMask) ][k] + + 4 * in[ ((i*2)&inHeightMask)*inWidth + ((j*2)&inWidthMask) ][k] + + 4 * in[ ((i*2)&inHeightMask)*inWidth + ((j*2+1)&inWidthMask) ][k] + + 2 * in[ ((i*2)&inHeightMask)*inWidth + ((j*2+2)&inWidthMask) ][k] + + + 2 * in[ ((i*2+1)&inHeightMask)*inWidth + ((j*2-1)&inWidthMask) ][k] + + 4 * in[ ((i*2+1)&inHeightMask)*inWidth + ((j*2)&inWidthMask) ][k] + + 4 * in[ ((i*2+1)&inHeightMask)*inWidth + ((j*2+1)&inWidthMask) ][k] + + 2 * in[ ((i*2+1)&inHeightMask)*inWidth + ((j*2+2)&inWidthMask) ][k] + + + 1 * in[ ((i*2+2)&inHeightMask)*inWidth + ((j*2-1)&inWidthMask) ][k] + + 2 * in[ ((i*2+2)&inHeightMask)*inWidth + ((j*2)&inWidthMask) ][k] + + 2 * in[ ((i*2+2)&inHeightMask)*inWidth + ((j*2+1)&inWidthMask) ][k] + + 1 * in[ ((i*2+2)&inHeightMask)*inWidth + ((j*2+2)&inWidthMask) ][k]; + temp[i * outWidth + j][k] = total / 36; } } } - Com_Memcpy( in, temp, outWidth * outHeight * 4 ); + Com_Memcpy( in, temp, outWidth * outHeight * sizeof(color4ub_t) ); ri.Hunk_FreeTempMemory( temp ); } @@ -392,13 +384,13 @@ R_MipMap Operates in place, quartering the size of the texture ================ */ -static void R_MipMap (byte *in, int width, int height) { +static void R_MipMap (color4ub_t *in, int width, int height) { int i, j; - byte *out; + color4ub_t *out; int row; if ( !r_simpleMipMaps->integer ) { - R_MipMap2( (unsigned *)in, width, height ); + R_MipMap2( in, width, height ); return; } @@ -406,28 +398,28 @@ static void R_MipMap (byte *in, int width, int height) { return; } - row = width * 4; + row = width; out = in; width >>= 1; height >>= 1; if ( width == 0 || height == 0 ) { width += height; // get largest - for (i=0 ; i>1; - out[1] = ( in[1] + in[5] )>>1; - out[2] = ( in[2] + in[6] )>>1; - out[3] = ( in[3] + in[7] )>>1; + for (i=0 ; i>1; + out[i][1] = ( in[2*i][1] + in[2*i+1][1] )>>1; + out[i][2] = ( in[2*i][2] + in[2*i+1][2] )>>1; + out[i][3] = ( in[2*i][3] + in[2*i+1][3] )>>1; } return; } - for (i=0 ; i>2; - out[1] = (in[1] + in[5] + in[row+1] + in[row+5])>>2; - out[2] = (in[2] + in[6] + in[row+2] + in[row+6])>>2; - out[3] = (in[3] + in[7] + in[row+3] + in[row+7])>>2; + for (i=0 ; i>2; + out[j][1] = (in[2*j][1] + in[2*j+1][1] + in[row+2*j][1] + in[row+2*j+1][1])>>2; + out[j][2] = (in[2*j][2] + in[2*j+1][2] + in[row+2*j][2] + in[row+2*j+1][2])>>2; + out[j][3] = (in[2*j][3] + in[2*j+1][3] + in[row+2*j][3] + in[row+2*j+1][3])>>2; } } } @@ -440,7 +432,7 @@ R_BlendOverTexture Apply a color blend over a set of pixels ================== */ -static void R_BlendOverTexture( byte *data, int pixelCount, byte blend[4] ) { +static void R_BlendOverTexture( color4ub_t *data, int pixelCount, color4ub_t blend ) { int i; int inverseAlpha; int premult[3]; @@ -450,14 +442,14 @@ static void R_BlendOverTexture( byte *data, int pixelCount, byte blend[4] ) { premult[1] = blend[1] * blend[3]; premult[2] = blend[2] * blend[3]; - for ( i = 0 ; i < pixelCount ; i++, data+=4 ) { - data[0] = ( data[0] * inverseAlpha + premult[0] ) >> 9; - data[1] = ( data[1] * inverseAlpha + premult[1] ) >> 9; - data[2] = ( data[2] * inverseAlpha + premult[2] ) >> 9; + for ( i = 0 ; i < pixelCount ; i++ ) { + data[i][0] = ( data[i][0] * inverseAlpha + premult[0] ) >> 9; + data[i][1] = ( data[i][1] * inverseAlpha + premult[1] ) >> 9; + data[i][2] = ( data[i][2] * inverseAlpha + premult[2] ) >> 9; } } -byte mipBlendColors[16][4] = { +color4ub_t mipBlendColors[16] = { {0,0,0,0}, {255,0,0,128}, {0,255,0,128}, @@ -484,7 +476,7 @@ Upload32 =============== */ extern qboolean charSet; -static void Upload32( unsigned *data, +static void Upload32( color4ub_t *data, int width, int height, qboolean mipmap, qboolean picmip, @@ -493,11 +485,11 @@ static void Upload32( unsigned *data, int *pUploadWidth, int *pUploadHeight ) { int samples; - unsigned *scaledBuffer = NULL; - unsigned *resampledBuffer = NULL; + color4ub_t *scaledBuffer = NULL; + color4ub_t *resampledBuffer = NULL; int scaled_width, scaled_height; int i, c; - byte *scan; + color4ub_t *scan; GLenum internalFormat = GL_RGB; float rMax = 0, gMax = 0, bMax = 0; @@ -550,34 +542,34 @@ static void Upload32( unsigned *data, scaled_height >>= 1; } - scaledBuffer = ri.Hunk_AllocateTempMemory( sizeof( unsigned ) * scaled_width * scaled_height ); + scaledBuffer = ri.Hunk_AllocateTempMemory( sizeof( color4ub_t ) * scaled_width * scaled_height ); // // scan the texture for each channel's max values // and verify if the alpha channel is being used or not // - c = width*height; - scan = ((byte *)data); + c = width * height; + scan = data; samples = 3; if( r_greyscale->integer ) { for ( i = 0; i < c; i++ ) { - byte luma = LUMA(scan[i*4], scan[i*4 + 1], scan[i*4 + 2]); - scan[i*4] = luma; - scan[i*4 + 1] = luma; - scan[i*4 + 2] = luma; + byte luma = LUMA(scan[i][0], scan[i][1], scan[i][2]); + scan[i][0] = luma; + scan[i][1] = luma; + scan[i][2] = luma; } } else if( r_greyscale->value ) { for ( i = 0; i < c; i++ ) { - float luma = LUMA(scan[i*4], scan[i*4 + 1], scan[i*4 + 2]); - scan[i*4] = LERP(scan[i*4], luma, r_greyscale->value); - scan[i*4 + 1] = LERP(scan[i*4 + 1], luma, r_greyscale->value); - scan[i*4 + 2] = LERP(scan[i*4 + 2], luma, r_greyscale->value); + float luma = LUMA(scan[i][0], scan[i][1], scan[i][2]); + scan[i][0] = LERP(scan[i][0], luma, r_greyscale->value); + scan[i][1] = LERP(scan[i][1], luma, r_greyscale->value); + scan[i][2] = LERP(scan[i][2], luma, r_greyscale->value); } } @@ -592,19 +584,19 @@ static void Upload32( unsigned *data, { for ( i = 0; i < c; i++ ) { - if ( scan[i*4+0] > rMax ) + if ( scan[i][0] > rMax ) { - rMax = scan[i*4+0]; + rMax = scan[i][0]; } - if ( scan[i*4+1] > gMax ) + if ( scan[i][1] > gMax ) { - gMax = scan[i*4+1]; + gMax = scan[i][1]; } - if ( scan[i*4+2] > bMax ) + if ( scan[i][2] > bMax ) { - bMax = scan[i*4+2]; + bMax = scan[i][2]; } - if ( scan[i*4 + 3] != 255 ) + if ( scan[i][3] != 255 ) { samples = 4; break; @@ -677,7 +669,7 @@ static void Upload32( unsigned *data, // copy or resample data as appropriate for first MIP level if ( ( scaled_width == width ) && - ( scaled_height == height ) ) { + ( scaled_height == height ) ) { if (!mipmap) { qglTexImage2D (GL_TEXTURE_2D, 0, internalFormat, scaled_width, scaled_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); @@ -687,13 +679,13 @@ static void Upload32( unsigned *data, goto done; } - Com_Memcpy (scaledBuffer, data, width*height*4); + Com_Memcpy (scaledBuffer, data, width*height*sizeof(color4ub_t)); } else { // use the normal mip-mapping function to go down from here while ( width > scaled_width || height > scaled_height ) { - R_MipMap( (byte *)data, width, height ); + R_MipMap( data, width, height ); width >>= 1; height >>= 1; if ( width < 1 ) { @@ -703,7 +695,7 @@ static void Upload32( unsigned *data, height = 1; } } - Com_Memcpy( scaledBuffer, data, width * height * 4 ); + Com_Memcpy( scaledBuffer, data, width * height * sizeof(color4ub_t) ); } R_LightScaleTexture (scaledBuffer, scaled_width, scaled_height, !mipmap ); @@ -721,7 +713,7 @@ static void Upload32( unsigned *data, miplevel = 0; while (scaled_width > 1 || scaled_height > 1) { - R_MipMap( (byte *)scaledBuffer, scaled_width, scaled_height ); + R_MipMap( scaledBuffer, scaled_width, scaled_height ); scaled_width >>= 1; scaled_height >>= 1; if (scaled_width < 1) @@ -731,7 +723,7 @@ static void Upload32( unsigned *data, miplevel++; if ( r_colorMipLevels->integer ) { - R_BlendOverTexture( (byte *)scaledBuffer, scaled_width * scaled_height, mipBlendColors[miplevel] ); + R_BlendOverTexture( scaledBuffer, scaled_width * scaled_height, mipBlendColors[miplevel] ); } qglTexImage2D (GL_TEXTURE_2D, miplevel, internalFormat, scaled_width, scaled_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, scaledBuffer ); @@ -745,23 +737,23 @@ done: qglTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, (GLint)Com_Clamp( 1, maxAnisotropy, r_ext_max_anisotropy->integer ) ); - qglTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_min); - qglTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_max); + qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, gl_filter_min); + qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, gl_filter_max); } else { if ( textureFilterAnisotropic ) qglTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 1 ); - qglTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); - qglTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); + qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); + qglTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); } GL_CheckErrors(); - if ( scaledBuffer != 0 ) + if ( scaledBuffer != NULL ) ri.Hunk_FreeTempMemory( scaledBuffer ); - if ( resampledBuffer != 0 ) + if ( resampledBuffer != NULL ) ri.Hunk_FreeTempMemory( resampledBuffer ); } @@ -816,7 +808,7 @@ image_t *R_CreateImage( const char *name, const byte *pic, int width, int height GL_Bind(image); - Upload32( (unsigned *)pic, image->width, image->height, + Upload32( (color4ub_t *)pic, image->width, image->height, image->mipmap, allowPicmip, isLightmap, @@ -1008,7 +1000,7 @@ R_CreateDlightImage #define DLIGHT_SIZE 16 static void R_CreateDlightImage( void ) { int x,y; - byte data[DLIGHT_SIZE][DLIGHT_SIZE][4]; + color4ub_t data[DLIGHT_SIZE][DLIGHT_SIZE]; int b; // make a centered inverse-square falloff blob for dynamic lighting @@ -1097,21 +1089,21 @@ R_CreateFogImage #define FOG_T 32 static void R_CreateFogImage( void ) { int x,y; - byte *data; + color4ub_t *data; float d; float borderColor[4]; - data = ri.Hunk_AllocateTempMemory( FOG_S * FOG_T * 4 ); + data = ri.Hunk_AllocateTempMemory( FOG_S * FOG_T * sizeof(color4ub_t) ); // S is distance, T is depth for (x=0 ; x