TextureEncoder.cpp 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496
  1. // Copyright 2009 Dolphin Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "VideoBackends/Software/TextureEncoder.h"
  4. #include "Common/Align.h"
  5. #include "Common/Assert.h"
  6. #include "Common/CommonFuncs.h"
  7. #include "Common/CommonTypes.h"
  8. #include "Common/MsgHandler.h"
  9. #include "Common/Swap.h"
  10. #include "VideoBackends/Software/EfbInterface.h"
  11. #include "VideoBackends/Software/SWTexture.h"
  12. #include "VideoCommon/BPMemory.h"
  13. #include "VideoCommon/LookUpTables.h"
  14. #include "VideoCommon/TextureCacheBase.h"
  15. #include "VideoCommon/TextureDecoder.h"
  16. namespace TextureEncoder
  17. {
  18. static inline void RGBA_to_RGBA8(const u8* src, u8* r, u8* g, u8* b, u8* a)
  19. {
  20. u32 srcColor = *(u32*)src;
  21. *a = Convert6To8(srcColor & 0x3f);
  22. *b = Convert6To8((srcColor >> 6) & 0x3f);
  23. *g = Convert6To8((srcColor >> 12) & 0x3f);
  24. *r = Convert6To8((srcColor >> 18) & 0x3f);
  25. }
  26. static inline void RGBA_to_RGB8(const u8* src, u8* r, u8* g, u8* b)
  27. {
  28. u32 srcColor = *(u32*)src;
  29. *b = Convert6To8((srcColor >> 6) & 0x3f);
  30. *g = Convert6To8((srcColor >> 12) & 0x3f);
  31. *r = Convert6To8((srcColor >> 18) & 0x3f);
  32. }
  33. static inline u8 RGB8_to_I(u8 r, u8 g, u8 b)
  34. {
  35. // values multiplied by 256 to keep math integer
  36. u16 val = 4096 + 66 * r + 129 * g + 25 * b;
  37. return val >> 8;
  38. }
  39. // box filter sampling averages 4 samples with the source texel being the top left of the box
  40. // components are scaled to the range 0-255 after all samples are taken
  41. static inline void BoxfilterRGBA_to_RGBA8(const u8* src, u8* r, u8* g, u8* b, u8* a)
  42. {
  43. u16 r16 = 0, g16 = 0, b16 = 0, a16 = 0;
  44. for (int y = 0; y < 2; y++)
  45. {
  46. for (int x = 0; x < 2; x++)
  47. {
  48. u32 srcColor = *(u32*)src;
  49. a16 += srcColor & 0x3f;
  50. b16 += (srcColor >> 6) & 0x3f;
  51. g16 += (srcColor >> 12) & 0x3f;
  52. r16 += (srcColor >> 18) & 0x3f;
  53. src += 3; // move to next pixel
  54. }
  55. src += (640 - 2) * 3; // move to next line
  56. }
  57. *r = r16 + (r16 >> 6);
  58. *g = g16 + (g16 >> 6);
  59. *b = b16 + (b16 >> 6);
  60. *a = a16 + (a16 >> 6);
  61. }
  62. static inline void BoxfilterRGBA_to_RGB8(const u8* src, u8* r, u8* g, u8* b)
  63. {
  64. u16 r16 = 0, g16 = 0, b16 = 0;
  65. for (int y = 0; y < 2; y++)
  66. {
  67. for (int x = 0; x < 2; x++)
  68. {
  69. u32 srcColor = *(u32*)src;
  70. b16 += (srcColor >> 6) & 0x3f;
  71. g16 += (srcColor >> 12) & 0x3f;
  72. r16 += (srcColor >> 18) & 0x3f;
  73. src += 3; // move to next pixel
  74. }
  75. src += (640 - 2) * 3; // move to next line
  76. }
  77. *r = r16 + (r16 >> 6);
  78. *g = g16 + (g16 >> 6);
  79. *b = b16 + (b16 >> 6);
  80. }
  81. static inline void BoxfilterRGBA_to_x8(const u8* src, u8* x8, int shift)
  82. {
  83. u16 x16 = 0;
  84. for (int y = 0; y < 2; y++)
  85. {
  86. for (int x = 0; x < 2; x++)
  87. {
  88. u32 srcColor = *(u32*)src;
  89. x16 += (srcColor >> shift) & 0x3f;
  90. src += 3; // move to next pixel
  91. }
  92. src += (640 - 2) * 3; // move to next line
  93. }
  94. *x8 = x16 + (x16 >> 6);
  95. }
  96. static inline void BoxfilterRGBA_to_xx8(const u8* src, u8* x1, u8* x2, int shift1, int shift2)
  97. {
  98. u16 x16_1 = 0;
  99. u16 x16_2 = 0;
  100. for (int y = 0; y < 2; y++)
  101. {
  102. for (int x = 0; x < 2; x++)
  103. {
  104. u32 srcColor = *(u32*)src;
  105. x16_1 += (srcColor >> shift1) & 0x3f;
  106. x16_2 += (srcColor >> shift2) & 0x3f;
  107. src += 3; // move to next pixel
  108. }
  109. src += (640 - 2) * 3; // move to next line
  110. }
  111. *x1 = x16_1 + (x16_1 >> 6);
  112. *x2 = x16_2 + (x16_2 >> 6);
  113. }
  114. static inline void BoxfilterRGB_to_RGB8(const u8* src, u8* r, u8* g, u8* b)
  115. {
  116. u16 r16 = 0, g16 = 0, b16 = 0;
  117. for (int y = 0; y < 2; y++)
  118. {
  119. for (int x = 0; x < 2; x++)
  120. {
  121. b16 += src[0];
  122. g16 += src[1];
  123. r16 += src[2];
  124. src += 3; // move to next pixel
  125. }
  126. src += (640 - 2) * 3; // move to next line
  127. }
  128. *r = r16 >> 2;
  129. *g = g16 >> 2;
  130. *b = b16 >> 2;
  131. }
  132. static inline void BoxfilterRGB_to_x8(const u8* src, u8* x8, int comp)
  133. {
  134. u16 x16 = 0;
  135. for (int y = 0; y < 2; y++)
  136. {
  137. for (int x = 0; x < 2; x++)
  138. {
  139. x16 += src[comp];
  140. src += 3; // move to next pixel
  141. }
  142. src += (640 - 2) * 3; // move to next line
  143. }
  144. *x8 = x16 >> 2;
  145. }
  146. static inline void BoxfilterRGB_to_xx8(const u8* src, u8* x1, u8* x2, int comp1, int comp2)
  147. {
  148. u16 x16_1 = 0;
  149. u16 x16_2 = 0;
  150. for (int y = 0; y < 2; y++)
  151. {
  152. for (int x = 0; x < 2; x++)
  153. {
  154. x16_1 += src[comp1];
  155. x16_2 += src[comp2];
  156. src += 3; // move to next pixel
  157. }
  158. src += (640 - 2) * 3; // move to next line
  159. }
  160. *x1 = x16_1 >> 2;
  161. *x2 = x16_2 >> 2;
  162. }
  163. static void SetBlockDimensions(int blkWidthLog2, int blkHeightLog2, u16* sBlkCount, u16* tBlkCount,
  164. u16* sBlkSize, u16* tBlkSize)
  165. {
  166. // if half_scale is 1 then the size is cut in half
  167. u32 width = bpmem.copyTexSrcWH.x >> bpmem.triggerEFBCopy.half_scale;
  168. u32 height = bpmem.copyTexSrcWH.y >> bpmem.triggerEFBCopy.half_scale;
  169. *sBlkCount = (width >> blkWidthLog2) + 1;
  170. *tBlkCount = (height >> blkHeightLog2) + 1;
  171. *sBlkSize = 1 << blkWidthLog2;
  172. *tBlkSize = 1 << blkHeightLog2;
  173. }
  174. static void SetSpans(int sBlkSize, int tBlkSize, s32* tSpan, s32* sBlkSpan, s32* tBlkSpan,
  175. s32* writeStride)
  176. {
  177. // width is 1 less than the number of pixels of width
  178. u32 width = bpmem.copyTexSrcWH.x >> bpmem.triggerEFBCopy.half_scale;
  179. u32 alignedWidth = Common::AlignUp(width + 1, sBlkSize);
  180. u32 readStride = 3 << bpmem.triggerEFBCopy.half_scale;
  181. *tSpan = (640 - sBlkSize) *
  182. readStride; // bytes to advance src pointer after each row of texels in a block
  183. *sBlkSpan =
  184. ((-640 * tBlkSize) + sBlkSize) * readStride; // bytes to advance src pointer after each block
  185. *tBlkSpan = ((640 * tBlkSize) - alignedWidth) *
  186. readStride; // bytes to advance src pointer after each row of blocks
  187. *writeStride = bpmem.copyDestStride << 5;
  188. }
  189. #define ENCODE_LOOP_BLOCKS \
  190. for (int tBlk = 0; tBlk < tBlkCount; tBlk++) \
  191. { \
  192. dst = dstBlockStart; \
  193. for (int sBlk = 0; sBlk < sBlkCount; sBlk++) \
  194. { \
  195. for (int t = 0; t < tBlkSize; t++) \
  196. { \
  197. for (int s = 0; s < sBlkSize; s++) \
  198. {
  199. #define ENCODE_LOOP_SPANS \
  200. } \
  201. src += tSpan; \
  202. } \
  203. src += sBlkSpan; \
  204. } \
  205. src += tBlkSpan; \
  206. dstBlockStart += writeStride; \
  207. }
  208. #define ENCODE_LOOP_SPANS2 \
  209. } \
  210. src += tSpan; \
  211. } \
  212. src += sBlkSpan; \
  213. dst += 32; \
  214. } \
  215. src += tBlkSpan; \
  216. dstBlockStart += writeStride; \
  217. }
  218. static void EncodeRGBA6(u8* dst, const u8* src, EFBCopyFormat format, bool yuv)
  219. {
  220. u16 sBlkCount, tBlkCount, sBlkSize, tBlkSize;
  221. s32 tSpan, sBlkSpan, tBlkSpan, writeStride;
  222. u8 r, g, b, a;
  223. u32 readStride = 3;
  224. u8* dstBlockStart = dst;
  225. switch (format)
  226. {
  227. case EFBCopyFormat::R4:
  228. SetBlockDimensions(3, 3, &sBlkCount, &tBlkCount, &sBlkSize, &tBlkSize);
  229. SetSpans(sBlkSize, tBlkSize, &tSpan, &sBlkSpan, &tBlkSpan, &writeStride);
  230. sBlkSize /= 2;
  231. if (yuv)
  232. {
  233. ENCODE_LOOP_BLOCKS
  234. {
  235. RGBA_to_RGB8(src, &r, &g, &b);
  236. src += readStride;
  237. *dst = RGB8_to_I(r, g, b) & 0xf0;
  238. RGBA_to_RGB8(src, &r, &g, &b);
  239. src += readStride;
  240. *dst |= RGB8_to_I(r, g, b) >> 4;
  241. dst++;
  242. }
  243. ENCODE_LOOP_SPANS
  244. }
  245. else
  246. {
  247. ENCODE_LOOP_BLOCKS
  248. {
  249. u32 srcColor = *(u32*)src;
  250. src += readStride;
  251. *dst = (srcColor >> 16) & 0xf0;
  252. srcColor = *(u32*)src;
  253. src += readStride;
  254. *dst |= (srcColor >> 20) & 0x0f;
  255. dst++;
  256. }
  257. ENCODE_LOOP_SPANS
  258. }
  259. break;
  260. case EFBCopyFormat::R8_0x1:
  261. case EFBCopyFormat::R8:
  262. SetBlockDimensions(3, 2, &sBlkCount, &tBlkCount, &sBlkSize, &tBlkSize);
  263. SetSpans(sBlkSize, tBlkSize, &tSpan, &sBlkSpan, &tBlkSpan, &writeStride);
  264. if (yuv)
  265. {
  266. ENCODE_LOOP_BLOCKS
  267. {
  268. RGBA_to_RGB8(src, &r, &g, &b);
  269. src += readStride;
  270. *dst++ = RGB8_to_I(r, g, b);
  271. }
  272. ENCODE_LOOP_SPANS
  273. }
  274. else
  275. {
  276. ENCODE_LOOP_BLOCKS
  277. {
  278. u32 srcColor = *(u32*)src;
  279. *dst++ = Convert6To8((srcColor >> 18) & 0x3f);
  280. src += readStride;
  281. }
  282. ENCODE_LOOP_SPANS
  283. }
  284. break;
  285. case EFBCopyFormat::RA4:
  286. SetBlockDimensions(3, 2, &sBlkCount, &tBlkCount, &sBlkSize, &tBlkSize);
  287. SetSpans(sBlkSize, tBlkSize, &tSpan, &sBlkSpan, &tBlkSpan, &writeStride);
  288. if (yuv)
  289. {
  290. ENCODE_LOOP_BLOCKS
  291. {
  292. RGBA_to_RGBA8(src, &r, &g, &b, &a);
  293. src += readStride;
  294. *dst++ = (a & 0xf0) | (RGB8_to_I(r, g, b) >> 4);
  295. }
  296. ENCODE_LOOP_SPANS
  297. }
  298. else
  299. {
  300. ENCODE_LOOP_BLOCKS
  301. {
  302. u32 srcColor = *(u32*)src;
  303. src += readStride;
  304. *dst++ = ((srcColor << 2) & 0xf0) | ((srcColor >> 20) & 0x0f);
  305. }
  306. ENCODE_LOOP_SPANS
  307. }
  308. break;
  309. case EFBCopyFormat::RA8:
  310. SetBlockDimensions(2, 2, &sBlkCount, &tBlkCount, &sBlkSize, &tBlkSize);
  311. SetSpans(sBlkSize, tBlkSize, &tSpan, &sBlkSpan, &tBlkSpan, &writeStride);
  312. if (yuv)
  313. {
  314. ENCODE_LOOP_BLOCKS
  315. {
  316. RGBA_to_RGBA8(src, &r, &g, &b, &a);
  317. src += readStride;
  318. *dst++ = a;
  319. *dst++ = RGB8_to_I(r, g, b);
  320. }
  321. ENCODE_LOOP_SPANS
  322. }
  323. else
  324. {
  325. ENCODE_LOOP_BLOCKS
  326. {
  327. u32 srcColor = *(u32*)src;
  328. src += readStride;
  329. *dst++ = Convert6To8(srcColor & 0x3f);
  330. *dst++ = Convert6To8((srcColor >> 18) & 0x3f);
  331. }
  332. ENCODE_LOOP_SPANS
  333. }
  334. break;
  335. case EFBCopyFormat::RGB565:
  336. SetBlockDimensions(2, 2, &sBlkCount, &tBlkCount, &sBlkSize, &tBlkSize);
  337. SetSpans(sBlkSize, tBlkSize, &tSpan, &sBlkSpan, &tBlkSpan, &writeStride);
  338. ENCODE_LOOP_BLOCKS
  339. {
  340. u32 srcColor = *(u32*)src;
  341. src += readStride;
  342. u16 val =
  343. ((srcColor >> 8) & 0xf800) | ((srcColor >> 7) & 0x07e0) | ((srcColor >> 7) & 0x001f);
  344. *(u16*)dst = Common::swap16(val);
  345. dst += 2;
  346. }
  347. ENCODE_LOOP_SPANS
  348. break;
  349. case EFBCopyFormat::RGB5A3:
  350. SetBlockDimensions(2, 2, &sBlkCount, &tBlkCount, &sBlkSize, &tBlkSize);
  351. SetSpans(sBlkSize, tBlkSize, &tSpan, &sBlkSpan, &tBlkSpan, &writeStride);
  352. ENCODE_LOOP_BLOCKS
  353. {
  354. u32 srcColor = *(u32*)src;
  355. src += readStride;
  356. u16 alpha = (srcColor << 9) & 0x7000;
  357. u16 val;
  358. if (alpha == 0x7000) // 555
  359. val = 0x8000 | ((srcColor >> 9) & 0x7c00) | ((srcColor >> 8) & 0x03e0) |
  360. ((srcColor >> 7) & 0x001f);
  361. else // 4443
  362. val = alpha | ((srcColor >> 12) & 0x0f00) | ((srcColor >> 10) & 0x00f0) |
  363. ((srcColor >> 8) & 0x000f);
  364. *(u16*)dst = Common::swap16(val);
  365. dst += 2;
  366. }
  367. ENCODE_LOOP_SPANS
  368. break;
  369. case EFBCopyFormat::RGBA8:
  370. SetBlockDimensions(2, 2, &sBlkCount, &tBlkCount, &sBlkSize, &tBlkSize);
  371. SetSpans(sBlkSize, tBlkSize, &tSpan, &sBlkSpan, &tBlkSpan, &writeStride);
  372. ENCODE_LOOP_BLOCKS
  373. {
  374. RGBA_to_RGBA8(src, &dst[1], &dst[32], &dst[33], &dst[0]);
  375. src += readStride;
  376. dst += 2;
  377. }
  378. ENCODE_LOOP_SPANS2
  379. break;
  380. case EFBCopyFormat::A8:
  381. SetBlockDimensions(3, 2, &sBlkCount, &tBlkCount, &sBlkSize, &tBlkSize);
  382. SetSpans(sBlkSize, tBlkSize, &tSpan, &sBlkSpan, &tBlkSpan, &writeStride);
  383. ENCODE_LOOP_BLOCKS
  384. {
  385. u32 srcColor = *(u32*)src;
  386. *dst++ = Convert6To8(srcColor & 0x3f);
  387. src += readStride;
  388. }
  389. ENCODE_LOOP_SPANS
  390. break;
  391. case EFBCopyFormat::G8:
  392. SetBlockDimensions(3, 2, &sBlkCount, &tBlkCount, &sBlkSize, &tBlkSize);
  393. SetSpans(sBlkSize, tBlkSize, &tSpan, &sBlkSpan, &tBlkSpan, &writeStride);
  394. ENCODE_LOOP_BLOCKS
  395. {
  396. u32 srcColor = *(u32*)src;
  397. *dst++ = Convert6To8((srcColor >> 12) & 0x3f);
  398. src += readStride;
  399. }
  400. ENCODE_LOOP_SPANS
  401. break;
  402. case EFBCopyFormat::B8:
  403. SetBlockDimensions(3, 2, &sBlkCount, &tBlkCount, &sBlkSize, &tBlkSize);
  404. SetSpans(sBlkSize, tBlkSize, &tSpan, &sBlkSpan, &tBlkSpan, &writeStride);
  405. ENCODE_LOOP_BLOCKS
  406. {
  407. u32 srcColor = *(u32*)src;
  408. *dst++ = Convert6To8((srcColor >> 6) & 0x3f);
  409. src += readStride;
  410. }
  411. ENCODE_LOOP_SPANS
  412. break;
  413. case EFBCopyFormat::RG8:
  414. SetBlockDimensions(2, 2, &sBlkCount, &tBlkCount, &sBlkSize, &tBlkSize);
  415. SetSpans(sBlkSize, tBlkSize, &tSpan, &sBlkSpan, &tBlkSpan, &writeStride);
  416. ENCODE_LOOP_BLOCKS
  417. {
  418. u32 srcColor = *(u32*)src;
  419. src += readStride;
  420. *dst++ = Convert6To8((srcColor >> 12) & 0x3f);
  421. *dst++ = Convert6To8((srcColor >> 18) & 0x3f);
  422. }
  423. ENCODE_LOOP_SPANS
  424. break;
  425. case EFBCopyFormat::GB8:
  426. SetBlockDimensions(2, 2, &sBlkCount, &tBlkCount, &sBlkSize, &tBlkSize);
  427. SetSpans(sBlkSize, tBlkSize, &tSpan, &sBlkSpan, &tBlkSpan, &writeStride);
  428. ENCODE_LOOP_BLOCKS
  429. {
  430. u32 srcColor = *(u32*)src;
  431. src += readStride;
  432. *dst++ = Convert6To8((srcColor >> 6) & 0x3f);
  433. *dst++ = Convert6To8((srcColor >> 12) & 0x3f);
  434. }
  435. ENCODE_LOOP_SPANS
  436. break;
  437. default:
  438. PanicAlertFmt("Unknown texture copy format: {}\n", format);
  439. break;
  440. }
  441. }
  442. static void EncodeRGBA6halfscale(u8* dst, const u8* src, EFBCopyFormat format, bool yuv)
  443. {
  444. u16 sBlkCount, tBlkCount, sBlkSize, tBlkSize;
  445. s32 tSpan, sBlkSpan, tBlkSpan, writeStride;
  446. u8 r, g, b, a;
  447. u32 readStride = 6;
  448. u8* dstBlockStart = dst;
  449. switch (format)
  450. {
  451. case EFBCopyFormat::R4:
  452. SetBlockDimensions(3, 3, &sBlkCount, &tBlkCount, &sBlkSize, &tBlkSize);
  453. SetSpans(sBlkSize, tBlkSize, &tSpan, &sBlkSpan, &tBlkSpan, &writeStride);
  454. sBlkSize /= 2;
  455. if (yuv)
  456. {
  457. ENCODE_LOOP_BLOCKS
  458. {
  459. BoxfilterRGBA_to_RGB8(src, &r, &g, &b);
  460. src += readStride;
  461. *dst = RGB8_to_I(r, g, b) & 0xf0;
  462. BoxfilterRGBA_to_RGB8(src, &r, &g, &b);
  463. src += readStride;
  464. *dst |= RGB8_to_I(r, g, b) >> 4;
  465. dst++;
  466. }
  467. ENCODE_LOOP_SPANS
  468. }
  469. else
  470. {
  471. ENCODE_LOOP_BLOCKS
  472. {
  473. BoxfilterRGBA_to_x8(src, &r, 18);
  474. src += readStride;
  475. *dst = r & 0xf0;
  476. BoxfilterRGBA_to_x8(src, &r, 18);
  477. src += readStride;
  478. *dst |= r >> 4;
  479. dst++;
  480. }
  481. ENCODE_LOOP_SPANS
  482. }
  483. break;
  484. case EFBCopyFormat::R8_0x1:
  485. case EFBCopyFormat::R8:
  486. SetBlockDimensions(3, 2, &sBlkCount, &tBlkCount, &sBlkSize, &tBlkSize);
  487. SetSpans(sBlkSize, tBlkSize, &tSpan, &sBlkSpan, &tBlkSpan, &writeStride);
  488. if (yuv)
  489. {
  490. ENCODE_LOOP_BLOCKS
  491. {
  492. BoxfilterRGBA_to_RGB8(src, &r, &g, &b);
  493. src += readStride;
  494. *dst++ = RGB8_to_I(r, g, b);
  495. }
  496. ENCODE_LOOP_SPANS
  497. }
  498. else
  499. {
  500. ENCODE_LOOP_BLOCKS
  501. {
  502. BoxfilterRGBA_to_x8(src, &r, 18);
  503. *dst++ = r;
  504. src += readStride;
  505. }
  506. ENCODE_LOOP_SPANS
  507. }
  508. break;
  509. case EFBCopyFormat::RA4:
  510. SetBlockDimensions(3, 2, &sBlkCount, &tBlkCount, &sBlkSize, &tBlkSize);
  511. SetSpans(sBlkSize, tBlkSize, &tSpan, &sBlkSpan, &tBlkSpan, &writeStride);
  512. if (yuv)
  513. {
  514. ENCODE_LOOP_BLOCKS
  515. {
  516. BoxfilterRGBA_to_RGBA8(src, &r, &g, &b, &a);
  517. src += readStride;
  518. *dst++ = (a & 0xf0) | (RGB8_to_I(r, g, b) >> 4);
  519. }
  520. ENCODE_LOOP_SPANS
  521. }
  522. else
  523. {
  524. ENCODE_LOOP_BLOCKS
  525. {
  526. BoxfilterRGBA_to_xx8(src, &r, &a, 18, 0);
  527. src += readStride;
  528. *dst++ = (a & 0xf0) | (r >> 4);
  529. }
  530. ENCODE_LOOP_SPANS
  531. }
  532. break;
  533. case EFBCopyFormat::RA8:
  534. SetBlockDimensions(2, 2, &sBlkCount, &tBlkCount, &sBlkSize, &tBlkSize);
  535. SetSpans(sBlkSize, tBlkSize, &tSpan, &sBlkSpan, &tBlkSpan, &writeStride);
  536. if (yuv)
  537. {
  538. ENCODE_LOOP_BLOCKS
  539. {
  540. BoxfilterRGBA_to_RGBA8(src, &r, &g, &b, &a);
  541. src += readStride;
  542. *dst++ = a;
  543. *dst++ = RGB8_to_I(r, g, b);
  544. }
  545. ENCODE_LOOP_SPANS
  546. }
  547. else
  548. {
  549. ENCODE_LOOP_BLOCKS
  550. {
  551. BoxfilterRGBA_to_xx8(src, &r, &a, 18, 0);
  552. src += readStride;
  553. *dst++ = a;
  554. *dst++ = r;
  555. }
  556. ENCODE_LOOP_SPANS
  557. }
  558. break;
  559. case EFBCopyFormat::RGB565:
  560. SetBlockDimensions(2, 2, &sBlkCount, &tBlkCount, &sBlkSize, &tBlkSize);
  561. SetSpans(sBlkSize, tBlkSize, &tSpan, &sBlkSpan, &tBlkSpan, &writeStride);
  562. ENCODE_LOOP_BLOCKS
  563. {
  564. BoxfilterRGBA_to_RGB8(src, &r, &g, &b);
  565. src += readStride;
  566. u16 val = ((r << 8) & 0xf800) | ((g << 3) & 0x07e0) | ((b >> 3) & 0x001f);
  567. *(u16*)dst = Common::swap16(val);
  568. dst += 2;
  569. }
  570. ENCODE_LOOP_SPANS
  571. break;
  572. case EFBCopyFormat::RGB5A3:
  573. SetBlockDimensions(2, 2, &sBlkCount, &tBlkCount, &sBlkSize, &tBlkSize);
  574. SetSpans(sBlkSize, tBlkSize, &tSpan, &sBlkSpan, &tBlkSpan, &writeStride);
  575. ENCODE_LOOP_BLOCKS
  576. {
  577. BoxfilterRGBA_to_RGBA8(src, &r, &g, &b, &a);
  578. src += readStride;
  579. u16 val;
  580. if (a >= 224) // 5551
  581. val = 0x8000 | ((r << 7) & 0x7c00) | ((g << 2) & 0x03e0) | ((b >> 3) & 0x001f);
  582. else // 4443
  583. val = ((a << 7) & 0x7000) | ((r << 4) & 0x0f00) | (g & 0x00f0) | ((b >> 4) & 0x000f);
  584. *(u16*)dst = Common::swap16(val);
  585. dst += 2;
  586. }
  587. ENCODE_LOOP_SPANS
  588. break;
  589. case EFBCopyFormat::RGBA8:
  590. SetBlockDimensions(2, 2, &sBlkCount, &tBlkCount, &sBlkSize, &tBlkSize);
  591. SetSpans(sBlkSize, tBlkSize, &tSpan, &sBlkSpan, &tBlkSpan, &writeStride);
  592. ENCODE_LOOP_BLOCKS
  593. {
  594. BoxfilterRGBA_to_RGBA8(src, &dst[1], &dst[32], &dst[33], &dst[0]);
  595. src += readStride;
  596. dst += 2;
  597. }
  598. ENCODE_LOOP_SPANS2
  599. break;
  600. case EFBCopyFormat::A8:
  601. SetBlockDimensions(3, 2, &sBlkCount, &tBlkCount, &sBlkSize, &tBlkSize);
  602. SetSpans(sBlkSize, tBlkSize, &tSpan, &sBlkSpan, &tBlkSpan, &writeStride);
  603. ENCODE_LOOP_BLOCKS
  604. {
  605. BoxfilterRGBA_to_x8(src, &a, 0);
  606. *dst++ = a;
  607. src += readStride;
  608. }
  609. ENCODE_LOOP_SPANS
  610. break;
  611. case EFBCopyFormat::G8:
  612. SetBlockDimensions(3, 2, &sBlkCount, &tBlkCount, &sBlkSize, &tBlkSize);
  613. SetSpans(sBlkSize, tBlkSize, &tSpan, &sBlkSpan, &tBlkSpan, &writeStride);
  614. ENCODE_LOOP_BLOCKS
  615. {
  616. BoxfilterRGBA_to_x8(src, &g, 12);
  617. *dst++ = g;
  618. src += readStride;
  619. }
  620. ENCODE_LOOP_SPANS
  621. break;
  622. case EFBCopyFormat::B8:
  623. SetBlockDimensions(3, 2, &sBlkCount, &tBlkCount, &sBlkSize, &tBlkSize);
  624. SetSpans(sBlkSize, tBlkSize, &tSpan, &sBlkSpan, &tBlkSpan, &writeStride);
  625. ENCODE_LOOP_BLOCKS
  626. {
  627. BoxfilterRGBA_to_x8(src, &b, 6);
  628. *dst++ = b;
  629. src += readStride;
  630. }
  631. ENCODE_LOOP_SPANS
  632. break;
  633. case EFBCopyFormat::RG8:
  634. SetBlockDimensions(2, 2, &sBlkCount, &tBlkCount, &sBlkSize, &tBlkSize);
  635. SetSpans(sBlkSize, tBlkSize, &tSpan, &sBlkSpan, &tBlkSpan, &writeStride);
  636. ENCODE_LOOP_BLOCKS
  637. {
  638. BoxfilterRGBA_to_xx8(src, &r, &g, 18, 12);
  639. src += readStride;
  640. *dst++ = g;
  641. *dst++ = r;
  642. }
  643. ENCODE_LOOP_SPANS
  644. break;
  645. case EFBCopyFormat::GB8:
  646. SetBlockDimensions(2, 2, &sBlkCount, &tBlkCount, &sBlkSize, &tBlkSize);
  647. SetSpans(sBlkSize, tBlkSize, &tSpan, &sBlkSpan, &tBlkSpan, &writeStride);
  648. ENCODE_LOOP_BLOCKS
  649. {
  650. BoxfilterRGBA_to_xx8(src, &g, &b, 12, 6);
  651. src += readStride;
  652. *dst++ = b;
  653. *dst++ = g;
  654. }
  655. ENCODE_LOOP_SPANS
  656. break;
  657. default:
  658. PanicAlertFmt("Unknown texture copy format: {}\n", format);
  659. break;
  660. }
  661. }
  662. static void EncodeRGB8(u8* dst, const u8* src, EFBCopyFormat format, bool yuv)
  663. {
  664. u16 sBlkCount, tBlkCount, sBlkSize, tBlkSize;
  665. s32 tSpan, sBlkSpan, tBlkSpan, writeStride;
  666. u32 readStride = 3;
  667. u8* dstBlockStart = dst;
  668. switch (format)
  669. {
  670. case EFBCopyFormat::R4:
  671. SetBlockDimensions(3, 3, &sBlkCount, &tBlkCount, &sBlkSize, &tBlkSize);
  672. SetSpans(sBlkSize, tBlkSize, &tSpan, &sBlkSpan, &tBlkSpan, &writeStride);
  673. sBlkSize /= 2;
  674. if (yuv)
  675. {
  676. ENCODE_LOOP_BLOCKS
  677. {
  678. *dst = RGB8_to_I(src[2], src[1], src[0]) & 0xf0;
  679. src += readStride;
  680. *dst |= RGB8_to_I(src[2], src[1], src[0]) >> 4;
  681. src += readStride;
  682. dst++;
  683. }
  684. ENCODE_LOOP_SPANS
  685. }
  686. else
  687. {
  688. ENCODE_LOOP_BLOCKS
  689. {
  690. *dst = src[2] & 0xf0;
  691. src += readStride;
  692. *dst |= src[2] >> 4;
  693. src += readStride;
  694. dst++;
  695. }
  696. ENCODE_LOOP_SPANS
  697. }
  698. break;
  699. case EFBCopyFormat::R8_0x1:
  700. case EFBCopyFormat::R8:
  701. SetBlockDimensions(3, 2, &sBlkCount, &tBlkCount, &sBlkSize, &tBlkSize);
  702. SetSpans(sBlkSize, tBlkSize, &tSpan, &sBlkSpan, &tBlkSpan, &writeStride);
  703. if (yuv)
  704. {
  705. ENCODE_LOOP_BLOCKS
  706. {
  707. *dst++ = RGB8_to_I(src[2], src[1], src[0]);
  708. src += readStride;
  709. }
  710. ENCODE_LOOP_SPANS
  711. }
  712. else
  713. {
  714. ENCODE_LOOP_BLOCKS
  715. {
  716. *dst++ = src[2];
  717. src += readStride;
  718. }
  719. ENCODE_LOOP_SPANS
  720. }
  721. break;
  722. case EFBCopyFormat::RA4:
  723. SetBlockDimensions(3, 2, &sBlkCount, &tBlkCount, &sBlkSize, &tBlkSize);
  724. SetSpans(sBlkSize, tBlkSize, &tSpan, &sBlkSpan, &tBlkSpan, &writeStride);
  725. if (yuv)
  726. {
  727. ENCODE_LOOP_BLOCKS
  728. {
  729. *dst++ = 0xf0 | (RGB8_to_I(src[2], src[1], src[0]) >> 4);
  730. src += readStride;
  731. }
  732. ENCODE_LOOP_SPANS
  733. }
  734. else
  735. {
  736. ENCODE_LOOP_BLOCKS
  737. {
  738. *dst++ = 0xf0 | (src[2] >> 4);
  739. src += readStride;
  740. }
  741. ENCODE_LOOP_SPANS
  742. }
  743. break;
  744. case EFBCopyFormat::RA8:
  745. SetBlockDimensions(2, 2, &sBlkCount, &tBlkCount, &sBlkSize, &tBlkSize);
  746. SetSpans(sBlkSize, tBlkSize, &tSpan, &sBlkSpan, &tBlkSpan, &writeStride);
  747. if (yuv)
  748. {
  749. ENCODE_LOOP_BLOCKS
  750. {
  751. *dst++ = 0xff;
  752. *dst++ = RGB8_to_I(src[2], src[1], src[0]);
  753. src += readStride;
  754. }
  755. ENCODE_LOOP_SPANS
  756. }
  757. else
  758. {
  759. ENCODE_LOOP_BLOCKS
  760. {
  761. *dst++ = 0xff;
  762. *dst++ = src[2];
  763. src += readStride;
  764. }
  765. ENCODE_LOOP_SPANS
  766. }
  767. break;
  768. case EFBCopyFormat::RGB565:
  769. SetBlockDimensions(2, 2, &sBlkCount, &tBlkCount, &sBlkSize, &tBlkSize);
  770. SetSpans(sBlkSize, tBlkSize, &tSpan, &sBlkSpan, &tBlkSpan, &writeStride);
  771. ENCODE_LOOP_BLOCKS
  772. {
  773. u16 val = ((src[2] << 8) & 0xf800) | ((src[1] << 3) & 0x07e0) | ((src[0] >> 3) & 0x001f);
  774. *(u16*)dst = Common::swap16(val);
  775. src += readStride;
  776. dst += 2;
  777. }
  778. ENCODE_LOOP_SPANS
  779. break;
  780. case EFBCopyFormat::RGB5A3:
  781. SetBlockDimensions(2, 2, &sBlkCount, &tBlkCount, &sBlkSize, &tBlkSize);
  782. SetSpans(sBlkSize, tBlkSize, &tSpan, &sBlkSpan, &tBlkSpan, &writeStride);
  783. ENCODE_LOOP_BLOCKS
  784. {
  785. u16 val =
  786. 0x8000 | ((src[2] << 7) & 0x7c00) | ((src[1] << 2) & 0x03e0) | ((src[0] >> 3) & 0x001f);
  787. *(u16*)dst = Common::swap16(val);
  788. src += readStride;
  789. dst += 2;
  790. }
  791. ENCODE_LOOP_SPANS
  792. break;
  793. case EFBCopyFormat::RGBA8:
  794. SetBlockDimensions(2, 2, &sBlkCount, &tBlkCount, &sBlkSize, &tBlkSize);
  795. SetSpans(sBlkSize, tBlkSize, &tSpan, &sBlkSpan, &tBlkSpan, &writeStride);
  796. ENCODE_LOOP_BLOCKS
  797. {
  798. dst[0] = 0xff;
  799. dst[1] = src[2];
  800. dst[32] = src[1];
  801. dst[33] = src[0];
  802. src += readStride;
  803. dst += 2;
  804. }
  805. ENCODE_LOOP_SPANS2
  806. break;
  807. case EFBCopyFormat::A8:
  808. SetBlockDimensions(3, 2, &sBlkCount, &tBlkCount, &sBlkSize, &tBlkSize);
  809. SetSpans(sBlkSize, tBlkSize, &tSpan, &sBlkSpan, &tBlkSpan, &writeStride);
  810. ENCODE_LOOP_BLOCKS { *dst++ = 0xff; }
  811. ENCODE_LOOP_SPANS
  812. break;
  813. case EFBCopyFormat::G8:
  814. SetBlockDimensions(3, 2, &sBlkCount, &tBlkCount, &sBlkSize, &tBlkSize);
  815. SetSpans(sBlkSize, tBlkSize, &tSpan, &sBlkSpan, &tBlkSpan, &writeStride);
  816. ENCODE_LOOP_BLOCKS
  817. {
  818. *dst++ = src[1];
  819. src += readStride;
  820. }
  821. ENCODE_LOOP_SPANS
  822. break;
  823. case EFBCopyFormat::B8:
  824. SetBlockDimensions(3, 2, &sBlkCount, &tBlkCount, &sBlkSize, &tBlkSize);
  825. SetSpans(sBlkSize, tBlkSize, &tSpan, &sBlkSpan, &tBlkSpan, &writeStride);
  826. ENCODE_LOOP_BLOCKS
  827. {
  828. *dst++ = src[0];
  829. src += readStride;
  830. }
  831. ENCODE_LOOP_SPANS
  832. break;
  833. case EFBCopyFormat::RG8:
  834. SetBlockDimensions(2, 2, &sBlkCount, &tBlkCount, &sBlkSize, &tBlkSize);
  835. SetSpans(sBlkSize, tBlkSize, &tSpan, &sBlkSpan, &tBlkSpan, &writeStride);
  836. ENCODE_LOOP_BLOCKS
  837. {
  838. // FIXME: is this backwards?
  839. *dst++ = src[1];
  840. *dst++ = src[2];
  841. src += readStride;
  842. }
  843. ENCODE_LOOP_SPANS
  844. break;
  845. case EFBCopyFormat::GB8:
  846. SetBlockDimensions(2, 2, &sBlkCount, &tBlkCount, &sBlkSize, &tBlkSize);
  847. SetSpans(sBlkSize, tBlkSize, &tSpan, &sBlkSpan, &tBlkSpan, &writeStride);
  848. ENCODE_LOOP_BLOCKS
  849. {
  850. *dst++ = src[0];
  851. *dst++ = src[1];
  852. src += readStride;
  853. }
  854. ENCODE_LOOP_SPANS
  855. break;
  856. default:
  857. PanicAlertFmt("Unknown texture copy format: {}\n", format);
  858. break;
  859. }
  860. }
  861. static void EncodeRGB8halfscale(u8* dst, const u8* src, EFBCopyFormat format, bool yuv)
  862. {
  863. u16 sBlkCount, tBlkCount, sBlkSize, tBlkSize;
  864. s32 tSpan, sBlkSpan, tBlkSpan, writeStride;
  865. u8 r, g, b;
  866. u32 readStride = 6;
  867. u8* dstBlockStart = dst;
  868. switch (format)
  869. {
  870. case EFBCopyFormat::R4:
  871. SetBlockDimensions(3, 3, &sBlkCount, &tBlkCount, &sBlkSize, &tBlkSize);
  872. SetSpans(sBlkSize, tBlkSize, &tSpan, &sBlkSpan, &tBlkSpan, &writeStride);
  873. sBlkSize /= 2;
  874. if (yuv)
  875. {
  876. ENCODE_LOOP_BLOCKS
  877. {
  878. BoxfilterRGB_to_RGB8(src, &r, &g, &b);
  879. *dst = RGB8_to_I(r, g, b) & 0xf0;
  880. src += readStride;
  881. BoxfilterRGB_to_RGB8(src, &r, &g, &b);
  882. *dst |= RGB8_to_I(r, g, b) >> 4;
  883. src += readStride;
  884. dst++;
  885. }
  886. ENCODE_LOOP_SPANS
  887. }
  888. else
  889. {
  890. ENCODE_LOOP_BLOCKS
  891. {
  892. BoxfilterRGB_to_x8(src, &r, 2);
  893. *dst = r & 0xf0;
  894. src += readStride;
  895. BoxfilterRGB_to_x8(src, &r, 2);
  896. *dst |= r >> 4;
  897. src += readStride;
  898. dst++;
  899. }
  900. ENCODE_LOOP_SPANS
  901. }
  902. break;
  903. case EFBCopyFormat::R8_0x1:
  904. case EFBCopyFormat::R8:
  905. SetBlockDimensions(3, 2, &sBlkCount, &tBlkCount, &sBlkSize, &tBlkSize);
  906. SetSpans(sBlkSize, tBlkSize, &tSpan, &sBlkSpan, &tBlkSpan, &writeStride);
  907. if (yuv)
  908. {
  909. ENCODE_LOOP_BLOCKS
  910. {
  911. BoxfilterRGB_to_RGB8(src, &r, &g, &b);
  912. *dst++ = RGB8_to_I(r, g, b);
  913. src += readStride;
  914. }
  915. ENCODE_LOOP_SPANS
  916. }
  917. else
  918. {
  919. ENCODE_LOOP_BLOCKS
  920. {
  921. BoxfilterRGB_to_x8(src, &r, 2);
  922. *dst++ = r;
  923. src += readStride;
  924. }
  925. ENCODE_LOOP_SPANS
  926. }
  927. break;
  928. case EFBCopyFormat::RA4:
  929. SetBlockDimensions(3, 2, &sBlkCount, &tBlkCount, &sBlkSize, &tBlkSize);
  930. SetSpans(sBlkSize, tBlkSize, &tSpan, &sBlkSpan, &tBlkSpan, &writeStride);
  931. if (yuv)
  932. {
  933. ENCODE_LOOP_BLOCKS
  934. {
  935. BoxfilterRGB_to_RGB8(src, &r, &g, &b);
  936. *dst++ = 0xf0 | (RGB8_to_I(r, g, b) >> 4);
  937. src += readStride;
  938. }
  939. ENCODE_LOOP_SPANS
  940. }
  941. else
  942. {
  943. ENCODE_LOOP_BLOCKS
  944. {
  945. BoxfilterRGB_to_x8(src, &r, 2);
  946. *dst++ = 0xf0 | (r >> 4);
  947. src += readStride;
  948. }
  949. ENCODE_LOOP_SPANS
  950. }
  951. break;
  952. case EFBCopyFormat::RA8:
  953. SetBlockDimensions(2, 2, &sBlkCount, &tBlkCount, &sBlkSize, &tBlkSize);
  954. SetSpans(sBlkSize, tBlkSize, &tSpan, &sBlkSpan, &tBlkSpan, &writeStride);
  955. if (yuv)
  956. {
  957. ENCODE_LOOP_BLOCKS
  958. {
  959. BoxfilterRGB_to_RGB8(src, &r, &g, &b);
  960. *dst++ = 0xff;
  961. *dst++ = RGB8_to_I(r, g, b);
  962. src += readStride;
  963. }
  964. ENCODE_LOOP_SPANS
  965. }
  966. else
  967. {
  968. ENCODE_LOOP_BLOCKS
  969. {
  970. BoxfilterRGB_to_x8(src, &r, 2);
  971. *dst++ = 0xff;
  972. *dst++ = r;
  973. src += readStride;
  974. }
  975. ENCODE_LOOP_SPANS
  976. }
  977. break;
  978. case EFBCopyFormat::RGB565:
  979. SetBlockDimensions(2, 2, &sBlkCount, &tBlkCount, &sBlkSize, &tBlkSize);
  980. SetSpans(sBlkSize, tBlkSize, &tSpan, &sBlkSpan, &tBlkSpan, &writeStride);
  981. ENCODE_LOOP_BLOCKS
  982. {
  983. BoxfilterRGB_to_RGB8(src, &r, &g, &b);
  984. u16 val = ((r << 8) & 0xf800) | ((g << 3) & 0x07e0) | ((b >> 3) & 0x001f);
  985. *(u16*)dst = Common::swap16(val);
  986. src += readStride;
  987. dst += 2;
  988. }
  989. ENCODE_LOOP_SPANS
  990. break;
  991. case EFBCopyFormat::RGB5A3:
  992. SetBlockDimensions(2, 2, &sBlkCount, &tBlkCount, &sBlkSize, &tBlkSize);
  993. SetSpans(sBlkSize, tBlkSize, &tSpan, &sBlkSpan, &tBlkSpan, &writeStride);
  994. ENCODE_LOOP_BLOCKS
  995. {
  996. BoxfilterRGB_to_RGB8(src, &r, &g, &b);
  997. u16 val = 0x8000 | ((r << 7) & 0x7c00) | ((g << 2) & 0x03e0) | ((b >> 3) & 0x001f);
  998. *(u16*)dst = Common::swap16(val);
  999. src += readStride;
  1000. dst += 2;
  1001. }
  1002. ENCODE_LOOP_SPANS
  1003. break;
  1004. case EFBCopyFormat::RGBA8:
  1005. SetBlockDimensions(2, 2, &sBlkCount, &tBlkCount, &sBlkSize, &tBlkSize);
  1006. SetSpans(sBlkSize, tBlkSize, &tSpan, &sBlkSpan, &tBlkSpan, &writeStride);
  1007. ENCODE_LOOP_BLOCKS
  1008. {
  1009. BoxfilterRGB_to_RGB8(src, &r, &g, &b);
  1010. dst[0] = 0xff;
  1011. dst[1] = r;
  1012. dst[32] = g;
  1013. dst[33] = b;
  1014. src += readStride;
  1015. dst += 2;
  1016. }
  1017. ENCODE_LOOP_SPANS2
  1018. break;
  1019. case EFBCopyFormat::A8:
  1020. SetBlockDimensions(3, 2, &sBlkCount, &tBlkCount, &sBlkSize, &tBlkSize);
  1021. SetSpans(sBlkSize, tBlkSize, &tSpan, &sBlkSpan, &tBlkSpan, &writeStride);
  1022. ENCODE_LOOP_BLOCKS { *dst++ = 0xff; }
  1023. ENCODE_LOOP_SPANS
  1024. break;
  1025. case EFBCopyFormat::G8:
  1026. SetBlockDimensions(3, 2, &sBlkCount, &tBlkCount, &sBlkSize, &tBlkSize);
  1027. SetSpans(sBlkSize, tBlkSize, &tSpan, &sBlkSpan, &tBlkSpan, &writeStride);
  1028. ENCODE_LOOP_BLOCKS
  1029. {
  1030. BoxfilterRGB_to_x8(src, &g, 1);
  1031. *dst++ = g;
  1032. src += readStride;
  1033. }
  1034. ENCODE_LOOP_SPANS
  1035. break;
  1036. case EFBCopyFormat::B8:
  1037. SetBlockDimensions(3, 2, &sBlkCount, &tBlkCount, &sBlkSize, &tBlkSize);
  1038. SetSpans(sBlkSize, tBlkSize, &tSpan, &sBlkSpan, &tBlkSpan, &writeStride);
  1039. ENCODE_LOOP_BLOCKS
  1040. {
  1041. BoxfilterRGB_to_x8(src, &b, 0);
  1042. *dst++ = b;
  1043. src += readStride;
  1044. }
  1045. ENCODE_LOOP_SPANS
  1046. break;
  1047. case EFBCopyFormat::RG8:
  1048. SetBlockDimensions(2, 2, &sBlkCount, &tBlkCount, &sBlkSize, &tBlkSize);
  1049. SetSpans(sBlkSize, tBlkSize, &tSpan, &sBlkSpan, &tBlkSpan, &writeStride);
  1050. ENCODE_LOOP_BLOCKS
  1051. {
  1052. BoxfilterRGB_to_xx8(src, &r, &g, 2, 1);
  1053. *dst++ = g;
  1054. *dst++ = r;
  1055. src += readStride;
  1056. }
  1057. ENCODE_LOOP_SPANS
  1058. break;
  1059. case EFBCopyFormat::GB8:
  1060. SetBlockDimensions(2, 2, &sBlkCount, &tBlkCount, &sBlkSize, &tBlkSize);
  1061. SetSpans(sBlkSize, tBlkSize, &tSpan, &sBlkSpan, &tBlkSpan, &writeStride);
  1062. ENCODE_LOOP_BLOCKS
  1063. {
  1064. BoxfilterRGB_to_xx8(src, &g, &b, 1, 0);
  1065. *dst++ = b;
  1066. *dst++ = g;
  1067. src += readStride;
  1068. }
  1069. ENCODE_LOOP_SPANS
  1070. break;
  1071. default:
  1072. PanicAlertFmt("Unknown texture copy format: {}\n", format);
  1073. break;
  1074. }
  1075. }
  1076. static void EncodeZ24(u8* dst, const u8* src, EFBCopyFormat format)
  1077. {
  1078. u16 sBlkCount, tBlkCount, sBlkSize, tBlkSize;
  1079. s32 tSpan, sBlkSpan, tBlkSpan, writeStride;
  1080. u32 readStride = 3;
  1081. u8* dstBlockStart = dst;
  1082. switch (format)
  1083. {
  1084. case EFBCopyFormat::R8_0x1:
  1085. case EFBCopyFormat::R8:
  1086. SetBlockDimensions(3, 2, &sBlkCount, &tBlkCount, &sBlkSize, &tBlkSize);
  1087. SetSpans(sBlkSize, tBlkSize, &tSpan, &sBlkSpan, &tBlkSpan, &writeStride);
  1088. ENCODE_LOOP_BLOCKS
  1089. {
  1090. *dst++ = src[2];
  1091. src += readStride;
  1092. }
  1093. ENCODE_LOOP_SPANS
  1094. break;
  1095. // FIXME: handle RA8?
  1096. case EFBCopyFormat::RGBA8:
  1097. SetBlockDimensions(2, 2, &sBlkCount, &tBlkCount, &sBlkSize, &tBlkSize);
  1098. SetSpans(sBlkSize, tBlkSize, &tSpan, &sBlkSpan, &tBlkSpan, &writeStride);
  1099. ENCODE_LOOP_BLOCKS
  1100. {
  1101. dst[0] = 0xff;
  1102. dst[1] = src[2];
  1103. dst[32] = src[1];
  1104. dst[33] = src[0];
  1105. src += readStride;
  1106. dst += 2;
  1107. }
  1108. ENCODE_LOOP_SPANS2
  1109. break;
  1110. case EFBCopyFormat::R4:
  1111. SetBlockDimensions(3, 3, &sBlkCount, &tBlkCount, &sBlkSize, &tBlkSize);
  1112. SetSpans(sBlkSize, tBlkSize, &tSpan, &sBlkSpan, &tBlkSpan, &writeStride);
  1113. sBlkSize /= 2;
  1114. ENCODE_LOOP_BLOCKS
  1115. {
  1116. *dst = src[2] & 0xf0;
  1117. src += readStride;
  1118. *dst |= src[2] >> 4;
  1119. src += readStride;
  1120. dst++;
  1121. }
  1122. ENCODE_LOOP_SPANS
  1123. break;
  1124. case EFBCopyFormat::G8:
  1125. SetBlockDimensions(3, 2, &sBlkCount, &tBlkCount, &sBlkSize, &tBlkSize);
  1126. SetSpans(sBlkSize, tBlkSize, &tSpan, &sBlkSpan, &tBlkSpan, &writeStride);
  1127. ENCODE_LOOP_BLOCKS
  1128. {
  1129. *dst++ = src[1];
  1130. src += readStride;
  1131. }
  1132. ENCODE_LOOP_SPANS
  1133. break;
  1134. case EFBCopyFormat::B8:
  1135. SetBlockDimensions(3, 2, &sBlkCount, &tBlkCount, &sBlkSize, &tBlkSize);
  1136. SetSpans(sBlkSize, tBlkSize, &tSpan, &sBlkSpan, &tBlkSpan, &writeStride);
  1137. ENCODE_LOOP_BLOCKS
  1138. {
  1139. *dst++ = src[0];
  1140. src += readStride;
  1141. }
  1142. ENCODE_LOOP_SPANS
  1143. break;
  1144. case EFBCopyFormat::RG8:
  1145. SetBlockDimensions(2, 2, &sBlkCount, &tBlkCount, &sBlkSize, &tBlkSize);
  1146. SetSpans(sBlkSize, tBlkSize, &tSpan, &sBlkSpan, &tBlkSpan, &writeStride);
  1147. ENCODE_LOOP_BLOCKS
  1148. {
  1149. // FIXME: should these be reversed?
  1150. *dst++ = src[1];
  1151. *dst++ = src[2];
  1152. src += readStride;
  1153. }
  1154. ENCODE_LOOP_SPANS
  1155. break;
  1156. case EFBCopyFormat::GB8:
  1157. SetBlockDimensions(2, 2, &sBlkCount, &tBlkCount, &sBlkSize, &tBlkSize);
  1158. SetSpans(sBlkSize, tBlkSize, &tSpan, &sBlkSpan, &tBlkSpan, &writeStride);
  1159. ENCODE_LOOP_BLOCKS
  1160. {
  1161. *dst++ = src[0];
  1162. *dst++ = src[1];
  1163. src += readStride;
  1164. }
  1165. ENCODE_LOOP_SPANS
  1166. break;
  1167. default:
  1168. PanicAlertFmt("Unknown texture copy format: {}\n", format);
  1169. break;
  1170. }
  1171. }
  1172. static void EncodeZ24halfscale(u8* dst, const u8* src, EFBCopyFormat format)
  1173. {
  1174. u16 sBlkCount, tBlkCount, sBlkSize, tBlkSize;
  1175. s32 tSpan, sBlkSpan, tBlkSpan, writeStride;
  1176. u32 readStride = 6;
  1177. u8 r, g, b;
  1178. u8* dstBlockStart = dst;
  1179. switch (format)
  1180. {
  1181. case EFBCopyFormat::R8_0x1:
  1182. case EFBCopyFormat::R8:
  1183. SetBlockDimensions(3, 2, &sBlkCount, &tBlkCount, &sBlkSize, &tBlkSize);
  1184. SetSpans(sBlkSize, tBlkSize, &tSpan, &sBlkSpan, &tBlkSpan, &writeStride);
  1185. ENCODE_LOOP_BLOCKS
  1186. {
  1187. BoxfilterRGB_to_x8(src, &b, 2);
  1188. *dst++ = b;
  1189. src += readStride;
  1190. }
  1191. ENCODE_LOOP_SPANS
  1192. break;
  1193. // FIXME: handle RA8?
  1194. case EFBCopyFormat::RGBA8:
  1195. SetBlockDimensions(2, 2, &sBlkCount, &tBlkCount, &sBlkSize, &tBlkSize);
  1196. SetSpans(sBlkSize, tBlkSize, &tSpan, &sBlkSpan, &tBlkSpan, &writeStride);
  1197. ENCODE_LOOP_BLOCKS
  1198. {
  1199. BoxfilterRGB_to_RGB8(src, &dst[33], &dst[32], &dst[1]);
  1200. dst[0] = 255;
  1201. src += readStride;
  1202. dst += 2;
  1203. }
  1204. ENCODE_LOOP_SPANS2
  1205. break;
  1206. case EFBCopyFormat::R4: // Z4
  1207. SetBlockDimensions(3, 3, &sBlkCount, &tBlkCount, &sBlkSize, &tBlkSize);
  1208. SetSpans(sBlkSize, tBlkSize, &tSpan, &sBlkSpan, &tBlkSpan, &writeStride);
  1209. sBlkSize /= 2;
  1210. ENCODE_LOOP_BLOCKS
  1211. {
  1212. BoxfilterRGB_to_x8(src, &b, 2);
  1213. *dst = b & 0xf0;
  1214. src += readStride;
  1215. BoxfilterRGB_to_x8(src, &b, 2);
  1216. *dst |= b >> 4;
  1217. src += readStride;
  1218. dst++;
  1219. }
  1220. ENCODE_LOOP_SPANS
  1221. break;
  1222. case EFBCopyFormat::G8: // Z8M
  1223. SetBlockDimensions(3, 2, &sBlkCount, &tBlkCount, &sBlkSize, &tBlkSize);
  1224. SetSpans(sBlkSize, tBlkSize, &tSpan, &sBlkSpan, &tBlkSpan, &writeStride);
  1225. ENCODE_LOOP_BLOCKS
  1226. {
  1227. BoxfilterRGB_to_x8(src, &g, 1);
  1228. *dst++ = g;
  1229. src += readStride;
  1230. }
  1231. ENCODE_LOOP_SPANS
  1232. break;
  1233. case EFBCopyFormat::B8: // Z8L
  1234. SetBlockDimensions(3, 2, &sBlkCount, &tBlkCount, &sBlkSize, &tBlkSize);
  1235. SetSpans(sBlkSize, tBlkSize, &tSpan, &sBlkSpan, &tBlkSpan, &writeStride);
  1236. ENCODE_LOOP_BLOCKS
  1237. {
  1238. BoxfilterRGB_to_x8(src, &r, 0);
  1239. *dst++ = r;
  1240. src += readStride;
  1241. }
  1242. ENCODE_LOOP_SPANS
  1243. break;
  1244. case EFBCopyFormat::RG8: // RG8
  1245. SetBlockDimensions(2, 2, &sBlkCount, &tBlkCount, &sBlkSize, &tBlkSize);
  1246. SetSpans(sBlkSize, tBlkSize, &tSpan, &sBlkSpan, &tBlkSpan, &writeStride);
  1247. ENCODE_LOOP_BLOCKS
  1248. {
  1249. BoxfilterRGB_to_xx8(src, &r, &g, 0, 1);
  1250. *dst++ = g;
  1251. *dst++ = r;
  1252. src += readStride;
  1253. }
  1254. ENCODE_LOOP_SPANS
  1255. break;
  1256. case EFBCopyFormat::GB8:
  1257. SetBlockDimensions(2, 2, &sBlkCount, &tBlkCount, &sBlkSize, &tBlkSize);
  1258. SetSpans(sBlkSize, tBlkSize, &tSpan, &sBlkSpan, &tBlkSpan, &writeStride);
  1259. ENCODE_LOOP_BLOCKS
  1260. {
  1261. BoxfilterRGB_to_xx8(src, &g, &b, 1, 2);
  1262. // FIXME: is this backwards?
  1263. *dst++ = b;
  1264. *dst++ = g;
  1265. src += readStride;
  1266. }
  1267. ENCODE_LOOP_SPANS
  1268. break;
  1269. default:
  1270. PanicAlertFmt("Unknown texture copy format: {}\n", format);
  1271. break;
  1272. }
  1273. }
  1274. namespace
  1275. {
  1276. void EncodeEfbCopy(u8* dst, const EFBCopyParams& params, u32 native_width, u32 bytes_per_row,
  1277. u32 num_blocks_y, u32 memory_stride, const MathUtil::Rectangle<int>& src_rect,
  1278. bool scale_by_half)
  1279. {
  1280. const u8* src = EfbInterface::GetPixelPointer(src_rect.left, src_rect.top, params.depth);
  1281. if (scale_by_half)
  1282. {
  1283. switch (params.efb_format)
  1284. {
  1285. case PixelFormat::RGBA6_Z24:
  1286. EncodeRGBA6halfscale(dst, src, params.copy_format, params.yuv);
  1287. break;
  1288. case PixelFormat::RGB8_Z24:
  1289. EncodeRGB8halfscale(dst, src, params.copy_format, params.yuv);
  1290. break;
  1291. case PixelFormat::RGB565_Z16:
  1292. EncodeRGB8halfscale(dst, src, params.copy_format, params.yuv);
  1293. break;
  1294. case PixelFormat::Z24:
  1295. EncodeZ24halfscale(dst, src, params.copy_format);
  1296. break;
  1297. default:
  1298. break;
  1299. }
  1300. }
  1301. else
  1302. {
  1303. switch (params.efb_format)
  1304. {
  1305. case PixelFormat::RGBA6_Z24:
  1306. EncodeRGBA6(dst, src, params.copy_format, params.yuv);
  1307. break;
  1308. case PixelFormat::RGB8_Z24:
  1309. EncodeRGB8(dst, src, params.copy_format, params.yuv);
  1310. break;
  1311. case PixelFormat::RGB565_Z16:
  1312. EncodeRGB8(dst, src, params.copy_format, params.yuv);
  1313. break;
  1314. case PixelFormat::Z24:
  1315. EncodeZ24(dst, src, params.copy_format);
  1316. break;
  1317. default:
  1318. break;
  1319. }
  1320. }
  1321. }
  1322. } // namespace
  1323. void Encode(AbstractStagingTexture* dst, const EFBCopyParams& params, u32 native_width,
  1324. u32 bytes_per_row, u32 num_blocks_y, u32 memory_stride,
  1325. const MathUtil::Rectangle<int>& src_rect, bool scale_by_half, float y_scale,
  1326. float gamma)
  1327. {
  1328. // HACK: Override the memory stride for this staging texture with new copy stride.
  1329. // This is required because the texture encoder assumes that we're writing directly to memory,
  1330. // and each row is tightly packed with no padding, whereas our encoding abstract texture has
  1331. // a width of 2560. When we copy the texture back later on, it'll use the tightly packed stride.
  1332. ASSERT(memory_stride <= (dst->GetConfig().width * dst->GetTexelSize()));
  1333. static_cast<SW::SWStagingTexture*>(dst)->SetMapStride(memory_stride);
  1334. if (params.copy_format == EFBCopyFormat::XFB)
  1335. {
  1336. EfbInterface::EncodeXFB(reinterpret_cast<u8*>(dst->GetMappedPointer()), native_width, src_rect,
  1337. y_scale, gamma);
  1338. }
  1339. else
  1340. {
  1341. EncodeEfbCopy(reinterpret_cast<u8*>(dst->GetMappedPointer()), params, native_width,
  1342. bytes_per_row, num_blocks_y, memory_stride, src_rect, scale_by_half);
  1343. }
  1344. }
  1345. } // namespace TextureEncoder