image.cpp 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342
  1. /*
  2. * Copyright 2011-2013 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #include "bgfx_p.h"
  6. #include <bx/float4_t.h>
  7. #include <math.h> // powf, sqrtf
  8. #include "image.h"
  9. namespace bgfx
  10. {
  11. static const uint32_t s_bitsPerPixel[TextureFormat::Count] =
  12. {
  13. 4, // BC1
  14. 8, // BC2
  15. 8, // BC3
  16. 4, // BC4
  17. 8, // BC5
  18. 4, // ETC1
  19. 4, // ETC2
  20. 4, // ETC2A
  21. 4, // ETC2A1
  22. 2, // PTC12
  23. 4, // PTC14
  24. 2, // PTC12A
  25. 4, // PTC14A
  26. 2, // PTC22
  27. 4, // PTC24
  28. 0, // Unknown
  29. 8, // L8
  30. 32, // BGRA8
  31. 64, // RGBA16
  32. 64, // RGBA16F
  33. 16, // R5G6B5
  34. 16, // RGBA4
  35. 16, // RGB5A1
  36. 32, // RGB10A2
  37. };
  38. uint32_t getBitsPerPixel(TextureFormat::Enum _format)
  39. {
  40. return s_bitsPerPixel[_format];
  41. }
  42. void imageSolid(uint32_t _width, uint32_t _height, uint32_t _solid, void* _dst)
  43. {
  44. uint32_t* dst = (uint32_t*)_dst;
  45. for (uint32_t ii = 0, num = _width*_height; ii < num; ++ii)
  46. {
  47. *dst++ = _solid;
  48. }
  49. }
  50. void imageCheckerboard(uint32_t _width, uint32_t _height, uint32_t _step, uint32_t _0, uint32_t _1, void* _dst)
  51. {
  52. uint32_t* dst = (uint32_t*)_dst;
  53. for (uint32_t yy = 0; yy < _height; ++yy)
  54. {
  55. for (uint32_t xx = 0; xx < _width; ++xx)
  56. {
  57. uint32_t abgr = ( (xx/_step)&1) ^ ( (yy/_step)&1) ? _1 : _0;
  58. *dst++ = abgr;
  59. }
  60. }
  61. }
  62. void imageRgba8Downsample2x2Ref(uint32_t _width, uint32_t _height, uint32_t _pitch, const void* _src, void* _dst)
  63. {
  64. const uint32_t dstwidth = _width/2;
  65. const uint32_t dstheight = _height/2;
  66. if (0 == dstwidth
  67. || 0 == dstheight)
  68. {
  69. return;
  70. }
  71. uint8_t* dst = (uint8_t*)_dst;
  72. const uint8_t* src = (const uint8_t*)_src;
  73. for (uint32_t yy = 0, ystep = _pitch*2; yy < dstheight; ++yy, src += ystep)
  74. {
  75. const uint8_t* rgba = src;
  76. for (uint32_t xx = 0; xx < dstwidth; ++xx, rgba += 8, dst += 4)
  77. {
  78. float rr = powf(rgba[ 0], 2.2f);
  79. float gg = powf(rgba[ 1], 2.2f);
  80. float bb = powf(rgba[ 2], 2.2f);
  81. float aa = rgba[ 3];
  82. rr += powf(rgba[ 4], 2.2f);
  83. gg += powf(rgba[ 5], 2.2f);
  84. bb += powf(rgba[ 6], 2.2f);
  85. aa += rgba[ 7];
  86. rr += powf(rgba[_pitch+0], 2.2f);
  87. gg += powf(rgba[_pitch+1], 2.2f);
  88. bb += powf(rgba[_pitch+2], 2.2f);
  89. aa += rgba[_pitch+3];
  90. rr += powf(rgba[_pitch+4], 2.2f);
  91. gg += powf(rgba[_pitch+5], 2.2f);
  92. bb += powf(rgba[_pitch+6], 2.2f);
  93. aa += rgba[_pitch+7];
  94. rr *= 0.25f;
  95. gg *= 0.25f;
  96. bb *= 0.25f;
  97. aa *= 0.25f;
  98. rr = powf(rr, 1.0f/2.2f);
  99. gg = powf(gg, 1.0f/2.2f);
  100. bb = powf(bb, 1.0f/2.2f);
  101. dst[0] = (uint8_t)rr;
  102. dst[1] = (uint8_t)gg;
  103. dst[2] = (uint8_t)bb;
  104. dst[3] = (uint8_t)aa;
  105. }
  106. }
  107. }
  108. void imageRgba8Downsample2x2(uint32_t _width, uint32_t _height, uint32_t _pitch, const void* _src, void* _dst)
  109. {
  110. const uint32_t dstwidth = _width/2;
  111. const uint32_t dstheight = _height/2;
  112. if (0 == dstwidth
  113. || 0 == dstheight)
  114. {
  115. return;
  116. }
  117. uint8_t* dst = (uint8_t*)_dst;
  118. const uint8_t* src = (const uint8_t*)_src;
  119. using namespace bx;
  120. const float4_t unpack = float4_ld(1.0f, 1.0f/256.0f, 1.0f/65536.0f, 1.0f/16777216.0f);
  121. const float4_t pack = float4_ld(1.0f, 256.0f*0.5f, 65536.0f, 16777216.0f*0.5f);
  122. const float4_t umask = float4_ild(0xff, 0xff00, 0xff0000, 0xff000000);
  123. const float4_t pmask = float4_ild(0xff, 0x7f80, 0xff0000, 0x7f800000);
  124. const float4_t wflip = float4_ild(0, 0, 0, 0x80000000);
  125. const float4_t wadd = float4_ld(0.0f, 0.0f, 0.0f, 32768.0f*65536.0f);
  126. const float4_t gamma = float4_ld(1.0f/2.2f, 1.0f/2.2f, 1.0f/2.2f, 1.0f);
  127. const float4_t linear = float4_ld(2.2f, 2.2f, 2.2f, 1.0f);
  128. const float4_t quater = float4_splat(0.25f);
  129. for (uint32_t yy = 0, ystep = _pitch*2; yy < dstheight; ++yy, src += ystep)
  130. {
  131. const uint8_t* rgba = src;
  132. for (uint32_t xx = 0; xx < dstwidth; ++xx, rgba += 8, dst += 4)
  133. {
  134. const float4_t abgr0 = float4_splat(rgba);
  135. const float4_t abgr1 = float4_splat(rgba+4);
  136. const float4_t abgr2 = float4_splat(rgba+_pitch);
  137. const float4_t abgr3 = float4_splat(rgba+_pitch+4);
  138. const float4_t abgr0m = float4_and(abgr0, umask);
  139. const float4_t abgr1m = float4_and(abgr1, umask);
  140. const float4_t abgr2m = float4_and(abgr2, umask);
  141. const float4_t abgr3m = float4_and(abgr3, umask);
  142. const float4_t abgr0x = float4_xor(abgr0m, wflip);
  143. const float4_t abgr1x = float4_xor(abgr1m, wflip);
  144. const float4_t abgr2x = float4_xor(abgr2m, wflip);
  145. const float4_t abgr3x = float4_xor(abgr3m, wflip);
  146. const float4_t abgr0f = float4_itof(abgr0x);
  147. const float4_t abgr1f = float4_itof(abgr1x);
  148. const float4_t abgr2f = float4_itof(abgr2x);
  149. const float4_t abgr3f = float4_itof(abgr3x);
  150. const float4_t abgr0c = float4_add(abgr0f, wadd);
  151. const float4_t abgr1c = float4_add(abgr1f, wadd);
  152. const float4_t abgr2c = float4_add(abgr2f, wadd);
  153. const float4_t abgr3c = float4_add(abgr3f, wadd);
  154. const float4_t abgr0n = float4_mul(abgr0c, unpack);
  155. const float4_t abgr1n = float4_mul(abgr1c, unpack);
  156. const float4_t abgr2n = float4_mul(abgr2c, unpack);
  157. const float4_t abgr3n = float4_mul(abgr3c, unpack);
  158. const float4_t abgr0l = float4_pow(abgr0n, linear);
  159. const float4_t abgr1l = float4_pow(abgr1n, linear);
  160. const float4_t abgr2l = float4_pow(abgr2n, linear);
  161. const float4_t abgr3l = float4_pow(abgr3n, linear);
  162. const float4_t sum0 = float4_add(abgr0l, abgr1l);
  163. const float4_t sum1 = float4_add(abgr2l, abgr3l);
  164. const float4_t sum2 = float4_add(sum0, sum1);
  165. const float4_t avg0 = float4_mul(sum2, quater);
  166. const float4_t avg1 = float4_pow(avg0, gamma);
  167. const float4_t avg2 = float4_mul(avg1, pack);
  168. const float4_t ftoi0 = float4_ftoi(avg2);
  169. const float4_t ftoi1 = float4_and(ftoi0, pmask);
  170. const float4_t zwxy = float4_swiz_zwxy(ftoi1);
  171. const float4_t tmp0 = float4_or(ftoi1, zwxy);
  172. const float4_t yyyy = float4_swiz_yyyy(tmp0);
  173. const float4_t tmp1 = float4_iadd(yyyy, yyyy);
  174. const float4_t result = float4_or(tmp0, tmp1);
  175. float4_stx(dst, result);
  176. }
  177. }
  178. }
  179. void imageSwizzleBgra8Ref(uint32_t _width, uint32_t _height, const void* _src, void* _dst)
  180. {
  181. const uint8_t* src = (uint8_t*) _src;
  182. uint8_t* dst = (uint8_t*)_dst;
  183. for (uint32_t xx = 0, num = _width*_height; xx < num; ++xx, src += 4, dst += 4)
  184. {
  185. uint8_t rr = src[0];
  186. uint8_t gg = src[1];
  187. uint8_t bb = src[2];
  188. uint8_t aa = src[3];
  189. dst[0] = bb;
  190. dst[1] = gg;
  191. dst[2] = rr;
  192. dst[3] = aa;
  193. }
  194. }
  195. void imageSwizzleBgra8(uint32_t _width, uint32_t _height, const void* _src, void* _dst)
  196. {
  197. // Test can we do four 4-byte pixels at the time.
  198. if (0 != (_width&0x3)
  199. || _width < 4
  200. || !bx::isPtrAligned(_src, 16)
  201. || !bx::isPtrAligned(_dst, 16) )
  202. {
  203. BX_WARN(false, "Image swizzle is taking slow path.");
  204. BX_WARN(bx::isPtrAligned(_src, 16), "Source %p is not 16-byte aligned.", _src);
  205. BX_WARN(bx::isPtrAligned(_dst, 16), "Destination %p is not 16-byte aligned.", _dst);
  206. BX_WARN(_width < 4, "Image width must be multiple of 4 (width %d).", _width);
  207. imageSwizzleBgra8Ref(_width, _height, _src, _dst);
  208. return;
  209. }
  210. const uint32_t dstpitch = _width*4;
  211. using namespace bx;
  212. const float4_t mf0f0 = float4_isplat(0xff00ff00);
  213. const float4_t m0f0f = float4_isplat(0x00ff00ff);
  214. const uint8_t* src = (uint8_t*) _src;
  215. uint8_t* dst = (uint8_t*)_dst;
  216. for (uint32_t xx = 0, num = dstpitch/16*_height; xx < num; ++xx, src += 16, dst += 16)
  217. {
  218. const float4_t tabgr = float4_ld(src);
  219. const float4_t t00ab = float4_srl(tabgr, 16);
  220. const float4_t tgr00 = float4_sll(tabgr, 16);
  221. const float4_t tgrab = float4_or(t00ab, tgr00);
  222. const float4_t ta0g0 = float4_and(tabgr, mf0f0);
  223. const float4_t t0r0b = float4_and(tgrab, m0f0f);
  224. const float4_t targb = float4_or(ta0g0, t0r0b);
  225. float4_st(dst, targb);
  226. }
  227. }
  228. void imageWriteTga(bx::WriterI* _writer, uint32_t _width, uint32_t _height, uint32_t _srcPitch, const void* _src, bool _grayscale, bool _yflip)
  229. {
  230. uint8_t type = _grayscale ? 3 : 2;
  231. uint8_t bpp = _grayscale ? 8 : 32;
  232. uint8_t header[18] = {};
  233. header[2] = type;
  234. header[12] = _width&0xff;
  235. header[13] = (_width>>8)&0xff;
  236. header[14] = _height&0xff;
  237. header[15] = (_height>>8)&0xff;
  238. header[16] = bpp;
  239. header[17] = 32;
  240. bx::write(_writer, header, sizeof(header) );
  241. uint32_t dstPitch = _width*bpp/8;
  242. if (_yflip)
  243. {
  244. uint8_t* data = (uint8_t*)_src + _srcPitch*_height - _srcPitch;
  245. for (uint32_t yy = 0; yy < _height; ++yy)
  246. {
  247. bx::write(_writer, data, dstPitch);
  248. data -= _srcPitch;
  249. }
  250. }
  251. else if (_srcPitch == dstPitch)
  252. {
  253. bx::write(_writer, _src, _height*_srcPitch);
  254. }
  255. else
  256. {
  257. uint8_t* data = (uint8_t*)_src;
  258. for (uint32_t yy = 0; yy < _height; ++yy)
  259. {
  260. bx::write(_writer, data, dstPitch);
  261. data += _srcPitch;
  262. }
  263. }
  264. }
  265. uint32_t bitRangeConvert(uint32_t _in, uint32_t _from, uint32_t _to)
  266. {
  267. using namespace bx;
  268. uint32_t tmp0 = uint32_sll(1, _to);
  269. uint32_t tmp1 = uint32_sll(1, _from);
  270. uint32_t tmp2 = uint32_dec(tmp0);
  271. uint32_t tmp3 = uint32_dec(tmp1);
  272. uint32_t tmp4 = uint32_mul(_in, tmp2);
  273. uint32_t tmp5 = uint32_add(tmp3, tmp4);
  274. uint32_t tmp6 = uint32_srl(tmp5, _from);
  275. uint32_t tmp7 = uint32_add(tmp5, tmp6);
  276. uint32_t result = uint32_srl(tmp7, _from);
  277. return result;
  278. }
  279. void decodeBlockDxt(uint8_t _dst[16*4], const uint8_t _src[8])
  280. {
  281. uint8_t colors[4*3];
  282. uint32_t c0 = _src[0] | (_src[1] << 8);
  283. colors[0] = bitRangeConvert( (c0>> 0)&0x1f, 5, 8);
  284. colors[1] = bitRangeConvert( (c0>> 5)&0x3f, 6, 8);
  285. colors[2] = bitRangeConvert( (c0>>11)&0x1f, 5, 8);
  286. uint32_t c1 = _src[2] | (_src[3] << 8);
  287. colors[3] = bitRangeConvert( (c1>> 0)&0x1f, 5, 8);
  288. colors[4] = bitRangeConvert( (c1>> 5)&0x3f, 6, 8);
  289. colors[5] = bitRangeConvert( (c1>>11)&0x1f, 5, 8);
  290. colors[6] = (2*colors[0] + colors[3]) / 3;
  291. colors[7] = (2*colors[1] + colors[4]) / 3;
  292. colors[8] = (2*colors[2] + colors[5]) / 3;
  293. colors[ 9] = (colors[0] + 2*colors[3]) / 3;
  294. colors[10] = (colors[1] + 2*colors[4]) / 3;
  295. colors[11] = (colors[2] + 2*colors[5]) / 3;
  296. for (uint32_t ii = 0, next = 8*4; ii < 16*4; ii += 4, next += 2)
  297. {
  298. int idx = ( (_src[next>>3] >> (next & 7) ) & 3) * 3;
  299. _dst[ii+0] = colors[idx+0];
  300. _dst[ii+1] = colors[idx+1];
  301. _dst[ii+2] = colors[idx+2];
  302. }
  303. }
  304. void decodeBlockDxt1(uint8_t _dst[16*4], const uint8_t _src[8])
  305. {
  306. uint8_t colors[4*4];
  307. uint32_t c0 = _src[0] | (_src[1] << 8);
  308. colors[0] = bitRangeConvert( (c0>> 0)&0x1f, 5, 8);
  309. colors[1] = bitRangeConvert( (c0>> 5)&0x3f, 6, 8);
  310. colors[2] = bitRangeConvert( (c0>>11)&0x1f, 5, 8);
  311. colors[3] = 255;
  312. uint32_t c1 = _src[2] | (_src[3] << 8);
  313. colors[4] = bitRangeConvert( (c1>> 0)&0x1f, 5, 8);
  314. colors[5] = bitRangeConvert( (c1>> 5)&0x3f, 6, 8);
  315. colors[6] = bitRangeConvert( (c1>>11)&0x1f, 5, 8);
  316. colors[7] = 255;
  317. if (c0 > c1)
  318. {
  319. colors[ 8] = (2*colors[0] + colors[4]) / 3;
  320. colors[ 9] = (2*colors[1] + colors[5]) / 3;
  321. colors[10] = (2*colors[2] + colors[6]) / 3;
  322. colors[11] = 255;
  323. colors[12] = (colors[0] + 2*colors[4]) / 3;
  324. colors[13] = (colors[1] + 2*colors[5]) / 3;
  325. colors[14] = (colors[2] + 2*colors[6]) / 3;
  326. colors[15] = 255;
  327. }
  328. else
  329. {
  330. colors[ 8] = (colors[0] + colors[4]) / 2;
  331. colors[ 9] = (colors[1] + colors[5]) / 2;
  332. colors[10] = (colors[2] + colors[6]) / 2;
  333. colors[11] = 255;
  334. colors[12] = 0;
  335. colors[13] = 0;
  336. colors[14] = 0;
  337. colors[15] = 0;
  338. }
  339. for (uint32_t ii = 0, next = 8*4; ii < 16*4; ii += 4, next += 2)
  340. {
  341. int idx = ( (_src[next>>3] >> (next & 7) ) & 3) * 4;
  342. _dst[ii+0] = colors[idx+0];
  343. _dst[ii+1] = colors[idx+1];
  344. _dst[ii+2] = colors[idx+2];
  345. _dst[ii+3] = colors[idx+3];
  346. }
  347. }
  348. void decodeBlockDxt23A(uint8_t _dst[16*4], const uint8_t _src[8])
  349. {
  350. for (uint32_t ii = 0, next = 0; ii < 16*4; ii += 4, next += 4)
  351. {
  352. uint32_t c0 = (_src[next>>3] >> (next&7) ) & 0xf;
  353. _dst[ii] = bitRangeConvert(c0, 4, 8);
  354. }
  355. }
  356. void decodeBlockDxt45A(uint8_t _dst[16*4], const uint8_t _src[8])
  357. {
  358. uint8_t alpha[8];
  359. alpha[0] = _src[0];
  360. alpha[1] = _src[1];
  361. if (alpha[0] > alpha[1])
  362. {
  363. alpha[2] = (6*alpha[0] + 1*alpha[1]) / 7;
  364. alpha[3] = (5*alpha[0] + 2*alpha[1]) / 7;
  365. alpha[4] = (4*alpha[0] + 3*alpha[1]) / 7;
  366. alpha[5] = (3*alpha[0] + 4*alpha[1]) / 7;
  367. alpha[6] = (2*alpha[0] + 5*alpha[1]) / 7;
  368. alpha[7] = (1*alpha[0] + 6*alpha[1]) / 7;
  369. }
  370. else
  371. {
  372. alpha[2] = (4*alpha[0] + 1*alpha[1]) / 5;
  373. alpha[3] = (3*alpha[0] + 2*alpha[1]) / 5;
  374. alpha[4] = (2*alpha[0] + 3*alpha[1]) / 5;
  375. alpha[5] = (1*alpha[0] + 4*alpha[1]) / 5;
  376. alpha[6] = 0;
  377. alpha[7] = 255;
  378. }
  379. uint32_t idx0 = _src[2];
  380. uint32_t idx1 = _src[5];
  381. idx0 |= uint32_t(_src[3])<<8;
  382. idx1 |= uint32_t(_src[6])<<8;
  383. idx0 |= uint32_t(_src[4])<<16;
  384. idx1 |= uint32_t(_src[7])<<16;
  385. for (uint32_t ii = 0; ii < 8*4; ii += 4)
  386. {
  387. _dst[ii] = alpha[idx0&7];
  388. _dst[ii+32] = alpha[idx1&7];
  389. idx0 >>= 3;
  390. idx1 >>= 3;
  391. }
  392. }
  393. static const int32_t s_mod[8][4] =
  394. {
  395. { 2, 8, -2, -8},
  396. { 15, 17, -15, -17},
  397. { 9, 29, -9, -29},
  398. { 13, 42, -13, -42},
  399. { 18, 60, -18, -60},
  400. { 24, 80, -24, -80},
  401. { 33, 106, -33, -106},
  402. { 47, 183, -47, -183},
  403. };
  404. uint8_t uint8_satadd(int32_t _a, int32_t _b)
  405. {
  406. using namespace bx;
  407. const int32_t add = _a + _b;
  408. const uint32_t min = uint32_imin(add, 255);
  409. const uint32_t result = uint32_imax(min, 0);
  410. return (uint8_t)result;
  411. }
  412. void decodeBlockEtc1(uint8_t _dst[16*4], const uint8_t _src[8])
  413. {
  414. bool flipBit = 0 != (_src[3] & 0x1);
  415. bool diffBit = 0 != (_src[3] & 0x2);
  416. uint8_t rgb[8];
  417. if (diffBit)
  418. {
  419. rgb[0] = _src[0] >> 3;
  420. rgb[1] = _src[1] >> 3;
  421. rgb[2] = _src[2] >> 3;
  422. int8_t diff[3];
  423. diff[0] = int8_t( (_src[0] & 0x07)<<5)>>5;
  424. diff[1] = int8_t( (_src[1] & 0x07)<<5)>>5;
  425. diff[2] = int8_t( (_src[2] & 0x07)<<5)>>5;
  426. rgb[4] = rgb[0] + diff[0];
  427. rgb[5] = rgb[1] + diff[1];
  428. rgb[6] = rgb[2] + diff[2];
  429. rgb[0] = bitRangeConvert(rgb[0], 5, 8);
  430. rgb[1] = bitRangeConvert(rgb[1], 5, 8);
  431. rgb[2] = bitRangeConvert(rgb[2], 5, 8);
  432. rgb[4] = bitRangeConvert(rgb[4], 5, 8);
  433. rgb[5] = bitRangeConvert(rgb[5], 5, 8);
  434. rgb[6] = bitRangeConvert(rgb[6], 5, 8);
  435. }
  436. else
  437. {
  438. rgb[0] = _src[0] >> 4;
  439. rgb[1] = _src[1] >> 4;
  440. rgb[2] = _src[2] >> 4;
  441. rgb[4] = _src[0] & 0xf;
  442. rgb[5] = _src[1] & 0xf;
  443. rgb[6] = _src[2] & 0xf;
  444. rgb[0] = bitRangeConvert(rgb[0], 4, 8);
  445. rgb[1] = bitRangeConvert(rgb[1], 4, 8);
  446. rgb[2] = bitRangeConvert(rgb[2], 4, 8);
  447. rgb[4] = bitRangeConvert(rgb[4], 4, 8);
  448. rgb[5] = bitRangeConvert(rgb[5], 4, 8);
  449. rgb[6] = bitRangeConvert(rgb[6], 4, 8);
  450. }
  451. uint32_t table[2];
  452. table[0] = (_src[3] >> 5) & 0x7;
  453. table[1] = (_src[3] >> 2) & 0x7;
  454. uint32_t indexMsb = (_src[4]<<8) | _src[5];
  455. uint32_t indexLsb = (_src[6]<<8) | _src[7];
  456. if (flipBit)
  457. {
  458. for (uint32_t ii = 0; ii < 16; ++ii)
  459. {
  460. const uint32_t block = (ii>>1)&1;
  461. const uint32_t color = block<<2;
  462. const uint32_t idx = (ii&0xc) | ( (ii & 0x3)<<4);
  463. const uint32_t lsbi = indexLsb & 1;
  464. const uint32_t msbi = indexMsb & 1;
  465. const int32_t mod = s_mod[table[block] ][lsbi + msbi*2];
  466. _dst[idx + 0] = uint8_satadd(rgb[color+2], mod);
  467. _dst[idx + 1] = uint8_satadd(rgb[color+1], mod);
  468. _dst[idx + 2] = uint8_satadd(rgb[color+0], mod);
  469. indexLsb >>= 1;
  470. indexMsb >>= 1;
  471. }
  472. }
  473. else
  474. {
  475. for (uint32_t ii = 0; ii < 16; ++ii)
  476. {
  477. const uint32_t block = ii>>3;
  478. const uint32_t color = block<<2;
  479. const uint32_t idx = (ii&0xc) | ( (ii & 0x3)<<4);
  480. const uint32_t lsbi = indexLsb & 1;
  481. const uint32_t msbi = indexMsb & 1;
  482. const int32_t mod = s_mod[table[block] ][lsbi + msbi*2];
  483. _dst[idx + 0] = uint8_satadd(rgb[color+2], mod);
  484. _dst[idx + 1] = uint8_satadd(rgb[color+1], mod);
  485. _dst[idx + 2] = uint8_satadd(rgb[color+0], mod);
  486. indexLsb >>= 1;
  487. indexMsb >>= 1;
  488. }
  489. }
  490. }
  491. // DDS
  492. #define DDS_MAGIC BX_MAKEFOURCC('D', 'D', 'S', ' ')
  493. #define DDS_HEADER_SIZE 124
  494. #define DDS_IMAGE_DATA_OFFSET (DDS_HEADER_SIZE + 4)
  495. #define DDS_DXT1 BX_MAKEFOURCC('D', 'X', 'T', '1')
  496. #define DDS_DXT2 BX_MAKEFOURCC('D', 'X', 'T', '2')
  497. #define DDS_DXT3 BX_MAKEFOURCC('D', 'X', 'T', '3')
  498. #define DDS_DXT4 BX_MAKEFOURCC('D', 'X', 'T', '4')
  499. #define DDS_DXT5 BX_MAKEFOURCC('D', 'X', 'T', '5')
  500. #define DDS_ATI1 BX_MAKEFOURCC('A', 'T', 'I', '1')
  501. #define DDS_BC4U BX_MAKEFOURCC('B', 'C', '4', 'U')
  502. #define DDS_ATI2 BX_MAKEFOURCC('A', 'T', 'I', '2')
  503. #define DDS_BC5U BX_MAKEFOURCC('B', 'C', '5', 'U')
  504. #define D3DFMT_A16B16G16R16 36
  505. #define D3DFMT_A16B16G16R16F 113
  506. #define DDSD_CAPS 0x00000001
  507. #define DDSD_HEIGHT 0x00000002
  508. #define DDSD_WIDTH 0x00000004
  509. #define DDSD_PITCH 0x00000008
  510. #define DDSD_PIXELFORMAT 0x00001000
  511. #define DDSD_MIPMAPCOUNT 0x00020000
  512. #define DDSD_LINEARSIZE 0x00080000
  513. #define DDSD_DEPTH 0x00800000
  514. #define DDPF_ALPHAPIXELS 0x00000001
  515. #define DDPF_ALPHA 0x00000002
  516. #define DDPF_FOURCC 0x00000004
  517. #define DDPF_INDEXED 0x00000020
  518. #define DDPF_RGB 0x00000040
  519. #define DDPF_YUV 0x00000200
  520. #define DDPF_LUMINANCE 0x00020000
  521. #define DDSCAPS_COMPLEX 0x00000008
  522. #define DDSCAPS_TEXTURE 0x00001000
  523. #define DDSCAPS_MIPMAP 0x00400000
  524. #define DDSCAPS2_CUBEMAP 0x00000200
  525. #define DDSCAPS2_CUBEMAP_POSITIVEX 0x00000400
  526. #define DDSCAPS2_CUBEMAP_NEGATIVEX 0x00000800
  527. #define DDSCAPS2_CUBEMAP_POSITIVEY 0x00001000
  528. #define DDSCAPS2_CUBEMAP_NEGATIVEY 0x00002000
  529. #define DDSCAPS2_CUBEMAP_POSITIVEZ 0x00004000
  530. #define DDSCAPS2_CUBEMAP_NEGATIVEZ 0x00008000
  531. #define DDS_CUBEMAP_ALLFACES (DDSCAPS2_CUBEMAP_POSITIVEX|DDSCAPS2_CUBEMAP_NEGATIVEX \
  532. |DDSCAPS2_CUBEMAP_POSITIVEY|DDSCAPS2_CUBEMAP_NEGATIVEY \
  533. |DDSCAPS2_CUBEMAP_POSITIVEZ|DDSCAPS2_CUBEMAP_NEGATIVEZ)
  534. #define DDSCAPS2_VOLUME 0x00200000
  535. static struct TranslateDdsFormat
  536. {
  537. uint32_t m_format;
  538. TextureFormat::Enum m_textureFormat;
  539. } s_translateDdsFormat[] =
  540. {
  541. { DDS_DXT1, TextureFormat::BC1 },
  542. { DDS_DXT2, TextureFormat::BC2 },
  543. { DDS_DXT3, TextureFormat::BC2 },
  544. { DDS_DXT4, TextureFormat::BC3 },
  545. { DDS_DXT5, TextureFormat::BC3 },
  546. { DDS_ATI1, TextureFormat::BC4 },
  547. { DDS_BC4U, TextureFormat::BC4 },
  548. { DDS_ATI2, TextureFormat::BC5 },
  549. { DDS_BC5U, TextureFormat::BC5 },
  550. { D3DFMT_A16B16G16R16, TextureFormat::RGBA16 },
  551. { D3DFMT_A16B16G16R16F, TextureFormat::RGBA16F },
  552. { DDPF_RGB|DDPF_ALPHAPIXELS, TextureFormat::BGRA8 },
  553. { DDPF_INDEXED, TextureFormat::L8 },
  554. { DDPF_LUMINANCE, TextureFormat::L8 },
  555. { DDPF_ALPHA, TextureFormat::L8 },
  556. };
  557. bool imageParseDds(ImageContainer& _imageContainer, bx::ReaderSeekerI* _reader)
  558. {
  559. uint32_t headerSize;
  560. bx::read(_reader, headerSize);
  561. if (headerSize < DDS_HEADER_SIZE)
  562. {
  563. return false;
  564. }
  565. uint32_t flags;
  566. bx::read(_reader, flags);
  567. if ( (flags & (DDSD_CAPS|DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT) ) != (DDSD_CAPS|DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT) )
  568. {
  569. return false;
  570. }
  571. uint32_t height;
  572. bx::read(_reader, height);
  573. uint32_t width;
  574. bx::read(_reader, width);
  575. uint32_t pitch;
  576. bx::read(_reader, pitch);
  577. uint32_t depth;
  578. bx::read(_reader, depth);
  579. uint32_t mips;
  580. bx::read(_reader, mips);
  581. bx::skip(_reader, 44); // reserved
  582. bx::skip(_reader, 4); // pixel format size
  583. uint32_t pixelFlags;
  584. bx::read(_reader, pixelFlags);
  585. uint32_t fourcc;
  586. bx::read(_reader, fourcc);
  587. uint32_t rgbCount;
  588. bx::read(_reader, rgbCount);
  589. uint32_t rbitmask;
  590. bx::read(_reader, rbitmask);
  591. uint32_t gbitmask;
  592. bx::read(_reader, gbitmask);
  593. uint32_t bbitmask;
  594. bx::read(_reader, bbitmask);
  595. uint32_t abitmask;
  596. bx::read(_reader, abitmask);
  597. uint32_t caps[4];
  598. bx::read(_reader, caps);
  599. if ( (caps[0] & DDSCAPS_TEXTURE) == 0)
  600. {
  601. return false;
  602. }
  603. bool cubeMap = 0 != (caps[1] & DDSCAPS2_CUBEMAP);
  604. if (cubeMap)
  605. {
  606. if ( (caps[1] & DDS_CUBEMAP_ALLFACES) != DDS_CUBEMAP_ALLFACES)
  607. {
  608. // parital cube map is not supported.
  609. return false;
  610. }
  611. }
  612. bx::skip(_reader, 4); // reserved
  613. uint8_t bpp = 0;
  614. uint8_t blockSize = 1;
  615. TextureFormat::Enum format = TextureFormat::Unknown;
  616. bool hasAlpha = pixelFlags & DDPF_ALPHAPIXELS;
  617. uint32_t ddsFormat = pixelFlags & DDPF_FOURCC ? fourcc : pixelFlags;
  618. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateDdsFormat); ++ii)
  619. {
  620. if (s_translateDdsFormat[ii].m_format == ddsFormat)
  621. {
  622. format = s_translateDdsFormat[ii].m_textureFormat;
  623. break;
  624. }
  625. }
  626. bpp = getBitsPerPixel(format);
  627. blockSize = format < TextureFormat::Unknown ? 4*4 : 1;
  628. blockSize = blockSize*bpp/8;
  629. _imageContainer.m_data = NULL;
  630. _imageContainer.m_size = 0;
  631. _imageContainer.m_offset = DDS_IMAGE_DATA_OFFSET;
  632. _imageContainer.m_width = width;
  633. _imageContainer.m_height = height;
  634. _imageContainer.m_depth = depth;
  635. _imageContainer.m_format = format;
  636. _imageContainer.m_blockSize = blockSize;
  637. _imageContainer.m_numMips = (caps[0] & DDSCAPS_MIPMAP) ? mips : 1;
  638. _imageContainer.m_bpp = bpp;
  639. _imageContainer.m_hasAlpha = hasAlpha;
  640. _imageContainer.m_cubeMap = cubeMap;
  641. _imageContainer.m_ktx = false;
  642. return TextureFormat::Unknown != format;
  643. }
  644. // KTX
  645. #define KTX_MAGIC BX_MAKEFOURCC(0xAB, 'K', 'T', 'X')
  646. #define KTX_HEADER_SIZE 64
  647. #define KTX_ETC1_RGB8_OES 0x8D64
  648. #define KTX_COMPRESSED_R11_EAC 0x9270
  649. #define KTX_COMPRESSED_SIGNED_R11_EAC 0x9271
  650. #define KTX_COMPRESSED_RG11_EAC 0x9272
  651. #define KTX_COMPRESSED_SIGNED_RG11_EAC 0x9273
  652. #define KTX_COMPRESSED_RGB8_ETC2 0x9274
  653. #define KTX_COMPRESSED_SRGB8_ETC2 0x9275
  654. #define KTX_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2 0x9276
  655. #define KTX_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2 0x9277
  656. #define KTX_COMPRESSED_RGBA8_ETC2_EAC 0x9278
  657. #define KTX_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC 0x9279
  658. #define KTX_COMPRESSED_RGB_PVRTC_4BPPV1_IMG 0x8C00
  659. #define KTX_COMPRESSED_RGB_PVRTC_2BPPV1_IMG 0x8C01
  660. #define KTX_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG 0x8C02
  661. #define KTX_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG 0x8C03
  662. #define KTX_COMPRESSED_RGBA_PVRTC_2BPPV2_IMG 0x9137
  663. #define KTX_COMPRESSED_RGBA_PVRTC_4BPPV2_IMG 0x9138
  664. #define KTX_COMPRESSED_RGBA_S3TC_DXT1_EXT 0x83F1
  665. #define KTX_COMPRESSED_RGBA_S3TC_DXT3_EXT 0x83F2
  666. #define KTX_COMPRESSED_RGBA_S3TC_DXT5_EXT 0x83F3
  667. #define KTX_COMPRESSED_LUMINANCE_LATC1_EXT 0x8C70
  668. #define KTX_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT 0x8C72
  669. #define KTX_RGBA16 0x805B
  670. #define KTX_RGBA16F 0x881A
  671. static struct TranslateKtxFormat
  672. {
  673. uint32_t m_format;
  674. TextureFormat::Enum m_textureFormat;
  675. } s_translateKtxFormat[] =
  676. {
  677. { KTX_COMPRESSED_RGBA_S3TC_DXT1_EXT, TextureFormat::BC1 },
  678. { KTX_COMPRESSED_RGBA_S3TC_DXT3_EXT, TextureFormat::BC2 },
  679. { KTX_COMPRESSED_RGBA_S3TC_DXT5_EXT, TextureFormat::BC3 },
  680. { KTX_COMPRESSED_LUMINANCE_LATC1_EXT, TextureFormat::BC4 },
  681. { KTX_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT, TextureFormat::BC5 },
  682. { KTX_ETC1_RGB8_OES, TextureFormat::ETC1 },
  683. { KTX_COMPRESSED_RGB8_ETC2, TextureFormat::ETC2 },
  684. { KTX_COMPRESSED_RGBA8_ETC2_EAC, TextureFormat::ETC2A },
  685. { KTX_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2, TextureFormat::ETC2A1 },
  686. { KTX_COMPRESSED_RGB_PVRTC_2BPPV1_IMG, TextureFormat::PTC12 },
  687. { KTX_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG, TextureFormat::PTC12A },
  688. { KTX_COMPRESSED_RGB_PVRTC_4BPPV1_IMG, TextureFormat::PTC14 },
  689. { KTX_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG, TextureFormat::PTC14A },
  690. { KTX_COMPRESSED_RGBA_PVRTC_2BPPV2_IMG, TextureFormat::PTC22 },
  691. { KTX_COMPRESSED_RGBA_PVRTC_4BPPV2_IMG, TextureFormat::PTC24 },
  692. { KTX_RGBA16, TextureFormat::RGBA16 },
  693. { KTX_RGBA16F, TextureFormat::RGBA16F },
  694. { KTX_COMPRESSED_R11_EAC, TextureFormat::Unknown },
  695. { KTX_COMPRESSED_SIGNED_R11_EAC, TextureFormat::Unknown },
  696. { KTX_COMPRESSED_RG11_EAC, TextureFormat::Unknown },
  697. { KTX_COMPRESSED_SIGNED_RG11_EAC, TextureFormat::Unknown },
  698. { KTX_COMPRESSED_SRGB8_ETC2, TextureFormat::Unknown },
  699. { KTX_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2, TextureFormat::Unknown },
  700. { KTX_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC, TextureFormat::Unknown },
  701. };
  702. bool imageParseKtx(ImageContainer& _imageContainer, bx::ReaderSeekerI* _reader)
  703. {
  704. uint8_t identifier[8];
  705. bx::read(_reader, identifier);
  706. if (identifier[1] != '1'
  707. && identifier[2] != '1')
  708. {
  709. return false;
  710. }
  711. uint32_t endianness;
  712. bx::read(_reader, endianness);
  713. bool fromLittleEndian = 0x04030201 == endianness;
  714. uint32_t glType;
  715. bx::readHE(_reader, glType, fromLittleEndian);
  716. uint32_t glTypeSize;
  717. bx::readHE(_reader, glTypeSize, fromLittleEndian);
  718. uint32_t glFormat;
  719. bx::readHE(_reader, glFormat, fromLittleEndian);
  720. uint32_t glInternalFormat;
  721. bx::readHE(_reader, glInternalFormat, fromLittleEndian);
  722. uint32_t glBaseInternalFormat;
  723. bx::readHE(_reader, glBaseInternalFormat, fromLittleEndian);
  724. uint32_t width;
  725. bx::readHE(_reader, width, fromLittleEndian);
  726. uint32_t height;
  727. bx::readHE(_reader, height, fromLittleEndian);
  728. uint32_t depth;
  729. bx::readHE(_reader, depth, fromLittleEndian);
  730. uint32_t numberOfArrayElements;
  731. bx::readHE(_reader, numberOfArrayElements, fromLittleEndian);
  732. uint32_t numFaces;
  733. bx::readHE(_reader, numFaces, fromLittleEndian);
  734. uint32_t numMips;
  735. bx::readHE(_reader, numMips, fromLittleEndian);
  736. uint32_t metaDataSize;
  737. bx::readHE(_reader, metaDataSize, fromLittleEndian);
  738. // skip meta garbage...
  739. int64_t offset = bx::skip(_reader, metaDataSize);
  740. uint8_t bpp = 0;
  741. uint8_t blockSize = 1;
  742. TextureFormat::Enum format = TextureFormat::Unknown;
  743. bool hasAlpha = false;
  744. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateKtxFormat); ++ii)
  745. {
  746. if (s_translateKtxFormat[ii].m_format == glInternalFormat)
  747. {
  748. format = s_translateKtxFormat[ii].m_textureFormat;
  749. break;
  750. }
  751. }
  752. bpp = getBitsPerPixel(format);
  753. blockSize = format < TextureFormat::Unknown ? 4*4 : 1;
  754. blockSize = blockSize*bpp/8;
  755. _imageContainer.m_data = NULL;
  756. _imageContainer.m_size = 0;
  757. _imageContainer.m_offset = (uint32_t)offset;
  758. _imageContainer.m_width = width;
  759. _imageContainer.m_height = height;
  760. _imageContainer.m_depth = depth;
  761. _imageContainer.m_format = format;
  762. _imageContainer.m_blockSize = blockSize;
  763. _imageContainer.m_numMips = numMips;
  764. _imageContainer.m_bpp = bpp;
  765. _imageContainer.m_hasAlpha = hasAlpha;
  766. _imageContainer.m_cubeMap = numFaces > 1;
  767. _imageContainer.m_ktx = true;
  768. return TextureFormat::Unknown != format;
  769. }
  770. // PVR3
  771. #define PVR3_MAKE8CC(_a, _b, _c, _d, _e, _f, _g, _h) (uint64_t(BX_MAKEFOURCC(_a, _b, _c, _d) ) | (uint64_t(BX_MAKEFOURCC(_e, _f, _g, _h) )<<32) )
  772. #define PVR3_MAGIC BX_MAKEFOURCC('P', 'V', 'R', 3)
  773. #define PVR3_HEADER_SIZE 52
  774. #define PVR3_PVRTC1_2BPP_RGB 0
  775. #define PVR3_PVRTC1_2BPP_RGBA 1
  776. #define PVR3_PVRTC1_4BPP_RGB 2
  777. #define PVR3_PVRTC1_4BPP_RGBA 3
  778. #define PVR3_PVRTC2_2BPP_RGBA 4
  779. #define PVR3_PVRTC2_4BPP_RGBA 5
  780. #define PVR3_ETC1 6
  781. #define PVR3_DXT1 7
  782. #define PVR3_DXT2 8
  783. #define PVR3_DXT3 9
  784. #define PVR3_DXT4 10
  785. #define PVR3_DXT5 11
  786. #define PVR3_BC4 12
  787. #define PVR3_BC5 13
  788. #define PVR3_RGBA16 PVR3_MAKE8CC('r', 'g', 'b', 'a', 16, 16, 16, 16)
  789. #define PVR3_CHANNEL_TYPE_ANY UINT32_MAX
  790. #define PVR3_CHANNEL_TYPE_FLOAT UINT32_C(12)
  791. static struct TranslatePvr3Format
  792. {
  793. uint64_t m_format;
  794. uint32_t m_channelTypeMask;
  795. TextureFormat::Enum m_textureFormat;
  796. } s_translatePvr3Format[] =
  797. {
  798. { PVR3_PVRTC1_2BPP_RGB, PVR3_CHANNEL_TYPE_ANY, TextureFormat::PTC12 },
  799. { PVR3_PVRTC1_2BPP_RGBA, PVR3_CHANNEL_TYPE_ANY, TextureFormat::PTC12A },
  800. { PVR3_PVRTC1_4BPP_RGB, PVR3_CHANNEL_TYPE_ANY, TextureFormat::PTC14 },
  801. { PVR3_PVRTC1_4BPP_RGBA, PVR3_CHANNEL_TYPE_ANY, TextureFormat::PTC14A },
  802. { PVR3_PVRTC2_2BPP_RGBA, PVR3_CHANNEL_TYPE_ANY, TextureFormat::PTC22 },
  803. { PVR3_PVRTC2_4BPP_RGBA, PVR3_CHANNEL_TYPE_ANY, TextureFormat::PTC24 },
  804. { PVR3_ETC1, PVR3_CHANNEL_TYPE_ANY, TextureFormat::ETC1 },
  805. { PVR3_DXT1, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC1 },
  806. { PVR3_DXT2, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC2 },
  807. { PVR3_DXT3, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC2 },
  808. { PVR3_DXT4, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC3 },
  809. { PVR3_DXT5, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC3 },
  810. { PVR3_BC4, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC4 },
  811. { PVR3_BC5, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC5 },
  812. { PVR3_RGBA16, PVR3_CHANNEL_TYPE_FLOAT, TextureFormat::RGBA16F },
  813. { PVR3_RGBA16, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RGBA16 },
  814. };
  815. bool imageParsePvr3(ImageContainer& _imageContainer, bx::ReaderSeekerI* _reader)
  816. {
  817. uint32_t flags;
  818. bx::read(_reader, flags);
  819. uint64_t pixelFormat;
  820. bx::read(_reader, pixelFormat);
  821. uint32_t colorSpace;
  822. bx::read(_reader, colorSpace); // 0 - linearRGB, 1 - sRGB
  823. uint32_t channelType;
  824. bx::read(_reader, channelType);
  825. uint32_t height;
  826. bx::read(_reader, height);
  827. uint32_t width;
  828. bx::read(_reader, width);
  829. uint32_t depth;
  830. bx::read(_reader, depth);
  831. uint32_t numSurfaces;
  832. bx::read(_reader, numSurfaces);
  833. uint32_t numFaces;
  834. bx::read(_reader, numFaces);
  835. uint32_t numMips;
  836. bx::read(_reader, numMips);
  837. uint32_t metaDataSize;
  838. bx::read(_reader, metaDataSize);
  839. // skip meta garbage...
  840. int64_t offset = bx::skip(_reader, metaDataSize);
  841. uint8_t bpp = 0;
  842. uint8_t blockSize = 1;
  843. TextureFormat::Enum format = TextureFormat::Unknown;
  844. bool hasAlpha = false;
  845. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translatePvr3Format); ++ii)
  846. {
  847. if (s_translatePvr3Format[ii].m_format == pixelFormat
  848. && channelType == (s_translatePvr3Format[ii].m_channelTypeMask & channelType) )
  849. {
  850. format = s_translatePvr3Format[ii].m_textureFormat;
  851. break;
  852. }
  853. }
  854. bpp = getBitsPerPixel(format);
  855. blockSize = format < TextureFormat::Unknown ? 4*4 : 1;
  856. blockSize = blockSize*bpp/8;
  857. _imageContainer.m_data = NULL;
  858. _imageContainer.m_size = 0;
  859. _imageContainer.m_offset = (uint32_t)offset;
  860. _imageContainer.m_width = width;
  861. _imageContainer.m_height = height;
  862. _imageContainer.m_depth = depth;
  863. _imageContainer.m_format = format;
  864. _imageContainer.m_blockSize = blockSize;
  865. _imageContainer.m_numMips = numMips;
  866. _imageContainer.m_bpp = bpp;
  867. _imageContainer.m_hasAlpha = hasAlpha;
  868. _imageContainer.m_cubeMap = numFaces > 1;
  869. _imageContainer.m_ktx = false;
  870. return TextureFormat::Unknown != format;
  871. }
  872. bool imageParse(ImageContainer& _imageContainer, bx::ReaderSeekerI* _reader)
  873. {
  874. uint32_t magic;
  875. bx::read(_reader, magic);
  876. if (DDS_MAGIC == magic)
  877. {
  878. return imageParseDds(_imageContainer, _reader);
  879. }
  880. else if (KTX_MAGIC == magic)
  881. {
  882. return imageParseKtx(_imageContainer, _reader);
  883. }
  884. else if (PVR3_MAGIC == magic)
  885. {
  886. return imageParsePvr3(_imageContainer, _reader);
  887. }
  888. else if (BGFX_CHUNK_MAGIC_TEX == magic)
  889. {
  890. TextureCreate tc;
  891. bx::read(_reader, tc);
  892. uint32_t bpp = getBitsPerPixel(TextureFormat::Enum(tc.m_format) );
  893. uint32_t blockSize = tc.m_format < TextureFormat::Unknown ? 4*4 : 1;
  894. blockSize = blockSize*bpp/8;
  895. _imageContainer.m_format = tc.m_format;
  896. _imageContainer.m_offset = UINT32_MAX;
  897. if (NULL == tc.m_mem)
  898. {
  899. _imageContainer.m_data = NULL;
  900. _imageContainer.m_size = 0;
  901. }
  902. else
  903. {
  904. _imageContainer.m_data = tc.m_mem->data;
  905. _imageContainer.m_size = tc.m_mem->size;
  906. }
  907. _imageContainer.m_width = tc.m_width;
  908. _imageContainer.m_height = tc.m_height;
  909. _imageContainer.m_depth = tc.m_depth;
  910. _imageContainer.m_blockSize = blockSize;
  911. _imageContainer.m_numMips = tc.m_numMips;
  912. _imageContainer.m_bpp = getBitsPerPixel(TextureFormat::Enum(tc.m_format) );
  913. _imageContainer.m_hasAlpha = false;
  914. _imageContainer.m_cubeMap = tc.m_cubeMap;
  915. _imageContainer.m_ktx = false;
  916. return true;
  917. }
  918. return false;
  919. }
  920. bool imageParse(ImageContainer& _imageContainer, const void* _data, uint32_t _size)
  921. {
  922. bx::MemoryReader reader(_data, _size);
  923. return imageParse(_imageContainer, &reader);
  924. }
  925. void imageDecodeToBgra8(uint8_t* _dst, const uint8_t* _src, uint32_t _width, uint32_t _height, uint8_t _type)
  926. {
  927. const uint8_t* src = _src;
  928. uint32_t width = _width/4;
  929. uint32_t height = _height/4;
  930. uint32_t pitch = _width*4;
  931. uint8_t temp[16*4];
  932. switch (_type)
  933. {
  934. case TextureFormat::BC1:
  935. for (uint32_t yy = 0; yy < height; ++yy)
  936. {
  937. for (uint32_t xx = 0; xx < width; ++xx)
  938. {
  939. decodeBlockDxt1(temp, src);
  940. src += 8;
  941. uint8_t* dst = &_dst[(yy*pitch+xx*4)*4];
  942. memcpy(&dst[0*pitch], &temp[ 0], 16);
  943. memcpy(&dst[1*pitch], &temp[16], 16);
  944. memcpy(&dst[2*pitch], &temp[32], 16);
  945. memcpy(&dst[3*pitch], &temp[48], 16);
  946. }
  947. }
  948. break;
  949. case TextureFormat::BC2:
  950. for (uint32_t yy = 0; yy < height; ++yy)
  951. {
  952. for (uint32_t xx = 0; xx < width; ++xx)
  953. {
  954. decodeBlockDxt23A(temp+3, src);
  955. src += 8;
  956. decodeBlockDxt(temp, src);
  957. src += 8;
  958. uint8_t* dst = &_dst[(yy*pitch+xx*4)*4];
  959. memcpy(&dst[0*pitch], &temp[ 0], 16);
  960. memcpy(&dst[1*pitch], &temp[16], 16);
  961. memcpy(&dst[2*pitch], &temp[32], 16);
  962. memcpy(&dst[3*pitch], &temp[48], 16);
  963. }
  964. }
  965. break;
  966. case TextureFormat::BC3:
  967. for (uint32_t yy = 0; yy < height; ++yy)
  968. {
  969. for (uint32_t xx = 0; xx < width; ++xx)
  970. {
  971. decodeBlockDxt45A(temp+3, src);
  972. src += 8;
  973. decodeBlockDxt(temp, src);
  974. src += 8;
  975. uint8_t* dst = &_dst[(yy*pitch+xx*4)*4];
  976. memcpy(&dst[0*pitch], &temp[ 0], 16);
  977. memcpy(&dst[1*pitch], &temp[16], 16);
  978. memcpy(&dst[2*pitch], &temp[32], 16);
  979. memcpy(&dst[3*pitch], &temp[48], 16);
  980. }
  981. }
  982. break;
  983. case TextureFormat::BC4:
  984. for (uint32_t yy = 0; yy < height; ++yy)
  985. {
  986. for (uint32_t xx = 0; xx < width; ++xx)
  987. {
  988. decodeBlockDxt45A(temp, src);
  989. src += 8;
  990. uint8_t* dst = &_dst[(yy*pitch+xx*4)*4];
  991. memcpy(&dst[0*pitch], &temp[ 0], 16);
  992. memcpy(&dst[1*pitch], &temp[16], 16);
  993. memcpy(&dst[2*pitch], &temp[32], 16);
  994. memcpy(&dst[3*pitch], &temp[48], 16);
  995. }
  996. }
  997. break;
  998. case TextureFormat::BC5:
  999. for (uint32_t yy = 0; yy < height; ++yy)
  1000. {
  1001. for (uint32_t xx = 0; xx < width; ++xx)
  1002. {
  1003. decodeBlockDxt45A(temp+1, src);
  1004. src += 8;
  1005. decodeBlockDxt45A(temp+2, src);
  1006. src += 8;
  1007. for (uint32_t ii = 0; ii < 16; ++ii)
  1008. {
  1009. float nx = temp[ii*4+2]*2.0f/255.0f - 1.0f;
  1010. float ny = temp[ii*4+1]*2.0f/255.0f - 1.0f;
  1011. float nz = sqrtf(1.0f - nx*nx - ny*ny);
  1012. temp[ii*4+0] = uint8_t( (nz + 1.0f)*255.0f/2.0f);
  1013. temp[ii*4+3] = 0;
  1014. }
  1015. uint8_t* dst = &_dst[(yy*pitch+xx*4)*4];
  1016. memcpy(&dst[0*pitch], &temp[ 0], 16);
  1017. memcpy(&dst[1*pitch], &temp[16], 16);
  1018. memcpy(&dst[2*pitch], &temp[32], 16);
  1019. memcpy(&dst[3*pitch], &temp[48], 16);
  1020. }
  1021. }
  1022. break;
  1023. case TextureFormat::ETC1:
  1024. for (uint32_t yy = 0; yy < height; ++yy)
  1025. {
  1026. for (uint32_t xx = 0; xx < width; ++xx)
  1027. {
  1028. decodeBlockEtc1(temp, src);
  1029. src += 8;
  1030. uint8_t* dst = &_dst[(yy*pitch+xx*4)*4];
  1031. memcpy(&dst[0*pitch], &temp[ 0], 16);
  1032. memcpy(&dst[1*pitch], &temp[16], 16);
  1033. memcpy(&dst[2*pitch], &temp[32], 16);
  1034. memcpy(&dst[3*pitch], &temp[48], 16);
  1035. }
  1036. }
  1037. break;
  1038. case TextureFormat::ETC2:
  1039. BX_WARN(false, "ETC2 decoder is not implemented.");
  1040. imageCheckerboard(_width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff0000ff), _dst);
  1041. break;
  1042. case TextureFormat::ETC2A:
  1043. BX_WARN(false, "ETC2A decoder is not implemented.");
  1044. imageCheckerboard(_width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00), _dst);
  1045. break;
  1046. case TextureFormat::ETC2A1:
  1047. BX_WARN(false, "ETC2A1 decoder is not implemented.");
  1048. imageCheckerboard(_width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xffff0000), _dst);
  1049. break;
  1050. case TextureFormat::PTC12:
  1051. BX_WARN(false, "PTC12 decoder is not implemented.");
  1052. imageCheckerboard(_width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xffff00ff), _dst);
  1053. break;
  1054. case TextureFormat::PTC12A:
  1055. BX_WARN(false, "PTC12A decoder is not implemented.");
  1056. imageCheckerboard(_width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xffffff00), _dst);
  1057. break;
  1058. case TextureFormat::PTC14:
  1059. BX_WARN(false, "PTC14 decoder is not implemented.");
  1060. imageCheckerboard(_width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ffff), _dst);
  1061. break;
  1062. case TextureFormat::PTC14A:
  1063. BX_WARN(false, "PTC14A decoder is not implemented.");
  1064. imageCheckerboard(_width, _height, 16, UINT32_C(0xffff0000), UINT32_C(0xff0000ff), _dst);
  1065. break;
  1066. case TextureFormat::PTC22:
  1067. BX_WARN(false, "PTC22 decoder is not implemented.");
  1068. imageCheckerboard(_width, _height, 16, UINT32_C(0xff00ff00), UINT32_C(0xff0000ff), _dst);
  1069. break;
  1070. case TextureFormat::PTC24:
  1071. BX_WARN(false, "PTC24 decoder is not implemented.");
  1072. imageCheckerboard(_width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xffffffff), _dst);
  1073. break;
  1074. default:
  1075. // Decompression not implemented... Make ugly red-yellow checkerboard texture.
  1076. imageCheckerboard(_width, _height, 16, UINT32_C(0xffff0000), UINT32_C(0xffffff00), _dst);
  1077. break;
  1078. }
  1079. }
  1080. bool imageGetRawData(const ImageContainer& _imageContainer, uint8_t _side, uint8_t _lod, const void* _data, uint32_t _size, ImageMip& _mip)
  1081. {
  1082. const uint32_t blockSize = _imageContainer.m_blockSize;
  1083. uint32_t offset = _imageContainer.m_offset;
  1084. const uint8_t bpp = _imageContainer.m_bpp;
  1085. TextureFormat::Enum type = TextureFormat::Enum(_imageContainer.m_format);
  1086. bool hasAlpha = _imageContainer.m_hasAlpha;
  1087. if (UINT32_MAX == _imageContainer.m_offset)
  1088. {
  1089. if (NULL == _imageContainer.m_data)
  1090. {
  1091. return false;
  1092. }
  1093. offset = 0;
  1094. _data = _imageContainer.m_data;
  1095. _size = _imageContainer.m_size;
  1096. }
  1097. for (uint8_t side = 0, numSides = _imageContainer.m_cubeMap ? 6 : 1; side < numSides; ++side)
  1098. {
  1099. uint32_t width = _imageContainer.m_width;
  1100. uint32_t height = _imageContainer.m_height;
  1101. uint32_t depth = _imageContainer.m_depth;
  1102. for (uint8_t lod = 0, num = _imageContainer.m_numMips; lod < num; ++lod)
  1103. {
  1104. // skip imageSize in KTX format.
  1105. offset += _imageContainer.m_ktx ? sizeof(uint32_t) : 0;
  1106. width = bx::uint32_max(1, width);
  1107. height = bx::uint32_max(1, height);
  1108. depth = bx::uint32_max(1, depth);
  1109. uint32_t size = width*height*depth*blockSize;
  1110. if (TextureFormat::Unknown > type)
  1111. {
  1112. width = bx::uint32_max(1, (width + 3)>>2);
  1113. height = bx::uint32_max(1, (height + 3)>>2);
  1114. size = width*height*depth*blockSize;
  1115. width <<= 2;
  1116. height <<= 2;
  1117. }
  1118. if (side == _side
  1119. && lod == _lod)
  1120. {
  1121. _mip.m_width = width;
  1122. _mip.m_height = height;
  1123. _mip.m_blockSize = blockSize;
  1124. _mip.m_size = size;
  1125. _mip.m_data = (const uint8_t*)_data + offset;
  1126. _mip.m_bpp = bpp;
  1127. _mip.m_format = type;
  1128. _mip.m_hasAlpha = hasAlpha;
  1129. return true;
  1130. }
  1131. offset += size;
  1132. BX_CHECK(offset <= _size, "Reading past size of data buffer! (offset %d, size %d)", offset, _size);
  1133. BX_UNUSED(_size);
  1134. width >>= 1;
  1135. height >>= 1;
  1136. depth >>= 1;
  1137. }
  1138. }
  1139. return false;
  1140. }
  1141. } // namespace bgfx