Debian Codegraph @

Source/Core/VideoCommon/TextureDecoder_Generic.cpp

raw symbols SCIP
1 // Copyright 2008 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "VideoCommon/TextureDecoder.h"
5
6 #include <algorithm>
7 #include <cmath>
8
9 #include "Common/CPUDetect.h"
10 #include "Common/CommonTypes.h"
11 #include "Common/Swap.h"
12
13 #include "VideoCommon/LookUpTables.h"
14 #include "VideoCommon/TextureDecoder_Util.h"
15 #include "VideoCommon/VideoConfig.h"
16
17 // GameCube/Wii texture decoder
18
19 // Decodes all known GameCube/Wii texture formats.
20 // by ector
21
22 static inline u32 DecodePixel_IA8(u16 val)
23 {
24 int a = val & 0xFF;
25 int i = val >> 8;
26 return i | (i << 8) | (i << 16) | (a << 24);
27 }
28
29 static inline u32 DecodePixel_RGB565(u16 val)
30 {
31 int r, g, b, a;
32 r = Convert5To8((val >> 11) & 0x1f);
33 g = Convert6To8((val >> 5) & 0x3f);
34 b = Convert5To8((val) & 0x1f);
35 a = 0xFF;
36 return r | (g << 8) | (b << 16) | (a << 24);
37 }
38
39 static inline u32 DecodePixel_RGB5A3(u16 val)
40 {
41 int r, g, b, a;
42 if ((val & 0x8000))
43 {
44 r = Convert5To8((val >> 10) & 0x1f);
45 g = Convert5To8((val >> 5) & 0x1f);
46 b = Convert5To8((val) & 0x1f);
47 a = 0xFF;
48 }
49 else
50 {
51 a = Convert3To8((val >> 12) & 0x7);
52 r = Convert4To8((val >> 8) & 0xf);
53 g = Convert4To8((val >> 4) & 0xf);
54 b = Convert4To8((val) & 0xf);
55 }
56 return r | (g << 8) | (b << 16) | (a << 24);
57 }
58
59 static inline u32 DecodePixel_Paletted(u16 pixel, TLUTFormat tlutfmt)
60 {
61 switch (tlutfmt)
62 {
63 case TLUTFormat::IA8:
64 return DecodePixel_IA8(pixel);
65 case TLUTFormat::RGB565:
66 return DecodePixel_RGB565(Common::swap16(pixel));
67 case TLUTFormat::RGB5A3:
68 return DecodePixel_RGB5A3(Common::swap16(pixel));
69 default:
70 return 0;
71 }
72 }
73
74 static inline void DecodeBytes_C4(u32* dst, const u8* src, const u8* tlut_, TLUTFormat tlutfmt)
75 {
76 const u16* tlut = (u16*)tlut_;
77 for (int x = 0; x < 4; x++)
78 {
79 u8 val = src[x];
80 *dst++ = DecodePixel_Paletted(tlut[val >> 4], tlutfmt);
81 *dst++ = DecodePixel_Paletted(tlut[val & 0xF], tlutfmt);
82 }
83 }
84
85 static inline void DecodeBytes_C8(u32* dst, const u8* src, const u8* tlut_, TLUTFormat tlutfmt)
86 {
87 const u16* tlut = (u16*)tlut_;
88 for (int x = 0; x < 8; x++)
89 {
90 u8 val = src[x];
91 *dst++ = DecodePixel_Paletted(tlut[val], tlutfmt);
92 }
93 }
94
95 static inline void DecodeBytes_C14X2(u32* dst, const u16* src, const u8* tlut_, TLUTFormat tlutfmt)
96 {
97 const u16* tlut = (u16*)tlut_;
98 for (int x = 0; x < 4; x++)
99 {
100 u16 val = Common::swap16(src[x]);
101 *dst++ = DecodePixel_Paletted(tlut[(val & 0x3FFF)], tlutfmt);
102 }
103 }
104
105 static inline void DecodeBytes_IA4(u32* dst, const u8* src)
106 {
107 for (int x = 0; x < 8; x++)
108 {
109 const u8 val = src[x];
110 u8 a = Convert4To8(val >> 4);
111 u8 l = Convert4To8(val & 0xF);
112 dst[x] = (a << 24) | l << 16 | l << 8 | l;
113 }
114 }
115
116 static inline void DecodeBytes_RGB5A3(u32* dst, const u16* src)
117 {
118 for (int x = 0; x < 4; x++)
119 dst[x] = DecodePixel_RGB5A3(Common::swap16(src[x]));
120 }
121
122 static inline void DecodeBytes_RGBA8(u32* dst, const u16* src, const u16* src2)
123 {
124 for (int x = 0; x < 4; x++)
125 dst[x] = ((src[x] & 0xFF) << 24) | ((src[x] & 0xFF00) >> 8) | (src2[x] << 8);
126 }
127
128 static void DecodeDXTBlock(u32* dst, const DXTBlock* src, int pitch)
129 {
130 // S3TC Decoder (Note: GCN decodes differently from PC so we can't use native support)
131 // Needs more speed.
132 u16 c1 = Common::swap16(src->color1);
133 u16 c2 = Common::swap16(src->color2);
134 int blue1 = Convert5To8(c1 & 0x1F);
135 int blue2 = Convert5To8(c2 & 0x1F);
136 int green1 = Convert6To8((c1 >> 5) & 0x3F);
137 int green2 = Convert6To8((c2 >> 5) & 0x3F);
138 int red1 = Convert5To8((c1 >> 11) & 0x1F);
139 int red2 = Convert5To8((c2 >> 11) & 0x1F);
140 int colors[4];
141 colors[0] = MakeRGBA(red1, green1, blue1, 255);
142 colors[1] = MakeRGBA(red2, green2, blue2, 255);
143 if (c1 > c2)
144 {
145 colors[2] =
146 MakeRGBA(DXTBlend(red2, red1), DXTBlend(green2, green1), DXTBlend(blue2, blue1), 255);
147 colors[3] =
148 MakeRGBA(DXTBlend(red1, red2), DXTBlend(green1, green2), DXTBlend(blue1, blue2), 255);
149 }
150 else
151 {
152 // color[3] is the same as color[2] (average of both colors), but transparent.
153 // This differs from DXT1 where color[3] is transparent black.
154 colors[2] = MakeRGBA((red1 + red2) / 2, (green1 + green2) / 2, (blue1 + blue2) / 2, 255);
155 colors[3] = MakeRGBA((red1 + red2) / 2, (green1 + green2) / 2, (blue1 + blue2) / 2, 0);
156 }
157
158 for (int y = 0; y < 4; y++)
159 {
160 int val = src->lines[y];
161 for (int x = 0; x < 4; x++)
162 {
163 dst[x] = colors[(val >> 6) & 3];
164 val <<= 2;
165 }
166 dst += pitch;
167 }
168 }
169
170 // JSD 01/06/11:
171 // TODO: we really should ensure BOTH the source and destination addresses are aligned to 16-byte
172 // boundaries to
173 // squeeze out a little more performance. _mm_loadu_si128/_mm_storeu_si128 is slower than
174 // _mm_load_si128/_mm_store_si128
175 // because they work on unaligned addresses. The processor is free to make the assumption that
176 // addresses are multiples
177 // of 16 in the aligned case.
178 // TODO: complete SSE2 optimization of less often used texture formats.
179 // TODO: refactor algorithms using _mm_loadl_epi64 unaligned loads to prefer 128-bit aligned loads.
180
181 void _TexDecoder_DecodeImpl(u32* dst, const u8* src, int width, int height, TextureFormat texformat,
182 const u8* tlut, TLUTFormat tlutfmt)
183 {
184 const int Wsteps4 = (width + 3) / 4;
185 const int Wsteps8 = (width + 7) / 8;
186
187 switch (texformat)
188 {
189 case TextureFormat::C4:
190 for (int y = 0; y < height; y += 8)
191 for (int x = 0, yStep = (y / 8) * Wsteps8; x < width; x += 8, yStep++)
192 for (int iy = 0, xStep = 8 * yStep; iy < 8; iy++, xStep++)
193 DecodeBytes_C4(dst + (y + iy) * width + x, src + 4 * xStep, tlut, tlutfmt);
194 break;
195 case TextureFormat::I4:
196 {
197 // Reference C implementation:
198 for (int y = 0; y < height; y += 8)
199 for (int x = 0; x < width; x += 8)
200 for (int iy = 0; iy < 8; iy++, src += 4)
201 for (int ix = 0; ix < 4; ix++)
202 {
203 int val = src[ix];
204 u8 i1 = Convert4To8(val >> 4);
205 u8 i2 = Convert4To8(val & 0xF);
206 memset(dst + (y + iy) * width + x + ix * 2, i1, 4);
207 memset(dst + (y + iy) * width + x + ix * 2 + 1, i2, 4);
208 }
209 }
210 break;
211 case TextureFormat::I8: // speed critical
212 {
213 // Reference C implementation
214 for (int y = 0; y < height; y += 4)
215 for (int x = 0; x < width; x += 8)
216 for (int iy = 0; iy < 4; ++iy, src += 8)
217 {
218 u32* newdst = dst + (y + iy) * width + x;
219 const u8* newsrc = src;
220 u8 srcval;
221
222 srcval = (newsrc++)[0];
223 (newdst++)[0] = srcval | (srcval << 8) | (srcval << 16) | (srcval << 24);
224 srcval = (newsrc++)[0];
225 (newdst++)[0] = srcval | (srcval << 8) | (srcval << 16) | (srcval << 24);
226 srcval = (newsrc++)[0];
227 (newdst++)[0] = srcval | (srcval << 8) | (srcval << 16) | (srcval << 24);
228 srcval = (newsrc++)[0];
229 (newdst++)[0] = srcval | (srcval << 8) | (srcval << 16) | (srcval << 24);
230 srcval = (newsrc++)[0];
231 (newdst++)[0] = srcval | (srcval << 8) | (srcval << 16) | (srcval << 24);
232 srcval = (newsrc++)[0];
233 (newdst++)[0] = srcval | (srcval << 8) | (srcval << 16) | (srcval << 24);
234 srcval = (newsrc++)[0];
235 (newdst++)[0] = srcval | (srcval << 8) | (srcval << 16) | (srcval << 24);
236 srcval = newsrc[0];
237 newdst[0] = srcval | (srcval << 8) | (srcval << 16) | (srcval << 24);
238 }
239 }
240 break;
241 case TextureFormat::C8:
242 for (int y = 0; y < height; y += 4)
243 for (int x = 0, yStep = (y / 4) * Wsteps8; x < width; x += 8, yStep++)
244 for (int iy = 0, xStep = 4 * yStep; iy < 4; iy++, xStep++)
245 DecodeBytes_C8((u32*)dst + (y + iy) * width + x, src + 8 * xStep, tlut, tlutfmt);
246 break;
247 case TextureFormat::IA4:
248 {
249 for (int y = 0; y < height; y += 4)
250 for (int x = 0, yStep = (y / 4) * Wsteps8; x < width; x += 8, yStep++)
251 for (int iy = 0, xStep = 4 * yStep; iy < 4; iy++, xStep++)
252 DecodeBytes_IA4(dst + (y + iy) * width + x, src + 8 * xStep);
253 }
254 break;
255 case TextureFormat::IA8:
256 {
257 // Reference C implementation:
258 for (int y = 0; y < height; y += 4)
259 for (int x = 0; x < width; x += 4)
260 for (int iy = 0; iy < 4; iy++, src += 8)
261 {
262 u32* ptr = dst + (y + iy) * width + x;
263 u16* s = (u16*)src;
264 ptr[0] = DecodePixel_IA8(s[0]);
265 ptr[1] = DecodePixel_IA8(s[1]);
266 ptr[2] = DecodePixel_IA8(s[2]);
267 ptr[3] = DecodePixel_IA8(s[3]);
268 }
269 }
270 break;
271 case TextureFormat::C14X2:
272 for (int y = 0; y < height; y += 4)
273 for (int x = 0, yStep = (y / 4) * Wsteps4; x < width; x += 4, yStep++)
274 for (int iy = 0, xStep = 4 * yStep; iy < 4; iy++, xStep++)
275 DecodeBytes_C14X2(dst + (y + iy) * width + x, (u16*)(src + 8 * xStep), tlut, tlutfmt);
276 break;
277 case TextureFormat::RGB565:
278 {
279 // Reference C implementation.
280 for (int y = 0; y < height; y += 4)
281 for (int x = 0; x < width; x += 4)
282 for (int iy = 0; iy < 4; iy++, src += 8)
283 {
284 u32* ptr = dst + (y + iy) * width + x;
285 u16* s = (u16*)src;
286 for (int j = 0; j < 4; j++)
287 *ptr++ = DecodePixel_RGB565(Common::swap16(*s++));
288 }
289 }
290 break;
291 case TextureFormat::RGB5A3:
292 {
293 // Reference C implementation:
294 for (int y = 0; y < height; y += 4)
295 for (int x = 0; x < width; x += 4)
296 for (int iy = 0; iy < 4; iy++, src += 8)
297 DecodeBytes_RGB5A3(dst + (y + iy) * width + x, (u16*)src);
298 }
299 break;
300 case TextureFormat::RGBA8: // speed critical
301 {
302 // Reference C implementation.
303 for (int y = 0; y < height; y += 4)
304 for (int x = 0; x < width; x += 4)
305 {
306 for (int iy = 0; iy < 4; iy++)
307 DecodeBytes_RGBA8(dst + (y + iy) * width + x, (u16*)src + 4 * iy,
308 (u16*)src + 4 * iy + 16);
309 src += 64;
310 }
311 }
312 break;
313 case TextureFormat::CMPR: // speed critical
314 // The metroid games use this format almost exclusively.
315 {
316 for (int y = 0; y < height; y += 8)
317 {
318 for (int x = 0; x < width; x += 8)
319 {
320 DecodeDXTBlock((u32*)dst + y * width + x, (DXTBlock*)src, width);
321 src += sizeof(DXTBlock);
322 DecodeDXTBlock((u32*)dst + y * width + x + 4, (DXTBlock*)src, width);
323 src += sizeof(DXTBlock);
324 DecodeDXTBlock((u32*)dst + (y + 4) * width + x, (DXTBlock*)src, width);
325 src += sizeof(DXTBlock);
326 DecodeDXTBlock((u32*)dst + (y + 4) * width + x + 4, (DXTBlock*)src, width);
327 src += sizeof(DXTBlock);
328 }
329 }
330 break;
331 }
332 case TextureFormat::XFB:
333 TexDecoder_DecodeXFB(reinterpret_cast<u8*>(dst), src, width, height, width * 2);
334 break;
335 }
336 }