TextureDecoder_Common.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. // Copyright 2014 Dolphin Emulator Project
  2. // Licensed under GPLv2+
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <cmath>
  6. #include "Common/Common.h"
  7. #include "VideoCommon/LookUpTables.h"
  8. #include "VideoCommon/sfont.inc"
  9. #include "VideoCommon/TextureDecoder.h"
  10. static bool TexFmt_Overlay_Enable = false;
  11. static bool TexFmt_Overlay_Center = false;
  12. // TRAM
  13. // STATE_TO_SAVE
  14. GC_ALIGNED16(u8 texMem[TMEM_SIZE]);
  15. int TexDecoder_GetTexelSizeInNibbles(int format)
  16. {
  17. switch (format & 0x3f) {
  18. case GX_TF_I4: return 1;
  19. case GX_TF_I8: return 2;
  20. case GX_TF_IA4: return 2;
  21. case GX_TF_IA8: return 4;
  22. case GX_TF_RGB565: return 4;
  23. case GX_TF_RGB5A3: return 4;
  24. case GX_TF_RGBA8: return 8;
  25. case GX_TF_C4: return 1;
  26. case GX_TF_C8: return 2;
  27. case GX_TF_C14X2: return 4;
  28. case GX_TF_CMPR: return 1;
  29. case GX_CTF_R4: return 1;
  30. case GX_CTF_RA4: return 2;
  31. case GX_CTF_RA8: return 4;
  32. case GX_CTF_YUVA8: return 8;
  33. case GX_CTF_A8: return 2;
  34. case GX_CTF_R8: return 2;
  35. case GX_CTF_G8: return 2;
  36. case GX_CTF_B8: return 2;
  37. case GX_CTF_RG8: return 4;
  38. case GX_CTF_GB8: return 4;
  39. case GX_TF_Z8: return 2;
  40. case GX_TF_Z16: return 4;
  41. case GX_TF_Z24X8: return 8;
  42. case GX_CTF_Z4: return 1;
  43. case GX_CTF_Z8M: return 2;
  44. case GX_CTF_Z8L: return 2;
  45. case GX_CTF_Z16L: return 4;
  46. default: return 1;
  47. }
  48. }
  49. int TexDecoder_GetTextureSizeInBytes(int width, int height, int format)
  50. {
  51. return (width * height * TexDecoder_GetTexelSizeInNibbles(format)) / 2;
  52. }
  53. int TexDecoder_GetBlockWidthInTexels(u32 format)
  54. {
  55. switch (format)
  56. {
  57. case GX_TF_I4: return 8;
  58. case GX_TF_I8: return 8;
  59. case GX_TF_IA4: return 8;
  60. case GX_TF_IA8: return 4;
  61. case GX_TF_RGB565: return 4;
  62. case GX_TF_RGB5A3: return 4;
  63. case GX_TF_RGBA8: return 4;
  64. case GX_TF_C4: return 8;
  65. case GX_TF_C8: return 8;
  66. case GX_TF_C14X2: return 4;
  67. case GX_TF_CMPR: return 8;
  68. case GX_CTF_R4: return 8;
  69. case GX_CTF_RA4: return 8;
  70. case GX_CTF_RA8: return 4;
  71. case GX_CTF_A8: return 8;
  72. case GX_CTF_R8: return 8;
  73. case GX_CTF_G8: return 8;
  74. case GX_CTF_B8: return 8;
  75. case GX_CTF_RG8: return 4;
  76. case GX_CTF_GB8: return 4;
  77. case GX_TF_Z8: return 8;
  78. case GX_TF_Z16: return 4;
  79. case GX_TF_Z24X8: return 4;
  80. case GX_CTF_Z4: return 8;
  81. case GX_CTF_Z8M: return 8;
  82. case GX_CTF_Z8L: return 8;
  83. case GX_CTF_Z16L: return 4;
  84. default:
  85. ERROR_LOG(VIDEO, "Unsupported Texture Format (%08x)! (GetBlockWidthInTexels)", format);
  86. return 8;
  87. }
  88. }
  89. int TexDecoder_GetBlockHeightInTexels(u32 format)
  90. {
  91. switch (format)
  92. {
  93. case GX_TF_I4: return 8;
  94. case GX_TF_I8: return 4;
  95. case GX_TF_IA4: return 4;
  96. case GX_TF_IA8: return 4;
  97. case GX_TF_RGB565: return 4;
  98. case GX_TF_RGB5A3: return 4;
  99. case GX_TF_RGBA8: return 4;
  100. case GX_TF_C4: return 8;
  101. case GX_TF_C8: return 4;
  102. case GX_TF_C14X2: return 4;
  103. case GX_TF_CMPR: return 8;
  104. case GX_CTF_R4: return 8;
  105. case GX_CTF_RA4: return 4;
  106. case GX_CTF_RA8: return 4;
  107. case GX_CTF_A8: return 4;
  108. case GX_CTF_R8: return 4;
  109. case GX_CTF_G8: return 4;
  110. case GX_CTF_B8: return 4;
  111. case GX_CTF_RG8: return 4;
  112. case GX_CTF_GB8: return 4;
  113. case GX_TF_Z8: return 4;
  114. case GX_TF_Z16: return 4;
  115. case GX_TF_Z24X8: return 4;
  116. case GX_CTF_Z4: return 8;
  117. case GX_CTF_Z8M: return 4;
  118. case GX_CTF_Z8L: return 4;
  119. case GX_CTF_Z16L: return 4;
  120. default:
  121. ERROR_LOG(VIDEO, "Unsupported Texture Format (%08x)! (GetBlockHeightInTexels)", format);
  122. return 4;
  123. }
  124. }
  125. //returns bytes
  126. int TexDecoder_GetPaletteSize(int format)
  127. {
  128. switch (format)
  129. {
  130. case GX_TF_C4: return 16 * 2;
  131. case GX_TF_C8: return 256 * 2;
  132. case GX_TF_C14X2: return 16384 * 2;
  133. default:
  134. return 0;
  135. }
  136. }
  137. void TexDecoder_SetTexFmtOverlayOptions(bool enable, bool center)
  138. {
  139. TexFmt_Overlay_Enable = enable;
  140. TexFmt_Overlay_Center = center;
  141. }
  142. static const char* texfmt[] = {
  143. // pixel
  144. "I4", "I8", "IA4", "IA8",
  145. "RGB565", "RGB5A3", "RGBA8", "0x07",
  146. "C4", "C8", "C14X2", "0x0B",
  147. "0x0C", "0x0D", "CMPR", "0x0F",
  148. // Z-buffer
  149. "0x10", "Z8", "0x12", "Z16",
  150. "0x14", "0x15", "Z24X8", "0x17",
  151. "0x18", "0x19", "0x1A", "0x1B",
  152. "0x1C", "0x1D", "0x1E", "0x1F",
  153. // pixel + copy
  154. "CR4", "0x21", "CRA4", "CRA8",
  155. "0x24", "0x25", "CYUVA8", "CA8",
  156. "CR8", "CG8", "CB8", "CRG8",
  157. "CGB8", "0x2D", "0x2E", "0x2F",
  158. // Z + copy
  159. "CZ4", "0x31", "0x32", "0x33",
  160. "0x34", "0x35", "0x36", "0x37",
  161. "0x38", "CZ8M", "CZ8L", "0x3B",
  162. "CZ16L", "0x3D", "0x3E", "0x3F",
  163. };
  164. static void TexDecoder_DrawOverlay(u8 *dst, int width, int height, int texformat)
  165. {
  166. int w = std::min(width, 40);
  167. int h = std::min(height, 10);
  168. int xoff = (width - w) >> 1;
  169. int yoff = (height - h) >> 1;
  170. if (!TexFmt_Overlay_Center)
  171. {
  172. xoff = 0;
  173. yoff = 0;
  174. }
  175. const char* fmt = texfmt[texformat & 15];
  176. while (*fmt)
  177. {
  178. int xcnt = 0;
  179. int nchar = sfont_map[(int)*fmt];
  180. const unsigned char *ptr = sfont_raw[nchar]; // each char is up to 9x10
  181. for (int x = 0; x < 9; x++)
  182. {
  183. if (ptr[x] == 0x78)
  184. break;
  185. xcnt++;
  186. }
  187. for (int y = 0; y < 10; y++)
  188. {
  189. for (int x = 0; x < xcnt; x++)
  190. {
  191. int *dtp = (int*)dst;
  192. dtp[(y + yoff) * width + x + xoff] = ptr[x] ? 0xFFFFFFFF : 0xFF000000;
  193. }
  194. ptr += 9;
  195. }
  196. xoff += xcnt;
  197. fmt++;
  198. }
  199. }
  200. void TexDecoder_Decode(u8 *dst, const u8 *src, int width, int height, int texformat, const u8* tlut, TlutFormat tlutfmt)
  201. {
  202. _TexDecoder_DecodeImpl((u32*)dst, src, width, height, texformat, tlut, tlutfmt);
  203. if (TexFmt_Overlay_Enable)
  204. TexDecoder_DrawOverlay(dst, width, height, texformat);
  205. }
  206. static inline u32 DecodePixel_IA8(u16 val)
  207. {
  208. int a = val & 0xFF;
  209. int i = val >> 8;
  210. return i | (i << 8) | (i << 16) | (a << 24);
  211. }
  212. static inline u32 DecodePixel_RGB565(u16 val)
  213. {
  214. int r,g,b,a;
  215. r=Convert5To8((val>>11) & 0x1f);
  216. g=Convert6To8((val>>5 ) & 0x3f);
  217. b=Convert5To8((val ) & 0x1f);
  218. a=0xFF;
  219. return r | (g<<8) | (b << 16) | (a << 24);
  220. }
  221. static inline u32 DecodePixel_RGB5A3(u16 val)
  222. {
  223. int r,g,b,a;
  224. if ((val&0x8000))
  225. {
  226. r=Convert5To8((val>>10) & 0x1f);
  227. g=Convert5To8((val>>5 ) & 0x1f);
  228. b=Convert5To8((val ) & 0x1f);
  229. a=0xFF;
  230. }
  231. else
  232. {
  233. a=Convert3To8((val>>12) & 0x7);
  234. r=Convert4To8((val>>8 ) & 0xf);
  235. g=Convert4To8((val>>4 ) & 0xf);
  236. b=Convert4To8((val ) & 0xf);
  237. }
  238. return r | (g<<8) | (b << 16) | (a << 24);
  239. }
  240. static inline u32 DecodePixel_Paletted(u16 pixel, TlutFormat tlutfmt)
  241. {
  242. switch (tlutfmt)
  243. {
  244. case GX_TL_IA8:
  245. return DecodePixel_IA8(pixel);
  246. case GX_TL_RGB565:
  247. return DecodePixel_RGB565(Common::swap16(pixel));
  248. case GX_TL_RGB5A3:
  249. return DecodePixel_RGB5A3(Common::swap16(pixel));
  250. default:
  251. return 0;
  252. }
  253. }
  254. struct DXTBlock
  255. {
  256. u16 color1;
  257. u16 color2;
  258. u8 lines[4];
  259. };
  260. static inline u32 MakeRGBA(int r, int g, int b, int a)
  261. {
  262. return (a<<24)|(b<<16)|(g<<8)|r;
  263. }
  264. void TexDecoder_DecodeTexel(u8 *dst, const u8 *src, int s, int t, int imageWidth, int texformat, const u8* tlut_, TlutFormat tlutfmt)
  265. {
  266. /* General formula for computing texture offset
  267. //
  268. u16 sBlk = s / blockWidth;
  269. u16 tBlk = t / blockHeight;
  270. u16 widthBlks = (width / blockWidth) + 1;
  271. u32 base = (tBlk * widthBlks + sBlk) * blockWidth * blockHeight;
  272. u16 blkS = s & (blockWidth - 1);
  273. u16 blkT = t & (blockHeight - 1);
  274. u32 blkOff = blkT * blockWidth + blkS;
  275. */
  276. switch (texformat)
  277. {
  278. case GX_TF_C4:
  279. {
  280. u16 sBlk = s >> 3;
  281. u16 tBlk = t >> 3;
  282. u16 widthBlks = (imageWidth >> 3) + 1;
  283. u32 base = (tBlk * widthBlks + sBlk) << 5;
  284. u16 blkS = s & 7;
  285. u16 blkT = t & 7;
  286. u32 blkOff = (blkT << 3) + blkS;
  287. int rs = (blkOff & 1)?0:4;
  288. u32 offset = base + (blkOff >> 1);
  289. u8 val = (*(src + offset) >> rs) & 0xF;
  290. u16 *tlut = (u16*) tlut_;
  291. *((u32*)dst) = DecodePixel_Paletted(tlut[val], tlutfmt);
  292. }
  293. break;
  294. case GX_TF_I4:
  295. {
  296. u16 sBlk = s >> 3;
  297. u16 tBlk = t >> 3;
  298. u16 widthBlks = (imageWidth >> 3) + 1;
  299. u32 base = (tBlk * widthBlks + sBlk) << 5;
  300. u16 blkS = s & 7;
  301. u16 blkT = t & 7;
  302. u32 blkOff = (blkT << 3) + blkS;
  303. int rs = (blkOff & 1)?0:4;
  304. u32 offset = base + (blkOff >> 1);
  305. u8 val = (*(src + offset) >> rs) & 0xF;
  306. val = Convert4To8(val);
  307. dst[0] = val;
  308. dst[1] = val;
  309. dst[2] = val;
  310. dst[3] = val;
  311. }
  312. break;
  313. case GX_TF_I8:
  314. {
  315. u16 sBlk = s >> 3;
  316. u16 tBlk = t >> 2;
  317. u16 widthBlks = (imageWidth >> 3) + 1;
  318. u32 base = (tBlk * widthBlks + sBlk) << 5;
  319. u16 blkS = s & 7;
  320. u16 blkT = t & 3;
  321. u32 blkOff = (blkT << 3) + blkS;
  322. u8 val = *(src + base + blkOff);
  323. dst[0] = val;
  324. dst[1] = val;
  325. dst[2] = val;
  326. dst[3] = val;
  327. }
  328. break;
  329. case GX_TF_C8:
  330. {
  331. u16 sBlk = s >> 3;
  332. u16 tBlk = t >> 2;
  333. u16 widthBlks = (imageWidth >> 3) + 1;
  334. u32 base = (tBlk * widthBlks + sBlk) << 5;
  335. u16 blkS = s & 7;
  336. u16 blkT = t & 3;
  337. u32 blkOff = (blkT << 3) + blkS;
  338. u8 val = *(src + base + blkOff);
  339. u16 *tlut = (u16*) tlut_;
  340. *((u32*)dst) = DecodePixel_Paletted(tlut[val], tlutfmt);
  341. }
  342. break;
  343. case GX_TF_IA4:
  344. {
  345. u16 sBlk = s >> 3;
  346. u16 tBlk = t >> 2;
  347. u16 widthBlks = (imageWidth >> 3) + 1;
  348. u32 base = (tBlk * widthBlks + sBlk) << 5;
  349. u16 blkS = s & 7;
  350. u16 blkT = t & 3;
  351. u32 blkOff = (blkT << 3) + blkS;
  352. u8 val = *(src + base + blkOff);
  353. const u8 a = Convert4To8(val>>4);
  354. const u8 l = Convert4To8(val&0xF);
  355. dst[0] = l;
  356. dst[1] = l;
  357. dst[2] = l;
  358. dst[3] = a;
  359. }
  360. break;
  361. case GX_TF_IA8:
  362. {
  363. u16 sBlk = s >> 2;
  364. u16 tBlk = t >> 2;
  365. u16 widthBlks = (imageWidth >> 2) + 1;
  366. u32 base = (tBlk * widthBlks + sBlk) << 4;
  367. u16 blkS = s & 3;
  368. u16 blkT = t & 3;
  369. u32 blkOff = (blkT << 2) + blkS;
  370. u32 offset = (base + blkOff) << 1;
  371. const u16* valAddr = (u16*)(src + offset);
  372. *((u32*)dst) = DecodePixel_IA8(*valAddr);
  373. }
  374. break;
  375. case GX_TF_C14X2:
  376. {
  377. u16 sBlk = s >> 2;
  378. u16 tBlk = t >> 2;
  379. u16 widthBlks = (imageWidth >> 2) + 1;
  380. u32 base = (tBlk * widthBlks + sBlk) << 4;
  381. u16 blkS = s & 3;
  382. u16 blkT = t & 3;
  383. u32 blkOff = (blkT << 2) + blkS;
  384. u32 offset = (base + blkOff) << 1;
  385. const u16* valAddr = (u16*)(src + offset);
  386. u16 val = Common::swap16(*valAddr) & 0x3FFF;
  387. u16 *tlut = (u16*) tlut_;
  388. *((u32*)dst) = DecodePixel_Paletted(tlut[val], tlutfmt);
  389. }
  390. break;
  391. case GX_TF_RGB565:
  392. {
  393. u16 sBlk = s >> 2;
  394. u16 tBlk = t >> 2;
  395. u16 widthBlks = (imageWidth >> 2) + 1;
  396. u32 base = (tBlk * widthBlks + sBlk) << 4;
  397. u16 blkS = s & 3;
  398. u16 blkT = t & 3;
  399. u32 blkOff = (blkT << 2) + blkS;
  400. u32 offset = (base + blkOff) << 1;
  401. const u16* valAddr = (u16*)(src + offset);
  402. *((u32*)dst) = DecodePixel_RGB565(Common::swap16(*valAddr));
  403. }
  404. break;
  405. case GX_TF_RGB5A3:
  406. {
  407. u16 sBlk = s >> 2;
  408. u16 tBlk = t >> 2;
  409. u16 widthBlks = (imageWidth >> 2) + 1;
  410. u32 base = (tBlk * widthBlks + sBlk) << 4;
  411. u16 blkS = s & 3;
  412. u16 blkT = t & 3;
  413. u32 blkOff = (blkT << 2) + blkS;
  414. u32 offset = (base + blkOff) << 1;
  415. const u16* valAddr = (u16*)(src + offset);
  416. *((u32*)dst) = DecodePixel_RGB5A3(Common::swap16(*valAddr));
  417. }
  418. break;
  419. case GX_TF_RGBA8:
  420. {
  421. u16 sBlk = s >> 2;
  422. u16 tBlk = t >> 2;
  423. u16 widthBlks = (imageWidth >> 2) + 1;
  424. u32 base = (tBlk * widthBlks + sBlk) << 5; // shift by 5 is correct
  425. u16 blkS = s & 3;
  426. u16 blkT = t & 3;
  427. u32 blkOff = (blkT << 2) + blkS;
  428. u32 offset = (base + blkOff) << 1 ;
  429. const u8* valAddr = src + offset;
  430. dst[3] = valAddr[0];
  431. dst[0] = valAddr[1];
  432. dst[1] = valAddr[32];
  433. dst[2] = valAddr[33];
  434. }
  435. break;
  436. case GX_TF_CMPR:
  437. {
  438. u16 sDxt = s >> 2;
  439. u16 tDxt = t >> 2;
  440. u16 sBlk = sDxt >> 1;
  441. u16 tBlk = tDxt >> 1;
  442. u16 widthBlks = (imageWidth >> 3) + 1;
  443. u32 base = (tBlk * widthBlks + sBlk) << 2;
  444. u16 blkS = sDxt & 1;
  445. u16 blkT = tDxt & 1;
  446. u32 blkOff = (blkT << 1) + blkS;
  447. u32 offset = (base + blkOff) << 3;
  448. const DXTBlock* dxtBlock = (const DXTBlock*)(src + offset);
  449. u16 c1 = Common::swap16(dxtBlock->color1);
  450. u16 c2 = Common::swap16(dxtBlock->color2);
  451. int blue1 = Convert5To8(c1 & 0x1F);
  452. int blue2 = Convert5To8(c2 & 0x1F);
  453. int green1 = Convert6To8((c1 >> 5) & 0x3F);
  454. int green2 = Convert6To8((c2 >> 5) & 0x3F);
  455. int red1 = Convert5To8((c1 >> 11) & 0x1F);
  456. int red2 = Convert5To8((c2 >> 11) & 0x1F);
  457. u16 ss = s & 3;
  458. u16 tt = t & 3;
  459. int colorSel = dxtBlock->lines[tt];
  460. int rs = 6 - (ss << 1);
  461. colorSel = (colorSel >> rs) & 3;
  462. colorSel |= c1 > c2?0:4;
  463. u32 color = 0;
  464. switch (colorSel)
  465. {
  466. case 0:
  467. case 4:
  468. color = MakeRGBA(red1, green1, blue1, 255);
  469. break;
  470. case 1:
  471. case 5:
  472. color = MakeRGBA(red2, green2, blue2, 255);
  473. break;
  474. case 2:
  475. color = MakeRGBA(red1+(red2-red1)/3, green1+(green2-green1)/3, blue1+(blue2-blue1)/3, 255);
  476. break;
  477. case 3:
  478. color = MakeRGBA(red2+(red1-red2)/3, green2+(green1-green2)/3, blue2+(blue1-blue2)/3, 255);
  479. break;
  480. case 6:
  481. color = MakeRGBA((int)ceil((float)(red1+red2)/2), (int)ceil((float)(green1+green2)/2), (int)ceil((float)(blue1+blue2)/2), 255);
  482. break;
  483. case 7:
  484. color = MakeRGBA(red2, green2, blue2, 0);
  485. break;
  486. default:
  487. color = 0;
  488. break;
  489. }
  490. *((u32*)dst) = color;
  491. }
  492. break;
  493. }
  494. }
  495. void TexDecoder_DecodeTexelRGBA8FromTmem(u8 *dst, const u8 *src_ar, const u8* src_gb, int s, int t, int imageWidth)
  496. {
  497. u16 sBlk = s >> 2;
  498. u16 tBlk = t >> 2;
  499. u16 widthBlks = (imageWidth >> 2) + 1; // TODO: Looks wrong. Shouldn't this be ((imageWidth-1)>>2)+1 ?
  500. u32 base_ar = (tBlk * widthBlks + sBlk) << 4;
  501. u32 base_gb = (tBlk * widthBlks + sBlk) << 4;
  502. u16 blkS = s & 3;
  503. u16 blkT = t & 3;
  504. u32 blk_off = (blkT << 2) + blkS;
  505. u32 offset_ar = (base_ar + blk_off) << 1;
  506. u32 offset_gb = (base_gb + blk_off) << 1;
  507. const u8* val_addr_ar = src_ar + offset_ar;
  508. const u8* val_addr_gb = src_gb + offset_gb;
  509. dst[3] = val_addr_ar[0]; // A
  510. dst[0] = val_addr_ar[1]; // R
  511. dst[1] = val_addr_gb[0]; // G
  512. dst[2] = val_addr_gb[1]; // B
  513. }
  514. void TexDecoder_DecodeRGBA8FromTmem(u8* dst, const u8 *src_ar, const u8 *src_gb, int width, int height)
  515. {
  516. // TODO for someone who cares: Make this less slow!
  517. for (int y = 0; y < height; ++y)
  518. {
  519. for (int x = 0; x < width; ++x)
  520. {
  521. TexDecoder_DecodeTexelRGBA8FromTmem(dst, src_ar, src_gb, x, y, width-1);
  522. dst += 4;
  523. }
  524. }
  525. }