TextureDecoder.cl 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. // Copyright (C) 2003 Dolphin Project.
  2. // This program is free software: you can redistribute it and/or modify
  3. // it under the terms of the GNU General Public License as published by
  4. // the Free Software Foundation, version 2.0.
  5. // This program is distributed in the hope that it will be useful,
  6. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. // GNU General Public License 2.0 for more details.
  9. // A copy of the GPL 2.0 should have been included with the program.
  10. // If not, see http://www.gnu.org/licenses/
  11. // Official SVN repository and contact information can be found at
  12. // http://code.google.com/p/dolphin-emu/
  13. kernel void DecodeI4(global uchar *dst,
  14. const global uchar *src, int width)
  15. {
  16. int x = get_global_id(0) * 8, y = get_global_id(1) * 8;
  17. int srcOffset = x + y * width / 8;
  18. for (int iy = 0; iy < 8; iy++)
  19. {
  20. uchar4 val = vload4(srcOffset, src);
  21. uchar8 res;
  22. res.even = (val >> (uchar4)4) & (uchar4)0x0F;
  23. res.odd = val & (uchar4)0x0F;
  24. res |= res << (uchar8)4;
  25. vstore8(res, 0, dst + ((y + iy)*width + x));
  26. srcOffset++;
  27. }
  28. }
  29. kernel void DecodeI4_RGBA(global uint *dst,
  30. const global uchar *src, int width)
  31. {
  32. int x = get_global_id(0) * 8, y = get_global_id(1) * 8;
  33. int srcOffset = x + y * width / 8;
  34. for (int iy = 0; iy < 8; iy++)
  35. {
  36. uchar4 val = vload4(srcOffset, src);
  37. uchar8 res;
  38. res.even = (val >> (uchar4)4) & (uchar4)0x0F;
  39. res.odd = val & (uchar4)0x0F;
  40. res |= res << (uchar8)4;
  41. vstore8(upsample(upsample(res,res),upsample(res,res)), 0, dst + ((y + iy)*width + x));
  42. srcOffset++;
  43. }
  44. }
  45. kernel void DecodeI8(global uchar *dst,
  46. const global uchar *src, int width)
  47. {
  48. int x = get_global_id(0) * 8, y = get_global_id(1) * 4;
  49. int srcOffset = ((x * 4) + (y * width)) / 8;
  50. for (int iy = 0; iy < 4; iy++)
  51. {
  52. vstore8(vload8(srcOffset++, src),
  53. 0, dst + ((y + iy)*width + x));
  54. }
  55. }
  56. kernel void DecodeI8_RGBA(global uint *dst,
  57. const global uchar *src, int width)
  58. {
  59. int x = get_global_id(0) * 8, y = get_global_id(1) * 4;
  60. int srcOffset = ((x * 4) + (y * width)) / 8;
  61. for (int iy = 0; iy < 4; iy++)
  62. {
  63. uchar8 val = vload8(srcOffset++, src);
  64. vstore8(upsample(upsample(val,val),upsample(val,val)),
  65. 0, dst + ((y + iy)*width + x));
  66. }
  67. }
  68. kernel void DecodeIA8(global ushort *dst,
  69. const global uchar *src, int width)
  70. {
  71. int x = get_global_id(0) * 4, y = get_global_id(1) * 4;
  72. int srcOffset = ((x * 4) + (y * width)) / 4;
  73. for (int iy = 0; iy < 4; iy++)
  74. {
  75. uchar8 val = vload8(srcOffset++, src);
  76. vstore4(upsample(val.even, val.odd), 0, dst + ((y + iy)*width + x));
  77. }
  78. }
  79. kernel void DecodeIA8_RGBA(global uint *dst,
  80. const global uchar *src, int width)
  81. {
  82. int x = get_global_id(0) * 4, y = get_global_id(1) * 4;
  83. int srcOffset = ((x * 4) + (y * width)) / 4;
  84. for (int iy = 0; iy < 4; iy++)
  85. {
  86. uchar8 val = vload8(srcOffset++, src);
  87. vstore4(upsample(upsample(val.even,val.odd),upsample(val.odd, val.odd)), 0, dst + ((y + iy)*width + x));
  88. }
  89. }
  90. kernel void DecodeIA4(global ushort *dst,
  91. const global uchar *src, int width)
  92. {
  93. int x = get_global_id(0) * 8, y = get_global_id(1) * 4;
  94. int srcOffset = ((x * 4) + (y * width)) / 8;
  95. uchar8 val;
  96. ushort8 res;
  97. for (int iy = 0; iy < 4; iy++)
  98. {
  99. val = vload8(srcOffset++, src);
  100. res = upsample(val >> (uchar8)4, val & (uchar8)0xF);
  101. res |= res << (ushort8)4;
  102. vstore8(res, 0, dst + y*width + x);
  103. dst+=width;
  104. }
  105. }
  106. kernel void DecodeIA4_RGBA(global uint *dst,
  107. const global uchar *src, int width)
  108. {
  109. int x = get_global_id(0) * 8, y = get_global_id(1) * 4;
  110. int srcOffset = ((x * 4) + (y * width)) / 8;
  111. uchar8 val;
  112. uint8 res;
  113. for (int iy = 0; iy < 4; iy++)
  114. {
  115. val = vload8(srcOffset++, src);
  116. uchar8 a = val >> (uchar8)4;
  117. uchar8 l = val & (uchar8)0xF;
  118. res = upsample(upsample(a, l), upsample(l,l));
  119. res |= res << (uint8)4;
  120. vstore8(res, 0, dst + y*width + x);
  121. dst+=width;
  122. }
  123. }
  124. kernel void DecodeRGBA8(global ushort *dst,
  125. const global ushort *src, int width)
  126. {
  127. int x = get_global_id(0) * 4, y = get_global_id(1) * 4;
  128. int srcOffset = (x * 2) + (y * width) / 2;
  129. for (int iy = 0; iy < 4; iy++)
  130. {
  131. ushort8 val = (ushort8)(vload4(srcOffset, src), vload4(srcOffset + 4, src));
  132. ushort8 temp = rotate(val, (ushort8)4);
  133. ushort8 bgra = rotate(temp, (ushort8)4).s40516273;
  134. vstore8(bgra, 0, dst + ((y + iy)*width + x) * 2);
  135. srcOffset++;
  136. }
  137. }
  138. kernel void DecodeRGBA8_RGBA(global uchar *dst,
  139. const global uchar *src, int width)
  140. {
  141. int x = get_global_id(0) * 4, y = get_global_id(1) * 4;
  142. int srcOffset = (x * 2) + (y * width) / 2;
  143. for (int iy = 0; iy < 4; iy++)
  144. {
  145. uchar8 ar = vload8(srcOffset, src);
  146. uchar8 gb = vload8(srcOffset + 4, src);
  147. uchar16 res;
  148. res.even.even = ar.odd;
  149. res.even.odd = gb.odd;
  150. res.odd.even = gb.even;
  151. res.odd.odd = ar.even;
  152. vstore16(res, 0, dst + ((y + iy)*width + x) * 4);
  153. srcOffset++;
  154. }
  155. }
  156. kernel void DecodeRGB565(global ushort *dst,
  157. const global ushort *src, int width)
  158. {
  159. int x = get_global_id(0) * 4, y = get_global_id(1) * 4;
  160. int srcOffset = x + (y * width) / 4;
  161. dst += width*y + x;
  162. for (int iy = 0; iy < 4; iy++)
  163. {
  164. ushort4 val = rotate(vload4(srcOffset++, src),(ushort4)4);
  165. vstore4(rotate(val,(ushort4)4), 0, dst + iy*width);
  166. }
  167. }
  168. kernel void DecodeRGB565_RGBA(global uchar *dst,
  169. const global uchar *src, int width)
  170. {
  171. int x = get_global_id(0) * 4, y = get_global_id(1) * 4;
  172. int srcOffset = x + (y * width) / 4;
  173. for (int iy = 0; iy < 4; iy++)
  174. {
  175. uchar8 val = vload8(srcOffset++, src);
  176. uchar16 res;
  177. res.even.even = bitselect(val.even, val.even >> (uchar4)5, (uchar4)7);
  178. res.odd.even = bitselect((val.odd >> (uchar4)3) | (val.even << (uchar4)5), val.even >> (uchar4)1, (uchar4)3);
  179. res.even.odd = bitselect(val.odd << (uchar4)3, val.odd >> (uchar4)2, (uchar4)7);
  180. res.odd.odd = (uchar4)0xFF;
  181. vstore16(res, 0, dst + ((y + iy)*width + x) * 4);
  182. }
  183. }
  184. kernel void DecodeRGB5A3(global uchar *dst,
  185. const global uchar *src, int width)
  186. {
  187. int x = get_global_id(0) * 4, y = get_global_id(1) * 4;
  188. int srcOffset = x + (y * width) / 4;
  189. uchar8 val;
  190. uchar16 resNoAlpha, resAlpha, choice;
  191. #define iterateRGB5A3() \
  192. val = vload8(srcOffset++, src); \
  193. resNoAlpha.s26AE = val.even << (uchar4)1; \
  194. resNoAlpha.s159D = val.even << (uchar4)6 | val.odd >> (uchar4)2; \
  195. resNoAlpha.s048C = val.odd << (uchar4)3; \
  196. resNoAlpha = bitselect(resNoAlpha, resNoAlpha >> (uchar16)5, (uchar16)0x3); \
  197. resNoAlpha.s37BF = (uchar4)(0xFF); \
  198. resAlpha.s26AE = bitselect(val.even << (uchar4)4, val.even, (uchar4)0xF); \
  199. resAlpha.s159D = bitselect(val.odd, val.odd >> (uchar4)4, (uchar4)0xF); \
  200. resAlpha.s048C = bitselect(val.odd << (uchar4)4, val.odd, (uchar4)0xF); \
  201. resAlpha.s37BF = bitselect(val.even << (uchar4)1, val.even >> (uchar4)2, (uchar4)0x1C); \
  202. resAlpha.s37BF = bitselect(resAlpha.s37BF, val.even >> (uchar4)5, (uchar4)0x3); \
  203. choice = (uchar16)((uchar4)(val.even.s0), \
  204. (uchar4)(val.even.s1), \
  205. (uchar4)(val.even.s2), \
  206. (uchar4)(val.even.s3)); \
  207. vstore16(select(resAlpha, resNoAlpha, choice), 0, dst + (y * width + x) * 4);
  208. iterateRGB5A3(); dst += width*4;
  209. iterateRGB5A3(); dst += width*4;
  210. iterateRGB5A3(); dst += width*4;
  211. iterateRGB5A3();
  212. }
  213. kernel void DecodeRGB5A3_RGBA(global uchar *dst,
  214. const global uchar *src, int width)
  215. {
  216. int x = get_global_id(0) * 4, y = get_global_id(1) * 4;
  217. int srcOffset = x + (y * width) / 4;
  218. uchar8 val;
  219. uchar16 resNoAlpha, resAlpha, choice;
  220. #define iterateRGB5A3_RGBA() \
  221. val = vload8(srcOffset++, src); \
  222. resNoAlpha.s048C = val.even << (uchar4)1; \
  223. resNoAlpha.s159D = val.even << (uchar4)6 | val.odd >> (uchar4)2; \
  224. resNoAlpha.s26AE = val.odd << (uchar4)3; \
  225. resNoAlpha = bitselect(resNoAlpha, resNoAlpha >> (uchar16)5, (uchar16)0x3); \
  226. resNoAlpha.s37BF = (uchar4)(0xFF); \
  227. resAlpha.s048C = bitselect(val.even << (uchar4)4, val.even, (uchar4)0xF); \
  228. resAlpha.s159D = bitselect(val.odd, val.odd >> (uchar4)4, (uchar4)0xF); \
  229. resAlpha.s26AE = bitselect(val.odd << (uchar4)4, val.odd, (uchar4)0xF); \
  230. resAlpha.s37BF = bitselect(val.even << (uchar4)1, val.even >> (uchar4)2, (uchar4)0x1C); \
  231. resAlpha.s37BF = bitselect(resAlpha.s37BF, val.even >> (uchar4)5, (uchar4)0x3); \
  232. choice = (uchar16)((uchar4)(val.even.s0), \
  233. (uchar4)(val.even.s1), \
  234. (uchar4)(val.even.s2), \
  235. (uchar4)(val.even.s3)); \
  236. vstore16(select(resAlpha, resNoAlpha, choice), 0, dst + (y * width + x) * 4);
  237. iterateRGB5A3_RGBA(); dst += width*4;
  238. iterateRGB5A3_RGBA(); dst += width*4;
  239. iterateRGB5A3_RGBA(); dst += width*4;
  240. iterateRGB5A3_RGBA();
  241. }
  242. uint16 unpack(uchar b)
  243. {
  244. return (uint16)((uint4)(b >> 3 & 0x18),
  245. (uint4)(b >> 1 & 0x18),
  246. (uint4)(b << 1 & 0x18),
  247. (uint4)(b << 3 & 0x18));
  248. }
  249. kernel void decodeCMPRBlock(global uchar *dst,
  250. const global uchar *src, int width)
  251. {
  252. int x = get_global_id(0) * 4, y = get_global_id(1) * 4;
  253. uchar8 val = vload8(0, src);
  254. uchar2 colora565 = (uchar2)(val.s1, val.s3);
  255. uchar2 colorb565 = (uchar2)(val.s0, val.s2);
  256. uchar8 color32 = (uchar8)(bitselect(colora565 << (uchar2)3, colora565 >> (uchar2)2, (uchar2)7),
  257. bitselect((colora565 >> (uchar2)3) | (colorb565 << (uchar2)5), colorb565 >> (uchar2)1, (uchar2)3),
  258. bitselect(colorb565, colorb565 >> (uchar2)5, (uchar2)7),
  259. (uchar2)0xFF);
  260. ushort4 frac2 = convert_ushort4(color32.even) - convert_ushort4(color32.odd);
  261. uchar4 frac = convert_uchar4((frac2 * (ushort4)3) / (ushort4)8);
  262. ushort4 colorAlpha = upsample((uchar4)(color32.even.s0,color32.even.s1,color32.even.s2,0),
  263. rhadd(color32.odd, color32.even));
  264. colorAlpha.s3 = 0xFF;
  265. ushort4 colorNoAlpha = upsample(color32.odd + frac, color32.even - frac);
  266. uint4 colors = upsample((upsample(val.s0,val.s1) > upsample(val.s2,val.s3))?colorNoAlpha:colorAlpha,
  267. upsample(color32.odd, color32.even));
  268. uint16 colorsFull = (uint16)(colors, colors, colors, colors);
  269. vstore16(convert_uchar16(colorsFull >> unpack(val.s4)), 0, dst);
  270. vstore16(convert_uchar16(colorsFull >> unpack(val.s5)), 0, dst+=width*4);
  271. vstore16(convert_uchar16(colorsFull >> unpack(val.s6)), 0, dst+=width*4);
  272. vstore16(convert_uchar16(colorsFull >> unpack(val.s7)), 0, dst+=width*4);
  273. }
  274. kernel void DecodeCMPR(global uchar *dst,
  275. const global uchar *src, int width)
  276. {
  277. int x = get_global_id(0) * 8, y = get_global_id(1) * 8;
  278. src += x * 4 + (y * width) / 2;
  279. dst += (y * width + x) * 4;
  280. decodeCMPRBlock(dst, src, width); src += 8;
  281. decodeCMPRBlock(dst + 16, src, width); src += 8;
  282. decodeCMPRBlock(dst + 16 * width, src, width); src += 8;
  283. decodeCMPRBlock(dst + 16 * (width + 1), src, width);
  284. }
  285. kernel void decodeCMPRBlock_RGBA(global uchar *dst,
  286. const global uchar *src, int width)
  287. {
  288. int x = get_global_id(0) * 4, y = get_global_id(1) * 4;
  289. uchar8 val = vload8(0, src);
  290. uchar2 colora565 = (uchar2)(val.s1, val.s3);
  291. uchar2 colorb565 = (uchar2)(val.s0, val.s2);
  292. uchar8 color32 = (uchar8)(bitselect(colorb565, colorb565 >> (uchar2)5, (uchar2)7),
  293. bitselect((colora565 >> (uchar2)3) | (colorb565 << (uchar2)5), colorb565 >> (uchar2)1, (uchar2)3),
  294. bitselect(colora565 << (uchar2)3, colora565 >> (uchar2)2, (uchar2)7),
  295. (uchar2)0xFF);
  296. ushort4 frac2 = convert_ushort4(color32.even) - convert_ushort4(color32.odd);
  297. uchar4 frac = convert_uchar4((frac2 * (ushort4)3) / (ushort4)8);
  298. ushort4 colorAlpha = upsample((uchar4)(color32.even.s0,color32.even.s1,color32.even.s2,0),
  299. rhadd(color32.odd, color32.even));
  300. colorAlpha.s3 = 0xFF;
  301. ushort4 colorNoAlpha = upsample(color32.odd + frac, color32.even - frac);
  302. uint4 colors = upsample((upsample(val.s0,val.s1) > upsample(val.s2,val.s3))?colorNoAlpha:colorAlpha,
  303. upsample(color32.odd, color32.even));
  304. uint16 colorsFull = (uint16)(colors, colors, colors, colors);
  305. vstore16(convert_uchar16(colorsFull >> unpack(val.s4)), 0, dst);
  306. vstore16(convert_uchar16(colorsFull >> unpack(val.s5)), 0, dst+=width*4);
  307. vstore16(convert_uchar16(colorsFull >> unpack(val.s6)), 0, dst+=width*4);
  308. vstore16(convert_uchar16(colorsFull >> unpack(val.s7)), 0, dst+=width*4);
  309. }
  310. kernel void DecodeCMPR_RGBA(global uchar *dst,
  311. const global uchar *src, int width)
  312. {
  313. int x = get_global_id(0) * 8, y = get_global_id(1) * 8;
  314. src += x * 4 + (y * width) / 2;
  315. dst += (y * width + x) * 4;
  316. decodeCMPRBlock_RGBA(dst, src, width); src += 8;
  317. decodeCMPRBlock_RGBA(dst + 16, src, width); src += 8;
  318. decodeCMPRBlock_RGBA(dst + 16 * width, src, width); src += 8;
  319. decodeCMPRBlock_RGBA(dst + 16 * (width + 1), src, width);
  320. }