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
TLutFmt == sec.TLutFmt &&
115
mirrorS == sec.mirrorS &&
116
mirrorT == sec.mirrorT &&
117
clampS == sec.clampS &&
122
inline bool isEqual(const TxtrInfo& sec)
124
return (*this == sec);
131
typedef struct TxtrCacheEntry
134
pTexture(NULL),pEnhancedTexture(NULL),txtrBufIdx(0) {}
138
SAFE_DELETE(pTexture);
139
SAFE_DELETE(pEnhancedTexture);
142
struct TxtrCacheEntry *pNext; // Must be first element!
144
struct TxtrCacheEntry *pNextYoungest;
145
struct TxtrCacheEntry *pLastYoungest;
152
uint32 dwUses; // Total times used (for stats)
153
uint32 dwTimeLastUsed; // timeGetTime of time of last usage
154
uint32 FrameLastUsed; // Frame # that this was last used
155
uint32 FrameLastUpdated;
158
CTexture *pEnhancedTexture;
160
uint32 dwEnhancementFlag;
162
bool bExternalTxtrChecked;
164
TxtrCacheEntry *lastEntry;
168
//*****************************************************************************
169
// Texture cache implementation
170
//*****************************************************************************
171
class CTextureManager
174
TxtrCacheEntry * CreateNewCacheEntry(uint32 dwAddr, uint32 dwWidth, uint32 dwHeight);
175
void AddTexture(TxtrCacheEntry *pEntry);
176
void RemoveTexture(TxtrCacheEntry * pEntry);
177
void RecycleTexture(TxtrCacheEntry *pEntry);
178
TxtrCacheEntry * ReviveTexture( uint32 width, uint32 height );
179
TxtrCacheEntry * GetTxtrCacheEntry(TxtrInfo * pti);
181
void ConvertTexture(TxtrCacheEntry * pEntry, bool fromTMEM);
182
void ConvertTexture_16(TxtrCacheEntry * pEntry, bool fromTMEM);
184
void ClampS32(uint32 *array, uint32 width, uint32 towidth, uint32 arrayWidth, uint32 rows);
185
void ClampS16(uint16 *array, uint32 width, uint32 towidth, uint32 arrayWidth, uint32 rows);
186
void ClampT32(uint32 *array, uint32 height, uint32 toheight, uint32 arrayWidth, uint32 cols);
187
void ClampT16(uint16 *array, uint32 height, uint32 toheight, uint32 arrayWidth, uint32 cols);
189
void MirrorS32(uint32 *array, uint32 width, uint32 mask, uint32 towidth, uint32 arrayWidth, uint32 rows);
190
void MirrorS16(uint16 *array, uint32 width, uint32 mask, uint32 towidth, uint32 arrayWidth, uint32 rows);
191
void MirrorT32(uint32 *array, uint32 height, uint32 mask, uint32 toheight, uint32 arrayWidth, uint32 cols);
192
void MirrorT16(uint16 *array, uint32 height, uint32 mask, uint32 toheight, uint32 arrayWidth, uint32 cols);
194
void WrapS32(uint32 *array, uint32 width, uint32 mask, uint32 towidth, uint32 arrayWidth, uint32 rows);
195
void WrapS16(uint16 *array, uint32 width, uint32 mask, uint32 towidth, uint32 arrayWidth, uint32 rows);
196
void WrapT32(uint32 *array, uint32 height, uint32 mask, uint32 toheight, uint32 arrayWidth, uint32 cols);
197
void WrapT16(uint16 *array, uint32 height, uint32 mask, uint32 toheight, uint32 arrayWidth, uint32 cols);
199
void ExpandTextureS(TxtrCacheEntry * pEntry);
200
void ExpandTextureT(TxtrCacheEntry * pEntry);
201
void ExpandTexture(TxtrCacheEntry * pEntry, uint32 sizeOfLoad, uint32 sizeToCreate, uint32 sizeCreated,
202
int arrayWidth, int flag, int mask, int mirror, int clamp, uint32 otherSize);
204
uint32 Hash(uint32 dwValue);
205
bool TCacheEntryIsLoaded(TxtrCacheEntry *pEntry);
207
void updateColorTexture(CTexture *ptexture, uint32 color);
210
void Wrap(void *array, uint32 width, uint32 mask, uint32 towidth, uint32 arrayWidth, uint32 rows, int flag, int size );
211
void Clamp(void *array, uint32 width, uint32 towidth, uint32 arrayWidth, uint32 rows, int flag, int size );
212
void Mirror(void *array, uint32 width, uint32 mask, uint32 towidth, uint32 arrayWidth, uint32 rows, int flag, int size );
215
TxtrCacheEntry * m_pHead;
216
TxtrCacheEntry ** m_pCacheTxtrList;
217
uint32 m_numOfCachedTxtrList;
219
TxtrCacheEntry m_blackTextureEntry;
220
TxtrCacheEntry m_PrimColorTextureEntry;
221
TxtrCacheEntry m_EnvColorTextureEntry;
222
TxtrCacheEntry m_LODFracTextureEntry;
223
TxtrCacheEntry m_PrimLODFracTextureEntry;
224
TxtrCacheEntry * GetPrimColorTexture(uint32 color);
225
TxtrCacheEntry * GetEnvColorTexture(uint32 color);
226
TxtrCacheEntry * GetLODFracTexture(uint8 fac);
227
TxtrCacheEntry * GetPrimLODFracTexture(uint8 fac);
229
void MakeTextureYoungest(TxtrCacheEntry *pEntry);
230
unsigned int m_currentTextureMemUsage;
231
TxtrCacheEntry *m_pYoungestTexture;
232
TxtrCacheEntry *m_pOldestTexture;
238
TxtrCacheEntry * GetBlackTexture(void);
239
TxtrCacheEntry * GetConstantColorTexture(uint32 constant);
240
TxtrCacheEntry * GetTexture(TxtrInfo * pgti, bool fromTMEM, bool doCRCCheck=true, bool AutoExtendTexture = false);
242
void PurgeOldTextures();
243
void RecycleAllTextures();
244
void RecheckHiresForAllTextures();
248
TxtrCacheEntry * GetCachedTexture(uint32 tex);
249
uint32 GetNumOfCachedTexture();
253
extern CTextureManager gTextureManager; // The global instance of CTextureManager class
254
extern void DumpCachedTexture(TxtrCacheEntry &entry);