forked from cyxx/f2bgl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtexturecache.cpp
243 lines (224 loc) · 6.32 KB
/
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
/*
* Fade To Black engine rewrite
* Copyright (C) 2006-2012 Gregory Montoir ([email protected])
*/
#ifdef USE_GLES
#include <GLES/gl.h>
#else
#include <SDL_opengl.h>
#endif
#include "scaler.h"
#include "texturecache.h"
static const int kDefaultTexBufSize = 320 * 200;
static const int kTextureMinMaxFilter = GL_LINEAR; // GL_NEAREST
uint16_t convert_RGBA_5551(int r, int g, int b) {
return ((r >> 3) << 11) | ((g >> 3) << 6) | ((b >> 3) << 1) | 1;
}
uint16_t convert_BGRA_1555(int r, int g, int b) {
return 0x8000 | ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3);
}
static const struct {
int internal;
int format;
int type;
uint16_t (*convertColor)(int, int, int);
} _formats[] = {
#ifdef __amigaos4__
{ GL_RGB5_A1, GL_BGRA, GL_UNSIGNED_SHORT_1_5_5_5_REV, &convert_BGRA_1555 },
#endif
#ifdef USE_GLES
{ GL_RGBA, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, &convert_RGBA_5551 },
#else
{ GL_RGB5_A1, GL_RGBA, GL_UNSIGNED_SHORT_5_5_5_1, &convert_RGBA_5551 },
#endif
{ -1, -1, -1, 0 }
};
static const struct {
void (*proc)(uint16_t *dst, int dstPitch, const uint16_t *src, int srcPitch, int w, int h);
int factor;
} _scalers[] = {
{ point1x, 1 },
{ point2x, 2 },
{ scale2x, 2 },
{ point3x, 3 },
{ scale3x, 3 },
};
static const int _scaler = 2;
TextureCache::TextureCache()
: _fmt(0), _texturesListHead(0), _texturesListTail(0) {
memset(_clut, 0, sizeof(_clut));
if (_scalers[_scaler].factor != 1) {
_texBuf = (uint16_t *)malloc(kDefaultTexBufSize * sizeof(uint16_t));
} else {
_texBuf = 0;
}
_npotTex = false;
}
TextureCache::~TextureCache() {
free(_texBuf);
flush();
}
static bool hasExt(const char *exts, const char *name) {
const char *p = strstr(exts, name);
if (p) {
p += strlen(name);
return *p == ' ' || *p == 0;
}
return false;
}
void TextureCache::init() {
const char *exts = (const char *)glGetString(GL_EXTENSIONS);
if (hasExt(exts, "GL_ARB_texture_non_power_of_two")) {
_npotTex = true;
}
}
void TextureCache::flush() {
Texture *t = _texturesListHead;
while (t) {
Texture *next = t->next;
glDeleteTextures(1, &t->id);
free(t->bitmapData);
delete t;
t = next;
}
_texturesListHead = _texturesListTail = 0;
memset(_clut, 0, sizeof(_clut));
}
Texture *TextureCache::getCachedTexture(const uint8_t *data, int w, int h, int16_t key) {
for (Texture *t = _texturesListHead; t; t = t->next) {
if (t->key == key) {
return t;
}
}
Texture *t = createTexture(data, w, h);
if (t) {
t->key = key;
}
return t;
}
static int roundPow2(int sz) {
if (sz != 0 && (sz & (sz - 1)) == 0) {
return sz;
}
int textureSize = 1;
while (textureSize < sz) {
textureSize <<= 1;
}
return textureSize;
}
void TextureCache::convertTexture(const uint8_t *src, int w, int h, const uint16_t *clut, uint16_t *dst, int dstPitch) {
if (_scalers[_scaler].factor == 1) {
for (int y = 0; y < h; ++y) {
for (int x = 0; x < w; ++x) {
dst[x] = clut[src[x]];
}
dst += dstPitch;
src += w;
}
} else {
assert(w * h <= kDefaultTexBufSize);
for (int y = 0; y < h; ++y) {
for (int x = 0; x < w; ++x) {
_texBuf[y * w + x] = clut[src[x]];
}
src += w;
}
_scalers[_scaler].proc(dst, dstPitch, _texBuf, w, w, h);
}
}
Texture *TextureCache::createTexture(const uint8_t *data, int w, int h) {
Texture *t = new Texture;
t->bitmapW = w;
t->bitmapH = h;
t->bitmapData = (uint8_t *)malloc(w * h);
if (!t->bitmapData) {
delete t;
return 0;
}
memcpy(t->bitmapData, data, w * h);
w *= _scalers[_scaler].factor;
h *= _scalers[_scaler].factor;
t->texW = _npotTex ? w : roundPow2(w);
t->texH = _npotTex ? h : roundPow2(h);
t->u = w / (float)t->texW;
t->v = h / (float)t->texH;
glGenTextures(1, &t->id);
uint16_t *texData = (uint16_t *)malloc(t->texW * t->texH * sizeof(uint16_t));
if (texData) {
convertTexture(t->bitmapData, t->bitmapW, t->bitmapH, _clut, texData, t->texW);
glBindTexture(GL_TEXTURE_2D, t->id);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, kTextureMinMaxFilter);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, kTextureMinMaxFilter);
if (kTextureMinMaxFilter == GL_LINEAR) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
}
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage2D(GL_TEXTURE_2D, 0, _formats[_fmt].internal, t->texW, t->texH, 0, _formats[_fmt].format, _formats[_fmt].type, texData);
free(texData);
}
if (!_texturesListHead) {
_texturesListHead = _texturesListTail = t;
} else {
_texturesListTail->next = t;
_texturesListTail = t;
}
t->next = 0;
t->key = -1;
return t;
}
void TextureCache::destroyTexture(Texture *texture) {
glDeleteTextures(1, &texture->id);
free(texture->bitmapData);
if (texture == _texturesListHead) {
_texturesListHead = texture->next;
if (texture == _texturesListTail) {
_texturesListTail = _texturesListHead;
}
} else {
for (Texture *t = _texturesListHead; t; t = t->next) {
if (t->next == texture) {
t->next = texture->next;
if (texture == _texturesListTail) {
_texturesListTail = t;
}
break;
}
}
}
delete texture;
}
void TextureCache::updateTexture(Texture *t, const uint8_t *data, int w, int h) {
assert(t->bitmapW == w && t->bitmapH == h);
memcpy(t->bitmapData, data, w * h);
uint16_t *texData = (uint16_t *)malloc(t->texW * t->texH * sizeof(uint16_t));
if (texData) {
convertTexture(t->bitmapData, t->bitmapW, t->bitmapH, _clut, texData, t->texW);
glBindTexture(GL_TEXTURE_2D, t->id);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, t->texW, t->texH, _formats[_fmt].format, _formats[_fmt].type, texData);
free(texData);
}
}
void TextureCache::setPalette(const uint8_t *pal, bool updateTextures) {
for (int i = 0; i < 256; ++i, pal += 3) {
const int r = pal[0];
const int g = pal[1];
const int b = pal[2];
if (r == 0 && g == 0 && b == 0) {
_clut[i] = 0;
} else {
_clut[i] = _formats[_fmt].convertColor(r, g, b);
}
}
if (updateTextures) {
for (Texture *t = _texturesListHead; t; t = t->next) {
uint16_t *texData = (uint16_t *)malloc(t->texW * t->texH * sizeof(uint16_t));
if (texData) {
convertTexture(t->bitmapData, t->bitmapW, t->bitmapH, _clut, texData, t->texW);
glBindTexture(GL_TEXTURE_2D, t->id);
glTexImage2D(GL_TEXTURE_2D, 0, _formats[_fmt].internal, t->texW, t->texH, 0, _formats[_fmt].format, _formats[_fmt].type, texData);
free(texData);
}
}
}
}