~ubuntu-branches/ubuntu/saucy/mupen64plus-video-rice/saucy

« back to all changes in this revision

Viewing changes to src/TextureManager.h

  • Committer: Bazaar Package Importer
  • Author(s): Sven Eckelmann
  • Date: 2011-01-22 11:05:28 UTC
  • Revision ID: james.westby@ubuntu.com-20110122110528-k6z84gdespqqd9zp
Tags: upstream-1.99.4
ImportĀ upstreamĀ versionĀ 1.99.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
#ifndef SAFE_DELETE
 
24
#define SAFE_DELETE(p)  { if(p) { delete (p);     (p)=NULL; } }
 
25
#endif
 
26
 
 
27
#ifndef SAFE_CHECK
 
28
# define SAFE_CHECK(a)  if( (a) == NULL ) {DebugMessage(M64MSG_ERROR, "Creater out of memory"); throw new std::exception();}
 
29
#endif
 
30
 
 
31
#include <string.h>
 
32
 
 
33
#include "typedefs.h"
 
34
#include "Texture.h"
 
35
#define absi(x)     ((x)>=0?(x):(-x))
 
36
#define S_FLAG  0
 
37
#define T_FLAG  1
 
38
 
 
39
class TxtrInfo
 
40
{
 
41
public:
 
42
    uint32 WidthToCreate;
 
43
    uint32 HeightToCreate;
 
44
 
 
45
    uint32 Address;
 
46
    void  *pPhysicalAddress;
 
47
 
 
48
    uint32 Format;
 
49
    uint32 Size;
 
50
 
 
51
    int  LeftToLoad;
 
52
    int  TopToLoad;
 
53
    uint32 WidthToLoad;
 
54
    uint32 HeightToLoad;
 
55
    uint32 Pitch;
 
56
 
 
57
    uchar *PalAddress;
 
58
    uint32 TLutFmt;
 
59
    uint32 Palette;
 
60
    
 
61
    BOOL  bSwapped;
 
62
    
 
63
    uint32 maskS;
 
64
    uint32 maskT;
 
65
 
 
66
    BOOL  clampS;
 
67
    BOOL  clampT;
 
68
    BOOL  mirrorS;
 
69
    BOOL  mirrorT;
 
70
 
 
71
    int   tileNo;
 
72
 
 
73
    inline TxtrInfo& operator = (const TxtrInfo& src)
 
74
    {
 
75
        memcpy(this, &src, sizeof( TxtrInfo ));
 
76
        return *this;
 
77
    }
 
78
 
 
79
    inline TxtrInfo& operator = (const Tile& tile)
 
80
    {
 
81
        Format = tile.dwFormat;
 
82
        Size = tile.dwSize;
 
83
        Palette = tile.dwPalette;
 
84
        
 
85
        maskS = tile.dwMaskS;
 
86
        maskT = tile.dwMaskT;
 
87
        mirrorS = tile.bMirrorS;
 
88
        mirrorT = tile.bMirrorT;
 
89
        clampS = tile.bClampS;
 
90
        clampT = tile.bClampT;
 
91
 
 
92
        return *this;
 
93
    }
 
94
 
 
95
    inline bool operator == ( const TxtrInfo& sec)
 
96
    {
 
97
        return (
 
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 &&         
 
111
            Size == sec.Size &&
 
112
            Pitch == sec.Pitch &&
 
113
            bSwapped == sec.bSwapped &&
 
114
            TLutFmt == sec.TLutFmt &&
 
115
            mirrorS == sec.mirrorS &&
 
116
            mirrorT == sec.mirrorT &&
 
117
            clampS == sec.clampS &&
 
118
            clampT == sec.clampT
 
119
            );
 
120
    }
 
121
 
 
122
    inline bool isEqual(const TxtrInfo& sec)
 
123
    {
 
124
        return (*this == sec);
 
125
    }
 
126
    
 
127
} ;
 
128
 
 
129
 
 
130
 
 
131
typedef struct TxtrCacheEntry
 
132
{
 
133
    TxtrCacheEntry():
 
134
        pTexture(NULL),pEnhancedTexture(NULL),txtrBufIdx(0) {}
 
135
 
 
136
    ~TxtrCacheEntry()
 
137
    {
 
138
        SAFE_DELETE(pTexture);
 
139
        SAFE_DELETE(pEnhancedTexture);
 
140
    }
 
141
    
 
142
    struct TxtrCacheEntry *pNext;       // Must be first element!
 
143
 
 
144
    struct TxtrCacheEntry *pNextYoungest;
 
145
    struct TxtrCacheEntry *pLastYoungest;
 
146
 
 
147
    TxtrInfo ti;
 
148
    uint32      dwCRC;
 
149
    uint32      dwPalCRC;
 
150
    int         maxCI;
 
151
 
 
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;
 
156
 
 
157
    CTexture    *pTexture;
 
158
    CTexture    *pEnhancedTexture;
 
159
 
 
160
    uint32      dwEnhancementFlag;
 
161
    int         txtrBufIdx;
 
162
    bool        bExternalTxtrChecked;
 
163
 
 
164
    TxtrCacheEntry *lastEntry;
 
165
} TxtrCacheEntry;
 
166
 
 
167
 
 
168
//*****************************************************************************
 
169
// Texture cache implementation
 
170
//*****************************************************************************
 
171
class CTextureManager
 
172
{
 
173
protected:
 
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);
 
180
    
 
181
    void ConvertTexture(TxtrCacheEntry * pEntry, bool fromTMEM);
 
182
    void ConvertTexture_16(TxtrCacheEntry * pEntry, bool fromTMEM);
 
183
 
 
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);
 
188
 
 
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);
 
193
 
 
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);
 
198
 
 
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);
 
203
 
 
204
    uint32 Hash(uint32 dwValue);
 
205
    bool TCacheEntryIsLoaded(TxtrCacheEntry *pEntry);
 
206
 
 
207
    void updateColorTexture(CTexture *ptexture, uint32 color);
 
208
    
 
209
public:
 
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 );
 
213
    
 
214
protected:
 
215
    TxtrCacheEntry * m_pHead;
 
216
    TxtrCacheEntry ** m_pCacheTxtrList;
 
217
    uint32 m_numOfCachedTxtrList;
 
218
 
 
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);
 
228
 
 
229
    void MakeTextureYoungest(TxtrCacheEntry *pEntry);
 
230
    unsigned int m_currentTextureMemUsage;
 
231
    TxtrCacheEntry *m_pYoungestTexture;
 
232
    TxtrCacheEntry *m_pOldestTexture;
 
233
 
 
234
public:
 
235
    CTextureManager();
 
236
    ~CTextureManager();
 
237
 
 
238
    TxtrCacheEntry * GetBlackTexture(void);
 
239
    TxtrCacheEntry * GetConstantColorTexture(uint32 constant);
 
240
    TxtrCacheEntry * GetTexture(TxtrInfo * pgti, bool fromTMEM, bool doCRCCheck=true, bool AutoExtendTexture = false);
 
241
    
 
242
    void PurgeOldTextures();
 
243
    void RecycleAllTextures();
 
244
    void RecheckHiresForAllTextures();
 
245
    bool CleanUp();
 
246
    
 
247
#ifdef DEBUGGER
 
248
    TxtrCacheEntry * GetCachedTexture(uint32 tex);
 
249
    uint32 GetNumOfCachedTexture();
 
250
#endif
 
251
};
 
252
 
 
253
extern CTextureManager gTextureManager;     // The global instance of CTextureManager class
 
254
extern void DumpCachedTexture(TxtrCacheEntry &entry);
 
255
 
 
256
#endif
 
257