Debian Codegraph @

Source/Core/VideoCommon/TextureDecoder_Common.cpp

raw symbols SCIP
1 // Copyright 2014 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include <algorithm>
5 #include <array>
6 #include <cstddef>
7 #include <span>
8
9 #include "Common/CommonTypes.h"
10 #include "Common/MsgHandler.h"
11 #include "Common/SpanUtils.h"
12 #include "Common/Swap.h"
13
14 #include "VideoCommon/LookUpTables.h"
15 #include "VideoCommon/TextureDecoder.h"
16 #include "VideoCommon/TextureDecoder_Util.h"
17 #include "VideoCommon/sfont.inc"
18
19 static bool TexFmt_Overlay_Enable = false;
20 static bool TexFmt_Overlay_Center = false;
21
22 // TRAM
23 // STATE_TO_SAVE
24 alignas(16) std::array<u8, TMEM_SIZE> s_tex_mem;
25
26 int TexDecoder_GetTexelSizeInNibbles(TextureFormat format)
27 {
28 switch (format)
29 {
30 // 4-bit formats
31 case TextureFormat::I4:
32 case TextureFormat::C4:
33 return 1;
34 // 8-bit formats
35 case TextureFormat::I8:
36 case TextureFormat::IA4:
37 case TextureFormat::C8:
38 return 2;
39 // 16-bit formats
40 case TextureFormat::IA8:
41 case TextureFormat::RGB565:
42 case TextureFormat::RGB5A3:
43 case TextureFormat::C14X2:
44 return 4;
45 // 32-bit formats
46 case TextureFormat::RGBA8:
47 return 8;
48 // Compressed format
49 case TextureFormat::CMPR:
50 return 1;
51 // Special formats
52 case TextureFormat::XFB:
53 return 4;
54 default:
55 PanicAlertFmt("Invalid Texture Format {}! (GetTexelSizeInNibbles)", format);
56 return 1;
57 }
58 }
59
60 int TexDecoder_GetTextureSizeInBytes(int width, int height, TextureFormat format)
61 {
62 return (width * height * TexDecoder_GetTexelSizeInNibbles(format)) / 2;
63 }
64
65 int TexDecoder_GetBlockWidthInTexels(TextureFormat format)
66 {
67 switch (format)
68 {
69 // 4-bit formats
70 case TextureFormat::I4:
71 case TextureFormat::C4:
72 return 8;
73 // 8-bit formats
74 case TextureFormat::I8:
75 case TextureFormat::IA4:
76 case TextureFormat::C8:
77 return 8;
78 // 16-bit formats
79 case TextureFormat::IA8:
80 case TextureFormat::RGB565:
81 case TextureFormat::RGB5A3:
82 case TextureFormat::C14X2:
83 return 4;
84 // 32-bit formats
85 case TextureFormat::RGBA8:
86 return 4;
87 // Compressed format
88 case TextureFormat::CMPR:
89 return 8;
90 // Special formats
91 case TextureFormat::XFB:
92 return 16;
93 default:
94 PanicAlertFmt("Invalid Texture Format {}! (GetBlockWidthInTexels)", format);
95 return 8;
96 }
97 }
98
99 int TexDecoder_GetBlockHeightInTexels(TextureFormat format)
100 {
101 switch (format)
102 {
103 // 4-bit formats
104 case TextureFormat::I4:
105 case TextureFormat::C4:
106 return 8;
107 // 8-bit formats
108 case TextureFormat::I8:
109 case TextureFormat::IA4:
110 case TextureFormat::C8:
111 return 4;
112 // 16-bit formats
113 case TextureFormat::IA8:
114 case TextureFormat::RGB565:
115 case TextureFormat::RGB5A3:
116 case TextureFormat::C14X2:
117 return 4;
118 // 32-bit formats
119 case TextureFormat::RGBA8:
120 return 4;
121 // Compressed format
122 case TextureFormat::CMPR:
123 return 8;
124 // Special formats
125 case TextureFormat::XFB:
126 return 1;
127 default:
128 PanicAlertFmt("Invalid Texture Format {}! (GetBlockHeightInTexels)", format);
129 return 4;
130 }
131 }
132
133 int TexDecoder_GetEFBCopyBlockWidthInTexels(EFBCopyFormat format)
134 {
135 switch (format)
136 {
137 // 4-bit formats
138 case EFBCopyFormat::R4:
139 return 8;
140 // 8-bit formats
141 case EFBCopyFormat::RA4:
142 case EFBCopyFormat::A8:
143 case EFBCopyFormat::R8_0x1:
144 case EFBCopyFormat::R8:
145 case EFBCopyFormat::G8:
146 case EFBCopyFormat::B8:
147 return 8;
148 // 16-bit formats
149 case EFBCopyFormat::RA8:
150 case EFBCopyFormat::RGB565:
151 case EFBCopyFormat::RGB5A3:
152 case EFBCopyFormat::RG8:
153 case EFBCopyFormat::GB8:
154 return 4;
155 // 32-bit formats
156 case EFBCopyFormat::RGBA8:
157 return 4;
158 // Special formats
159 case EFBCopyFormat::XFB:
160 return 16;
161 default:
162 PanicAlertFmt("Invalid EFB Copy Format {}! (GetEFBCopyBlockWidthInTexels)", format);
163 return 8;
164 }
165 }
166
167 int TexDecoder_GetEFBCopyBlockHeightInTexels(EFBCopyFormat format)
168 {
169 switch (format)
170 {
171 // 4-bit formats
172 case EFBCopyFormat::R4:
173 return 8;
174 // 8-bit formats
175 case EFBCopyFormat::RA4:
176 case EFBCopyFormat::A8:
177 case EFBCopyFormat::R8_0x1:
178 case EFBCopyFormat::R8:
179 case EFBCopyFormat::G8:
180 case EFBCopyFormat::B8:
181 return 4;
182 // 16-bit formats
183 case EFBCopyFormat::RA8:
184 case EFBCopyFormat::RGB565:
185 case EFBCopyFormat::RGB5A3:
186 case EFBCopyFormat::RG8:
187 case EFBCopyFormat::GB8:
188 return 4;
189 // 32-bit formats
190 case EFBCopyFormat::RGBA8:
191 return 4;
192 // Special formats
193 case EFBCopyFormat::XFB:
194 return 1;
195 default:
196 PanicAlertFmt("Invalid EFB Copy Format {}! (GetEFBCopyBlockHeightInTexels)", format);
197 return 4;
198 }
199 }
200
201 // returns bytes
202 int TexDecoder_GetPaletteSize(TextureFormat format)
203 {
204 switch (format)
205 {
206 case TextureFormat::C4:
207 return 16 * 2;
208 case TextureFormat::C8:
209 return 256 * 2;
210 case TextureFormat::C14X2:
211 return 16384 * 2;
212 default:
213 return 0;
214 }
215 }
216
217 // Get the "in memory" texture format of an EFB copy's format.
218 // With the exception of c4/c8/c14 paletted texture formats (which are handled elsewhere)
219 // this is the format the game should be using when it is drawing an EFB copy back.
220 TextureFormat TexDecoder_GetEFBCopyBaseFormat(EFBCopyFormat format)
221 {
222 switch (format)
223 {
224 case EFBCopyFormat::R4:
225 return TextureFormat::I4;
226 case EFBCopyFormat::A8:
227 case EFBCopyFormat::R8_0x1:
228 case EFBCopyFormat::R8:
229 case EFBCopyFormat::G8:
230 case EFBCopyFormat::B8:
231 return TextureFormat::I8;
232 case EFBCopyFormat::RA4:
233 return TextureFormat::IA4;
234 case EFBCopyFormat::RA8:
235 case EFBCopyFormat::RG8:
236 case EFBCopyFormat::GB8:
237 return TextureFormat::IA8;
238 case EFBCopyFormat::RGB565:
239 return TextureFormat::RGB565;
240 case EFBCopyFormat::RGB5A3:
241 return TextureFormat::RGB5A3;
242 case EFBCopyFormat::RGBA8:
243 return TextureFormat::RGBA8;
244 case EFBCopyFormat::XFB:
245 return TextureFormat::XFB;
246 default:
247 PanicAlertFmt("Invalid EFB Copy Format {}! (GetEFBCopyBaseFormat)", format);
248 return static_cast<TextureFormat>(format);
249 }
250 }
251
252 void TexDecoder_SetTexFmtOverlayOptions(bool enable, bool center)
253 {
254 TexFmt_Overlay_Enable = enable;
255 TexFmt_Overlay_Center = center;
256 }
257
258 static void TexDecoder_DrawOverlay(u8* dst, int width, int height, TextureFormat texformat)
259 {
260 int w = std::min(width, 40);
261 int h = std::min(height, 10);
262
263 int xoff = (width - w) >> 1;
264 int yoff = (height - h) >> 1;
265
266 if (!TexFmt_Overlay_Center)
267 {
268 xoff = 0;
269 yoff = 0;
270 }
271
272 const auto fmt_str = fmt::to_string(texformat);
273 for (char ch : fmt_str)
274 {
275 int xcnt = 0;
276 int nchar = sfont_map[static_cast<u8>(ch)];
277
278 const unsigned char* ptr = sfont_raw[nchar]; // each char is up to 9x10
279
280 for (int x = 0; x < 9; x++)
281 {
282 if (ptr[x] == 0x78)
283 break;
284 xcnt++;
285 }
286
287 for (int y = 0; y < 10; y++)
288 {
289 for (int x = 0; x < xcnt; x++)
290 {
291 int* dtp = (int*)dst;
292 dtp[(y + yoff) * width + x + xoff] = ptr[x] ? 0xFFFFFFFF : 0xFF000000;
293 }
294 ptr += 9;
295 }
296 xoff += xcnt;
297 }
298 }
299
300 void TexDecoder_Decode(u8* dst, const u8* src, int width, int height, TextureFormat texformat,
301 const u8* tlut, TLUTFormat tlutfmt)
302 {
303 _TexDecoder_DecodeImpl((u32*)dst, src, width, height, texformat, tlut, tlutfmt);
304
305 if (TexFmt_Overlay_Enable)
306 TexDecoder_DrawOverlay(dst, width, height, texformat);
307 }
308
309 static inline u32 DecodePixel_IA8(u16 val)
310 {
311 int a = val & 0xFF;
312 int i = val >> 8;
313 return i | (i << 8) | (i << 16) | (a << 24);
314 }
315
316 static inline u32 DecodePixel_RGB565(u16 val)
317 {
318 int r, g, b, a;
319 r = Convert5To8((val >> 11) & 0x1f);
320 g = Convert6To8((val >> 5) & 0x3f);
321 b = Convert5To8((val) & 0x1f);
322 a = 0xFF;
323 return r | (g << 8) | (b << 16) | (a << 24);
324 }
325
326 static inline u32 DecodePixel_RGB5A3(u16 val)
327 {
328 int r, g, b, a;
329 if ((val & 0x8000))
330 {
331 r = Convert5To8((val >> 10) & 0x1f);
332 g = Convert5To8((val >> 5) & 0x1f);
333 b = Convert5To8((val) & 0x1f);
334 a = 0xFF;
335 }
336 else
337 {
338 a = Convert3To8((val >> 12) & 0x7);
339 r = Convert4To8((val >> 8) & 0xf);
340 g = Convert4To8((val >> 4) & 0xf);
341 b = Convert4To8((val) & 0xf);
342 }
343 return r | (g << 8) | (b << 16) | (a << 24);
344 }
345
346 static inline u32 DecodePixel_Paletted(u16 pixel, TLUTFormat tlutfmt)
347 {
348 switch (tlutfmt)
349 {
350 case TLUTFormat::IA8:
351 return DecodePixel_IA8(pixel);
352 case TLUTFormat::RGB565:
353 return DecodePixel_RGB565(Common::swap16(pixel));
354 case TLUTFormat::RGB5A3:
355 return DecodePixel_RGB5A3(Common::swap16(pixel));
356 default:
357 return 0;
358 }
359 }
360
361 void TexDecoder_DecodeTexel(u8* dst, std::span<const u8> src, int s, int t, int imageWidth,
362 TextureFormat texformat, std::span<const u8> tlut_, TLUTFormat tlutfmt)
363 {
364 /* General formula for computing texture offset
365 //
366 u16 sBlk = s / blockWidth;
367 u16 tBlk = t / blockHeight;
368 u16 widthBlks = (width / blockWidth) + 1;
369 u32 base = (tBlk * widthBlks + sBlk) * blockWidth * blockHeight;
370 u16 blkS = s & (blockWidth - 1);
371 u16 blkT = t & (blockHeight - 1);
372 u32 blkOff = blkT * blockWidth + blkS;
373 */
374
375 switch (texformat)
376 {
377 case TextureFormat::C4:
378 {
379 u16 sBlk = s >> 3;
380 u16 tBlk = t >> 3;
381 u16 widthBlks = (imageWidth >> 3) + 1;
382 u32 base = (tBlk * widthBlks + sBlk) << 5;
383 u16 blkS = s & 7;
384 u16 blkT = t & 7;
385 u32 blkOff = (blkT << 3) + blkS;
386
387 int rs = (blkOff & 1) ? 0 : 4;
388 u32 offset = base + (blkOff >> 1);
389
390 u8 val = (Common::SafeSpanRead<u8>(src, offset) >> rs) & 0xF;
391 u16 pixel = Common::SafeSpanRead<u16>(tlut_, sizeof(u16) * val);
392
393 *((u32*)dst) = DecodePixel_Paletted(pixel, tlutfmt);
394 }
395 break;
396 case TextureFormat::I4:
397 {
398 u16 sBlk = s >> 3;
399 u16 tBlk = t >> 3;
400 u16 widthBlks = (imageWidth >> 3) + 1;
401 u32 base = (tBlk * widthBlks + sBlk) << 5;
402 u16 blkS = s & 7;
403 u16 blkT = t & 7;
404 u32 blkOff = (blkT << 3) + blkS;
405
406 int rs = (blkOff & 1) ? 0 : 4;
407 u32 offset = base + (blkOff >> 1);
408
409 u8 val = (Common::SafeSpanRead<u8>(src, offset) >> rs) & 0xF;
410 val = Convert4To8(val);
411 dst[0] = val;
412 dst[1] = val;
413 dst[2] = val;
414 dst[3] = val;
415 }
416 break;
417 case TextureFormat::I8:
418 {
419 u16 sBlk = s >> 3;
420 u16 tBlk = t >> 2;
421 u16 widthBlks = (imageWidth >> 3) + 1;
422 u32 base = (tBlk * widthBlks + sBlk) << 5;
423 u16 blkS = s & 7;
424 u16 blkT = t & 3;
425 u32 blkOff = (blkT << 3) + blkS;
426
427 u8 val = Common::SafeSpanRead<u8>(src, base + blkOff);
428 dst[0] = val;
429 dst[1] = val;
430 dst[2] = val;
431 dst[3] = val;
432 }
433 break;
434 case TextureFormat::C8:
435 {
436 u16 sBlk = s >> 3;
437 u16 tBlk = t >> 2;
438 u16 widthBlks = (imageWidth >> 3) + 1;
439 u32 base = (tBlk * widthBlks + sBlk) << 5;
440 u16 blkS = s & 7;
441 u16 blkT = t & 3;
442 u32 blkOff = (blkT << 3) + blkS;
443
444 u8 val = Common::SafeSpanRead<u8>(src, base + blkOff);
445 u16 pixel = Common::SafeSpanRead<u16>(tlut_, sizeof(u16) * val);
446
447 *((u32*)dst) = DecodePixel_Paletted(pixel, tlutfmt);
448 }
449 break;
450 case TextureFormat::IA4:
451 {
452 u16 sBlk = s >> 3;
453 u16 tBlk = t >> 2;
454 u16 widthBlks = (imageWidth >> 3) + 1;
455 u32 base = (tBlk * widthBlks + sBlk) << 5;
456 u16 blkS = s & 7;
457 u16 blkT = t & 3;
458 u32 blkOff = (blkT << 3) + blkS;
459
460 u8 val = Common::SafeSpanRead<u8>(src, base + blkOff);
461 const u8 a = Convert4To8(val >> 4);
462 const u8 l = Convert4To8(val & 0xF);
463 dst[0] = l;
464 dst[1] = l;
465 dst[2] = l;
466 dst[3] = a;
467 }
468 break;
469 case TextureFormat::IA8:
470 {
471 u16 sBlk = s >> 2;
472 u16 tBlk = t >> 2;
473 u16 widthBlks = (imageWidth >> 2) + 1;
474 u32 base = (tBlk * widthBlks + sBlk) << 4;
475 u16 blkS = s & 3;
476 u16 blkT = t & 3;
477 u32 blkOff = (blkT << 2) + blkS;
478
479 u32 offset = (base + blkOff) << 1;
480 u16 val = Common::SafeSpanRead<u16>(src, offset);
481
482 *((u32*)dst) = DecodePixel_IA8(val);
483 }
484 break;
485 case TextureFormat::C14X2:
486 {
487 u16 sBlk = s >> 2;
488 u16 tBlk = t >> 2;
489 u16 widthBlks = (imageWidth >> 2) + 1;
490 u32 base = (tBlk * widthBlks + sBlk) << 4;
491 u16 blkS = s & 3;
492 u16 blkT = t & 3;
493 u32 blkOff = (blkT << 2) + blkS;
494
495 u32 offset = (base + blkOff) << 1;
496 u16 val = Common::swap16(Common::SafeSpanRead<u16>(src, offset)) & 0x3FFF;
497 u16 pixel = Common::SafeSpanRead<u16>(tlut_, sizeof(u16) * val);
498
499 *((u32*)dst) = DecodePixel_Paletted(pixel, tlutfmt);
500 }
501 break;
502 case TextureFormat::RGB565:
503 {
504 u16 sBlk = s >> 2;
505 u16 tBlk = t >> 2;
506 u16 widthBlks = (imageWidth >> 2) + 1;
507 u32 base = (tBlk * widthBlks + sBlk) << 4;
508 u16 blkS = s & 3;
509 u16 blkT = t & 3;
510 u32 blkOff = (blkT << 2) + blkS;
511
512 u32 offset = (base + blkOff) << 1;
513 u16 val = Common::SafeSpanRead<u16>(src, offset);
514
515 *((u32*)dst) = DecodePixel_RGB565(Common::swap16(val));
516 }
517 break;
518 case TextureFormat::RGB5A3:
519 {
520 u16 sBlk = s >> 2;
521 u16 tBlk = t >> 2;
522 u16 widthBlks = (imageWidth >> 2) + 1;
523 u32 base = (tBlk * widthBlks + sBlk) << 4;
524 u16 blkS = s & 3;
525 u16 blkT = t & 3;
526 u32 blkOff = (blkT << 2) + blkS;
527
528 u32 offset = (base + blkOff) << 1;
529 u16 val = Common::SafeSpanRead<u16>(src, offset);
530
531 *((u32*)dst) = DecodePixel_RGB5A3(Common::swap16(val));
532 }
533 break;
534 case TextureFormat::RGBA8:
535 {
536 u16 sBlk = s >> 2;
537 u16 tBlk = t >> 2;
538 u16 widthBlks = (imageWidth >> 2) + 1;
539 u32 base = (tBlk * widthBlks + sBlk) << 5; // shift by 5 is correct
540 u16 blkS = s & 3;
541 u16 blkT = t & 3;
542 u32 blkOff = (blkT << 2) + blkS;
543
544 u32 offset = (base + blkOff) << 1;
545
546 dst[3] = Common::SafeSpanRead<u8>(src, offset);
547 dst[0] = Common::SafeSpanRead<u8>(src, offset + 1);
548 dst[1] = Common::SafeSpanRead<u8>(src, offset + 32);
549 dst[2] = Common::SafeSpanRead<u8>(src, offset + 33);
550 }
551 break;
552 case TextureFormat::CMPR:
553 {
554 u16 sDxt = s >> 2;
555 u16 tDxt = t >> 2;
556
557 u16 sBlk = sDxt >> 1;
558 u16 tBlk = tDxt >> 1;
559 u16 widthBlks = (imageWidth >> 3) + 1;
560 u32 base = (tBlk * widthBlks + sBlk) << 2;
561 u16 blkS = sDxt & 1;
562 u16 blkT = tDxt & 1;
563 u32 blkOff = (blkT << 1) + blkS;
564
565 u32 offset = (base + blkOff) << 3;
566
567 DXTBlock dxtBlock = Common::SafeSpanRead<DXTBlock>(src, offset);
568
569 u16 c1 = Common::swap16(dxtBlock.color1);
570 u16 c2 = Common::swap16(dxtBlock.color2);
571 int blue1 = Convert5To8(c1 & 0x1F);
572 int blue2 = Convert5To8(c2 & 0x1F);
573 int green1 = Convert6To8((c1 >> 5) & 0x3F);
574 int green2 = Convert6To8((c2 >> 5) & 0x3F);
575 int red1 = Convert5To8((c1 >> 11) & 0x1F);
576 int red2 = Convert5To8((c2 >> 11) & 0x1F);
577
578 u16 ss = s & 3;
579 u16 tt = t & 3;
580
581 int colorSel = dxtBlock.lines[tt];
582 int rs = 6 - (ss << 1);
583 colorSel = (colorSel >> rs) & 3;
584 colorSel |= c1 > c2 ? 0 : 4;
585
586 u32 color = 0;
587
588 switch (colorSel)
589 {
590 case 0:
591 case 4:
592 color = MakeRGBA(red1, green1, blue1, 255);
593 break;
594 case 1:
595 case 5:
596 color = MakeRGBA(red2, green2, blue2, 255);
597 break;
598 case 2:
599 color = MakeRGBA(DXTBlend(red2, red1), DXTBlend(green2, green1), DXTBlend(blue2, blue1), 255);
600 break;
601 case 3:
602 color = MakeRGBA(DXTBlend(red1, red2), DXTBlend(green1, green2), DXTBlend(blue1, blue2), 255);
603 break;
604 case 6:
605 color = MakeRGBA((red1 + red2) / 2, (green1 + green2) / 2, (blue1 + blue2) / 2, 255);
606 break;
607 case 7:
608 // color[3] is the same as color[2] (average of both colors), but transparent.
609 // This differs from DXT1 where color[3] is transparent black.
610 color = MakeRGBA((red1 + red2) / 2, (green1 + green2) / 2, (blue1 + blue2) / 2, 0);
611 break;
612 default:
613 color = 0;
614 break;
615 }
616
617 *((u32*)dst) = color;
618 }
619 break;
620 case TextureFormat::XFB:
621 {
622 size_t offset = (t * imageWidth + (s & (~1))) * 2;
623
624 // We do this one color sample (aka 2 RGB pixles) at a time
625 int Y = int((s & 1) == 0 ? src[offset] : src[offset + 2]) - 16;
626 int U = int(src[offset + 1]) - 128;
627 int V = int(src[offset + 3]) - 128;
628
629 // We do the inverse BT.601 conversion for YCbCr to RGB
630 // http://www.equasys.de/colorconversion.html#YCbCr-RGBColorFormatConversion
631 // TODO: Use more precise numbers for this conversion (although on real hardware, the XFB isn't
632 // in a real texture format, so does this conversion actually ever happen?)
633 u8 R = std::clamp(int(1.164f * Y + 1.596f * V), 0, 255);
634 u8 G = std::clamp(int(1.164f * Y - 0.392f * U - 0.813f * V), 0, 255);
635 u8 B = std::clamp(int(1.164f * Y + 2.017f * U), 0, 255);
636 dst[t * imageWidth + s] = 0xff000000 | B << 16 | G << 8 | R;
637 }
638 break;
639 }
640 }
641
642 void TexDecoder_DecodeTexelRGBA8FromTmem(u8* dst, std::span<const u8> src_ar,
643 std::span<const u8> src_gb, int s, int t, int imageWidth)
644 {
645 u16 sBlk = s >> 2;
646 u16 tBlk = t >> 2;
647 u16 widthBlks =
648 (imageWidth >> 2) + 1; // TODO: Looks wrong. Shouldn't this be ((imageWidth-1)>>2)+1 ?
649 u32 base_ar = (tBlk * widthBlks + sBlk) << 4;
650 u32 base_gb = (tBlk * widthBlks + sBlk) << 4;
651 u16 blkS = s & 3;
652 u16 blkT = t & 3;
653 u32 blk_off = (blkT << 2) + blkS;
654
655 u32 offset_ar = (base_ar + blk_off) << 1;
656 u32 offset_gb = (base_gb + blk_off) << 1;
657
658 dst[3] = Common::SafeSpanRead<u8>(src_ar, offset_ar); // A
659 dst[0] = Common::SafeSpanRead<u8>(src_ar, offset_ar + 1); // R
660 dst[1] = Common::SafeSpanRead<u8>(src_gb, offset_gb); // G
661 dst[2] = Common::SafeSpanRead<u8>(src_gb, offset_gb + 1); // B
662 }
663
664 void TexDecoder_DecodeTexelRGBA8FromTmem(u8* dst, const u8* src_ar, const u8* src_gb, int s, int t,
665 int imageWidth)
666 {
667 u16 sBlk = s >> 2;
668 u16 tBlk = t >> 2;
669 u16 widthBlks =
670 (imageWidth >> 2) + 1; // TODO: Looks wrong. Shouldn't this be ((imageWidth-1)>>2)+1 ?
671 u32 base_ar = (tBlk * widthBlks + sBlk) << 4;
672 u32 base_gb = (tBlk * widthBlks + sBlk) << 4;
673 u16 blkS = s & 3;
674 u16 blkT = t & 3;
675 u32 blk_off = (blkT << 2) + blkS;
676
677 u32 offset_ar = (base_ar + blk_off) << 1;
678 u32 offset_gb = (base_gb + blk_off) << 1;
679 const u8* val_addr_ar = src_ar + offset_ar;
680 const u8* val_addr_gb = src_gb + offset_gb;
681
682 dst[3] = val_addr_ar[0]; // A
683 dst[0] = val_addr_ar[1]; // R
684 dst[1] = val_addr_gb[0]; // G
685 dst[2] = val_addr_gb[1]; // B
686 }
687
688 void TexDecoder_DecodeRGBA8FromTmem(u8* dst, const u8* src_ar, const u8* src_gb, int width,
689 int height)
690 {
691 // TODO for someone who cares: Make this less slow!
692 for (int y = 0; y < height; ++y)
693 {
694 for (int x = 0; x < width; ++x)
695 {
696 TexDecoder_DecodeTexelRGBA8FromTmem(dst, src_ar, src_gb, x, y, width - 1);
697 dst += 4;
698 }
699 }
700 }
701
702 void TexDecoder_DecodeXFB(u8* dst, const u8* src, u32 width, u32 height, u32 stride)
703 {
704 const u8* src_ptr = src;
705 u8* dst_ptr = dst;
706
707 for (u32 y = 0; y < height; y++)
708 {
709 const u8* row_ptr = src_ptr;
710 for (u32 x = 0; x < width; x += 2)
711 {
712 // We do this one color sample (aka 2 RGB pixels) at a time
713 int Y1 = int(*(row_ptr++)) - 16;
714 int U = int(*(row_ptr++)) - 128;
715 int Y2 = int(*(row_ptr++)) - 16;
716 int V = int(*(row_ptr++)) - 128;
717
718 // We do the inverse BT.601 conversion for YCbCr to RGB
719 // http://www.equasys.de/colorconversion.html#YCbCr-RGBColorFormatConversion
720 // TODO: Use more precise numbers for this conversion (although on real hardware, the XFB
721 // isn't in a real texture format, so does this conversion actually ever happen?)
722 u8 R1 = static_cast<u8>(std::clamp(int(1.164f * Y1 + 1.596f * V), 0, 255));
723 u8 G1 = static_cast<u8>(std::clamp(int(1.164f * Y1 - 0.392f * U - 0.813f * V), 0, 255));
724 u8 B1 = static_cast<u8>(std::clamp(int(1.164f * Y1 + 2.017f * U), 0, 255));
725
726 u8 R2 = static_cast<u8>(std::clamp(int(1.164f * Y2 + 1.596f * V), 0, 255));
727 u8 G2 = static_cast<u8>(std::clamp(int(1.164f * Y2 - 0.392f * U - 0.813f * V), 0, 255));
728 u8 B2 = static_cast<u8>(std::clamp(int(1.164f * Y2 + 2.017f * U), 0, 255));
729
730 u32 rgba = 0xff000000 | B1 << 16 | G1 << 8 | R1;
731 std::memcpy(dst_ptr, &rgba, sizeof(rgba));
732 dst_ptr += sizeof(rgba);
733 rgba = 0xff000000 | B2 << 16 | G2 << 8 | R2;
734 std::memcpy(dst_ptr, &rgba, sizeof(rgba));
735 dst_ptr += sizeof(rgba);
736 }
737
738 src_ptr += stride;
739 }
740 }