]> git.rm.cloudns.org Git - xonotic/darkplaces.git/commitdiff
more concise version of Texture2DBGRA8
authoreihrul <eihrul@d7cf8633-e32d-0410-b094-e92efae38249>
Wed, 6 Apr 2011 16:37:31 +0000 (16:37 +0000)
committereihrul <eihrul@d7cf8633-e32d-0410-b094-e92efae38249>
Wed, 6 Apr 2011 16:37:31 +0000 (16:37 +0000)
git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@11033 d7cf8633-e32d-0410-b094-e92efae38249

dpsoftrast.c

index 954bfcdf5c16e15f3fd8a8cda2cbebcdd56f0156..609f444e70bc0a26816d09581e2c6d8bc6231da9 100644 (file)
@@ -2297,166 +2297,63 @@ void DPSOFTRAST_Draw_Span_FinishBGRA8(DPSOFTRAST_State_Thread *thread, const DPS
 #endif
 }
 
-static void DPSOFTRAST_Texture2D(DPSOFTRAST_Texture *texture, int mip, float x, float y, float c[4])
-       // warning: this is SLOW, only use if the optimized per-span functions won't do
-       // FIXME does this function need flipping of the color order?
-{
-       const unsigned char * RESTRICT pixelbase;
-       const unsigned char * RESTRICT pixel[4];
-       int tciwrapmask[2];
-       tciwrapmask[0] = texture->mipmap[mip][2]-1;
-       tciwrapmask[1] = texture->mipmap[mip][3]-1;
-       pixelbase = (unsigned char *)texture->bytes + texture->mipmap[mip][0];
-       if(texture->filter & DPSOFTRAST_TEXTURE_FILTER_LINEAR)
-       {
-               if (texture->flags & DPSOFTRAST_TEXTURE_FLAG_CLAMPTOEDGE)
-               {
-                       unsigned int tc[2] = { x * (texture->mipmap[mip][2]<<12) - 2048, y * (texture->mipmap[mip][3]<<12) - 2048};
-                       unsigned int frac[2] = { tc[0]&0xFFF, tc[1]&0xFFF };
-                       unsigned int ifrac[2] = { 0x1000 - frac[0], 0x1000 - frac[1] };
-                       unsigned int lerp[4] = { ifrac[0]*ifrac[1], frac[0]*ifrac[1], ifrac[0]*frac[1], frac[0]*frac[1] };
-                       int tci[2] = { tc[0]>>12, tc[1]>>12 };
-                       int tci1[2] = { tci[0] + 1, tci[1] + 1 };
-                       tci[0] = tci[0] >= 0 ? (tci[0] <= texture->mipmap[mip][2]-1 ? tci[0] : texture->mipmap[mip][2]-1) : 0;
-                       tci[1] = tci[1] >= 0 ? (tci[1] <= texture->mipmap[mip][3]-1 ? tci[1] : texture->mipmap[mip][3]-1) : 0;
-                       tci1[0] = tci1[0] >= 0 ? (tci1[0] <= texture->mipmap[mip][2]-1 ? tci1[0] : texture->mipmap[mip][2]-1) : 0;
-                       tci1[1] = tci1[1] >= 0 ? (tci1[1] <= texture->mipmap[mip][3]-1 ? tci1[1] : texture->mipmap[mip][3]-1) : 0;
-                       pixel[0] = pixelbase + 4 * (tci[1]*texture->mipmap[mip][2]+tci[0]);
-                       pixel[1] = pixelbase + 4 * (tci[1]*texture->mipmap[mip][2]+tci1[0]);
-                       pixel[2] = pixelbase + 4 * (tci1[1]*texture->mipmap[mip][2]+tci[0]);
-                       pixel[3] = pixelbase + 4 * (tci1[1]*texture->mipmap[mip][2]+tci1[0]);
-                       c[0] = (pixel[0][0]*lerp[0]+pixel[1][0]*lerp[1]+pixel[2][0]*lerp[2]+pixel[3][0]*lerp[3]) * (1.0f / 0xFF00000);
-                       c[1] = (pixel[0][1]*lerp[0]+pixel[1][1]*lerp[1]+pixel[2][1]*lerp[2]+pixel[3][1]*lerp[3]) * (1.0f / 0xFF00000);
-                       c[2] = (pixel[0][2]*lerp[0]+pixel[1][2]*lerp[1]+pixel[2][2]*lerp[2]+pixel[3][2]*lerp[3]) * (1.0f / 0xFF00000);
-                       c[3] = (pixel[0][3]*lerp[0]+pixel[1][3]*lerp[1]+pixel[2][3]*lerp[2]+pixel[3][3]*lerp[3]) * (1.0f / 0xFF00000);
-               }
-               else
-               {
-                       unsigned int tc[2] = { x * (texture->mipmap[mip][2]<<12) - 2048, y * (texture->mipmap[mip][3]<<12) - 2048};
-                       unsigned int frac[2] = { tc[0]&0xFFF, tc[1]&0xFFF };
-                       unsigned int ifrac[2] = { 0x1000 - frac[0], 0x1000 - frac[1] };
-                       unsigned int lerp[4] = { ifrac[0]*ifrac[1], frac[0]*ifrac[1], ifrac[0]*frac[1], frac[0]*frac[1] };
-                       int tci[2] = { tc[0]>>12, tc[1]>>12 };
-                       int tci1[2] = { tci[0] + 1, tci[1] + 1 };
-                       tci[0] &= tciwrapmask[0];
-                       tci[1] &= tciwrapmask[1];
-                       tci1[0] &= tciwrapmask[0];
-                       tci1[1] &= tciwrapmask[1];
-                       pixel[0] = pixelbase + 4 * (tci[1]*texture->mipmap[mip][2]+tci[0]);
-                       pixel[1] = pixelbase + 4 * (tci[1]*texture->mipmap[mip][2]+tci1[0]);
-                       pixel[2] = pixelbase + 4 * (tci1[1]*texture->mipmap[mip][2]+tci[0]);
-                       pixel[3] = pixelbase + 4 * (tci1[1]*texture->mipmap[mip][2]+tci1[0]);
-                       c[0] = (pixel[0][0]*lerp[0]+pixel[1][0]*lerp[1]+pixel[2][0]*lerp[2]+pixel[3][0]*lerp[3]) * (1.0f / 0xFF00000);
-                       c[1] = (pixel[0][1]*lerp[0]+pixel[1][1]*lerp[1]+pixel[2][1]*lerp[2]+pixel[3][1]*lerp[3]) * (1.0f / 0xFF00000);
-                       c[2] = (pixel[0][2]*lerp[0]+pixel[1][2]*lerp[1]+pixel[2][2]*lerp[2]+pixel[3][2]*lerp[3]) * (1.0f / 0xFF00000);
-                       c[3] = (pixel[0][3]*lerp[0]+pixel[1][3]*lerp[1]+pixel[2][3]*lerp[2]+pixel[3][3]*lerp[3]) * (1.0f / 0xFF00000);
-               }
-       }
-       else
-       {
-               if (texture->flags & DPSOFTRAST_TEXTURE_FLAG_CLAMPTOEDGE)
-               {
-                       int tci[2] = { x * texture->mipmap[mip][2], y * texture->mipmap[mip][3] };
-                       tci[0] = tci[0] >= 0 ? (tci[0] <= texture->mipmap[mip][2]-1 ? tci[0] : texture->mipmap[mip][2]-1) : 0;
-                       tci[1] = tci[1] >= 0 ? (tci[1] <= texture->mipmap[mip][3]-1 ? tci[1] : texture->mipmap[mip][3]-1) : 0;
-                       pixel[0] = pixelbase + 4 * (tci[1]*texture->mipmap[mip][2]+tci[0]);
-                       c[0] = pixel[0][0] * (1.0f / 255.0f);
-                       c[1] = pixel[0][1] * (1.0f / 255.0f);
-                       c[2] = pixel[0][2] * (1.0f / 255.0f);
-                       c[3] = pixel[0][3] * (1.0f / 255.0f);
-               }
-               else
-               {
-                       int tci[2] = { x * texture->mipmap[mip][2], y * texture->mipmap[mip][3] };
-                       tci[0] &= tciwrapmask[0];
-                       tci[1] &= tciwrapmask[1];
-                       pixel[0] = pixelbase + 4 * (tci[1]*texture->mipmap[mip][2]+tci[0]);
-                       c[0] = pixel[0][0] * (1.0f / 255.0f);
-                       c[1] = pixel[0][1] * (1.0f / 255.0f);
-                       c[2] = pixel[0][2] * (1.0f / 255.0f);
-                       c[3] = pixel[0][3] * (1.0f / 255.0f);
-               }
-       }
-}
-
 static void DPSOFTRAST_Texture2DBGRA8(DPSOFTRAST_Texture *texture, int mip, float x, float y, unsigned char c[4])
        // warning: this is SLOW, only use if the optimized per-span functions won't do
 {
        const unsigned char * RESTRICT pixelbase;
        const unsigned char * RESTRICT pixel[4];
-       int tciwrapmask[2];
-       tciwrapmask[0] = texture->mipmap[mip][2]-1;
-       tciwrapmask[1] = texture->mipmap[mip][3]-1;
+       int width = texture->mipmap[mip][2], height = texture->mipmap[mip][3];
+       int wrapmask[2] = { width-1, height-1 };
        pixelbase = (unsigned char *)texture->bytes + texture->mipmap[mip][0];
        if(texture->filter & DPSOFTRAST_TEXTURE_FILTER_LINEAR)
        {
+               unsigned int tc[2] = { x * (width<<12) - 2048, y * (height<<12) - 2048};
+               unsigned int frac[2] = { tc[0]&0xFFF, tc[1]&0xFFF };
+               unsigned int ifrac[2] = { 0x1000 - frac[0], 0x1000 - frac[1] };
+               unsigned int lerp[4] = { ifrac[0]*ifrac[1], frac[0]*ifrac[1], ifrac[0]*frac[1], frac[0]*frac[1] };
+               int tci[2] = { tc[0]>>12, tc[1]>>12 };
+               int tci1[2] = { tci[0] + 1, tci[1] + 1 };
                if (texture->flags & DPSOFTRAST_TEXTURE_FLAG_CLAMPTOEDGE)
                {
-                       unsigned int tc[2] = { x * (texture->mipmap[mip][2]<<12) - 2048, y * (texture->mipmap[mip][3]<<12) - 2048};
-                       unsigned int frac[2] = { tc[0]&0xFFF, tc[1]&0xFFF };
-                       unsigned int ifrac[2] = { 0x1000 - frac[0], 0x1000 - frac[1] };
-                       unsigned int lerp[4] = { ifrac[0]*ifrac[1], frac[0]*ifrac[1], ifrac[0]*frac[1], frac[0]*frac[1] };
-                       int tci[2] = { tc[0]>>12, tc[1]>>12 };
-                       int tci1[2] = { tci[0] + 1, tci[1] + 1 };
-                       tci[0] = tci[0] >= 0 ? (tci[0] <= texture->mipmap[mip][2]-1 ? tci[0] : texture->mipmap[mip][2]-1) : 0;
-                       tci[1] = tci[1] >= 0 ? (tci[1] <= texture->mipmap[mip][3]-1 ? tci[1] : texture->mipmap[mip][3]-1) : 0;
-                       tci1[0] = tci1[0] >= 0 ? (tci1[0] <= texture->mipmap[mip][2]-1 ? tci1[0] : texture->mipmap[mip][2]-1) : 0;
-                       tci1[1] = tci1[1] >= 0 ? (tci1[1] <= texture->mipmap[mip][3]-1 ? tci1[1] : texture->mipmap[mip][3]-1) : 0;
-                       pixel[0] = pixelbase + 4 * (tci[1]*texture->mipmap[mip][2]+tci[0]);
-                       pixel[1] = pixelbase + 4 * (tci[1]*texture->mipmap[mip][2]+tci1[0]);
-                       pixel[2] = pixelbase + 4 * (tci1[1]*texture->mipmap[mip][2]+tci[0]);
-                       pixel[3] = pixelbase + 4 * (tci1[1]*texture->mipmap[mip][2]+tci1[0]);
-                       c[0] = (pixel[0][0]*lerp[0]+pixel[1][0]*lerp[1]+pixel[2][0]*lerp[2]+pixel[3][0]*lerp[3])>>24;
-                       c[1] = (pixel[0][1]*lerp[0]+pixel[1][1]*lerp[1]+pixel[2][1]*lerp[2]+pixel[3][1]*lerp[3])>>24;
-                       c[2] = (pixel[0][2]*lerp[0]+pixel[1][2]*lerp[1]+pixel[2][2]*lerp[2]+pixel[3][2]*lerp[3])>>24;
-                       c[3] = (pixel[0][3]*lerp[0]+pixel[1][3]*lerp[1]+pixel[2][3]*lerp[2]+pixel[3][3]*lerp[3])>>24;
+                       tci[0] = tci[0] >= 0 ? (tci[0] <= wrapmask[0] ? tci[0] : wrapmask[0]) : 0;
+                       tci[1] = tci[1] >= 0 ? (tci[1] <= wrapmask[1] ? tci[1] : wrapmask[1]) : 0;
+                       tci1[0] = tci1[0] >= 0 ? (tci1[0] <= wrapmask[0] ? tci1[0] : wrapmask[0]) : 0;
+                       tci1[1] = tci1[1] >= 0 ? (tci1[1] <= wrapmask[1] ? tci1[1] : wrapmask[1]) : 0;
                }
                else
                {
-                       unsigned int tc[2] = { x * (texture->mipmap[mip][2]<<12) - 2048, y * (texture->mipmap[mip][3]<<12) - 2048};
-                       unsigned int frac[2] = { tc[0]&0xFFF, tc[1]&0xFFF };
-                       unsigned int ifrac[2] = { 0x1000 - frac[0], 0x1000 - frac[1] };
-                       unsigned int lerp[4] = { ifrac[0]*ifrac[1], frac[0]*ifrac[1], ifrac[0]*frac[1], frac[0]*frac[1] };
-                       int tci[2] = { tc[0]>>12, tc[1]>>12 };
-                       int tci1[2] = { tci[0] + 1, tci[1] + 1 };
-                       tci[0] &= tciwrapmask[0];
-                       tci[1] &= tciwrapmask[1];
-                       tci1[0] &= tciwrapmask[0];
-                       tci1[1] &= tciwrapmask[1];
-                       pixel[0] = pixelbase + 4 * (tci[1]*texture->mipmap[mip][2]+tci[0]);
-                       pixel[1] = pixelbase + 4 * (tci[1]*texture->mipmap[mip][2]+tci1[0]);
-                       pixel[2] = pixelbase + 4 * (tci1[1]*texture->mipmap[mip][2]+tci[0]);
-                       pixel[3] = pixelbase + 4 * (tci1[1]*texture->mipmap[mip][2]+tci1[0]);
-                       c[0] = (pixel[0][0]*lerp[0]+pixel[1][0]*lerp[1]+pixel[2][0]*lerp[2]+pixel[3][0]*lerp[3])>>24;
-                       c[1] = (pixel[0][1]*lerp[0]+pixel[1][1]*lerp[1]+pixel[2][1]*lerp[2]+pixel[3][1]*lerp[3])>>24;
-                       c[2] = (pixel[0][2]*lerp[0]+pixel[1][2]*lerp[1]+pixel[2][2]*lerp[2]+pixel[3][2]*lerp[3])>>24;
-                       c[3] = (pixel[0][3]*lerp[0]+pixel[1][3]*lerp[1]+pixel[2][3]*lerp[2]+pixel[3][3]*lerp[3])>>24;
+                       tci[0] &= wrapmask[0];
+                       tci[1] &= wrapmask[1];
+                       tci1[0] &= wrapmask[0];
+                       tci1[1] &= wrapmask[1];
                }
+               pixel[0] = pixelbase + 4 * (tci[1]*width+tci[0]);
+               pixel[1] = pixelbase + 4 * (tci[1]*width+tci1[0]);
+               pixel[2] = pixelbase + 4 * (tci1[1]*width+tci[0]);
+               pixel[3] = pixelbase + 4 * (tci1[1]*width+tci1[0]);
+               c[0] = (pixel[0][0]*lerp[0]+pixel[1][0]*lerp[1]+pixel[2][0]*lerp[2]+pixel[3][0]*lerp[3])>>24;
+               c[1] = (pixel[0][1]*lerp[0]+pixel[1][1]*lerp[1]+pixel[2][1]*lerp[2]+pixel[3][1]*lerp[3])>>24;
+               c[2] = (pixel[0][2]*lerp[0]+pixel[1][2]*lerp[1]+pixel[2][2]*lerp[2]+pixel[3][2]*lerp[3])>>24;
+               c[3] = (pixel[0][3]*lerp[0]+pixel[1][3]*lerp[1]+pixel[2][3]*lerp[2]+pixel[3][3]*lerp[3])>>24;
        }
        else
        {
+               int tci[2] = { x * width, y * height };
                if (texture->flags & DPSOFTRAST_TEXTURE_FLAG_CLAMPTOEDGE)
                {
-                       int tci[2] = { x * texture->mipmap[mip][2], y * texture->mipmap[mip][3] };
-                       tci[0] = tci[0] >= 0 ? (tci[0] <= texture->mipmap[mip][2]-1 ? tci[0] : texture->mipmap[mip][2]-1) : 0;
-                       tci[1] = tci[1] >= 0 ? (tci[1] <= texture->mipmap[mip][3]-1 ? tci[1] : texture->mipmap[mip][3]-1) : 0;
-                       pixel[0] = pixelbase + 4 * (tci[1]*texture->mipmap[mip][2]+tci[0]);
-                       c[0] = pixel[0][0];
-                       c[1] = pixel[0][1];
-                       c[2] = pixel[0][2];
-                       c[3] = pixel[0][3];
+                       tci[0] = tci[0] >= 0 ? (tci[0] <= wrapmask[0] ? tci[0] : wrapmask[0]) : 0;
+                       tci[1] = tci[1] >= 0 ? (tci[1] <= wrapmask[1] ? tci[1] : wrapmask[1]) : 0;
                }
                else
                {
-                       int tci[2] = { x * texture->mipmap[mip][2], y * texture->mipmap[mip][3] };
-                       tci[0] &= tciwrapmask[0];
-                       tci[1] &= tciwrapmask[1];
-                       pixel[0] = pixelbase + 4 * (tci[1]*texture->mipmap[mip][2]+tci[0]);
-                       c[0] = pixel[0][0];
-                       c[1] = pixel[0][1];
-                       c[2] = pixel[0][2];
-                       c[3] = pixel[0][3];
+                       tci[0] &= wrapmask[0];
+                       tci[1] &= wrapmask[1];
                }
+               pixel[0] = pixelbase + 4 * (tci[1]*width+tci[0]);
+               c[0] = pixel[0][0];
+               c[1] = pixel[0][1];
+               c[2] = pixel[0][2];
+               c[3] = pixel[0][3];
        }
 }