-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgl_texturecache.cpp
250 lines (230 loc) · 8.75 KB
/
gl_texturecache.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/************************************************************************
* Copyright (c) 2005-2007 [email protected] *
* *
* This software is provided as-is, without any express or implied *
* warranty. In no event will the authors be held liable for any *
* damages arising from the use of this software. *
* *
* Permission is granted to anyone to use this software for any purpose, *
* including commercial applications, and to alter it and redistribute *
* it freely, subject to the following restrictions: *
* *
* 1. The origin of this software must not be misrepresented; you must *
* not claim that you wrote the original software. If you use this *
* software in a product, an acknowledgment in the product documentation *
* would be appreciated but is not required. *
* *
* 2. Altered source versions must be plainly marked as such, and must *
* not be misrepresented as being the original software. *
* *
* 3. This notice may not be removed or altered from any source *
* distribution. *
************************************************************************/
#include <iostream>
#include <limits>
#include <sstream>
#include "gl_texturecache.h"
#include "log.h"
namespace OpenGL {
template <typename key_type>
TextureCache<key_type>::TextureCache(const char* with_name) : m_name(with_name) {
instance_id = instance_count++;
clearMagic = 0;
has_cached_query = false;
last_query_result = 0;
minClearElements = 50;
}
template <typename key_type>
TextureCache<key_type>::TextureCache() {
instance_id = instance_count++;
std::ostringstream stream;
stream << "TextureCache_" << instance_count;
m_name = stream.str();
has_cached_query = false;
clearMagic = 0;
minClearElements = 50;
}
template <typename key_type>
TextureCache<key_type>::~TextureCache() {
unsigned int ts = cached.size();
clearAll();
INFO << m_name << " exited - " << ts << " textures recycled" << std::endl;
m_name.clear();
instance_count--;
}
template <typename key_type>
void TextureCache<key_type>::clearAll() {
typename std::map<key_type, texTuple*>::iterator i = cached.begin();
while (i != cached.end()) {
GLuint tid = (i->second->texId);
glDeleteTextures(1, &tid);
delete i->second;
i++;
}
cached.clear();
}
template <typename key_type>
void TextureCache<key_type>::status() {
std::cout << "* " << m_name << " status: " << cached.size() << " textures total" << std::endl
<< "position = game_id : usage_count" << std::endl;
printStats();
}
template <typename key_type>
void TextureCache<key_type>::sink() {
typename std::map<key_type, texTuple*>::iterator i = cached.begin();
while (i != cached.end()) {
if (i->second->refCount <= 1)
i->second->refCount = 0;
else if (i->second->refCount < _max_4)
i->second->refCount = i->second->refCount >> 1;
else if (i->second->refCount < _max_2) {
INFO << m_name << " texture id " << int(i->first) <<
" -- half-count reached" << std::endl;
i->second->refCount = i->second->refCount >> 2;
}
else {
WARN << m_name << " texture id " << int(i->first) <<
" -- going critical" << std::endl;
i->second->refCount = i->second->refCount >> 3;
}
i++;
}
}
template <typename key_type>
void TextureCache<key_type>::clear() {
if (clearMagic == 0)
return;
if (cached.size() < minClearElements)
return;
typename std::map<key_type, texTuple*>::iterator i = cached.begin();
uint32_t numCleared = 0;
while (i != cached.end()) {
if (i->second->refCount < clearMagic) {
//INFO <<"## " << m_name << " clearing: " << int(i->first) << " count: " << i->second->refCount << std::endl;
GLuint tid = (i->second->texId);
glDeleteTextures(1, &tid);
delete i->second;
cached.erase(i);
numCleared++;
}
i++;
}
INFO << m_name << " " << numCleared << " textures recycled" << std::endl;
}
template <typename key_type>
void TextureCache<key_type>::clearStats() {
typename std::map<key_type, texTuple*>::iterator i = cached.begin();
while (i != cached.end()) {
i->second->refCount = 0;
i++;
}
}
template <typename key_type>
void TextureCache<key_type>::printStats() {
typename std::map<key_type, texTuple*>::iterator i = cached.begin();
size_t c = 1;
size_t c_active = 0;
while (i != cached.end()) {
if (i->second->refCount > 0) {
std::cout << c << " = " << uint32_t(i->first) << " : " << i->second->refCount << std::endl;
c_active++;
}
i++;
c++;
}
std::cout << c_active << " different textures used" << std::endl;
}
template <typename key_type>
GLuint TextureCache<key_type>::getTextureWithId(key_type id) {
if (matchingCachedQuery(id)) {
last_query_result->refCount++;
return last_query_result->texId;
}
typename std::map<key_type, texTuple*>::iterator i = cached.find(id);
if (i == cached.end()) {
ERROR << m_name << " failed to find texture " << int(id) << std::endl;
return 0;
}
else {
cacheQuery(id, i->second);
i->second->refCount++;
}
/*
* if (i->second->isAnimated) {
AnimControl->lookup(i->second)
* }
*/
return i->second->texId;
}
template <typename key_type>
bool TextureCache<key_type>::hasTexture(key_type id) {
if (matchingCachedQuery(id))
return true; // last_query_result;
typename std::map<key_type, texTuple*>::iterator i = cached.find(id);
if (i == cached.end())
return false;
cacheQuery(id, i->second);
return true;
}
template <typename key_type>
void TextureCache<key_type>::setToAlpha(key_type id) {
typename std::map<key_type, texTuple*>::iterator i = cached.find(id);
if (i == cached.end()) {
ERROR << m_name << " texture not found when trying to set alpha" << std::endl;
return;
}
i->second->hasAlpha = true;
}
template <typename key_type>
void TextureCache<key_type>::setToAnimated(key_type id) {
typename std::map<key_type, texTuple*>::iterator i = cached.find(id);
if (i == cached.end()) {
ERROR << m_name << " texture not found when trying to set animation" << std::endl;
return;
}
i->second->isAnimated = true;
}
template <typename key_type>
void TextureCache<key_type>::addTexture(key_type id, GLuint texId) {
/*
std::map<uint8_t, texTuple*>::iterator i = cached.find(id);
if (i == cached.end())
return;*/
texTuple* tt = new texTuple();
tt->texId = texId;
tt->refCount = 1;
tt->hasAlpha = false;
tt->isAnimated = false;
cached[id] = tt;
INFO << m_name << " GL texture " << texId << " added for key: " << int(id) << std::endl;
}
template <typename key_type>
void TextureCache<key_type>::cacheQuery(key_type id, texTuple *pos) {
has_cached_query = true;
last_query_id = id;
last_query_result = pos;
}
template <typename key_type>
bool TextureCache<key_type>::matchingCachedQuery(key_type id) {
return ((has_cached_query) && (id == last_query_id));
}
template <typename key_type>
void TextureCache<key_type>::setClearMagic(uint32_t removeLesser) {
clearMagic = removeLesser;
}
template <typename key_type>
void TextureCache<key_type>::setMinClearElements(uint32_t minElements) {
minClearElements = minElements;
}
template <typename key_type>
unsigned int TextureCache<key_type>::instance_count = 0;
std::numeric_limits<uint32_t> _countInfo;
template <typename key_type>
const uint32_t TextureCache<key_type>::_max_4 = _countInfo.max() / 4;
template <typename key_type>
const uint32_t TextureCache<key_type>::_max_2 = _countInfo.max() / 2;
template class TextureCache<uint8_t>;
template class TextureCache<char>;
template class TextureCache<uint16_t>;
template class TextureCache<uint32_t>;
}