From 5ffb7f7d23139b504e4bd3b4a635dcb468b2368c Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 5 May 2013 20:58:52 -0700 Subject: [PATCH 1/7] Correct error handling in sceFontGet*Info(). And tone down the logging a bit. --- Core/Font/PGF.cpp | 3 ++- Core/HLE/sceFont.cpp | 52 ++++++++++++++++++++++++-------------------- 2 files changed, 30 insertions(+), 25 deletions(-) diff --git a/Core/Font/PGF.cpp b/Core/Font/PGF.cpp index 9af5e65f9d..87754ab1e8 100644 --- a/Core/Font/PGF.cpp +++ b/Core/Font/PGF.cpp @@ -241,11 +241,12 @@ int PGF::GetCharIndex(int charCode, const std::vector &charmapCompressed) { bool PGF::GetCharInfo(int charCode, PGFCharInfo *charInfo) { Glyph glyph; + memset(charInfo, 0, sizeof(*charInfo)); + if (!GetCharGlyph(charCode, FONT_PGF_CHARGLYPH, glyph)) { // Character not in font, return zeroed charInfo as on real PSP. return false; } - memset(charInfo, 0, sizeof(*charInfo)); charInfo->bitmapWidth = glyph.w; charInfo->bitmapHeight = glyph.h; diff --git a/Core/HLE/sceFont.cpp b/Core/HLE/sceFont.cpp index 80a74952bd..e6b4a33aed 100644 --- a/Core/HLE/sceFont.cpp +++ b/Core/HLE/sceFont.cpp @@ -763,21 +763,22 @@ int sceFontFindFont(u32 libHandlePtr, u32 fontStylePtr, u32 errorCodePtr) { } int sceFontGetFontInfo(u32 fontHandle, u32 fontInfoPtr) { - ERROR_LOG(HLE, "sceFontGetFontInfo(%x, %x)", fontHandle, fontInfoPtr); - - PGFFontInfo fi; - memset (&fi, 0, sizeof(fi)); - if (!Memory::IsValidAddress(fontInfoPtr)) - return 0; - + if (!Memory::IsValidAddress(fontInfoPtr)) { + ERROR_LOG(HLE, "sceFontGetFontInfo(%x, %x): bad fontInfo pointer", fontHandle, fontInfoPtr); + return ERROR_FONT_INVALID_PARAMETER; + } LoadedFont *font = GetLoadedFont(fontHandle, true); - if (!font) - return 0; + if (!font) { + ERROR_LOG(HLE, "sceFontGetFontInfo(%x, %x): bad font", fontHandle, fontInfoPtr); + return ERROR_FONT_INVALID_PARAMETER; + } + + INFO_LOG(HLE, "sceFontGetFontInfo(%x, %x)", fontHandle, fontInfoPtr); + auto fi = Memory::GetStruct(fontInfoPtr); PGF *pgf = font->GetFont()->GetPGF(); - pgf->GetFontInfo(&fi); - fi.fontStyle = font->GetFont()->GetFontStyle(); + pgf->GetFontInfo(fi); + fi->fontStyle = font->GetFont()->GetFontStyle(); - Memory::WriteStruct(fontInfoPtr, &fi); return 0; } @@ -789,19 +790,22 @@ int sceFontGetFontInfoByIndexNumber(u32 libHandle, u32 fontInfoPtr, u32 unknown, } int sceFontGetCharInfo(u32 fontHandle, u32 charCode, u32 charInfoPtr) { - INFO_LOG(HLE, "sceFontGetCharInfo(%08x, %i, %08x)", fontHandle, charCode, charInfoPtr); - if (!Memory::IsValidAddress(charInfoPtr)) - return -1; - - PGFCharInfo charInfo; - memset(&charInfo, 0, sizeof(charInfo)); - LoadedFont *font = GetLoadedFont(fontHandle, false); - if (font) { - font->GetFont()->GetPGF()->GetCharInfo(charCode, &charInfo); - } else { - ERROR_LOG(HLE, "sceFontGetCharInfo - invalid font"); + if (!Memory::IsValidAddress(charInfoPtr)) { + ERROR_LOG(HLE, "sceFontGetCharInfo(%08x, %i, %08x): bad charInfo pointer", fontHandle, charCode, charInfoPtr); + return ERROR_FONT_INVALID_PARAMETER; } - Memory::WriteStruct(charInfoPtr, &charInfo); + LoadedFont *font = GetLoadedFont(fontHandle, false); + if (!font) { + // The PSP crashes, but we assume it'd work like sceFontGetFontInfo(), and not touch charInfo. + ERROR_LOG(HLE, "sceFontGetCharInfo(%08x, %i, %08x): bad font", fontHandle, charCode, charInfoPtr); + return ERROR_FONT_INVALID_PARAMETER; + } + + DEBUG_LOG(HLE, "sceFontGetCharInfo(%08x, %i, %08x)", fontHandle, charCode, charInfoPtr); + auto charInfo = Memory::GetStruct(charInfoPtr); + PGF *pgf = font->GetFont()->GetPGF(); + pgf->GetCharInfo(charCode, charInfo); + return 0; } From 4ab165ebd7133d6aa5fed191157738d819216e8c Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 5 May 2013 21:02:13 -0700 Subject: [PATCH 2/7] Simplify the font code a tiny bit. --- Core/HLE/sceFont.cpp | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/Core/HLE/sceFont.cpp b/Core/HLE/sceFont.cpp index e6b4a33aed..224fef538a 100644 --- a/Core/HLE/sceFont.cpp +++ b/Core/HLE/sceFont.cpp @@ -231,6 +231,7 @@ public: : fontLibID_(fontLibID), font_(font), handle_(handle) {} Font *GetFont() { return font_; } + PGF *GetPGF() { return font_->GetPGF(); } FontLib *GetFontLib() { if (!IsOpen()) return NULL; return fontLibList[fontLibID_]; } u32 Handle() const { return handle_; } @@ -775,8 +776,7 @@ int sceFontGetFontInfo(u32 fontHandle, u32 fontInfoPtr) { INFO_LOG(HLE, "sceFontGetFontInfo(%x, %x)", fontHandle, fontInfoPtr); auto fi = Memory::GetStruct(fontInfoPtr); - PGF *pgf = font->GetFont()->GetPGF(); - pgf->GetFontInfo(fi); + font->GetPGF()->GetFontInfo(fi); fi->fontStyle = font->GetFont()->GetFontStyle(); return 0; @@ -803,8 +803,7 @@ int sceFontGetCharInfo(u32 fontHandle, u32 charCode, u32 charInfoPtr) { DEBUG_LOG(HLE, "sceFontGetCharInfo(%08x, %i, %08x)", fontHandle, charCode, charInfoPtr); auto charInfo = Memory::GetStruct(charInfoPtr); - PGF *pgf = font->GetFont()->GetPGF(); - pgf->GetCharInfo(charCode, charInfo); + font->GetPGF()->GetCharInfo(charCode, charInfo); return 0; } @@ -824,7 +823,7 @@ int sceFontGetCharImageRect(u32 fontHandle, u32 charCode, u32 charRectPtr) { PGFCharInfo charInfo; LoadedFont *font = GetLoadedFont(fontHandle, false); if (font) { - font->GetFont()->GetPGF()->GetCharInfo(charCode, &charInfo); + font->GetPGF()->GetCharInfo(charCode, &charInfo); Memory::Write_U16(charInfo.bitmapWidth, charRectPtr); // character bitmap width in pixels Memory::Write_U16(charInfo.bitmapHeight, charRectPtr + 2); // character bitmap height in pixels } else { @@ -855,7 +854,7 @@ int sceFontGetCharGlyphImage(u32 fontHandle, u32 charCode, u32 glyphImagePtr) { return 0; } int altCharCode = font->GetFontLib()->GetAltCharCode(); - font->GetFont()->GetPGF()->DrawCharacter(buffer, bytesPerLine, bufWidth, bufHeight, xPos64 >> 6, yPos64 >> 6, 0, 0, 8192, 8192, pixelFormat, charCode, altCharCode, FONT_PGF_CHARGLYPH); + font->GetPGF()->DrawCharacter(buffer, bytesPerLine, bufWidth, bufHeight, xPos64 >> 6, yPos64 >> 6, 0, 0, 8192, 8192, pixelFormat, charCode, altCharCode, FONT_PGF_CHARGLYPH); return 0; } @@ -876,7 +875,7 @@ int sceFontGetCharGlyphImage_Clip(u32 fontHandle, u32 charCode, u32 glyphImagePt return 0; } int altCharCode = font->GetFontLib()->GetAltCharCode(); - font->GetFont()->GetPGF()->DrawCharacter(buffer, bytesPerLine, bufWidth, bufHeight, xPos64 >> 6, yPos64 >> 6, clipXPos, clipYPos, clipXPos + clipWidth, clipYPos + clipHeight, pixelFormat, charCode, altCharCode, FONT_PGF_CHARGLYPH); + font->GetPGF()->DrawCharacter(buffer, bytesPerLine, bufWidth, bufHeight, xPos64 >> 6, yPos64 >> 6, clipXPos, clipYPos, clipXPos + clipWidth, clipYPos + clipHeight, pixelFormat, charCode, altCharCode, FONT_PGF_CHARGLYPH); return 0; } From 95823a776b2687a7fca9e08816ad897b17a8fbc8 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 5 May 2013 21:13:50 -0700 Subject: [PATCH 3/7] Don't pass quite so many args to DrawCharacter(). --- Core/Font/PGF.cpp | 9 ++++++--- Core/Font/PGF.h | 13 ++++++++++++- Core/HLE/sceFont.cpp | 31 ++++--------------------------- 3 files changed, 22 insertions(+), 31 deletions(-) diff --git a/Core/Font/PGF.cpp b/Core/Font/PGF.cpp index 87754ab1e8..90da78ad24 100644 --- a/Core/Font/PGF.cpp +++ b/Core/Font/PGF.cpp @@ -409,7 +409,7 @@ bool PGF::GetCharGlyph(int charCode, int glyphType, Glyph &glyph) { return true; } -void PGF::DrawCharacter(u32 base, int bpl, int bufWidth, int bufHeight, int x, int y, int clipX, int clipY, int clipWidth, int clipHeight, int pixelformat, int charCode, int altCharCode, int glyphType) { +void PGF::DrawCharacter(const GlyphImage *image, int clipX, int clipY, int clipWidth, int clipHeight, int charCode, int altCharCode, int glyphType) { Glyph glyph; if (!GetCharGlyph(charCode, glyphType, glyph)) { // No Glyph available for this charCode, try to use the alternate char. @@ -432,6 +432,9 @@ void PGF::DrawCharacter(u32 base, int bpl, int bufWidth, int bufHeight, int x, i int numberPixels = glyph.w * glyph.h; int pixelIndex = 0; + int x = image->xPos64 >> 6; + int y = image->yPos64 >> 6; + while (pixelIndex < numberPixels && bitPtr + 8 < fontDataSize * 8) { // This is some kind of nibble based RLE compression. int nibble = getBits(4, fontData, bitPtr); @@ -467,7 +470,7 @@ void PGF::DrawCharacter(u32 base, int bpl, int bufWidth, int bufHeight, int x, i if (pixelX >= clipX && pixelX < clipX + clipWidth && pixelY >= clipY && pixelY < clipY + clipHeight) { // 4-bit color value int pixelColor = value; - switch (pixelformat) { + switch (image->pixelFormat) { case PSP_FONT_PIXELFORMAT_8: // 8-bit color value pixelColor |= pixelColor << 4; @@ -486,7 +489,7 @@ void PGF::DrawCharacter(u32 base, int bpl, int bufWidth, int bufHeight, int x, i break; } - SetFontPixel(base, bpl, bufWidth, bufHeight, pixelX, pixelY, pixelColor, pixelformat); + SetFontPixel(image->bufferPtr, image->bytesPerLine, image->bufWidth, image->bufHeight, pixelX, pixelY, pixelColor, image->pixelFormat); } pixelIndex++; diff --git a/Core/Font/PGF.h b/Core/Font/PGF.h index 3a133394ed..5a0fa78be7 100644 --- a/Core/Font/PGF.h +++ b/Core/Font/PGF.h @@ -108,6 +108,17 @@ struct Glyph { u32 ptr; }; +struct GlyphImage { + FontPixelFormat pixelFormat; + s32 xPos64; + s32 yPos64; + u16 bufWidth; + u16 bufHeight; + u16 bytesPerLine; + u16 pad; + u32 bufferPtr; +}; + #pragma pack(push,1) struct PGFHeader { @@ -243,7 +254,7 @@ public: bool GetCharInfo(int charCode, PGFCharInfo *ci); void GetFontInfo(PGFFontInfo *fi); - void DrawCharacter(u32 base, int bpl, int bufWidth, int bufHeight, int x, int y, int clipX, int clipY, int clipWidth, int clipHeight, int pixelformat, int charCode, int altCharCode, int glyphType); + void DrawCharacter(const GlyphImage *image, int clipX, int clipY, int clipWidth, int clipHeight, int charCode, int altCharCode, int glyphType); void DoState(PointerWrap &p); diff --git a/Core/HLE/sceFont.cpp b/Core/HLE/sceFont.cpp index 224fef538a..efe2986c7b 100644 --- a/Core/HLE/sceFont.cpp +++ b/Core/HLE/sceFont.cpp @@ -57,17 +57,6 @@ struct FontNewLibParams { u32 ioFinishFuncAddr; }; -struct GlyphImage { - FontPixelFormat pixelFormat; - s32 xPos64; - s32 yPos64; - u16 bufWidth; - u16 bufHeight; - u16 bytesPerLine; - u16 pad; - u32 bufferPtr; -}; - struct FontRegistryEntry { int hSize; int vSize; @@ -840,13 +829,7 @@ int sceFontGetShadowImageRect(u32 fontHandle, u32 charCode, u32 charRectPtr) { int sceFontGetCharGlyphImage(u32 fontHandle, u32 charCode, u32 glyphImagePtr) { INFO_LOG(HLE, "sceFontGetCharGlyphImage(%x, %x, %x)", fontHandle, charCode, glyphImagePtr); - int pixelFormat = Memory::Read_U32(glyphImagePtr); - int xPos64 = Memory::Read_U32(glyphImagePtr+4); - int yPos64 = Memory::Read_U32(glyphImagePtr+8); - int bufWidth = Memory::Read_U16(glyphImagePtr+12); - int bufHeight = Memory::Read_U16(glyphImagePtr+14); - int bytesPerLine = Memory::Read_U16(glyphImagePtr+16); - int buffer = Memory::Read_U32(glyphImagePtr+20); + auto glyph = Memory::GetStruct(glyphImagePtr); LoadedFont *font = GetLoadedFont(fontHandle, false); if (!font) { @@ -854,20 +837,14 @@ int sceFontGetCharGlyphImage(u32 fontHandle, u32 charCode, u32 glyphImagePtr) { return 0; } int altCharCode = font->GetFontLib()->GetAltCharCode(); - font->GetPGF()->DrawCharacter(buffer, bytesPerLine, bufWidth, bufHeight, xPos64 >> 6, yPos64 >> 6, 0, 0, 8192, 8192, pixelFormat, charCode, altCharCode, FONT_PGF_CHARGLYPH); + font->GetPGF()->DrawCharacter(glyph, 0, 0, 8192, 8192, charCode, altCharCode, FONT_PGF_CHARGLYPH); return 0; } int sceFontGetCharGlyphImage_Clip(u32 fontHandle, u32 charCode, u32 glyphImagePtr, int clipXPos, int clipYPos, int clipWidth, int clipHeight) { INFO_LOG(HLE, "sceFontGetCharGlyphImage_Clip(%08x, %i, %08x, %i, %i, %i, %i)", fontHandle, charCode, glyphImagePtr, clipXPos, clipYPos, clipWidth, clipHeight); - int pixelFormat = Memory::Read_U32(glyphImagePtr); - int xPos64 = Memory::Read_U32(glyphImagePtr+4); - int yPos64 = Memory::Read_U32(glyphImagePtr+8); - int bufWidth = Memory::Read_U16(glyphImagePtr+12); - int bufHeight = Memory::Read_U16(glyphImagePtr+14); - int bytesPerLine = Memory::Read_U16(glyphImagePtr+16); - int buffer = Memory::Read_U32(glyphImagePtr+20); + auto glyph = Memory::GetStruct(glyphImagePtr); LoadedFont *font = GetLoadedFont(fontHandle, false); if (!font) { @@ -875,7 +852,7 @@ int sceFontGetCharGlyphImage_Clip(u32 fontHandle, u32 charCode, u32 glyphImagePt return 0; } int altCharCode = font->GetFontLib()->GetAltCharCode(); - font->GetPGF()->DrawCharacter(buffer, bytesPerLine, bufWidth, bufHeight, xPos64 >> 6, yPos64 >> 6, clipXPos, clipYPos, clipXPos + clipWidth, clipYPos + clipHeight, pixelFormat, charCode, altCharCode, FONT_PGF_CHARGLYPH); + font->GetPGF()->DrawCharacter(glyph, clipXPos, clipYPos, clipXPos + clipWidth, clipYPos + clipHeight, charCode, altCharCode, FONT_PGF_CHARGLYPH); return 0; } From f9e4739c6444aeed3291e57c79dce32798c21eb3 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 5 May 2013 21:21:58 -0700 Subject: [PATCH 4/7] Clean up error codes in sceFontGetCharGlyphImage(). --- Core/HLE/sceFont.cpp | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/Core/HLE/sceFont.cpp b/Core/HLE/sceFont.cpp index efe2986c7b..4201f8bce9 100644 --- a/Core/HLE/sceFont.cpp +++ b/Core/HLE/sceFont.cpp @@ -827,30 +827,36 @@ int sceFontGetShadowImageRect(u32 fontHandle, u32 charCode, u32 charRectPtr) { } int sceFontGetCharGlyphImage(u32 fontHandle, u32 charCode, u32 glyphImagePtr) { - INFO_LOG(HLE, "sceFontGetCharGlyphImage(%x, %x, %x)", fontHandle, charCode, glyphImagePtr); - - auto glyph = Memory::GetStruct(glyphImagePtr); - + if (!Memory::IsValidAddress(glyphImagePtr)) { + ERROR_LOG(HLE, "sceFontGetCharGlyphImage(%x, %x, %x): bad glyphImage pointer", fontHandle, charCode, glyphImagePtr); + return ERROR_FONT_INVALID_PARAMETER; + } LoadedFont *font = GetLoadedFont(fontHandle, false); if (!font) { - ERROR_LOG(HLE, "%08x is not a valid font handle!", fontHandle); - return 0; + ERROR_LOG(HLE, "sceFontGetCharGlyphImage(%x, %x, %x): bad font", fontHandle, charCode, glyphImagePtr); + return ERROR_FONT_INVALID_PARAMETER; } + + INFO_LOG(HLE, "sceFontGetCharGlyphImage(%x, %x, %x)", fontHandle, charCode, glyphImagePtr); + auto glyph = Memory::GetStruct(glyphImagePtr); int altCharCode = font->GetFontLib()->GetAltCharCode(); font->GetPGF()->DrawCharacter(glyph, 0, 0, 8192, 8192, charCode, altCharCode, FONT_PGF_CHARGLYPH); return 0; } int sceFontGetCharGlyphImage_Clip(u32 fontHandle, u32 charCode, u32 glyphImagePtr, int clipXPos, int clipYPos, int clipWidth, int clipHeight) { - INFO_LOG(HLE, "sceFontGetCharGlyphImage_Clip(%08x, %i, %08x, %i, %i, %i, %i)", fontHandle, charCode, glyphImagePtr, clipXPos, clipYPos, clipWidth, clipHeight); - - auto glyph = Memory::GetStruct(glyphImagePtr); - + if (!Memory::IsValidAddress(glyphImagePtr)) { + ERROR_LOG(HLE, "sceFontGetCharGlyphImage_Clip(%08x, %i, %08x, %i, %i, %i, %i): bad glyphImage pointer", fontHandle, charCode, glyphImagePtr, clipXPos, clipYPos, clipWidth, clipHeight); + return ERROR_FONT_INVALID_PARAMETER; + } LoadedFont *font = GetLoadedFont(fontHandle, false); if (!font) { - ERROR_LOG(HLE, "%08x is not a valid font handle!", fontHandle); - return 0; + ERROR_LOG(HLE, "sceFontGetCharGlyphImage_Clip(%08x, %i, %08x, %i, %i, %i, %i): bad font", fontHandle, charCode, glyphImagePtr, clipXPos, clipYPos, clipWidth, clipHeight); + return ERROR_FONT_INVALID_PARAMETER; } + + INFO_LOG(HLE, "sceFontGetCharGlyphImage_Clip(%08x, %i, %08x, %i, %i, %i, %i)", fontHandle, charCode, glyphImagePtr, clipXPos, clipYPos, clipWidth, clipHeight); + auto glyph = Memory::GetStruct(glyphImagePtr); int altCharCode = font->GetFontLib()->GetAltCharCode(); font->GetPGF()->DrawCharacter(glyph, clipXPos, clipYPos, clipXPos + clipWidth, clipYPos + clipHeight, charCode, altCharCode, FONT_PGF_CHARGLYPH); return 0; From 7741376a3822370cd9ea8fab3cffcffe6266c034 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 5 May 2013 21:29:41 -0700 Subject: [PATCH 5/7] Check a potential crash in sceFontOpen(). --- Core/HLE/sceFont.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Core/HLE/sceFont.cpp b/Core/HLE/sceFont.cpp index 4201f8bce9..f0fdea1eff 100644 --- a/Core/HLE/sceFont.cpp +++ b/Core/HLE/sceFont.cpp @@ -605,13 +605,18 @@ int sceFontDoneLib(u32 fontLibHandle) { // Open internal font into a FontLib u32 sceFontOpen(u32 libHandle, u32 index, u32 mode, u32 errorCodePtr) { - INFO_LOG(HLE, "sceFontOpen(%x, %x, %x, %x)", libHandle, index, mode, errorCodePtr); if (!Memory::IsValidAddress(errorCodePtr)) { - Memory::Write_U32(ERROR_FONT_INVALID_PARAMETER, errorCodePtr); + // Would crash on the PSP. + ERROR_LOG(HLE, "sceFontOpen(%x, %x, %x, %x): invalid pointer", libHandle, index, mode, errorCodePtr); return 0; } + INFO_LOG(HLE, "sceFontOpen(%x, %x, %x, %x)", libHandle, index, mode, errorCodePtr); FontLib *fontLib = GetFontLib(libHandle); + if (fontLib == NULL) { + Memory::Write_U32(ERROR_FONT_INVALID_LIBID, errorCodePtr); + return 0; + } if (index >= internalFonts.size()) { Memory::Write_U32(ERROR_FONT_INVALID_PARAMETER, errorCodePtr); return 0; From 995ab510e9c3148a63fce649273f58ba1696cb26 Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 5 May 2013 22:34:35 -0700 Subject: [PATCH 6/7] Optimize alpha-only CLUTs, common for fonts. This optimizes the case where fonts are drawn using animation (common in RPGs, for example), and textures have to be decoded over and over. Could probably be faster with SIMD. --- GPU/GLES/TextureCache.cpp | 60 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 58 insertions(+), 2 deletions(-) diff --git a/GPU/GLES/TextureCache.cpp b/GPU/GLES/TextureCache.cpp index 30b74de0be..44b5a86aae 100644 --- a/GPU/GLES/TextureCache.cpp +++ b/GPU/GLES/TextureCache.cpp @@ -318,12 +318,39 @@ inline void DeIndexTexture4(ClutT *dest, const u8 *indexed, int length, const Cl } } +template +inline void DeIndexTexture4Optimal(ClutT *dest, const u8 *indexed, int length, ClutT color) { + for (int i = 0; i < length; i += 2) { + u8 index = *indexed++; + dest[i + 0] = color | ((index >> 0) & 0xf); + dest[i + 1] = color | ((index >> 4) & 0xf); + } +} + +template <> +inline void DeIndexTexture4Optimal(u16 *dest, const u8 *indexed, int length, u16 color) { + const u16 *indexed16 = (const u16 *)indexed; + const u32 color32 = (color << 16) | color; + u32 *dest32 = (u32 *)dest; + for (int i = 0; i < length / 2; i += 2) { + u16 index = *indexed16++; + dest32[i + 0] = color32 | ((index & 0x00f0) << 12) | ((index & 0x000f) >> 0); + dest32[i + 1] = color32 | ((index & 0xf000) << 4) | ((index & 0x0f00) >> 8); + } +} + template inline void DeIndexTexture4(ClutT *dest, const u32 texaddr, int length, const ClutT *clut) { const u8 *indexed = (const u8 *) Memory::GetPointer(texaddr); DeIndexTexture4(dest, indexed, length, clut); } +template +inline void DeIndexTexture4Optimal(ClutT *dest, const u32 texaddr, int length, ClutT color) { + const u8 *indexed = (const u8 *) Memory::GetPointer(texaddr); + DeIndexTexture4Optimal(dest, indexed, length, color); +} + void *TextureCache::readIndexedTex(int level, u32 texaddr, int bytesPerIndex, GLuint dstFmt) { // Special rules for kernel textures (PPGe): int mask = 0x3FF; @@ -1036,12 +1063,41 @@ void *TextureCache::DecodeTextureLevel(u8 format, u8 clutformat, int level, u32 const u16 *clut = clutBuf16; u32 clutSharingOffset = 0; //(gstate.mipmapShareClut & 1) ? 0 : level * 16; texByteAlign = 2; + + // Special optimization: fonts typically draw clut4 with just alpha values in a single color. + bool linearClut = false; + u16 linearColor = 0; + if (gstate.clutformat == (0xC500FF00 | GE_CMODE_16BIT_ABGR4444)) { + // TODO: Do this check once per CLUT load? + linearClut = true; + linearColor = clut[clutSharingOffset + 15] & 0xFFF0; + for (int i = 0; i < 16; ++i) { + if ((clut[clutSharingOffset + i] & 0xf) != i) { + linearClut = false; + break; + } + // Alpha 0 doesn't matter. + if (i != 0 && (clut[clutSharingOffset + i] & 0xFFF0) != linearColor) { + linearClut = false; + break; + } + } + } + if (!(gstate.texmode & 1)) { - DeIndexTexture4(tmpTexBuf16.data(), texaddr, bufw * h, clut + clutSharingOffset); + if (linearClut) { + DeIndexTexture4Optimal(tmpTexBuf16.data(), texaddr, bufw * h, linearColor); + } else { + DeIndexTexture4(tmpTexBuf16.data(), texaddr, bufw * h, clut + clutSharingOffset); + } } else { tmpTexBuf32.resize(std::max(bufw, w) * h); UnswizzleFromMem(texaddr, bufw, 0, level); - DeIndexTexture4(tmpTexBuf16.data(), (u8 *)tmpTexBuf32.data(), bufw * h, clut + clutSharingOffset); + if (linearClut) { + DeIndexTexture4Optimal(tmpTexBuf16.data(), (u8 *)tmpTexBuf32.data(), bufw * h, linearColor); + } else { + DeIndexTexture4(tmpTexBuf16.data(), (u8 *)tmpTexBuf32.data(), bufw * h, clut + clutSharingOffset); + } } finalBuf = tmpTexBuf16.data(); } From 3c0ed69fd27cd69af1e2461374f51d692a4dc75a Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 5 May 2013 22:52:01 -0700 Subject: [PATCH 7/7] Improve GPU invalidation interface, check fonts. When a font is rendered, tell the GPU about it, but don't rehash. This not only improves perf (less hashing) but makes font changes more immediate (no deferred hashing.) But only if they use sceFont. --- Core/Font/PGF.cpp | 5 +++++ Core/HLE/sceKernel.cpp | 10 +++++----- GPU/GLES/DisplayListInterpreter.cpp | 15 ++++----------- GPU/GLES/DisplayListInterpreter.h | 3 +-- GPU/GLES/TextureCache.cpp | 12 ++++++------ GPU/GLES/TextureCache.h | 5 +++-- GPU/GPUInterface.h | 12 ++++++++++-- GPU/Null/NullGpu.cpp | 7 +------ GPU/Null/NullGpu.h | 3 +-- 9 files changed, 36 insertions(+), 36 deletions(-) diff --git a/Core/Font/PGF.cpp b/Core/Font/PGF.cpp index 90da78ad24..5503755470 100644 --- a/Core/Font/PGF.cpp +++ b/Core/Font/PGF.cpp @@ -25,6 +25,9 @@ #include "Core/Font/PGF.h" #include "Core/HLE/HLE.h" +#include "GPU/GPUInterface.h" +#include "GPU/GPUState.h" + // These fonts, created by ttf2pgf, don't have complete glyph info and need to be identified. static bool isJPCSPFont(const char *fontName) { return !strcmp(fontName, "Liberation") || !strcmp(fontName, "Sazanami") || !strcmp(fontName, "UnDotum"); @@ -495,6 +498,8 @@ void PGF::DrawCharacter(const GlyphImage *image, int clipX, int clipY, int clipW pixelIndex++; } } + + gpu->InvalidateCache(image->bufferPtr, image->bytesPerLine * image->bufHeight, GPU_INVALIDATE_SAFE); } void PGF::SetFontPixel(u32 base, int bpl, int bufWidth, int bufHeight, int x, int y, int pixelColor, int pixelformat) { diff --git a/Core/HLE/sceKernel.cpp b/Core/HLE/sceKernel.cpp index 176b136108..b29f964133 100644 --- a/Core/HLE/sceKernel.cpp +++ b/Core/HLE/sceKernel.cpp @@ -307,7 +307,7 @@ int sceKernelDcacheInvalidateRange(u32 addr, int size) return SCE_KERNEL_ERROR_CACHE_ALIGNMENT; if (addr != 0) - gpu->InvalidateCache(addr, size); + gpu->InvalidateCache(addr, size, GPU_INVALIDATE_HINT); } return 0; } @@ -325,7 +325,7 @@ int sceKernelDcacheWritebackAll() #endif // Some games seem to use this a lot, it doesn't make sense // to zap the whole texture cache. - gpu->InvalidateCacheHint(0, -1); + gpu->InvalidateCache(0, -1, GPU_INVALIDATE_ALL); return 0; } @@ -338,7 +338,7 @@ int sceKernelDcacheWritebackRange(u32 addr, int size) return SCE_KERNEL_ERROR_INVALID_SIZE; if (size > 0 && addr != 0) { - gpu->InvalidateCache(addr, size); + gpu->InvalidateCache(addr, size, GPU_INVALIDATE_HINT); } return 0; } @@ -351,7 +351,7 @@ int sceKernelDcacheWritebackInvalidateRange(u32 addr, int size) return SCE_KERNEL_ERROR_INVALID_SIZE; if (size > 0 && addr != 0) { - gpu->InvalidateCache(addr, size); + gpu->InvalidateCache(addr, size, GPU_INVALIDATE_HINT); } return 0; } @@ -360,7 +360,7 @@ int sceKernelDcacheWritebackInvalidateAll() #ifdef LOG_CACHE NOTICE_LOG(HLE,"sceKernelDcacheInvalidateAll()"); #endif - gpu->InvalidateCacheHint(0, -1); + gpu->InvalidateCache(0, -1, GPU_INVALIDATE_ALL); return 0; } diff --git a/GPU/GLES/DisplayListInterpreter.cpp b/GPU/GLES/DisplayListInterpreter.cpp index e1ebeddb59..415f657a10 100644 --- a/GPU/GLES/DisplayListInterpreter.cpp +++ b/GPU/GLES/DisplayListInterpreter.cpp @@ -980,21 +980,14 @@ void GLES_GPU::DoBlockTransfer() { // TODO: Notify all overlapping FBOs that they need to reload. - textureCache_.Invalidate(dstBasePtr + dstY * dstStride + dstX, height * dstStride + width * bpp, true); + textureCache_.Invalidate(dstBasePtr + dstY * dstStride + dstX, height * dstStride + width * bpp, GPU_INVALIDATE_HINT); } -void GLES_GPU::InvalidateCache(u32 addr, int size) { +void GLES_GPU::InvalidateCache(u32 addr, int size, GPUInvalidationType type) { if (size > 0) - textureCache_.Invalidate(addr, size, true); + textureCache_.Invalidate(addr, size, type); else - textureCache_.InvalidateAll(true); -} - -void GLES_GPU::InvalidateCacheHint(u32 addr, int size) { - if (size > 0) - textureCache_.Invalidate(addr, size, false); - else - textureCache_.InvalidateAll(false); + textureCache_.InvalidateAll(type); } void GLES_GPU::ClearCacheNextFrame() { diff --git a/GPU/GLES/DisplayListInterpreter.h b/GPU/GLES/DisplayListInterpreter.h index ae19f739be..d3e2cd5c34 100644 --- a/GPU/GLES/DisplayListInterpreter.h +++ b/GPU/GLES/DisplayListInterpreter.h @@ -44,8 +44,7 @@ public: virtual void CopyDisplayToOutput(); virtual void BeginFrame(); virtual void UpdateStats(); - virtual void InvalidateCache(u32 addr, int size); - virtual void InvalidateCacheHint(u32 addr, int size); + virtual void InvalidateCache(u32 addr, int size, GPUInvalidationType type); virtual void ClearCacheNextFrame(); virtual void DeviceLost(); // Only happens on Android. Drop all textures and shaders. diff --git a/GPU/GLES/TextureCache.cpp b/GPU/GLES/TextureCache.cpp index 44b5a86aae..d90f61f7c6 100644 --- a/GPU/GLES/TextureCache.cpp +++ b/GPU/GLES/TextureCache.cpp @@ -108,7 +108,7 @@ void TextureCache::Decimate() { } } -void TextureCache::Invalidate(u32 addr, int size, bool force) { +void TextureCache::Invalidate(u32 addr, int size, GPUInvalidationType type) { addr &= 0xFFFFFFF; u32 addr_end = addr + size; @@ -125,10 +125,10 @@ void TextureCache::Invalidate(u32 addr, int size, bool force) { if (iter->second.status == TexCacheEntry::STATUS_RELIABLE) { iter->second.status = TexCacheEntry::STATUS_HASHING; } - if (force) { + if (type != GPU_INVALIDATE_ALL) { gpuStats.numTextureInvalidations++; - // Start it over from 0. - iter->second.numFrames = 0; + // Start it over from 0 (unless it's safe.) + iter->second.numFrames = type == GPU_INVALIDATE_SAFE ? 256 : 0; iter->second.framesUntilNextFullHash = 0; } else { iter->second.invalidHint++; @@ -137,8 +137,8 @@ void TextureCache::Invalidate(u32 addr, int size, bool force) { } } -void TextureCache::InvalidateAll(bool force) { - Invalidate(0, 0xFFFFFFFF, force); +void TextureCache::InvalidateAll(GPUInvalidationType type) { + Invalidate(0, 0xFFFFFFFF, type); } void TextureCache::ClearNextFrame() { diff --git a/GPU/GLES/TextureCache.h b/GPU/GLES/TextureCache.h index 6a4fc9bcfe..1553c15cde 100644 --- a/GPU/GLES/TextureCache.h +++ b/GPU/GLES/TextureCache.h @@ -19,6 +19,7 @@ #include "../Globals.h" #include "gfx_es2/fbo.h" +#include "GPU/GPUInterface.h" #include "GPU/GPUState.h" #include "TextureScaler.h" @@ -34,8 +35,8 @@ public: void Clear(bool delete_them); void StartFrame(); - void Invalidate(u32 addr, int size, bool force); - void InvalidateAll(bool force); + void Invalidate(u32 addr, int size, GPUInvalidationType type); + void InvalidateAll(GPUInvalidationType type); void ClearNextFrame(); // FramebufferManager keeps TextureCache updated about what regions of memory diff --git a/GPU/GPUInterface.h b/GPU/GPUInterface.h index 21c067d9af..93d3ceb3c7 100644 --- a/GPU/GPUInterface.h +++ b/GPU/GPUInterface.h @@ -132,6 +132,15 @@ struct DisplayList u64 waitTicks; }; +enum GPUInvalidationType { + // Affects all memory. Not considered highly. + GPU_INVALIDATE_ALL, + // Indicates some memory may have changed. + GPU_INVALIDATE_HINT, + // Reliable invalidation (where any hashing, etc. is unneeded, it'll always invalidate.) + GPU_INVALIDATE_SAFE, +}; + class GPUInterface { public: @@ -171,8 +180,7 @@ public: // Invalidate any cached content sourced from the specified range. // If size = -1, invalidate everything. - virtual void InvalidateCache(u32 addr, int size) = 0; - virtual void InvalidateCacheHint(u32 addr, int size) = 0; + virtual void InvalidateCache(u32 addr, int size, GPUInvalidationType type) = 0; // Will cause the texture cache to be cleared at the start of the next frame. virtual void ClearCacheNextFrame() = 0; diff --git a/GPU/Null/NullGpu.cpp b/GPU/Null/NullGpu.cpp index c1e230c6a8..5a9c041805 100644 --- a/GPU/Null/NullGpu.cpp +++ b/GPU/Null/NullGpu.cpp @@ -674,12 +674,7 @@ void NullGPU::UpdateStats() gpuStats.numTextures = 0; } -void NullGPU::InvalidateCache(u32 addr, int size) -{ - // Nothing to invalidate. -} - -void NullGPU::InvalidateCacheHint(u32 addr, int size) +void NullGPU::InvalidateCache(u32 addr, int size, GPUInvalidationType type) { // Nothing to invalidate. } diff --git a/GPU/Null/NullGpu.h b/GPU/Null/NullGpu.h index 4dfb856d20..3715511cd4 100644 --- a/GPU/Null/NullGpu.h +++ b/GPU/Null/NullGpu.h @@ -34,8 +34,7 @@ public: virtual void SetDisplayFramebuffer(u32 framebuf, u32 stride, int format) {} virtual void CopyDisplayToOutput() {} virtual void UpdateStats(); - virtual void InvalidateCache(u32 addr, int size); - virtual void InvalidateCacheHint(u32 addr, int size); + virtual void InvalidateCache(u32 addr, int size, GPUInvalidationType type); virtual void ClearCacheNextFrame() {}; virtual void Flush() {}