Rice Video Plugin for GLES1.1
[mupen64plus-pandora.git] / source / rice_gles / src / TextureManager.h
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 <exception>
24
25 #ifndef SAFE_DELETE
26 #define SAFE_DELETE(p)  { if(p) { delete (p);     (p)=NULL; } }
27 #endif
28
29 #ifndef SAFE_CHECK
30 # define SAFE_CHECK(a)  if( (a) == NULL ) {DebugMessage(M64MSG_ERROR, "Creater out of memory"); throw new std::exception();}
31 #endif
32
33 #include <string.h>
34
35 #include "typedefs.h"
36 #include "Texture.h"
37 #define absi(x)     ((x)>=0?(x):(-x))
38 #define S_FLAG  0
39 #define T_FLAG  1
40
41 class TxtrInfo
42 {
43 public:
44     uint32 WidthToCreate;
45     uint32 HeightToCreate;
46
47     uint32 Address;
48     void  *pPhysicalAddress;
49
50     uint32 Format;
51     uint32 Size;
52
53     int  LeftToLoad;
54     int  TopToLoad;
55     uint32 WidthToLoad;
56     uint32 HeightToLoad;
57     uint32 Pitch;
58
59     uchar *PalAddress;
60     uint32 TLutFmt;
61     uint32 Palette;
62     
63     BOOL  bSwapped;
64     
65     uint32 maskS;
66     uint32 maskT;
67
68     BOOL  clampS;
69     BOOL  clampT;
70     BOOL  mirrorS;
71     BOOL  mirrorT;
72
73     int   tileNo;
74
75     inline TxtrInfo& operator = (const TxtrInfo& src)
76     {
77         memcpy(this, &src, sizeof( TxtrInfo ));
78         return *this;
79     }
80
81     inline TxtrInfo& operator = (const Tile& tile)
82     {
83         Format = tile.dwFormat;
84         Size = tile.dwSize;
85         Palette = tile.dwPalette;
86         
87         maskS = tile.dwMaskS;
88         maskT = tile.dwMaskT;
89         mirrorS = tile.bMirrorS;
90         mirrorT = tile.bMirrorT;
91         clampS = tile.bClampS;
92         clampT = tile.bClampT;
93
94         return *this;
95     }
96
97     inline bool operator == ( const TxtrInfo& sec)
98     {
99         return (
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 &&
113             Size == sec.Size &&
114             Pitch == sec.Pitch &&
115             bSwapped == sec.bSwapped &&
116             mirrorS == sec.mirrorS &&
117             mirrorT == sec.mirrorT &&
118             clampS == sec.clampS &&
119             clampT == sec.clampT
120             );
121     }
122
123     inline bool isEqual(const TxtrInfo& sec)
124     {
125         return (*this == sec);
126     }
127     
128 } ;
129
130
131
132 typedef struct TxtrCacheEntry
133 {
134     TxtrCacheEntry():
135         pTexture(NULL),pEnhancedTexture(NULL),txtrBufIdx(0) {}
136
137     ~TxtrCacheEntry()
138     {
139         SAFE_DELETE(pTexture);
140         SAFE_DELETE(pEnhancedTexture);
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      dwCRC;
150     uint32      dwPalCRC;
151     int         maxCI;
152
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;
157
158     CTexture    *pTexture;
159     CTexture    *pEnhancedTexture;
160
161     uint32      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 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);
181     
182     void ConvertTexture(TxtrCacheEntry * pEntry, bool fromTMEM);
183     void ConvertTexture_16(TxtrCacheEntry * pEntry, bool fromTMEM);
184
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);
189
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);
194
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);
199
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);
204
205     uint32 Hash(uint32 dwValue);
206     bool TCacheEntryIsLoaded(TxtrCacheEntry *pEntry);
207
208     void updateColorTexture(CTexture *ptexture, uint32 color);
209     
210 public:
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 );
214     
215 protected:
216     TxtrCacheEntry * m_pHead;
217     TxtrCacheEntry ** m_pCacheTxtrList;
218     uint32 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 color);
226     TxtrCacheEntry * GetEnvColorTexture(uint32 color);
227     TxtrCacheEntry * GetLODFracTexture(uint8 fac);
228     TxtrCacheEntry * GetPrimLODFracTexture(uint8 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 constant);
241     TxtrCacheEntry * GetTexture(TxtrInfo * pgti, bool fromTMEM, bool doCRCCheck=true, bool AutoExtendTexture = false);
242     
243     void PurgeOldTextures();
244     void RecycleAllTextures();
245     void RecheckHiresForAllTextures();
246     bool CleanUp();
247     
248 #ifdef DEBUGGER
249     TxtrCacheEntry * GetCachedTexture(uint32 tex);
250     uint32 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