-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPS2Texture.cpp
More file actions
277 lines (244 loc) · 9.95 KB
/
Copy pathPS2Texture.cpp
File metadata and controls
277 lines (244 loc) · 9.95 KB
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#include "ClimaxEngine/Platform/PS2/PS2Texture.h"
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
std::vector<uint8_t> Unswizzle8(const std::vector<uint8_t>& buf, int w, int h) {
std::vector<uint8_t> out(w * h);
if (buf.size() < (size_t)(w * h)) return out;
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
int block_loc = (y & (~0xf)) * w + (x & (~0xf)) * 2;
int swap_sel = (((y + 2) >> 2) & 0x1) * 4;
int posY = (((y & (~3)) >> 1) + (y & 1)) & 0x7;
int col_loc = posY * w * 2 + ((x + swap_sel) & 0x7) * 4;
int byte_num = ((y >> 1) & 1) + ((x >> 2) & 2);
int swizzleid = block_loc + col_loc + byte_num;
if ((size_t)swizzleid < buf.size()) out[y * w + x] = buf[swizzleid];
}
}
return out;
}
std::vector<uint8_t> UnswizzlePalette(const std::vector<uint8_t>& pal) {
std::vector<uint8_t> newPal(1024, 0);
if (pal.size() < 1024) return pal;
for (int p = 0; p < 256; p++) {
int pos = ((p & 0xE7) + ((p & 8) << 1) + ((p & 16) >> 1));
if (pos < 256 && (pos * 4 + 3) < (int)newPal.size() && (p * 4 + 3) < (int)pal.size()) {
for (int k = 0; k < 4; k++) newPal[pos * 4 + k] = pal[p * 4 + k];
}
}
return newPal;
}
static void UploadRGBA(RawTexture& raw, const std::vector<uint8_t>& rgba, int w, int h);
// Wii rasters come out of the GX decoder as plain RGBA with real 0..255 alpha,
// so they must skip the PS2 doubling that ProcessAndUploadTexture applies.
void UploadDecodedTexture(RawTexture& raw, const std::vector<uint8_t>& rgba) {
if (raw.width <= 0 || raw.height <= 0) return;
if (rgba.size() < (size_t)raw.width * raw.height * 4) return;
size_t partial = 0, clear = 0;
const size_t total = (size_t)raw.width * raw.height;
for (size_t i = 0; i + 3 < rgba.size(); i += 4) {
const uint8_t a = rgba[i + 3];
if (a > 8 && a < 248) partial++;
else if (a <= 8) clear++;
}
raw.hasAlphaGradient = total && (partial * 100 / total) >= 15;
raw.hasTransparentTexels = total && (clear * 100 / total) >= 2;
UploadRGBA(raw, rgba, raw.width, raw.height);
}
void ProcessAndUploadTexture(RawTexture& raw) {
int w = raw.width, h = raw.height;
if (w <= 0 || h <= 0) return;
// PSMCT32: no palette, the texel data is already RGBA and stored linearly.
// Only 4- and 8-bit paletted rasters were handled before, so every 32-bit
// texture decoded to a blank white image — which in IntroRoad is exactly the
// tree and grass sheets.
if (raw.depth == 32) {
std::vector<uint8_t> rgba((size_t)w * h * 4, 255);
const size_t have = std::min(raw.pixels.size(), rgba.size());
for (size_t i = 0; i + 3 < have; i += 4) {
rgba[i + 0] = raw.pixels[i + 0];
rgba[i + 1] = raw.pixels[i + 1];
rgba[i + 2] = raw.pixels[i + 2];
// Same PS2 convention as the palette: 0x80 is fully opaque.
rgba[i + 3] = (uint8_t)std::min((int)raw.pixels[i + 3] * 2, 255);
}
UploadRGBA(raw, rgba, w, h);
return;
}
std::vector<uint8_t> indices;
if (raw.depth == 8) {
indices = Unswizzle8(raw.pixels, w, h);
} else if (raw.depth == 4) {
indices.assign(w * h, 0);
int padded_w = std::max(w, 32);
int logw = 0;
for (int i = 1; i < padded_w; i *= 2) logw++;
uint32_t mask = (1 << (logw + 2)) - 1;
for (int y = 0; y < h; y += 4) {
for (int i = 0; i < 4; i++) {
if (y + i >= h) break;
for (int x = 0; x < w; x++) {
uint32_t xx = x ^ (((((y+i) >> 1) & 1) ^ (((y+i) >> 2) & 1)) << 2);
uint32_t nx = (xx & 7) | ((xx >> 1) & ~7u);
uint32_t ny = ((y+i) & 1) | (((y+i) >> 1) & ~1u);
uint32_t n = (((y+i) >> 1) & 1) | (((xx >> 3) & 1) << 1);
uint32_t s = (n | (nx << 2) | (ny << (logw + 1))) & mask;
uint32_t byte_idx = (y * padded_w / 2) + (s >> 1);
if (byte_idx < raw.pixels.size()) {
uint8_t c = (s & 1) ? (raw.pixels[byte_idx] >> 4) : (raw.pixels[byte_idx] & 0xF);
indices[(y + i) * w + x] = c;
}
}
}
}
}
// The GS stores 256-entry CLUTs interleaved; 16-entry ones are linear.
// Trim to the raster's declared palette size first, so a 4-bit texture
// cannot read colours belonging to the padding behind its 16 entries.
std::vector<uint8_t> pal = raw.palette;
if (raw.depth == 4 && raw.paletteColors == 16) {
const size_t wanted = 16 * 4;
if (pal.size() > wanted) pal.resize(wanted);
}
// The CLUT reorder applies to 8-bit rasters only; 16-entry palettes are
// stored linearly. Keying this on the declared palette size instead of the
// pixel depth mangled every texture whose rasterFormat says otherwise.
std::vector<uint8_t> finalPal;
if (raw.depth == 8) finalPal = UnswizzlePalette(pal);
else finalPal = pal;
std::vector<uint8_t> rgba(w * h * 4, 255);
for (int i = 0; i < w * h; ++i) {
if ((size_t)i >= indices.size()) break;
uint8_t idx = indices[i];
if (raw.depth == 4) idx &= 0xF;
int pIdx = idx * 4;
if ((size_t)pIdx + 3 < finalPal.size()) {
rgba[i * 4 + 0] = finalPal[pIdx + 0];
rgba[i * 4 + 1] = finalPal[pIdx + 1];
rgba[i * 4 + 2] = finalPal[pIdx + 2];
rgba[i * 4 + 3] = std::min((int)finalPal[pIdx + 3] * 2, 255);
}
}
{
size_t partial = 0, total = (size_t)w * h;
for (size_t i = 0; i + 3 < rgba.size(); i += 4) {
const uint8_t a = rgba[i + 3];
if (a > 8 && a < 248) partial++;
}
size_t clear = 0;
for (size_t i = 0; i + 3 < rgba.size(); i += 4)
if (rgba[i + 3] <= 8) clear++;
raw.hasAlphaGradient = total && (partial * 100 / total) >= 15;
raw.hasTransparentTexels = total && (clear * 100 / total) >= 2;
}
UploadRGBA(raw, rgba, w, h);
}
namespace {
TextureSink g_sink;
TextureExistsFn g_exists;
}
void SetTextureSink(TextureSink sink) { g_sink = std::move(sink); }
void SetTextureExists(TextureExistsFn fn) { g_exists = std::move(fn); }
static void UploadRGBA(RawTexture& raw, const std::vector<uint8_t>& rgba, int w, int h) {
raw.width = w;
raw.height = h;
raw.pixels = rgba;
if (g_sink)
g_sink(raw, rgba, w, h);
}
namespace ClimaxEngine { namespace Platform { namespace PS2 {
void PS2TextureDecoder::LoadDictionary(const std::vector<uint8_t>& data, const std::vector<std::string>& allowedNames, bool fallback) {
const size_t sz = data.size();
if (sz < 128)
return;
const int MAGIC_OFFSET = 80;
size_t pos = 0;
while (pos + 12 <= sz) {
if (pos + 100 > sz) break; // not enough room for a minimal chunk + header
uint32_t type;
memcpy(&type, &data[pos], 4);
if (type == 0x15) {
// All bounds-checked: any out-of-range access skips the chunk.
uint32_t chunkSz;
memcpy(&chunkSz, &data[pos + 4], 4);
const size_t chunkEnd = pos + 12 + (size_t)chunkSz;
if (chunkEnd > sz || chunkSz < 64) { pos++; continue; }
size_t curr = pos + 32;
if (curr + 16 > sz) { pos++; continue; }
uint32_t sLen;
memcpy(&sLen, &data[curr + 4], 4);
if (sLen > 512 || curr + 12 + sLen + 12 > sz) { pos += 12 + chunkSz; continue; }
std::string tName = (sLen > 0) ? std::string((char*)&data[curr + 12], strnlen((char*)&data[curr + 12], sLen)) : "Unknown";
curr += 12 + sLen;
if (curr + 8 > sz) { pos += 12 + chunkSz; continue; }
memcpy(&sLen, &data[curr + 4], 4);
if (sLen > sz || curr + 12 + sLen + 24 + 56 > sz) { pos += 12 + chunkSz; continue; }
curr += 12 + sLen;
curr += 24;
// Raster header bounds check
if (curr + 56 > sz) { pos += 12 + chunkSz; continue; }
uint32_t w, h, d, rasterFormat;
memcpy(&w, &data[curr], 4);
memcpy(&h, &data[curr + 4], 4);
memcpy(&d, &data[curr + 8], 4);
memcpy(&rasterFormat, &data[curr + 12], 4);
uint32_t dSz, pSz;
memcpy(&dSz, &data[curr + 48], 4);
memcpy(&pSz, &data[curr + 52], 4);
// Sanity-check dimensions to reject garbage hits
if (w == 0 || h == 0 || w > 4096 || h > 4096 || d > 32
|| dSz > sz || pSz > sz) {
pos += 12 + chunkSz; continue;
}
curr += 76;
uint32_t filterAddressing = 0;
if (pos + 28 + 4 <= sz)
memcpy(&filterAddressing, &data[pos + 28], 4);
const uint32_t addrU = (filterAddressing >> 8) & 0xF;
const uint32_t addrV = (filterAddressing >> 12) & 0xF;
RawTexture t;
t.name = tName;
t.width = w;
t.height = h;
t.depth = d;
t.clampU = (addrU == 3);
t.clampV = (addrV == 3);
t.paletteColors = ((rasterFormat & 0xF000) == 0x4000) ? 16 : 256;
bool nameAllowed = fallback;
if (!fallback) {
for (const auto &allowed : allowedNames) {
if (sho_stricmp(t.name.c_str(), allowed.c_str()) == 0) {
nameAllowed = true;
break;
}
}
}
if (!nameAllowed) {
pos += 12 + chunkSz;
continue;
}
if (curr + MAGIC_OFFSET + (dSz - MAGIC_OFFSET) <= sz &&
dSz > (uint32_t)MAGIC_OFFSET) {
t.pixels.resize(dSz - MAGIC_OFFSET);
memcpy(t.pixels.data(), &data[curr + MAGIC_OFFSET],
dSz - MAGIC_OFFSET);
}
curr += dSz;
if (pSz > (uint32_t)MAGIC_OFFSET &&
curr + MAGIC_OFFSET + (pSz - MAGIC_OFFSET) <= sz) {
t.palette.resize(pSz - MAGIC_OFFSET);
memcpy(t.palette.data(), &data[curr + MAGIC_OFFSET],
pSz - MAGIC_OFFSET);
}
if (!t.pixels.empty() && !(g_exists && g_exists(t.name))) {
ProcessAndUploadTexture(t);
}
pos += 12 + chunkSz;
continue;
}
pos++;
}
}
} } }