diff --git a/GPU/GLES/TextureCache.cpp b/GPU/GLES/TextureCache.cpp index 4a25bb3af8..dc8004b10f 100644 --- a/GPU/GLES/TextureCache.cpp +++ b/GPU/GLES/TextureCache.cpp @@ -48,7 +48,11 @@ struct TexCacheEntry typedef std::map TexCache; static TexCache cache; -u8 *tempArea; +u32 tmpTexBuf32[1024 * 1024]; +u16 tmpTexBuf16[1024 * 1024]; + +u32 clutBuf32[4096]; +u16 clutBuf16[4096]; void TextureCache_Clear(bool delete_them) { @@ -66,54 +70,290 @@ void TextureCache_Clear(bool delete_them) } } -u32 PaletteLoad(int index) +u32 GetClutAddr(u32 clutEntrySize) { - int pf = gstate.clutformat & 3; - int shift = (gstate.clutformat >> 2) & 31; - int mask = (gstate.clutformat >> 8) & 255; - int start = ((gstate.clutformat >> 16) & 31) * 16; - - if (pf<3) - { - //16-bit - u16 *p = (u16*)gstate.paletteMem; - u16 col = p[((start+index)>>shift) & mask]; - int r,g,b,a; - - // TODO: properly expand the lower bits. - switch (pf) - { - case 0: - r = (col&0x1f)*8; - g = ((col>>5)&0x3f)*4; - b = ((col>>11)&0x1f)*8; - a = 255; - break; - case 1: - r = (col&0x1f)*8; - g = ((col>>5)&0x1f)*8; - b = ((col>>10)&0x1f)*8; - a = (col>>15)*255; - break; - case 2: - r = (col&0xf)*16; - g = ((col>>4)&0xf)*16; - b = ((col>>8)&0xf)*16; - a = ((col>>12)&0xF)*16; - break; - } - - // We now use OpenGL ES 2.0 style colors. - return (a << 24) | (b<<16) | (g<<8) | (r<<0); - } - else - { - u32 *p = (u32*)gstate.paletteMem; - u32 col = p[((start + index) >> shift) & mask]; - return col; - } + return ((gstate.clutaddr & 0xFFFFFF) | ((gstate.clutaddrupper << 8) & 0x0F000000)) + ((gstate.clutformat >> 16) & 0x1f) * clutEntrySize; } +u32 GetClutIndex(u32 index) +{ + return ((((gstate.clutformat >> 16) & 0x1f) + index) >> ((gstate.clutformat >> 2) & 0x1f)) & ((gstate.clutformat >> 8) & 0xff); +} + +u16 *ReadClut16() +{ + u32 clutNumEntries = (gstate.loadclut & 0x3f) * 16; + u32 clutAddr = GetClutAddr(2); + for (u32 i = ((gstate.clutformat >> 16) & 0x1f); i < clutNumEntries; i++) + clutBuf16[i] = Memory::Read_U16(clutAddr + i * 2); + return clutBuf16; +} + +u32 *ReadClut32() +{ + u32 clutNumEntries = (gstate.loadclut & 0x3f) * 8; + u32 clutAddr = GetClutAddr(4); + for (u32 i = ((gstate.clutformat >> 16) & 0x1f); i < clutNumEntries; i++) + clutBuf32[i] = Memory::Read_U32(clutAddr + i * 4); + return clutBuf32; +} + +void *UnswizzleFromMem(u32 texaddr, u32 bytesPerPixel, u32 level) +{ + u32 addr = texaddr; + u32 rowWidth = (bytesPerPixel > 0) ? ((gstate.texbufwidth[level] & 0x3FF) * bytesPerPixel) : ((gstate.texbufwidth[level] & 0x3FF) / 2); + u32 pitch = rowWidth / 4; + u32 bxc = rowWidth / 16; + u32 byc = ((1 << ((gstate.texsize[level] >> 8) & 0xf)) + 7) / 8; + if (byc == 0) + byc = 1; + + u32 ydest = 0; + + u32 by; + for (by = 0; by < byc; by++) + { + if (rowWidth >= 16) + { + u32 xdest = ydest; + u32 bx; + for (bx = 0; bx < bxc; bx++) + { + u32 dest = xdest; + u8 n; + for (n = 0; n < 8; n++) + { + u32 k; + for (k = 0; k < 4; k++) { + tmpTexBuf32[dest + k] = Memory::Read_U32(addr); + addr += 4; + } + dest += pitch; + } + xdest += 4; + } + ydest += (rowWidth * 8) / 4; + } + else if (rowWidth == 8) + { + u32 n; + for (n = 0; n < 8; n++, ydest += 2) + { + tmpTexBuf32[ydest + 0] = Memory::Read_U32(addr + 0); + tmpTexBuf32[ydest + 1] = Memory::Read_U32(addr + 4); + addr += 16; // skip two u32 + } + } + else if (rowWidth == 4) + { + u32 n; + for (n = 0; n < 8; n++, ydest++) { + tmpTexBuf32[ydest] = Memory::Read_U32(addr); + addr += 16; + } + } + else if (rowWidth == 2) + { + u32 n; + for (n = 0; n < 4; n++, ydest++) + { + u16 n1 = Memory::Read_U32(addr + 0) & 0xffff; + u16 n2 = Memory::Read_U32(addr + 16) & 0xffff; + tmpTexBuf32[ydest] = (u32)n1 | ((u32)n2 << 16); + addr += 32; + } + } + else if (rowWidth == 1) + { + u32 n; + for (n = 0; n < 2; n++, ydest++) + { + u8 n1 = Memory::Read_U32(addr + 0) & 0xf; + u8 n2 = Memory::Read_U32(addr + 16) & 0xf; + u8 n3 = Memory::Read_U32(addr + 32) & 0xf; + u8 n4 = Memory::Read_U32(addr + 48) & 0xf; + + tmpTexBuf32[ydest] = (u32)n1 | ((u32)n2 << 8) | ((u32)n3 << 16) | ((u32)n4 << 24); + } + } + } + return tmpTexBuf32; +} + +void *readIndexedTex(u32 level, u32 texaddr, u32 bytesPerIndex) +{ + u32 length = (gstate.texbufwidth[level] & 0x3FF) * (1 << ((gstate.texsize[level] >> 8) & 0xf)); + void *buf = NULL; + + switch ((gstate.clutformat & 3)) + { + case GE_CMODE_16BIT_BGR5650: + case GE_CMODE_16BIT_ABGR5551: + case GE_CMODE_16BIT_ABGR4444: + { + u16 *clut = ReadClut16(); + if (!(gstate.texmode & 1)) + { + u32 i; + switch (bytesPerIndex) + { + case 1: + for (i = 0; i < length; i++) { + u8 index = Memory::Read_U8(texaddr + i); + tmpTexBuf16[i] = clut[GetClutIndex(index)]; + } + break; + + case 2: + for (i = 0; i < length; i++) { + u16 index = Memory::Read_U16(texaddr + i * 2); + tmpTexBuf16[i] = clut[GetClutIndex(index)]; + } + break; + + case 4: + for (i = 0; i < length; i++) { + u32 index = Memory::Read_U32(texaddr + i * 4); + tmpTexBuf16[i] = clut[GetClutIndex(index)]; + } + break; + } + } + else + { + u32 i, j; + UnswizzleFromMem(texaddr, bytesPerIndex, level); + switch (bytesPerIndex) + { + case 1: + for (i = 0, j = 0; i < length; i += 4, j++) + { + u32 n = tmpTexBuf32[j]; + u32 k; + for (k = 0; k < 4; k++) { + u8 index = (n >> (k * 4)) & 0xff; + tmpTexBuf16[i + k] = clut[GetClutIndex(index)]; + } + } + break; + + case 2: + for (i = 0, j = 0; i < length; i += 2, j++) + { + u32 n = tmpTexBuf32[j]; + tmpTexBuf16[i + 0] = clut[GetClutIndex(n & 0xffff)]; + tmpTexBuf16[i + 1] = clut[GetClutIndex(n >> 16)]; + } + break; + + case 4: + for (i = 0; i < length; i++) { + u32 n = tmpTexBuf32[i]; + tmpTexBuf16[i] = clut[GetClutIndex(n)]; + } + break; + } + } + buf = tmpTexBuf16; + } + break; + + case GE_CMODE_32BIT_ABGR8888: + { + u32 *clut = ReadClut32(); + if (!(gstate.texmode & 1)) + { + u32 i; + switch (bytesPerIndex) + { + case 1: + for (i = 0; i < length; i++) { + u8 index = Memory::Read_U8(texaddr + i); + tmpTexBuf32[i] = clut[GetClutIndex(index)]; + } + break; + + case 2: + for (i = 0; i < length; i++) { + u16 index = Memory::Read_U16(texaddr + i * 2); + tmpTexBuf32[i] = clut[GetClutIndex(index)]; + } + break; + + case 4: + for (i = 0; i < length; i++) { + u32 index = Memory::Read_U32(texaddr + i * 4); + tmpTexBuf32[i] = clut[GetClutIndex(index)]; + } + break; + } + } + else + { + u32 j; + s32 i; + UnswizzleFromMem(texaddr, bytesPerIndex, level); + switch (bytesPerIndex) + { + case 1: + for (i = length - 4, j = (length / 4) - 1; i >= 0; i -= 4, j--) + { + u32 n = tmpTexBuf32[j]; + u32 k; + for (k = 0; k < 4; k++) { + u32 index = (n >> (k * 8)) & 0xff; + tmpTexBuf32[i + k] = clut[GetClutIndex(index)]; + } + } + break; + + case 2: + for (i = length - 2, j = (length / 2) - 1; i >= 0; i -= 2, j--) + { + u32 n = tmpTexBuf32[j]; + tmpTexBuf32[i + 0] = clut[GetClutIndex(n & 0xffff)]; + tmpTexBuf32[i + 1] = clut[GetClutIndex(n >> 16)]; + } + break; + + case 4: + for (i = 0; (u32)i < length; i++) { + u32 n = tmpTexBuf32[i]; + tmpTexBuf32[i] = clut[GetClutIndex(n)]; + } + break; + } + } + buf = tmpTexBuf32; + } + break; + + default: + ERROR_LOG(G3D, "Unhandled clut texture mode %d!!!", (gstate.clutformat & 3)); + break; + } + + return buf; +} + +GLenum getClutDestFormat(GEPaletteFormat format) +{ + switch (format) + { + case GE_CMODE_16BIT_ABGR4444: + return GL_UNSIGNED_SHORT_4_4_4_4_REV; + case GE_CMODE_16BIT_ABGR5551: + return GL_UNSIGNED_SHORT_1_5_5_5_REV; + case GE_CMODE_16BIT_BGR5650: + return GL_UNSIGNED_SHORT_5_6_5_REV; + case GE_CMODE_32BIT_ABGR8888: + return GL_UNSIGNED_BYTE; + } + return 0; +} + +u32 texByteAlignMap[] = {2, 2, 2, 4}; + // This should not have to be done per texture! OpenGL is silly yo // TODO: Dirty-check this against the current texture. void UpdateSamplingParams() @@ -133,7 +373,7 @@ void UpdateSamplingParams() // Convert from PSP bit order to GLES bit order u16 convert565(u16 c) { - return (c >> 11) | (c & 0x07E0) | (c << 11); + return (c >> 11) | (c & 0x07E0) | (c << 11); } // Convert from PSP bit order to GLES bit order @@ -146,7 +386,6 @@ u16 convert5551(u16 c) { return ((c & 0x8000) >> 15) | (c << 1); } - struct DXT1Block { u8 lines[4]; @@ -192,7 +431,7 @@ void decodeDXT1Block(u32 *dst, const DXT1Block *src, int pitch) int green3 = ((green2 - green1) >> 1) - ((green2 - green1) >> 3); int red3 = ((red2 - red1) >> 1) - ((red2 - red1) >> 3); colors[2] = makecol(red1 + red3, green1 + green3, blue1 + blue3, 255); - colors[3] = makecol(red2 - red3, green2 - green3, blue2 - blue3, 255); + colors[3] = makecol(red2 - red3, green2 - green3, blue2 - blue3, 255); } else { @@ -203,7 +442,7 @@ void decodeDXT1Block(u32 *dst, const DXT1Block *src, int pitch) } for (int y = 0; y < 4; y++) - { + { int val = src->lines[y]; for (int x = 0; x < 4; x++) { @@ -216,14 +455,13 @@ void decodeDXT1Block(u32 *dst, const DXT1Block *src, int pitch) void PSPSetTexture() { - if (!tempArea) - tempArea = new u8[512*512*4*4]; // PSP maximum texture size - u32 texaddr = (gstate.texaddr[0] & 0xFFFFF0) | ((gstate.texbufwidth[0]<<8) & 0xFF000000); texaddr &= 0xFFFFFFF; if (!texaddr) return; + u8 level = 0; + DEBUG_LOG(G3D,"Texture at %08x",texaddr); u8 *texptr = Memory::GetPointer(texaddr); @@ -271,16 +509,19 @@ void PSPSetTexture() glBindTexture(GL_TEXTURE_2D, entry.texture); - int bufw = gstate.texbufwidth[0] & 0x3ff; + u32 bufw = gstate.texbufwidth[0] & 0x3ff; entry.dim = gstate.texsize[0] & 0xF0F; - int w = 1 << (gstate.texsize[0] & 0xf); - int h = 1 << ((gstate.texsize[0]>>8) & 0xf); + u32 w = 1 << (gstate.texsize[0] & 0xf); + u32 h = 1 << ((gstate.texsize[0]>>8) & 0xf); gstate.curTextureHeight=h; gstate.curTextureWidth=w; int format = gstate.texformat & 0xF; + GLenum dstFmt = 0; + u32 texByteAlign = 1; + void *finalBuf = NULL; DEBUG_LOG(G3D,"Texture Width %04x Height %04x Bufw %d Fmt %d", w, h, bufw, format); @@ -288,202 +529,180 @@ void PSPSetTexture() switch (format) { + case GE_TFMT_CLUT4: + dstFmt = getClutDestFormat((GEPaletteFormat)(gstate.clutformat & 3)); + switch ((gstate.clutformat & 3)) + { + case GE_CMODE_16BIT_BGR5650: + case GE_CMODE_16BIT_ABGR5551: + case GE_CMODE_16BIT_ABGR4444: + { + u16 *clut = ReadClut16(); + u32 clutSharingOff = 0;//gstate.mipmapShareClut ? 0 : level * 16; + texByteAlign = 2; + if (!(gstate.texmode & 1)) + { + u32 i; + u32 addr = texaddr; + for (i = 0; i < bufw * h; i += 2) + { + u32 index = Memory::Read_U32(addr); + tmpTexBuf16[i + 0] = clut[GetClutIndex((index >> 0) & 0xf) + clutSharingOff]; + tmpTexBuf16[i + 1] = clut[GetClutIndex((index >> 4) & 0xf) + clutSharingOff]; + addr++; + } + } + else + { + u32 i, j; + UnswizzleFromMem(texaddr, 0, level); + for (i = 0, j = 0; i < bufw * h; i += 8, j++) + { + u32 n = tmpTexBuf32[j]; + u32 k, index; + for (k = 0; k < 8; k++) { + index = (n >> (k * 4)) & 0xf; + tmpTexBuf16[i + k] = clut[GetClutIndex(index) + clutSharingOff]; + } + } + } + finalBuf = tmpTexBuf16; + } + break; + + case GE_CMODE_32BIT_ABGR8888: + { + u32 *clut = ReadClut32(); + u32 clutSharingOff = 0;//gstate.mipmapShareClut ? 0 : level * 16; + if (!(gstate.texmode & 1)) + { + u32 i; + u32 addr = texaddr; + for (i = 0; i < bufw * h; i += 2) + { + u32 index = Memory::Read_U32(addr); + tmpTexBuf32[i + 0] = clut[GetClutIndex((index >> 0) & 0xf) + clutSharingOff]; + tmpTexBuf32[i + 1] = clut[GetClutIndex((index >> 4) & 0xf) + clutSharingOff]; + addr++; + } + } + else + { + s32 i; + u32 j; + u32 pixels = bufw * h; + UnswizzleFromMem(texaddr, 0, level); + for (i = pixels - 8, j = (pixels / 8) - 1; i >= 0; i -= 8, j--) + { + u32 n = tmpTexBuf32[j]; + u32 k, index; + for (k = 0; k < 8; k++) { + index = (n >> (k * 4)) & 0xf; + tmpTexBuf32[i + k] = clut[GetClutIndex(index) + clutSharingOff]; + } + } + } + finalBuf = tmpTexBuf32; + } + break; + + default: + ERROR_LOG(G3D, "Unknown CLUT4 texture mode %d", (gstate.clutformat & 3)); + return; + } + break; + + case GE_TFMT_CLUT8: + finalBuf = readIndexedTex(level, texaddr, 1); + dstFmt = getClutDestFormat((GEPaletteFormat)(gstate.clutformat & 3)); + texByteAlign = texByteAlignMap[(gstate.clutformat & 3)]; + break; + + case GE_TFMT_CLUT16: + finalBuf = readIndexedTex(level, texaddr, 2); + dstFmt = getClutDestFormat((GEPaletteFormat)(gstate.clutformat & 3)); + texByteAlign = texByteAlignMap[(gstate.clutformat & 3)]; + break; + + case GE_TFMT_CLUT32: + finalBuf = readIndexedTex(level, texaddr, 4); + dstFmt = getClutDestFormat((GEPaletteFormat)(gstate.clutformat & 3)); + texByteAlign = texByteAlignMap[(gstate.clutformat & 3)]; + break; + case GE_TFMT_4444: case GE_TFMT_5551: case GE_TFMT_5650: + if (format == GE_TFMT_4444) + dstFmt = GL_UNSIGNED_SHORT_4_4_4_4_REV; + else if (format == GE_TFMT_5551) + dstFmt = GL_UNSIGNED_SHORT_1_5_5_5_REV; + else if (format == GE_TFMT_5650) + dstFmt = GL_UNSIGNED_SHORT_5_6_5_REV; + texByteAlign = 2; + + if (!(gstate.texmode & 1)) { - u16 *dst = (u16*)tempArea; - u16 *src = (u16*)texptr; - - int fmt = GL_UNSIGNED_SHORT_5_6_5; - int internal_format = GL_RGBA; - u16 (*convFunc)(u16); - switch (format) - { - case GE_TFMT_4444: fmt = GL_UNSIGNED_SHORT_4_4_4_4; convFunc = &convert4444; break; - case GE_TFMT_5551: fmt = GL_UNSIGNED_SHORT_5_5_5_1; convFunc = &convert5551; break; - case GE_TFMT_5650: fmt = GL_UNSIGNED_SHORT_5_6_5; convFunc = &convert565; internal_format = GL_RGB; break; - } - - if (gstate.texmode & 1) //Swizzled! - { - for (int y=0; y>=4; else idx&=0xF; - dst[(y*8+yy)*w + x*32 + xx] = PaletteLoad(idx); - } - } - } - } - } - else - { - for (int y=0; y>= 4; else idx &= 0xF; - dst[x] = PaletteLoad(idx); - if (x&1) - src++; - } - src -= w/2; - src += bufw/2; - dst += w; - } - } - int fmt = GL_UNSIGNED_BYTE; - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, fmt, (GLvoid*)tempArea); - break; - } - - case GE_TFMT_CLUT8: - { - u32 *dst = (u32*)tempArea; - u8 *src = (u8*)texptr; - if (gstate.texmode & 1) //Swizzled! - { - for (int y=0; y w ? bufw : w) * h; + u32 i; + for (i = 0; i < len; i++) + tmpTexBuf16[i] = Memory::Read_U16(texaddr + i * 2); + finalBuf = tmpTexBuf16; } + else + finalBuf = UnswizzleFromMem(texaddr, 2, level); + break; case GE_TFMT_8888: + dstFmt = GL_UNSIGNED_BYTE; + if (!(gstate.texmode & 1)) { - u32 *dst = (u32*)tempArea; - u32 *src = (u32*)texptr; - if (gstate.texmode & 1) //Swizzled! - { - for (int y=0; y