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__
26 #define SAFE_DELETE(p) { if(p) { delete (p); (p)=NULL; } }
30 # define SAFE_CHECK(a) if( (a) == NULL ) {DebugMessage(M64MSG_ERROR, "Creater out of memory"); throw new std::exception();}
37 #define absi(x) ((x)>=0?(x):(-x))
45 uint32 HeightToCreate;
48 void *pPhysicalAddress;
75 inline TxtrInfo& operator = (const TxtrInfo& src)
77 memcpy(this, &src, sizeof( TxtrInfo ));
81 inline TxtrInfo& operator = (const Tile& tile)
83 Format = tile.dwFormat;
85 Palette = tile.dwPalette;
89 mirrorS = tile.bMirrorS;
90 mirrorT = tile.bMirrorT;
91 clampS = tile.bClampS;
92 clampT = tile.bClampT;
97 inline bool operator == ( const TxtrInfo& sec)
100 Address == sec.Address &&
101 WidthToLoad == sec.WidthToLoad &&
102 HeightToLoad == sec.HeightToLoad &&
103 WidthToCreate == sec.WidthToCreate &&
104 HeightToCreate == sec.HeightToCreate &&
105 maskS == sec.maskS &&
106 maskT == sec.maskT &&
107 TLutFmt == sec.TLutFmt &&
108 PalAddress == sec.PalAddress &&
109 Palette == sec.Palette &&
110 LeftToLoad == sec.LeftToLoad &&
111 TopToLoad == sec.TopToLoad &&
112 Format == sec.Format &&
114 Pitch == sec.Pitch &&
115 bSwapped == sec.bSwapped &&
116 mirrorS == sec.mirrorS &&
117 mirrorT == sec.mirrorT &&
118 clampS == sec.clampS &&
123 inline bool isEqual(const TxtrInfo& sec)
125 return (*this == sec);
132 typedef struct TxtrCacheEntry
135 pTexture(NULL),pEnhancedTexture(NULL),txtrBufIdx(0) {}
139 SAFE_DELETE(pTexture);
140 SAFE_DELETE(pEnhancedTexture);
143 struct TxtrCacheEntry *pNext; // Must be first element!
145 struct TxtrCacheEntry *pNextYoungest;
146 struct TxtrCacheEntry *pLastYoungest;
153 uint32 dwUses; // Total times used (for stats)
154 uint32 dwTimeLastUsed; // timeGetTime of time of last usage
155 uint32 FrameLastUsed; // Frame # that this was last used
156 uint32 FrameLastUpdated;
159 CTexture *pEnhancedTexture;
161 uint32 dwEnhancementFlag;
163 bool bExternalTxtrChecked;
165 TxtrCacheEntry *lastEntry;
169 //*****************************************************************************
170 // Texture cache implementation
171 //*****************************************************************************
172 class CTextureManager
175 TxtrCacheEntry * CreateNewCacheEntry(uint32 dwAddr, uint32 dwWidth, uint32 dwHeight);
176 void AddTexture(TxtrCacheEntry *pEntry);
177 void RemoveTexture(TxtrCacheEntry * pEntry);
178 void RecycleTexture(TxtrCacheEntry *pEntry);
179 TxtrCacheEntry * ReviveTexture( uint32 width, uint32 height );
180 TxtrCacheEntry * GetTxtrCacheEntry(TxtrInfo * pti);
182 void ConvertTexture(TxtrCacheEntry * pEntry, bool fromTMEM);
183 void ConvertTexture_16(TxtrCacheEntry * pEntry, bool fromTMEM);
185 void ClampS32(uint32 *array, uint32 width, uint32 towidth, uint32 arrayWidth, uint32 rows);
186 void ClampS16(uint16 *array, uint32 width, uint32 towidth, uint32 arrayWidth, uint32 rows);
187 void ClampT32(uint32 *array, uint32 height, uint32 toheight, uint32 arrayWidth, uint32 cols);
188 void ClampT16(uint16 *array, uint32 height, uint32 toheight, uint32 arrayWidth, uint32 cols);
190 void MirrorS32(uint32 *array, uint32 width, uint32 mask, uint32 towidth, uint32 arrayWidth, uint32 rows);
191 void MirrorS16(uint16 *array, uint32 width, uint32 mask, uint32 towidth, uint32 arrayWidth, uint32 rows);
192 void MirrorT32(uint32 *array, uint32 height, uint32 mask, uint32 toheight, uint32 arrayWidth, uint32 cols);
193 void MirrorT16(uint16 *array, uint32 height, uint32 mask, uint32 toheight, uint32 arrayWidth, uint32 cols);
195 void WrapS32(uint32 *array, uint32 width, uint32 mask, uint32 towidth, uint32 arrayWidth, uint32 rows);
196 void WrapS16(uint16 *array, uint32 width, uint32 mask, uint32 towidth, uint32 arrayWidth, uint32 rows);
197 void WrapT32(uint32 *array, uint32 height, uint32 mask, uint32 toheight, uint32 arrayWidth, uint32 cols);
198 void WrapT16(uint16 *array, uint32 height, uint32 mask, uint32 toheight, uint32 arrayWidth, uint32 cols);
200 void ExpandTextureS(TxtrCacheEntry * pEntry);
201 void ExpandTextureT(TxtrCacheEntry * pEntry);
202 void ExpandTexture(TxtrCacheEntry * pEntry, uint32 sizeOfLoad, uint32 sizeToCreate, uint32 sizeCreated,
203 int arrayWidth, int flag, int mask, int mirror, int clamp, uint32 otherSize);
205 uint32 Hash(uint32 dwValue);
206 bool TCacheEntryIsLoaded(TxtrCacheEntry *pEntry);
208 void updateColorTexture(CTexture *ptexture, uint32 color);
211 void Wrap(void *array, uint32 width, uint32 mask, uint32 towidth, uint32 arrayWidth, uint32 rows, int flag, int size );
212 void Clamp(void *array, uint32 width, uint32 towidth, uint32 arrayWidth, uint32 rows, int flag, int size );
213 void Mirror(void *array, uint32 width, uint32 mask, uint32 towidth, uint32 arrayWidth, uint32 rows, int flag, int size );
216 TxtrCacheEntry * m_pHead;
217 TxtrCacheEntry ** m_pCacheTxtrList;
218 uint32 m_numOfCachedTxtrList;
220 TxtrCacheEntry m_blackTextureEntry;
221 TxtrCacheEntry m_PrimColorTextureEntry;
222 TxtrCacheEntry m_EnvColorTextureEntry;
223 TxtrCacheEntry m_LODFracTextureEntry;
224 TxtrCacheEntry m_PrimLODFracTextureEntry;
225 TxtrCacheEntry * GetPrimColorTexture(uint32 color);
226 TxtrCacheEntry * GetEnvColorTexture(uint32 color);
227 TxtrCacheEntry * GetLODFracTexture(uint8 fac);
228 TxtrCacheEntry * GetPrimLODFracTexture(uint8 fac);
230 void MakeTextureYoungest(TxtrCacheEntry *pEntry);
231 unsigned int m_currentTextureMemUsage;
232 TxtrCacheEntry *m_pYoungestTexture;
233 TxtrCacheEntry *m_pOldestTexture;
239 TxtrCacheEntry * GetBlackTexture(void);
240 TxtrCacheEntry * GetConstantColorTexture(uint32 constant);
241 TxtrCacheEntry * GetTexture(TxtrInfo * pgti, bool fromTMEM, bool doCRCCheck=true, bool AutoExtendTexture = false);
243 void PurgeOldTextures();
244 void RecycleAllTextures();
245 void RecheckHiresForAllTextures();
249 TxtrCacheEntry * GetCachedTexture(uint32 tex);
250 uint32 GetNumOfCachedTexture();
254 extern CTextureManager gTextureManager; // The global instance of CTextureManager class
255 extern void DumpCachedTexture(TxtrCacheEntry &entry);