1 /* 2 Copyright (C) 2003 Rice1964 3 4 This program is free software; you can redistribute it and/or 5 modify it under the terms of the GNU General Public License 6 as published by the Free Software Foundation; either version 2 7 of the License, or (at your option) any later version. 8 9 This program is distributed in the hope that it will be useful, 10 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 GNU General Public License for more details. 13 14 You should have received a copy of the GNU General Public License 15 along with this program; if not, write to the Free Software 16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 17 18 */ 19 20 #ifndef __TEXTUREHANDLER_H__ 21 #define __TEXTUREHANDLER_H__ 22 23 #include <stdlib.h> 24 #include <string.h> 25 #ifndef _MSC_VER 26 #include <strings.h> 27 #endif 28 29 #include "../../Graphics/RDP/gDP_state.h" 30 #include "typedefs.h" 31 #include "Texture.h" 32 #define absi(x) ((x)>=0?(x):(-x)) 33 #define S_FLAG 0 34 #define T_FLAG 1 35 36 class TxtrInfo 37 { 38 public: 39 uint32_t WidthToCreate; 40 uint32_t HeightToCreate; 41 42 uint32_t Address; 43 void *pPhysicalAddress; 44 45 uint32_t Format; 46 uint32_t Size; 47 48 int LeftToLoad; 49 int TopToLoad; 50 uint32_t WidthToLoad; 51 uint32_t HeightToLoad; 52 uint32_t Pitch; 53 54 uint8_t *PalAddress; 55 uint32_t TLutFmt; 56 uint32_t Palette; 57 58 bool bSwapped; 59 60 uint32_t maskS; 61 uint32_t maskT; 62 63 bool clampS; 64 bool clampT; 65 bool mirrorS; 66 bool mirrorT; 67 68 int tileNo; 69 70 inline TxtrInfo& operator = (const TxtrInfo& src) 71 { 72 memcpy(this, &src, sizeof( TxtrInfo )); 73 return *this; 74 } 75 76 inline TxtrInfo& operator = (const gDPTile& tile) 77 { 78 Format = tile.format; 79 Size = tile.size; 80 Palette = tile.palette; 81 82 maskS = tile.masks; 83 maskT = tile.maskt; 84 mirrorS = tile.mirrors; 85 mirrorT = tile.mirrort; 86 clampS = tile.clamps; 87 clampT = tile.clampt; 88 89 return *this; 90 } 91 92 inline bool operator == ( const TxtrInfo& sec) 93 { 94 return ( 95 Address == sec.Address && 96 WidthToLoad == sec.WidthToLoad && 97 HeightToLoad == sec.HeightToLoad && 98 WidthToCreate == sec.WidthToCreate && 99 HeightToCreate == sec.HeightToCreate && 100 maskS == sec.maskS && 101 maskT == sec.maskT && 102 TLutFmt == sec.TLutFmt && 103 PalAddress == sec.PalAddress && 104 Palette == sec.Palette && 105 LeftToLoad == sec.LeftToLoad && 106 TopToLoad == sec.TopToLoad && 107 Format == sec.Format && 108 Size == sec.Size && 109 Pitch == sec.Pitch && 110 bSwapped == sec.bSwapped && 111 mirrorS == sec.mirrorS && 112 mirrorT == sec.mirrorT && 113 clampS == sec.clampS && 114 clampT == sec.clampT 115 ); 116 } 117 isEqual(const TxtrInfo & sec)118 inline bool isEqual(const TxtrInfo& sec) 119 { 120 return (*this == sec); 121 } 122 123 } ; 124 125 126 127 typedef struct TxtrCacheEntry 128 { TxtrCacheEntryTxtrCacheEntry129 TxtrCacheEntry(): 130 pTexture(NULL),pEnhancedTexture(NULL),txtrBufIdx(0) {} 131 ~TxtrCacheEntryTxtrCacheEntry132 ~TxtrCacheEntry() 133 { 134 if (pTexture) 135 free(pTexture); 136 if (pEnhancedTexture) 137 free(pEnhancedTexture); 138 139 pTexture = NULL; 140 pEnhancedTexture = NULL; 141 } 142 143 struct TxtrCacheEntry *pNext; // Must be first element! 144 145 struct TxtrCacheEntry *pNextYoungest; 146 struct TxtrCacheEntry *pLastYoungest; 147 148 TxtrInfo ti; 149 uint32_t dwCRC; 150 uint32_t dwPalCRC; 151 int maxCI; 152 153 uint32_t dwUses; // Total times used (for stats) 154 uint32_t dwTimeLastUsed; // timeGetTime of time of last usage 155 uint32_t FrameLastUsed; // Frame # that this was last used 156 uint32_t FrameLastUpdated; 157 158 CTexture *pTexture; 159 CTexture *pEnhancedTexture; 160 161 uint32_t dwEnhancementFlag; 162 int txtrBufIdx; 163 bool bExternalTxtrChecked; 164 165 TxtrCacheEntry *lastEntry; 166 } TxtrCacheEntry; 167 168 169 //***************************************************************************** 170 // Texture cache implementation 171 //***************************************************************************** 172 class CTextureManager 173 { 174 protected: 175 TxtrCacheEntry * CreateNewCacheEntry(uint32_t dwAddr, uint32_t dwWidth, uint32_t dwHeight); 176 void AddTexture(TxtrCacheEntry *pEntry); 177 void RemoveTexture(TxtrCacheEntry * pEntry); 178 void RecycleTexture(TxtrCacheEntry *pEntry); 179 TxtrCacheEntry * ReviveTexture( uint32_t width, uint32_t height ); 180 TxtrCacheEntry * GetTxtrCacheEntry(TxtrInfo * pti); 181 182 void ConvertTexture(TxtrCacheEntry * pEntry, bool fromTMEM); 183 void ConvertTexture_16(TxtrCacheEntry * pEntry, bool fromTMEM); 184 185 void ClampS32(uint32_t *array, uint32_t width, uint32_t towidth, uint32_t arrayWidth, uint32_t rows); 186 void ClampS16(uint16_t *array, uint32_t width, uint32_t towidth, uint32_t arrayWidth, uint32_t rows); 187 void ClampT32(uint32_t *array, uint32_t height, uint32_t toheight, uint32_t arrayWidth, uint32_t cols); 188 void ClampT16(uint16_t *array, uint32_t height, uint32_t toheight, uint32_t arrayWidth, uint32_t cols); 189 190 void MirrorS32(uint32_t *array, uint32_t width, uint32_t mask, uint32_t towidth, uint32_t arrayWidth, uint32_t rows); 191 void MirrorS16(uint16_t *array, uint32_t width, uint32_t mask, uint32_t towidth, uint32_t arrayWidth, uint32_t rows); 192 void MirrorT32(uint32_t *array, uint32_t height, uint32_t mask, uint32_t toheight, uint32_t arrayWidth, uint32_t cols); 193 void MirrorT16(uint16_t *array, uint32_t height, uint32_t mask, uint32_t toheight, uint32_t arrayWidth, uint32_t cols); 194 195 void WrapS32(uint32_t *array, uint32_t width, uint32_t mask, uint32_t towidth, uint32_t arrayWidth, uint32_t rows); 196 void WrapS16(uint16_t *array, uint32_t width, uint32_t mask, uint32_t towidth, uint32_t arrayWidth, uint32_t rows); 197 void WrapT32(uint32_t *array, uint32_t height, uint32_t mask, uint32_t toheight, uint32_t arrayWidth, uint32_t cols); 198 void WrapT16(uint16_t *array, uint32_t height, uint32_t mask, uint32_t toheight, uint32_t arrayWidth, uint32_t cols); 199 200 void ExpandTextureS(TxtrCacheEntry * pEntry); 201 void ExpandTextureT(TxtrCacheEntry * pEntry); 202 void ExpandTexture(TxtrCacheEntry * pEntry, uint32_t sizeOfLoad, uint32_t sizeToCreate, uint32_t sizeCreated, 203 int arrayWidth, int flag, int mask, int mirror, int clamp, uint32_t otherSize); 204 205 uint32_t Hash(uint32_t dwValue); 206 bool TCacheEntryIsLoaded(TxtrCacheEntry *pEntry); 207 208 void updateColorTexture(CTexture *ptexture, uint32_t color); 209 210 public: 211 void Wrap(void *array, uint32_t width, uint32_t mask, uint32_t towidth, uint32_t arrayWidth, uint32_t rows, int flag, int size ); 212 void Clamp(void *array, uint32_t width, uint32_t towidth, uint32_t arrayWidth, uint32_t rows, int flag, int size ); 213 void Mirror(void *array, uint32_t width, uint32_t mask, uint32_t towidth, uint32_t arrayWidth, uint32_t rows, int flag, int size ); 214 215 protected: 216 TxtrCacheEntry * m_pHead; 217 TxtrCacheEntry ** m_pCacheTxtrList; 218 uint32_t m_numOfCachedTxtrList; 219 220 TxtrCacheEntry m_blackTextureEntry; 221 TxtrCacheEntry m_PrimColorTextureEntry; 222 TxtrCacheEntry m_EnvColorTextureEntry; 223 TxtrCacheEntry m_LODFracTextureEntry; 224 TxtrCacheEntry m_PrimLODFracTextureEntry; 225 TxtrCacheEntry * GetPrimColorTexture(uint32_t color); 226 TxtrCacheEntry * GetEnvColorTexture(uint32_t color); 227 TxtrCacheEntry * GetLODFracTexture(uint8_t fac); 228 TxtrCacheEntry * GetPrimLODFracTexture(uint8_t fac); 229 230 void MakeTextureYoungest(TxtrCacheEntry *pEntry); 231 unsigned int m_currentTextureMemUsage; 232 TxtrCacheEntry *m_pYoungestTexture; 233 TxtrCacheEntry *m_pOldestTexture; 234 235 public: 236 CTextureManager(); 237 ~CTextureManager(); 238 239 TxtrCacheEntry * GetBlackTexture(void); 240 TxtrCacheEntry * GetConstantColorTexture(uint32_t constant); 241 TxtrCacheEntry * GetTexture(TxtrInfo * pgti, bool fromTMEM, bool doCRCCheck, bool AutoExtendTexture); 242 243 void PurgeOldTextures(); 244 void RecycleAllTextures(); 245 void RecheckHiresForAllTextures(); 246 bool CleanUp(); 247 248 #ifdef DEBUGGER 249 TxtrCacheEntry * GetCachedTexture(uint32_t tex); 250 uint32_t GetNumOfCachedTexture(); 251 #endif 252 }; 253 254 extern CTextureManager gTextureManager; // The global instance of CTextureManager class 255 extern void DumpCachedTexture(TxtrCacheEntry &entry); 256 257 #endif 258 259