2 Copyright (C) 2003 Rice1964
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.
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.
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.
20 #ifndef __TEXTUREHANDLER_H__
21 #define __TEXTUREHANDLER_H__
24 #define SAFE_DELETE(p) { if(p) { delete (p); (p)=NULL; } }
28 # define SAFE_CHECK(a) if( (a) == NULL ) {DebugMessage(M64MSG_ERROR, "Creater out of memory"); throw new std::exception();}
35 #define absi(x) ((x)>=0?(x):(-x))
43 uint32 HeightToCreate;
46 void *pPhysicalAddress;
73 inline TxtrInfo& operator = (const TxtrInfo& src)
75 memcpy(this, &src, sizeof( TxtrInfo ));
79 inline TxtrInfo& operator = (const Tile& tile)
81 Format = tile.dwFormat;
83 Palette = tile.dwPalette;
87 mirrorS = tile.bMirrorS;
88 mirrorT = tile.bMirrorT;
89 clampS = tile.bClampS;
90 clampT = tile.bClampT;
95 inline bool operator == ( const TxtrInfo& sec)
98 Address == sec.Address &&
99 WidthToLoad == sec.WidthToLoad &&
100 HeightToLoad == sec.HeightToLoad &&
101 WidthToCreate == sec.WidthToCreate &&
102 HeightToCreate == sec.HeightToCreate &&
103 maskS == sec.maskS &&
104 maskT == sec.maskT &&
105 TLutFmt == sec.TLutFmt &&
106 PalAddress == sec.PalAddress &&
107 Palette == sec.Palette &&
108 LeftToLoad == sec.LeftToLoad &&
109 TopToLoad == sec.TopToLoad &&
110 Format == sec.Format &&
112 Pitch == sec.Pitch &&
113 bSwapped == sec.bSwapped &&
114 mirrorS == sec.mirrorS &&
115 mirrorT == sec.mirrorT &&
116 clampS == sec.clampS &&
121 inline bool isEqual(const TxtrInfo& sec)
123 return (*this == sec);
130 typedef struct TxtrCacheEntry
133 pTexture(NULL),pEnhancedTexture(NULL),txtrBufIdx(0) {}
137 SAFE_DELETE(pTexture);
138 SAFE_DELETE(pEnhancedTexture);
141 struct TxtrCacheEntry *pNext; // Must be first element!
143 struct TxtrCacheEntry *pNextYoungest;
144 struct TxtrCacheEntry *pLastYoungest;
151 uint32 dwUses; // Total times used (for stats)
152 uint32 dwTimeLastUsed; // timeGetTime of time of last usage
153 uint32 FrameLastUsed; // Frame # that this was last used
154 uint32 FrameLastUpdated;
157 CTexture *pEnhancedTexture;
159 uint32 dwEnhancementFlag;
161 bool bExternalTxtrChecked;
163 TxtrCacheEntry *lastEntry;
167 //*****************************************************************************
168 // Texture cache implementation
169 //*****************************************************************************
170 class CTextureManager
173 TxtrCacheEntry * CreateNewCacheEntry(uint32 dwAddr, uint32 dwWidth, uint32 dwHeight);
174 void AddTexture(TxtrCacheEntry *pEntry);
175 void RemoveTexture(TxtrCacheEntry * pEntry);
176 void RecycleTexture(TxtrCacheEntry *pEntry);
177 TxtrCacheEntry * ReviveTexture( uint32 width, uint32 height );
178 TxtrCacheEntry * GetTxtrCacheEntry(TxtrInfo * pti);
180 void ConvertTexture(TxtrCacheEntry * pEntry, bool fromTMEM);
181 void ConvertTexture_16(TxtrCacheEntry * pEntry, bool fromTMEM);
183 void ClampS32(uint32 *array, uint32 width, uint32 towidth, uint32 arrayWidth, uint32 rows);
184 void ClampS16(uint16 *array, uint32 width, uint32 towidth, uint32 arrayWidth, uint32 rows);
185 void ClampT32(uint32 *array, uint32 height, uint32 toheight, uint32 arrayWidth, uint32 cols);
186 void ClampT16(uint16 *array, uint32 height, uint32 toheight, uint32 arrayWidth, uint32 cols);
188 void MirrorS32(uint32 *array, uint32 width, uint32 mask, uint32 towidth, uint32 arrayWidth, uint32 rows);
189 void MirrorS16(uint16 *array, uint32 width, uint32 mask, uint32 towidth, uint32 arrayWidth, uint32 rows);
190 void MirrorT32(uint32 *array, uint32 height, uint32 mask, uint32 toheight, uint32 arrayWidth, uint32 cols);
191 void MirrorT16(uint16 *array, uint32 height, uint32 mask, uint32 toheight, uint32 arrayWidth, uint32 cols);
193 void WrapS32(uint32 *array, uint32 width, uint32 mask, uint32 towidth, uint32 arrayWidth, uint32 rows);
194 void WrapS16(uint16 *array, uint32 width, uint32 mask, uint32 towidth, uint32 arrayWidth, uint32 rows);
195 void WrapT32(uint32 *array, uint32 height, uint32 mask, uint32 toheight, uint32 arrayWidth, uint32 cols);
196 void WrapT16(uint16 *array, uint32 height, uint32 mask, uint32 toheight, uint32 arrayWidth, uint32 cols);
198 void ExpandTextureS(TxtrCacheEntry * pEntry);
199 void ExpandTextureT(TxtrCacheEntry * pEntry);
200 void ExpandTexture(TxtrCacheEntry * pEntry, uint32 sizeOfLoad, uint32 sizeToCreate, uint32 sizeCreated,
201 int arrayWidth, int flag, int mask, int mirror, int clamp, uint32 otherSize);
203 uint32 Hash(uint32 dwValue);
204 bool TCacheEntryIsLoaded(TxtrCacheEntry *pEntry);
206 void updateColorTexture(CTexture *ptexture, uint32 color);
209 void Wrap(void *array, uint32 width, uint32 mask, uint32 towidth, uint32 arrayWidth, uint32 rows, int flag, int size );
210 void Clamp(void *array, uint32 width, uint32 towidth, uint32 arrayWidth, uint32 rows, int flag, int size );
211 void Mirror(void *array, uint32 width, uint32 mask, uint32 towidth, uint32 arrayWidth, uint32 rows, int flag, int size );
214 TxtrCacheEntry * m_pHead;
215 TxtrCacheEntry ** m_pCacheTxtrList;
216 uint32 m_numOfCachedTxtrList;
218 TxtrCacheEntry m_blackTextureEntry;
219 TxtrCacheEntry m_PrimColorTextureEntry;
220 TxtrCacheEntry m_EnvColorTextureEntry;
221 TxtrCacheEntry m_LODFracTextureEntry;
222 TxtrCacheEntry m_PrimLODFracTextureEntry;
223 TxtrCacheEntry * GetPrimColorTexture(uint32 color);
224 TxtrCacheEntry * GetEnvColorTexture(uint32 color);
225 TxtrCacheEntry * GetLODFracTexture(uint8 fac);
226 TxtrCacheEntry * GetPrimLODFracTexture(uint8 fac);
228 void MakeTextureYoungest(TxtrCacheEntry *pEntry);
229 unsigned int m_currentTextureMemUsage;
230 TxtrCacheEntry *m_pYoungestTexture;
231 TxtrCacheEntry *m_pOldestTexture;
237 TxtrCacheEntry * GetBlackTexture(void);
238 TxtrCacheEntry * GetConstantColorTexture(uint32 constant);
239 TxtrCacheEntry * GetTexture(TxtrInfo * pgti, bool fromTMEM, bool doCRCCheck=true, bool AutoExtendTexture = false);
241 void PurgeOldTextures();
242 void RecycleAllTextures();
243 void RecheckHiresForAllTextures();
247 TxtrCacheEntry * GetCachedTexture(uint32 tex);
248 uint32 GetNumOfCachedTexture();
252 extern CTextureManager gTextureManager; // The global instance of CTextureManager class
253 extern void DumpCachedTexture(TxtrCacheEntry &entry);