softgpu: Cleanup CLUT lookup code and fix a critical bug related to it. Also added semi-working fast texture storage mode support.

This commit is contained in:
Tony Wasserka authored and neobrain committed 2013-08-16 23:49:06 +02:00
1 parent 84425aafef
commit 0dbe5c5cc1
1 file changed
+35 -11
+35 -11
View File
@@ -32,6 +32,23 @@ static int orient2d(const DrawingCoords& v0, const DrawingCoords& v1, const Draw
return ((int)v1.x-(int)v0.x)*((int)v2.y-(int)v0.y) - ((int)v1.y-(int)v0.y)*((int)v2.x-(int)v0.x);
}
int GetPixelDataOffset(int texel_size_bits, int u, int v, int width)
{
if (!(gstate.texmode & 1))
return v * width * texel_size_bits / 8 + u * texel_size_bits / 8;
int tile_size_bits = 32;
int texels_per_tile = tile_size_bits / texel_size_bits; // 32/8
int block_width_in_tiles = 4; // 4 tiles (generally != 4 texels)
int block_height_in_tiles = 8; // 8 tiles = 8 texels
int tiles_per_block = block_width_in_tiles * block_height_in_tiles;
int block_stride_bits = tiles_per_block * tile_size_bits;
return u / (texels_per_tile * block_width_in_tiles) * (block_stride_bits/8) +
(u % (texels_per_tile * block_width_in_tiles)) * (texel_size_bits / 8) +
(v % block_height_in_tiles) * (block_width_in_tiles * tile_size_bits / 8) +
(v / block_height_in_tiles) * (width * texel_size_bits * block_height_in_tiles / 8);
}
u32 SampleNearest(int level, float s, float t)
{
int texfmt = gstate.texformat & 0xF;
@@ -41,15 +58,18 @@ u32 SampleNearest(int level, float s, float t)
int width = 1 << (gstate.texsize[level] & 0xf);
int height = 1 << ((gstate.texsize[level]>>8) & 0xf);
// TODO: Should probably check if textures are aligned properly...
// TODO: Not sure if that through mode treatment is correct..
int u = (gstate.isModeThrough()) ? s : s * width; // TODO: -1?
int v = (gstate.isModeThrough()) ? t : t * height; // TODO: -1?
// TODO: Assert tmode.hsm == 0 (normal storage mode)
// TODO: texcoord wrapping!!
// TODO: Assert tmap.tmn == 0 (uv texture mapping mode)
if (texfmt == GE_TFMT_4444) {
srcptr += 2 * v * width + 2 * u;
srcptr += GetPixelDataOffset(16, u, v, width);
u8 r = (*srcptr) >> 4;
u8 g = (*srcptr) & 0xF;
u8 b = (*(srcptr+1)) >> 4;
@@ -60,7 +80,7 @@ u32 SampleNearest(int level, float s, float t)
a = (a << 4) | a;
return (r << 24) | (g << 16) | (b << 8) | a;
} else if (texfmt == GE_TFMT_5551) {
srcptr += 2 * v * width + 2 * u;
srcptr += GetPixelDataOffset(16, u, v, width);
u8 r = (*srcptr) & 0x1F;
u8 g = (((*srcptr) & 0xE0) >> 5) | (((*(srcptr+1))&0x3) << 3);
u8 b = ((*srcptr+1) & 0x7C) >> 2;
@@ -71,7 +91,7 @@ u32 SampleNearest(int level, float s, float t)
a = (a) ? 0xff : 0;
return (r << 24) | (g << 16) | (b << 8) | a;
} else if (texfmt == GE_TFMT_5650) {
srcptr += 2 * v * width + 2 * u;
srcptr += GetPixelDataOffset(16, u, v, width);
u8 r = (*srcptr) & 0x1F;
u8 g = (((*srcptr) & 0xE0) >> 5) | (((*(srcptr+1))&0x7) << 3);
u8 b = ((*srcptr+1) & 0xF8) >> 3;
@@ -81,26 +101,30 @@ u32 SampleNearest(int level, float s, float t)
b = (b << 3) | (b >> 2);
return (r << 24) | (g << 16) | (b << 8) | a;
} else if (texfmt == GE_TFMT_8888) {
srcptr += 4 * v * width + 4 * u;
srcptr += GetPixelDataOffset(32, u, v, width);
u8 r = *srcptr++;
u8 g = *srcptr++;
u8 b = *srcptr++;
u8 a = *srcptr++;
return (r << 24) | (g << 16) | (b << 8) | a;
} else if (texfmt == GE_TFMT_CLUT8) {
// TODO: Assert that we're using GE_CMODE_32BIT_ABGR8888;
srcptr += v * width + u;
srcptr += GetPixelDataOffset(8, u, v, width);
u16 index = (((u32)*srcptr) >> gstate.getClutIndexShift()) & 0xFF;
index &= gstate.getClutIndexMask();
index = (index & 0xE) | gstate.getClutIndexStartPos(); // Topmost bit
index = (index & 0xFF) | gstate.getClutIndexStartPos(); // Topmost bit is copied from start pos
// TODO: Assert that we're using GE_CMODE_32BIT_ABGR8888;
return clut[index];
} else if (texfmt == GE_TFMT_CLUT4) {
// TODO: Assert that we're using GE_CMODE_32BIT_ABGR8888;
srcptr += v * width / 2 + u/2;
srcptr += GetPixelDataOffset(4, u, v, width);
u8 val = (u%2) ? (*srcptr & 0xF) : (*srcptr >> 4);
u16 index = (((u32)val) >> gstate.getClutIndexShift()) & 0xFF;
index &= gstate.getClutIndexMask();
index = (index & 0xE) | gstate.getClutIndexStartPos(); // Topmost bit
index = (index & 0xFF) | gstate.getClutIndexStartPos(); // Topmost bit is copied from start pos
// TODO: Assert that we're using GE_CMODE_32BIT_ABGR8888;
return clut[index];
} else {
ERROR_LOG(G3D, "Unsupported texture format: %x", texfmt);