image.cpp 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872
  1. /*
  2. * Copyright 2011-2014 Branimir Karadzic. All rights reserved.
  3. * License: http://www.opensource.org/licenses/BSD-2-Clause
  4. */
  5. #include "bgfx_p.h"
  6. #include <math.h> // powf, sqrtf
  7. #include "image.h"
  8. namespace bgfx
  9. {
  10. static const ImageBlockInfo s_imageBlockInfo[] =
  11. {
  12. { 4, 4, 4, 8 }, // BC1
  13. { 8, 4, 4, 16 }, // BC2
  14. { 8, 4, 4, 16 }, // BC3
  15. { 4, 4, 4, 8 }, // BC4
  16. { 8, 4, 4, 16 }, // BC5
  17. { 8, 4, 4, 16 }, // BC6H
  18. { 8, 4, 4, 16 }, // BC7
  19. { 4, 4, 4, 8 }, // ETC1
  20. { 4, 4, 4, 8 }, // ETC2
  21. { 8, 4, 4, 16 }, // ETC2A
  22. { 4, 4, 4, 8 }, // ETC2A1
  23. { 2, 8, 4, 8 }, // PTC12
  24. { 4, 4, 4, 8 }, // PTC14
  25. { 2, 8, 4, 8 }, // PTC12A
  26. { 4, 4, 4, 8 }, // PTC14A
  27. { 2, 8, 4, 8 }, // PTC22
  28. { 4, 4, 4, 8 }, // PTC24
  29. { 0, 0, 0, 0 }, // Unknown
  30. { 1, 8, 1, 1 }, // R1
  31. { 8, 1, 1, 1 }, // R8
  32. { 16, 1, 1, 2 }, // R16
  33. { 16, 1, 1, 2 }, // R16F
  34. { 32, 1, 1, 4 }, // R32
  35. { 32, 1, 1, 4 }, // R32F
  36. { 16, 1, 1, 2 }, // RG8
  37. { 32, 1, 1, 4 }, // RG16
  38. { 32, 1, 1, 4 }, // RG16F
  39. { 64, 1, 1, 8 }, // RG32
  40. { 64, 1, 1, 8 }, // RG32F
  41. { 32, 1, 1, 4 }, // BGRA8
  42. { 64, 1, 1, 8 }, // RGBA16
  43. { 64, 1, 1, 8 }, // RGBA16F
  44. { 128, 1, 1, 16 }, // RGBA32
  45. { 128, 1, 1, 16 }, // RGBA32F
  46. { 16, 1, 1, 2 }, // R5G6B5
  47. { 16, 1, 1, 2 }, // RGBA4
  48. { 16, 1, 1, 2 }, // RGB5A1
  49. { 32, 1, 1, 4 }, // RGB10A2
  50. { 32, 1, 1, 4 }, // R11G11B10F
  51. { 0, 0, 0, 0 }, // UnknownDepth
  52. { 16, 1, 1, 2 }, // D16
  53. { 24, 1, 1, 3 }, // D24
  54. { 32, 1, 1, 4 }, // D24S8
  55. { 32, 1, 1, 4 }, // D32
  56. { 16, 1, 1, 2 }, // D16F
  57. { 24, 1, 1, 3 }, // D24F
  58. { 32, 1, 1, 4 }, // D32F
  59. { 8, 1, 1, 1 }, // D0S8
  60. };
  61. BX_STATIC_ASSERT(TextureFormat::Count == BX_COUNTOF(s_imageBlockInfo) );
  62. static const char* s_textureFormatName[] =
  63. {
  64. "BC1", // BC1
  65. "BC2", // BC2
  66. "BC3", // BC3
  67. "BC4", // BC4
  68. "BC5", // BC5
  69. "BC6H", // BC6H
  70. "BC7", // BC7
  71. "ETC1", // ETC1
  72. "ETC2", // ETC2
  73. "ETC2A", // ETC2A
  74. "ETC2A1", // ETC2A1
  75. "PTC12", // PTC12
  76. "PTC14", // PTC14
  77. "PTC12A", // PTC12A
  78. "PTC14A", // PTC14A
  79. "PTC22", // PTC22
  80. "PTC24", // PTC24
  81. "<unknown>", // Unknown
  82. "R1", // R1
  83. "R8", // R8
  84. "R16", // R16
  85. "R16F", // R16F
  86. "R32", // R32
  87. "R32F", // R32F
  88. "RG8", // RG8
  89. "RG16", // RG16
  90. "RG16F", // RG16F
  91. "RG32", // RG32
  92. "RG32F", // RG32F
  93. "BGRA8", // BGRA8
  94. "RGBA16", // RGBA16
  95. "RGBA16F", // RGBA16F
  96. "RGBA32", // RGBA32
  97. "RGBA32F", // RGBA32F
  98. "R5G6B5", // R5G6B5
  99. "RGBA4", // RGBA4
  100. "RGB5A1", // RGB5A1
  101. "RGB10A2", // RGB10A2
  102. "R11G11B10F", // R11G11B10F
  103. "<unknown>", // UnknownDepth
  104. "D16", // D16
  105. "D24", // D24
  106. "D24S8", // D24S8
  107. "D32", // D32
  108. "D16F", // D16F
  109. "D24F", // D24F
  110. "D32F", // D32F
  111. "D0S8", // D0S8
  112. };
  113. BX_STATIC_ASSERT(TextureFormat::Count == BX_COUNTOF(s_textureFormatName) );
  114. bool isCompressed(TextureFormat::Enum _format)
  115. {
  116. return _format < TextureFormat::Unknown;
  117. }
  118. bool isColor(TextureFormat::Enum _format)
  119. {
  120. return _format > TextureFormat::Unknown
  121. && _format < TextureFormat::UnknownDepth
  122. ;
  123. }
  124. bool isDepth(TextureFormat::Enum _format)
  125. {
  126. return _format > TextureFormat::UnknownDepth
  127. && _format < TextureFormat::Count
  128. ;
  129. }
  130. uint8_t getBitsPerPixel(TextureFormat::Enum _format)
  131. {
  132. return s_imageBlockInfo[_format].bitsPerPixel;
  133. }
  134. const ImageBlockInfo& getBlockInfo(TextureFormat::Enum _format)
  135. {
  136. return s_imageBlockInfo[_format];
  137. }
  138. uint8_t getBlockSize(TextureFormat::Enum _format)
  139. {
  140. return s_imageBlockInfo[_format].blockSize;
  141. }
  142. const char* getName(TextureFormat::Enum _format)
  143. {
  144. return s_textureFormatName[_format];
  145. }
  146. void imageSolid(uint32_t _width, uint32_t _height, uint32_t _solid, void* _dst)
  147. {
  148. uint32_t* dst = (uint32_t*)_dst;
  149. for (uint32_t ii = 0, num = _width*_height; ii < num; ++ii)
  150. {
  151. *dst++ = _solid;
  152. }
  153. }
  154. void imageCheckerboard(uint32_t _width, uint32_t _height, uint32_t _step, uint32_t _0, uint32_t _1, void* _dst)
  155. {
  156. uint32_t* dst = (uint32_t*)_dst;
  157. for (uint32_t yy = 0; yy < _height; ++yy)
  158. {
  159. for (uint32_t xx = 0; xx < _width; ++xx)
  160. {
  161. uint32_t abgr = ( (xx/_step)&1) ^ ( (yy/_step)&1) ? _1 : _0;
  162. *dst++ = abgr;
  163. }
  164. }
  165. }
  166. void imageRgba8Downsample2x2Ref(uint32_t _width, uint32_t _height, uint32_t _srcPitch, const void* _src, void* _dst)
  167. {
  168. const uint32_t dstwidth = _width/2;
  169. const uint32_t dstheight = _height/2;
  170. if (0 == dstwidth
  171. || 0 == dstheight)
  172. {
  173. return;
  174. }
  175. uint8_t* dst = (uint8_t*)_dst;
  176. const uint8_t* src = (const uint8_t*)_src;
  177. for (uint32_t yy = 0, ystep = _srcPitch*2; yy < dstheight; ++yy, src += ystep)
  178. {
  179. const uint8_t* rgba = src;
  180. for (uint32_t xx = 0; xx < dstwidth; ++xx, rgba += 8, dst += 4)
  181. {
  182. float rr = powf(rgba[ 0], 2.2f);
  183. float gg = powf(rgba[ 1], 2.2f);
  184. float bb = powf(rgba[ 2], 2.2f);
  185. float aa = rgba[ 3];
  186. rr += powf(rgba[ 4], 2.2f);
  187. gg += powf(rgba[ 5], 2.2f);
  188. bb += powf(rgba[ 6], 2.2f);
  189. aa += rgba[ 7];
  190. rr += powf(rgba[_srcPitch+0], 2.2f);
  191. gg += powf(rgba[_srcPitch+1], 2.2f);
  192. bb += powf(rgba[_srcPitch+2], 2.2f);
  193. aa += rgba[_srcPitch+3];
  194. rr += powf(rgba[_srcPitch+4], 2.2f);
  195. gg += powf(rgba[_srcPitch+5], 2.2f);
  196. bb += powf(rgba[_srcPitch+6], 2.2f);
  197. aa += rgba[_srcPitch+7];
  198. rr *= 0.25f;
  199. gg *= 0.25f;
  200. bb *= 0.25f;
  201. aa *= 0.25f;
  202. rr = powf(rr, 1.0f/2.2f);
  203. gg = powf(gg, 1.0f/2.2f);
  204. bb = powf(bb, 1.0f/2.2f);
  205. dst[0] = (uint8_t)rr;
  206. dst[1] = (uint8_t)gg;
  207. dst[2] = (uint8_t)bb;
  208. dst[3] = (uint8_t)aa;
  209. }
  210. }
  211. }
  212. void imageRgba8Downsample2x2(uint32_t _width, uint32_t _height, uint32_t _srcPitch, const void* _src, void* _dst)
  213. {
  214. const uint32_t dstwidth = _width/2;
  215. const uint32_t dstheight = _height/2;
  216. if (0 == dstwidth
  217. || 0 == dstheight)
  218. {
  219. return;
  220. }
  221. uint8_t* dst = (uint8_t*)_dst;
  222. const uint8_t* src = (const uint8_t*)_src;
  223. using namespace bx;
  224. const float4_t unpack = float4_ld(1.0f, 1.0f/256.0f, 1.0f/65536.0f, 1.0f/16777216.0f);
  225. const float4_t pack = float4_ld(1.0f, 256.0f*0.5f, 65536.0f, 16777216.0f*0.5f);
  226. const float4_t umask = float4_ild(0xff, 0xff00, 0xff0000, 0xff000000);
  227. const float4_t pmask = float4_ild(0xff, 0x7f80, 0xff0000, 0x7f800000);
  228. const float4_t wflip = float4_ild(0, 0, 0, 0x80000000);
  229. const float4_t wadd = float4_ld(0.0f, 0.0f, 0.0f, 32768.0f*65536.0f);
  230. const float4_t gamma = float4_ld(1.0f/2.2f, 1.0f/2.2f, 1.0f/2.2f, 1.0f);
  231. const float4_t linear = float4_ld(2.2f, 2.2f, 2.2f, 1.0f);
  232. const float4_t quater = float4_splat(0.25f);
  233. for (uint32_t yy = 0, ystep = _srcPitch*2; yy < dstheight; ++yy, src += ystep)
  234. {
  235. const uint8_t* rgba = src;
  236. for (uint32_t xx = 0; xx < dstwidth; ++xx, rgba += 8, dst += 4)
  237. {
  238. const float4_t abgr0 = float4_splat(rgba);
  239. const float4_t abgr1 = float4_splat(rgba+4);
  240. const float4_t abgr2 = float4_splat(rgba+_srcPitch);
  241. const float4_t abgr3 = float4_splat(rgba+_srcPitch+4);
  242. const float4_t abgr0m = float4_and(abgr0, umask);
  243. const float4_t abgr1m = float4_and(abgr1, umask);
  244. const float4_t abgr2m = float4_and(abgr2, umask);
  245. const float4_t abgr3m = float4_and(abgr3, umask);
  246. const float4_t abgr0x = float4_xor(abgr0m, wflip);
  247. const float4_t abgr1x = float4_xor(abgr1m, wflip);
  248. const float4_t abgr2x = float4_xor(abgr2m, wflip);
  249. const float4_t abgr3x = float4_xor(abgr3m, wflip);
  250. const float4_t abgr0f = float4_itof(abgr0x);
  251. const float4_t abgr1f = float4_itof(abgr1x);
  252. const float4_t abgr2f = float4_itof(abgr2x);
  253. const float4_t abgr3f = float4_itof(abgr3x);
  254. const float4_t abgr0c = float4_add(abgr0f, wadd);
  255. const float4_t abgr1c = float4_add(abgr1f, wadd);
  256. const float4_t abgr2c = float4_add(abgr2f, wadd);
  257. const float4_t abgr3c = float4_add(abgr3f, wadd);
  258. const float4_t abgr0n = float4_mul(abgr0c, unpack);
  259. const float4_t abgr1n = float4_mul(abgr1c, unpack);
  260. const float4_t abgr2n = float4_mul(abgr2c, unpack);
  261. const float4_t abgr3n = float4_mul(abgr3c, unpack);
  262. const float4_t abgr0l = float4_pow(abgr0n, linear);
  263. const float4_t abgr1l = float4_pow(abgr1n, linear);
  264. const float4_t abgr2l = float4_pow(abgr2n, linear);
  265. const float4_t abgr3l = float4_pow(abgr3n, linear);
  266. const float4_t sum0 = float4_add(abgr0l, abgr1l);
  267. const float4_t sum1 = float4_add(abgr2l, abgr3l);
  268. const float4_t sum2 = float4_add(sum0, sum1);
  269. const float4_t avg0 = float4_mul(sum2, quater);
  270. const float4_t avg1 = float4_pow(avg0, gamma);
  271. const float4_t avg2 = float4_mul(avg1, pack);
  272. const float4_t ftoi0 = float4_ftoi(avg2);
  273. const float4_t ftoi1 = float4_and(ftoi0, pmask);
  274. const float4_t zwxy = float4_swiz_zwxy(ftoi1);
  275. const float4_t tmp0 = float4_or(ftoi1, zwxy);
  276. const float4_t yyyy = float4_swiz_yyyy(tmp0);
  277. const float4_t tmp1 = float4_iadd(yyyy, yyyy);
  278. const float4_t result = float4_or(tmp0, tmp1);
  279. float4_stx(dst, result);
  280. }
  281. }
  282. }
  283. void imageSwizzleBgra8Ref(uint32_t _width, uint32_t _height, uint32_t _srcPitch, const void* _src, void* _dst)
  284. {
  285. const uint8_t* src = (uint8_t*) _src;
  286. const uint8_t* next = src + _srcPitch;
  287. uint8_t* dst = (uint8_t*)_dst;
  288. for (uint32_t yy = 0; yy < _height; ++yy, src = next, next += _srcPitch)
  289. {
  290. for (uint32_t xx = 0; xx < _width; ++xx, src += 4, dst += 4)
  291. {
  292. uint8_t rr = src[0];
  293. uint8_t gg = src[1];
  294. uint8_t bb = src[2];
  295. uint8_t aa = src[3];
  296. dst[0] = bb;
  297. dst[1] = gg;
  298. dst[2] = rr;
  299. dst[3] = aa;
  300. }
  301. }
  302. }
  303. void imageSwizzleBgra8(uint32_t _width, uint32_t _height, uint32_t _srcPitch, const void* _src, void* _dst)
  304. {
  305. // Test can we do four 4-byte pixels at the time.
  306. if (0 != (_width&0x3)
  307. || _width < 4
  308. || !bx::isPtrAligned(_src, 16)
  309. || !bx::isPtrAligned(_dst, 16) )
  310. {
  311. BX_WARN(false, "Image swizzle is taking slow path.");
  312. BX_WARN(bx::isPtrAligned(_src, 16), "Source %p is not 16-byte aligned.", _src);
  313. BX_WARN(bx::isPtrAligned(_dst, 16), "Destination %p is not 16-byte aligned.", _dst);
  314. BX_WARN(_width < 4, "Image width must be multiple of 4 (width %d).", _width);
  315. imageSwizzleBgra8Ref(_width, _height, _srcPitch, _src, _dst);
  316. return;
  317. }
  318. using namespace bx;
  319. const float4_t mf0f0 = float4_isplat(0xff00ff00);
  320. const float4_t m0f0f = float4_isplat(0x00ff00ff);
  321. const uint8_t* src = (uint8_t*) _src;
  322. const uint8_t* next = src + _srcPitch;
  323. uint8_t* dst = (uint8_t*)_dst;
  324. const uint32_t width = _width/4;
  325. for (uint32_t yy = 0; yy < _height; ++yy, src = next, next += _srcPitch)
  326. {
  327. for (uint32_t xx = 0; xx < width; ++xx, src += 16, dst += 16)
  328. {
  329. const float4_t tabgr = float4_ld(src);
  330. const float4_t t00ab = float4_srl(tabgr, 16);
  331. const float4_t tgr00 = float4_sll(tabgr, 16);
  332. const float4_t tgrab = float4_or(t00ab, tgr00);
  333. const float4_t ta0g0 = float4_and(tabgr, mf0f0);
  334. const float4_t t0r0b = float4_and(tgrab, m0f0f);
  335. const float4_t targb = float4_or(ta0g0, t0r0b);
  336. float4_st(dst, targb);
  337. }
  338. }
  339. }
  340. void imageCopy(uint32_t _width, uint32_t _height, uint32_t _bpp, uint32_t _srcPitch, const void* _src, void* _dst)
  341. {
  342. const uint32_t pitch = _width*_bpp/8;
  343. const uint8_t* src = (uint8_t*) _src;
  344. const uint8_t* next = src + _srcPitch;
  345. uint8_t* dst = (uint8_t*)_dst;
  346. for (uint32_t yy = 0; yy < _height; ++yy, src = next, next += _srcPitch, dst += pitch)
  347. {
  348. memcpy(dst, src, pitch);
  349. }
  350. }
  351. void imageWriteTga(bx::WriterI* _writer, uint32_t _width, uint32_t _height, uint32_t _srcPitch, const void* _src, bool _grayscale, bool _yflip)
  352. {
  353. uint8_t type = _grayscale ? 3 : 2;
  354. uint8_t bpp = _grayscale ? 8 : 32;
  355. uint8_t header[18] = {};
  356. header[2] = type;
  357. header[12] = _width&0xff;
  358. header[13] = (_width>>8)&0xff;
  359. header[14] = _height&0xff;
  360. header[15] = (_height>>8)&0xff;
  361. header[16] = bpp;
  362. header[17] = 32;
  363. bx::write(_writer, header, sizeof(header) );
  364. uint32_t dstPitch = _width*bpp/8;
  365. if (_yflip)
  366. {
  367. uint8_t* data = (uint8_t*)_src + _srcPitch*_height - _srcPitch;
  368. for (uint32_t yy = 0; yy < _height; ++yy)
  369. {
  370. bx::write(_writer, data, dstPitch);
  371. data -= _srcPitch;
  372. }
  373. }
  374. else if (_srcPitch == dstPitch)
  375. {
  376. bx::write(_writer, _src, _height*_srcPitch);
  377. }
  378. else
  379. {
  380. uint8_t* data = (uint8_t*)_src;
  381. for (uint32_t yy = 0; yy < _height; ++yy)
  382. {
  383. bx::write(_writer, data, dstPitch);
  384. data += _srcPitch;
  385. }
  386. }
  387. }
  388. uint32_t bitRangeConvert(uint32_t _in, uint32_t _from, uint32_t _to)
  389. {
  390. using namespace bx;
  391. uint32_t tmp0 = uint32_sll(1, _to);
  392. uint32_t tmp1 = uint32_sll(1, _from);
  393. uint32_t tmp2 = uint32_dec(tmp0);
  394. uint32_t tmp3 = uint32_dec(tmp1);
  395. uint32_t tmp4 = uint32_mul(_in, tmp2);
  396. uint32_t tmp5 = uint32_add(tmp3, tmp4);
  397. uint32_t tmp6 = uint32_srl(tmp5, _from);
  398. uint32_t tmp7 = uint32_add(tmp5, tmp6);
  399. uint32_t result = uint32_srl(tmp7, _from);
  400. return result;
  401. }
  402. void decodeBlockDxt(uint8_t _dst[16*4], const uint8_t _src[8])
  403. {
  404. uint8_t colors[4*3];
  405. uint32_t c0 = _src[0] | (_src[1] << 8);
  406. colors[0] = bitRangeConvert( (c0>> 0)&0x1f, 5, 8);
  407. colors[1] = bitRangeConvert( (c0>> 5)&0x3f, 6, 8);
  408. colors[2] = bitRangeConvert( (c0>>11)&0x1f, 5, 8);
  409. uint32_t c1 = _src[2] | (_src[3] << 8);
  410. colors[3] = bitRangeConvert( (c1>> 0)&0x1f, 5, 8);
  411. colors[4] = bitRangeConvert( (c1>> 5)&0x3f, 6, 8);
  412. colors[5] = bitRangeConvert( (c1>>11)&0x1f, 5, 8);
  413. colors[6] = (2*colors[0] + colors[3]) / 3;
  414. colors[7] = (2*colors[1] + colors[4]) / 3;
  415. colors[8] = (2*colors[2] + colors[5]) / 3;
  416. colors[ 9] = (colors[0] + 2*colors[3]) / 3;
  417. colors[10] = (colors[1] + 2*colors[4]) / 3;
  418. colors[11] = (colors[2] + 2*colors[5]) / 3;
  419. for (uint32_t ii = 0, next = 8*4; ii < 16*4; ii += 4, next += 2)
  420. {
  421. int idx = ( (_src[next>>3] >> (next & 7) ) & 3) * 3;
  422. _dst[ii+0] = colors[idx+0];
  423. _dst[ii+1] = colors[idx+1];
  424. _dst[ii+2] = colors[idx+2];
  425. }
  426. }
  427. void decodeBlockDxt1(uint8_t _dst[16*4], const uint8_t _src[8])
  428. {
  429. uint8_t colors[4*4];
  430. uint32_t c0 = _src[0] | (_src[1] << 8);
  431. colors[0] = bitRangeConvert( (c0>> 0)&0x1f, 5, 8);
  432. colors[1] = bitRangeConvert( (c0>> 5)&0x3f, 6, 8);
  433. colors[2] = bitRangeConvert( (c0>>11)&0x1f, 5, 8);
  434. colors[3] = 255;
  435. uint32_t c1 = _src[2] | (_src[3] << 8);
  436. colors[4] = bitRangeConvert( (c1>> 0)&0x1f, 5, 8);
  437. colors[5] = bitRangeConvert( (c1>> 5)&0x3f, 6, 8);
  438. colors[6] = bitRangeConvert( (c1>>11)&0x1f, 5, 8);
  439. colors[7] = 255;
  440. if (c0 > c1)
  441. {
  442. colors[ 8] = (2*colors[0] + colors[4]) / 3;
  443. colors[ 9] = (2*colors[1] + colors[5]) / 3;
  444. colors[10] = (2*colors[2] + colors[6]) / 3;
  445. colors[11] = 255;
  446. colors[12] = (colors[0] + 2*colors[4]) / 3;
  447. colors[13] = (colors[1] + 2*colors[5]) / 3;
  448. colors[14] = (colors[2] + 2*colors[6]) / 3;
  449. colors[15] = 255;
  450. }
  451. else
  452. {
  453. colors[ 8] = (colors[0] + colors[4]) / 2;
  454. colors[ 9] = (colors[1] + colors[5]) / 2;
  455. colors[10] = (colors[2] + colors[6]) / 2;
  456. colors[11] = 255;
  457. colors[12] = 0;
  458. colors[13] = 0;
  459. colors[14] = 0;
  460. colors[15] = 0;
  461. }
  462. for (uint32_t ii = 0, next = 8*4; ii < 16*4; ii += 4, next += 2)
  463. {
  464. int idx = ( (_src[next>>3] >> (next & 7) ) & 3) * 4;
  465. _dst[ii+0] = colors[idx+0];
  466. _dst[ii+1] = colors[idx+1];
  467. _dst[ii+2] = colors[idx+2];
  468. _dst[ii+3] = colors[idx+3];
  469. }
  470. }
  471. void decodeBlockDxt23A(uint8_t _dst[16*4], const uint8_t _src[8])
  472. {
  473. for (uint32_t ii = 0, next = 0; ii < 16*4; ii += 4, next += 4)
  474. {
  475. uint32_t c0 = (_src[next>>3] >> (next&7) ) & 0xf;
  476. _dst[ii] = bitRangeConvert(c0, 4, 8);
  477. }
  478. }
  479. void decodeBlockDxt45A(uint8_t _dst[16*4], const uint8_t _src[8])
  480. {
  481. uint8_t alpha[8];
  482. alpha[0] = _src[0];
  483. alpha[1] = _src[1];
  484. if (alpha[0] > alpha[1])
  485. {
  486. alpha[2] = (6*alpha[0] + 1*alpha[1]) / 7;
  487. alpha[3] = (5*alpha[0] + 2*alpha[1]) / 7;
  488. alpha[4] = (4*alpha[0] + 3*alpha[1]) / 7;
  489. alpha[5] = (3*alpha[0] + 4*alpha[1]) / 7;
  490. alpha[6] = (2*alpha[0] + 5*alpha[1]) / 7;
  491. alpha[7] = (1*alpha[0] + 6*alpha[1]) / 7;
  492. }
  493. else
  494. {
  495. alpha[2] = (4*alpha[0] + 1*alpha[1]) / 5;
  496. alpha[3] = (3*alpha[0] + 2*alpha[1]) / 5;
  497. alpha[4] = (2*alpha[0] + 3*alpha[1]) / 5;
  498. alpha[5] = (1*alpha[0] + 4*alpha[1]) / 5;
  499. alpha[6] = 0;
  500. alpha[7] = 255;
  501. }
  502. uint32_t idx0 = _src[2];
  503. uint32_t idx1 = _src[5];
  504. idx0 |= uint32_t(_src[3])<<8;
  505. idx1 |= uint32_t(_src[6])<<8;
  506. idx0 |= uint32_t(_src[4])<<16;
  507. idx1 |= uint32_t(_src[7])<<16;
  508. for (uint32_t ii = 0; ii < 8*4; ii += 4)
  509. {
  510. _dst[ii] = alpha[idx0&7];
  511. _dst[ii+32] = alpha[idx1&7];
  512. idx0 >>= 3;
  513. idx1 >>= 3;
  514. }
  515. }
  516. static const int32_t s_etc1Mod[8][4] =
  517. {
  518. { 2, 8, -2, -8},
  519. { 5, 17, -5, -17},
  520. { 9, 29, -9, -29},
  521. { 13, 42, -13, -42},
  522. { 18, 60, -18, -60},
  523. { 24, 80, -24, -80},
  524. { 33, 106, -33, -106},
  525. { 47, 183, -47, -183},
  526. };
  527. static const uint8_t s_etc2Mod[8] = { 3, 6, 11, 16, 23, 32, 41, 64 };
  528. uint8_t uint8_sat(int32_t _a)
  529. {
  530. using namespace bx;
  531. const uint32_t min = uint32_imin(_a, 255);
  532. const uint32_t result = uint32_imax(min, 0);
  533. return (uint8_t)result;
  534. }
  535. uint8_t uint8_satadd(int32_t _a, int32_t _b)
  536. {
  537. const int32_t add = _a + _b;
  538. return uint8_sat(add);
  539. }
  540. void decodeBlockEtc2ModeT(uint8_t _dst[16*4], const uint8_t _src[8])
  541. {
  542. uint8_t rgb[16];
  543. // 0 1 2 3 4 5 6 7
  544. // 7654321076543210765432107654321076543210765432107654321076543210
  545. // ...rr.rrggggbbbbrrrrggggbbbbDDD.mmmmmmmmmmmmmmmmllllllllllllllll
  546. // ^ ^ ^ ^ ^
  547. // +-- c0 +-- c1 | +-- msb +-- lsb
  548. // +-- dist
  549. rgb[ 0] = ( (_src[0] >> 1) & 0xc)
  550. | (_src[0] & 0x3)
  551. ;
  552. rgb[ 1] = _src[1] >> 4;
  553. rgb[ 2] = _src[1] & 0xf;
  554. rgb[ 8] = _src[2] >> 4;
  555. rgb[ 9] = _src[2] & 0xf;
  556. rgb[10] = _src[3] >> 4;
  557. rgb[ 0] = bitRangeConvert(rgb[ 0], 4, 8);
  558. rgb[ 1] = bitRangeConvert(rgb[ 1], 4, 8);
  559. rgb[ 2] = bitRangeConvert(rgb[ 2], 4, 8);
  560. rgb[ 8] = bitRangeConvert(rgb[ 8], 4, 8);
  561. rgb[ 9] = bitRangeConvert(rgb[ 9], 4, 8);
  562. rgb[10] = bitRangeConvert(rgb[10], 4, 8);
  563. uint8_t dist = (_src[3] >> 1) & 0x7;
  564. int32_t mod = s_etc2Mod[dist];
  565. rgb[ 4] = uint8_satadd(rgb[ 8], mod);
  566. rgb[ 5] = uint8_satadd(rgb[ 9], mod);
  567. rgb[ 6] = uint8_satadd(rgb[10], mod);
  568. rgb[12] = uint8_satadd(rgb[ 8], -mod);
  569. rgb[13] = uint8_satadd(rgb[ 9], -mod);
  570. rgb[14] = uint8_satadd(rgb[10], -mod);
  571. uint32_t indexMsb = (_src[4]<<8) | _src[5];
  572. uint32_t indexLsb = (_src[6]<<8) | _src[7];
  573. for (uint32_t ii = 0; ii < 16; ++ii)
  574. {
  575. const uint32_t idx = (ii&0xc) | ( (ii & 0x3)<<4);
  576. const uint32_t lsbi = indexLsb & 1;
  577. const uint32_t msbi = (indexMsb & 1)<<1;
  578. const uint32_t pal = (lsbi | msbi)<<2;
  579. _dst[idx + 0] = rgb[pal+2];
  580. _dst[idx + 1] = rgb[pal+1];
  581. _dst[idx + 2] = rgb[pal+0];
  582. _dst[idx + 3] = 255;
  583. indexLsb >>= 1;
  584. indexMsb >>= 1;
  585. }
  586. }
  587. void decodeBlockEtc2ModeH(uint8_t _dst[16*4], const uint8_t _src[8])
  588. {
  589. uint8_t rgb[16];
  590. // 0 1 2 3 4 5 6 7
  591. // 7654321076543210765432107654321076543210765432107654321076543210
  592. // .rrrrggg...gb.bbbrrrrggggbbbbDD.mmmmmmmmmmmmmmmmllllllllllllllll
  593. // ^ ^ ^ ^ ^
  594. // +-- c0 +-- c1 | +-- msb +-- lsb
  595. // +-- dist
  596. rgb[ 0] = (_src[0] >> 3) & 0xf;
  597. rgb[ 1] = ( (_src[0] << 1) & 0xe)
  598. | ( (_src[1] >> 4) & 0x1)
  599. ;
  600. rgb[ 2] = (_src[1] & 0x8)
  601. | ( (_src[1] << 1) & 0x6)
  602. | (_src[2] >> 7)
  603. ;
  604. rgb[ 8] = (_src[2] >> 3) & 0xf;
  605. rgb[ 9] = ( (_src[2] << 1) & 0xe)
  606. | (_src[3] >> 7)
  607. ;
  608. rgb[10] = (_src[2] >> 3) & 0xf;
  609. rgb[ 0] = bitRangeConvert(rgb[ 0], 4, 8);
  610. rgb[ 1] = bitRangeConvert(rgb[ 1], 4, 8);
  611. rgb[ 2] = bitRangeConvert(rgb[ 2], 4, 8);
  612. rgb[ 8] = bitRangeConvert(rgb[ 8], 4, 8);
  613. rgb[ 9] = bitRangeConvert(rgb[ 9], 4, 8);
  614. rgb[10] = bitRangeConvert(rgb[10], 4, 8);
  615. uint32_t col0 = uint32_t(rgb[0]<<16) | uint32_t(rgb[1]<<8) | uint32_t(rgb[ 2]);
  616. uint32_t col1 = uint32_t(rgb[8]<<16) | uint32_t(rgb[9]<<8) | uint32_t(rgb[10]);
  617. uint8_t dist = (_src[3] & 0x6) | (col0 >= col1);
  618. int32_t mod = s_etc2Mod[dist];
  619. rgb[ 4] = uint8_satadd(rgb[ 0], -mod);
  620. rgb[ 5] = uint8_satadd(rgb[ 1], -mod);
  621. rgb[ 6] = uint8_satadd(rgb[ 2], -mod);
  622. rgb[ 0] = uint8_satadd(rgb[ 0], mod);
  623. rgb[ 1] = uint8_satadd(rgb[ 1], mod);
  624. rgb[ 2] = uint8_satadd(rgb[ 2], mod);
  625. rgb[12] = uint8_satadd(rgb[ 8], -mod);
  626. rgb[13] = uint8_satadd(rgb[ 9], -mod);
  627. rgb[14] = uint8_satadd(rgb[10], -mod);
  628. rgb[ 8] = uint8_satadd(rgb[ 8], mod);
  629. rgb[ 9] = uint8_satadd(rgb[ 9], mod);
  630. rgb[10] = uint8_satadd(rgb[10], mod);
  631. uint32_t indexMsb = (_src[4]<<8) | _src[5];
  632. uint32_t indexLsb = (_src[6]<<8) | _src[7];
  633. for (uint32_t ii = 0; ii < 16; ++ii)
  634. {
  635. const uint32_t idx = (ii&0xc) | ( (ii & 0x3)<<4);
  636. const uint32_t lsbi = indexLsb & 1;
  637. const uint32_t msbi = (indexMsb & 1)<<1;
  638. const uint32_t pal = (lsbi | msbi)<<2;
  639. _dst[idx + 0] = rgb[pal+2];
  640. _dst[idx + 1] = rgb[pal+1];
  641. _dst[idx + 2] = rgb[pal+0];
  642. _dst[idx + 3] = 255;
  643. indexLsb >>= 1;
  644. indexMsb >>= 1;
  645. }
  646. }
  647. void decodeBlockEtc2ModePlanar(uint8_t _dst[16*4], const uint8_t _src[8])
  648. {
  649. // 0 1 2 3 4 5 6 7
  650. // 7654321076543210765432107654321076543210765432107654321076543210
  651. // .rrrrrrg.ggggggb...bb.bbbrrrrr.rgggggggbbbbbbrrrrrrgggggggbbbbbb
  652. // ^ ^ ^
  653. // +-- c0 +-- cH +-- cV
  654. uint8_t c0[3];
  655. uint8_t cH[3];
  656. uint8_t cV[3];
  657. c0[0] = (_src[0] >> 1) & 0x3f;
  658. c0[1] = ( (_src[0] & 1) << 6)
  659. | ( (_src[1] >> 1) & 0x3f)
  660. ;
  661. c0[2] = ( (_src[1] & 1) << 5)
  662. | ( (_src[2] & 0x18) )
  663. | ( (_src[2] << 1) & 6)
  664. | ( (_src[3] >> 7) )
  665. ;
  666. cH[0] = ( (_src[3] >> 1) & 0x3e)
  667. | (_src[3] & 1)
  668. ;
  669. cH[1] = _src[4] >> 1;
  670. cH[2] = ( (_src[4] & 1) << 5)
  671. | (_src[5] >> 3)
  672. ;
  673. cV[0] = ( (_src[5] & 0x7) << 3)
  674. | (_src[6] >> 5)
  675. ;
  676. cV[1] = ( (_src[6] & 0x1f) << 2)
  677. | (_src[7] >> 5)
  678. ;
  679. cV[2] = _src[7] & 0x3f;
  680. c0[0] = bitRangeConvert(c0[0], 6, 8);
  681. c0[1] = bitRangeConvert(c0[1], 7, 8);
  682. c0[2] = bitRangeConvert(c0[2], 6, 8);
  683. cH[0] = bitRangeConvert(cH[0], 6, 8);
  684. cH[1] = bitRangeConvert(cH[1], 7, 8);
  685. cH[2] = bitRangeConvert(cH[2], 6, 8);
  686. cV[0] = bitRangeConvert(cV[0], 6, 8);
  687. cV[1] = bitRangeConvert(cV[1], 7, 8);
  688. cV[2] = bitRangeConvert(cV[2], 6, 8);
  689. int16_t dy[3];
  690. dy[0] = cV[0] - c0[0];
  691. dy[1] = cV[1] - c0[1];
  692. dy[2] = cV[2] - c0[2];
  693. int16_t sx[3];
  694. sx[0] = int16_t(c0[0])<<2;
  695. sx[1] = int16_t(c0[1])<<2;
  696. sx[2] = int16_t(c0[2])<<2;
  697. int16_t ex[3];
  698. ex[0] = int16_t(cH[0])<<2;
  699. ex[1] = int16_t(cH[1])<<2;
  700. ex[2] = int16_t(cH[2])<<2;
  701. for (int32_t vv = 0; vv < 4; ++vv)
  702. {
  703. int16_t dx[3];
  704. dx[0] = (ex[0] - sx[0])>>2;
  705. dx[1] = (ex[1] - sx[1])>>2;
  706. dx[2] = (ex[2] - sx[2])>>2;
  707. for (int32_t hh = 0; hh < 4; ++hh)
  708. {
  709. const uint32_t idx = (vv<<4) + (hh<<2);
  710. _dst[idx + 0] = uint8_sat( (sx[2] + dx[2]*hh)>>2);
  711. _dst[idx + 1] = uint8_sat( (sx[1] + dx[1]*hh)>>2);
  712. _dst[idx + 2] = uint8_sat( (sx[0] + dx[0]*hh)>>2);
  713. _dst[idx + 3] = 255;
  714. }
  715. sx[0] += dy[0];
  716. sx[1] += dy[1];
  717. sx[2] += dy[2];
  718. ex[0] += dy[0];
  719. ex[1] += dy[1];
  720. ex[2] += dy[2];
  721. }
  722. }
  723. void decodeBlockEtc12(uint8_t _dst[16*4], const uint8_t _src[8])
  724. {
  725. bool flipBit = 0 != (_src[3] & 0x1);
  726. bool diffBit = 0 != (_src[3] & 0x2);
  727. uint8_t rgb[8];
  728. if (diffBit)
  729. {
  730. rgb[0] = _src[0] >> 3;
  731. rgb[1] = _src[1] >> 3;
  732. rgb[2] = _src[2] >> 3;
  733. int8_t diff[3];
  734. diff[0] = int8_t( (_src[0] & 0x7)<<5)>>5;
  735. diff[1] = int8_t( (_src[1] & 0x7)<<5)>>5;
  736. diff[2] = int8_t( (_src[2] & 0x7)<<5)>>5;
  737. int8_t rr = rgb[0] + diff[0];
  738. int8_t gg = rgb[1] + diff[1];
  739. int8_t bb = rgb[2] + diff[2];
  740. // Etc2 3-modes
  741. if (rr < 0 || rr > 31)
  742. {
  743. decodeBlockEtc2ModeT(_dst, _src);
  744. return;
  745. }
  746. if (gg < 0 || gg > 31)
  747. {
  748. decodeBlockEtc2ModeH(_dst, _src);
  749. return;
  750. }
  751. if (bb < 0 || bb > 31)
  752. {
  753. decodeBlockEtc2ModePlanar(_dst, _src);
  754. return;
  755. }
  756. // Etc1
  757. rgb[0] = bitRangeConvert(rgb[0], 5, 8);
  758. rgb[1] = bitRangeConvert(rgb[1], 5, 8);
  759. rgb[2] = bitRangeConvert(rgb[2], 5, 8);
  760. rgb[4] = bitRangeConvert(rr, 5, 8);
  761. rgb[5] = bitRangeConvert(gg, 5, 8);
  762. rgb[6] = bitRangeConvert(bb, 5, 8);
  763. }
  764. else
  765. {
  766. rgb[0] = _src[0] >> 4;
  767. rgb[1] = _src[1] >> 4;
  768. rgb[2] = _src[2] >> 4;
  769. rgb[4] = _src[0] & 0xf;
  770. rgb[5] = _src[1] & 0xf;
  771. rgb[6] = _src[2] & 0xf;
  772. rgb[0] = bitRangeConvert(rgb[0], 4, 8);
  773. rgb[1] = bitRangeConvert(rgb[1], 4, 8);
  774. rgb[2] = bitRangeConvert(rgb[2], 4, 8);
  775. rgb[4] = bitRangeConvert(rgb[4], 4, 8);
  776. rgb[5] = bitRangeConvert(rgb[5], 4, 8);
  777. rgb[6] = bitRangeConvert(rgb[6], 4, 8);
  778. }
  779. uint32_t table[2];
  780. table[0] = (_src[3] >> 5) & 0x7;
  781. table[1] = (_src[3] >> 2) & 0x7;
  782. uint32_t indexMsb = (_src[4]<<8) | _src[5];
  783. uint32_t indexLsb = (_src[6]<<8) | _src[7];
  784. if (flipBit)
  785. {
  786. for (uint32_t ii = 0; ii < 16; ++ii)
  787. {
  788. const uint32_t block = (ii>>1)&1;
  789. const uint32_t color = block<<2;
  790. const uint32_t idx = (ii&0xc) | ( (ii & 0x3)<<4);
  791. const uint32_t lsbi = indexLsb & 1;
  792. const uint32_t msbi = (indexMsb & 1)<<1;
  793. const int32_t mod = s_etc1Mod[table[block] ][lsbi | msbi];
  794. _dst[idx + 0] = uint8_satadd(rgb[color+2], mod);
  795. _dst[idx + 1] = uint8_satadd(rgb[color+1], mod);
  796. _dst[idx + 2] = uint8_satadd(rgb[color+0], mod);
  797. _dst[idx + 3] = 255;
  798. indexLsb >>= 1;
  799. indexMsb >>= 1;
  800. }
  801. }
  802. else
  803. {
  804. for (uint32_t ii = 0; ii < 16; ++ii)
  805. {
  806. const uint32_t block = ii>>3;
  807. const uint32_t color = block<<2;
  808. const uint32_t idx = (ii&0xc) | ( (ii & 0x3)<<4);
  809. const uint32_t lsbi = indexLsb & 1;
  810. const uint32_t msbi = (indexMsb & 1)<<1;
  811. const int32_t mod = s_etc1Mod[table[block] ][lsbi | msbi];
  812. _dst[idx + 0] = uint8_satadd(rgb[color+2], mod);
  813. _dst[idx + 1] = uint8_satadd(rgb[color+1], mod);
  814. _dst[idx + 2] = uint8_satadd(rgb[color+0], mod);
  815. _dst[idx + 3] = 255;
  816. indexLsb >>= 1;
  817. indexMsb >>= 1;
  818. }
  819. }
  820. }
  821. // DDS
  822. #define DDS_MAGIC BX_MAKEFOURCC('D', 'D', 'S', ' ')
  823. #define DDS_HEADER_SIZE 124
  824. #define DDS_DXT1 BX_MAKEFOURCC('D', 'X', 'T', '1')
  825. #define DDS_DXT2 BX_MAKEFOURCC('D', 'X', 'T', '2')
  826. #define DDS_DXT3 BX_MAKEFOURCC('D', 'X', 'T', '3')
  827. #define DDS_DXT4 BX_MAKEFOURCC('D', 'X', 'T', '4')
  828. #define DDS_DXT5 BX_MAKEFOURCC('D', 'X', 'T', '5')
  829. #define DDS_ATI1 BX_MAKEFOURCC('A', 'T', 'I', '1')
  830. #define DDS_BC4U BX_MAKEFOURCC('B', 'C', '4', 'U')
  831. #define DDS_ATI2 BX_MAKEFOURCC('A', 'T', 'I', '2')
  832. #define DDS_BC5U BX_MAKEFOURCC('B', 'C', '5', 'U')
  833. #define DDS_DX10 BX_MAKEFOURCC('D', 'X', '1', '0')
  834. #define D3DFMT_A8R8G8B8 21
  835. #define D3DFMT_R5G6B5 23
  836. #define D3DFMT_A1R5G5B5 25
  837. #define D3DFMT_A4R4G4B4 26
  838. #define D3DFMT_A2B10G10R10 31
  839. #define D3DFMT_G16R16 34
  840. #define D3DFMT_A2R10G10B10 35
  841. #define D3DFMT_A16B16G16R16 36
  842. #define D3DFMT_A8L8 51
  843. #define D3DFMT_R16F 111
  844. #define D3DFMT_G16R16F 112
  845. #define D3DFMT_A16B16G16R16F 113
  846. #define D3DFMT_R32F 114
  847. #define D3DFMT_G32R32F 115
  848. #define D3DFMT_A32B32G32R32F 116
  849. #define DXGI_FORMAT_R32G32B32A32_FLOAT 2
  850. #define DXGI_FORMAT_R32G32B32A32_UINT 3
  851. #define DXGI_FORMAT_R16G16B16A16_FLOAT 10
  852. #define DXGI_FORMAT_R16G16B16A16_UNORM 11
  853. #define DXGI_FORMAT_R16G16B16A16_UINT 12
  854. #define DXGI_FORMAT_R32G32_FLOAT 16
  855. #define DXGI_FORMAT_R32G32_UINT 17
  856. #define DXGI_FORMAT_R10G10B10A2_UNORM 24
  857. #define DXGI_FORMAT_R16G16_FLOAT 34
  858. #define DXGI_FORMAT_R16G16_UNORM 35
  859. #define DXGI_FORMAT_R32_FLOAT 41
  860. #define DXGI_FORMAT_R32_UINT 42
  861. #define DXGI_FORMAT_R8G8_UNORM 49
  862. #define DXGI_FORMAT_R16_FLOAT 54
  863. #define DXGI_FORMAT_R16_UNORM 56
  864. #define DXGI_FORMAT_R8_UNORM 61
  865. #define DXGI_FORMAT_BC1_UNORM 71
  866. #define DXGI_FORMAT_BC2_UNORM 74
  867. #define DXGI_FORMAT_BC3_UNORM 77
  868. #define DXGI_FORMAT_BC4_UNORM 80
  869. #define DXGI_FORMAT_BC5_UNORM 83
  870. #define DXGI_FORMAT_B5G6R5_UNORM 85
  871. #define DXGI_FORMAT_B5G5R5A1_UNORM 86
  872. #define DXGI_FORMAT_B8G8R8A8_UNORM 87
  873. #define DXGI_FORMAT_BC6H_SF16 96
  874. #define DXGI_FORMAT_BC7_UNORM 98
  875. #define DXGI_FORMAT_B4G4R4A4_UNORM 115
  876. #define DDSD_CAPS 0x00000001
  877. #define DDSD_HEIGHT 0x00000002
  878. #define DDSD_WIDTH 0x00000004
  879. #define DDSD_PITCH 0x00000008
  880. #define DDSD_PIXELFORMAT 0x00001000
  881. #define DDSD_MIPMAPCOUNT 0x00020000
  882. #define DDSD_LINEARSIZE 0x00080000
  883. #define DDSD_DEPTH 0x00800000
  884. #define DDPF_ALPHAPIXELS 0x00000001
  885. #define DDPF_ALPHA 0x00000002
  886. #define DDPF_FOURCC 0x00000004
  887. #define DDPF_INDEXED 0x00000020
  888. #define DDPF_RGB 0x00000040
  889. #define DDPF_YUV 0x00000200
  890. #define DDPF_LUMINANCE 0x00020000
  891. #define DDSCAPS_COMPLEX 0x00000008
  892. #define DDSCAPS_TEXTURE 0x00001000
  893. #define DDSCAPS_MIPMAP 0x00400000
  894. #define DDSCAPS2_CUBEMAP 0x00000200
  895. #define DDSCAPS2_CUBEMAP_POSITIVEX 0x00000400
  896. #define DDSCAPS2_CUBEMAP_NEGATIVEX 0x00000800
  897. #define DDSCAPS2_CUBEMAP_POSITIVEY 0x00001000
  898. #define DDSCAPS2_CUBEMAP_NEGATIVEY 0x00002000
  899. #define DDSCAPS2_CUBEMAP_POSITIVEZ 0x00004000
  900. #define DDSCAPS2_CUBEMAP_NEGATIVEZ 0x00008000
  901. #define DDS_CUBEMAP_ALLFACES (DDSCAPS2_CUBEMAP_POSITIVEX|DDSCAPS2_CUBEMAP_NEGATIVEX \
  902. |DDSCAPS2_CUBEMAP_POSITIVEY|DDSCAPS2_CUBEMAP_NEGATIVEY \
  903. |DDSCAPS2_CUBEMAP_POSITIVEZ|DDSCAPS2_CUBEMAP_NEGATIVEZ)
  904. #define DDSCAPS2_VOLUME 0x00200000
  905. struct TranslateDdsFormat
  906. {
  907. uint32_t m_format;
  908. TextureFormat::Enum m_textureFormat;
  909. };
  910. static TranslateDdsFormat s_translateDdsFormat[] =
  911. {
  912. { DDS_DXT1, TextureFormat::BC1 },
  913. { DDS_DXT2, TextureFormat::BC2 },
  914. { DDS_DXT3, TextureFormat::BC2 },
  915. { DDS_DXT4, TextureFormat::BC3 },
  916. { DDS_DXT5, TextureFormat::BC3 },
  917. { DDS_ATI1, TextureFormat::BC4 },
  918. { DDS_BC4U, TextureFormat::BC4 },
  919. { DDS_ATI2, TextureFormat::BC5 },
  920. { DDS_BC5U, TextureFormat::BC5 },
  921. { D3DFMT_A16B16G16R16, TextureFormat::RGBA16 },
  922. { D3DFMT_A16B16G16R16F, TextureFormat::RGBA16F },
  923. { DDPF_RGB|DDPF_ALPHAPIXELS, TextureFormat::BGRA8 },
  924. { DDPF_INDEXED, TextureFormat::R8 },
  925. { DDPF_LUMINANCE, TextureFormat::R8 },
  926. { DDPF_ALPHA, TextureFormat::R8 },
  927. { D3DFMT_R16F, TextureFormat::R16F },
  928. { D3DFMT_R32F, TextureFormat::R32F },
  929. { D3DFMT_A8L8, TextureFormat::RG8 },
  930. { D3DFMT_G16R16, TextureFormat::RG16 },
  931. { D3DFMT_G16R16F, TextureFormat::RG16F },
  932. { D3DFMT_G32R32F, TextureFormat::RG32F },
  933. { D3DFMT_A8R8G8B8, TextureFormat::BGRA8 },
  934. { D3DFMT_A16B16G16R16, TextureFormat::RGBA16 },
  935. { D3DFMT_A16B16G16R16F, TextureFormat::RGBA16F },
  936. { D3DFMT_A32B32G32R32F, TextureFormat::RGBA32F },
  937. { D3DFMT_R5G6B5, TextureFormat::R5G6B5 },
  938. { D3DFMT_A4R4G4B4, TextureFormat::RGBA4 },
  939. { D3DFMT_A1R5G5B5, TextureFormat::RGB5A1 },
  940. { D3DFMT_A2B10G10R10, TextureFormat::RGB10A2 },
  941. };
  942. static TranslateDdsFormat s_translateDxgiFormat[] =
  943. {
  944. { DXGI_FORMAT_BC1_UNORM, TextureFormat::BC1 },
  945. { DXGI_FORMAT_BC2_UNORM, TextureFormat::BC2 },
  946. { DXGI_FORMAT_BC3_UNORM, TextureFormat::BC3 },
  947. { DXGI_FORMAT_BC4_UNORM, TextureFormat::BC4 },
  948. { DXGI_FORMAT_BC5_UNORM, TextureFormat::BC5 },
  949. { DXGI_FORMAT_BC6H_SF16, TextureFormat::BC6H },
  950. { DXGI_FORMAT_BC7_UNORM, TextureFormat::BC7 },
  951. { DXGI_FORMAT_R8_UNORM, TextureFormat::R8 },
  952. { DXGI_FORMAT_R16_UNORM, TextureFormat::R16 },
  953. { DXGI_FORMAT_R16_FLOAT, TextureFormat::R16F },
  954. { DXGI_FORMAT_R32_UINT, TextureFormat::R32 },
  955. { DXGI_FORMAT_R32_FLOAT, TextureFormat::R32F },
  956. { DXGI_FORMAT_R8G8_UNORM, TextureFormat::RG8 },
  957. { DXGI_FORMAT_R16G16_UNORM, TextureFormat::RG16 },
  958. { DXGI_FORMAT_R16G16_FLOAT, TextureFormat::RG16F },
  959. { DXGI_FORMAT_R32G32_UINT, TextureFormat::RG32 },
  960. { DXGI_FORMAT_R32G32_FLOAT, TextureFormat::RG32F },
  961. { DXGI_FORMAT_B8G8R8A8_UNORM, TextureFormat::BGRA8 },
  962. { DXGI_FORMAT_R16G16B16A16_UNORM, TextureFormat::RGBA16 },
  963. { DXGI_FORMAT_R16G16B16A16_FLOAT, TextureFormat::RGBA16F },
  964. { DXGI_FORMAT_R32G32B32A32_UINT, TextureFormat::RGBA32 },
  965. { DXGI_FORMAT_R32G32B32A32_FLOAT, TextureFormat::RGBA32F },
  966. { DXGI_FORMAT_B5G6R5_UNORM, TextureFormat::R5G6B5 },
  967. { DXGI_FORMAT_B4G4R4A4_UNORM, TextureFormat::RGBA4 },
  968. { DXGI_FORMAT_B5G5R5A1_UNORM, TextureFormat::RGB5A1 },
  969. { DXGI_FORMAT_R10G10B10A2_UNORM, TextureFormat::RGB10A2 },
  970. };
  971. bool imageParseDds(ImageContainer& _imageContainer, bx::ReaderSeekerI* _reader)
  972. {
  973. uint32_t headerSize;
  974. bx::read(_reader, headerSize);
  975. if (headerSize < DDS_HEADER_SIZE)
  976. {
  977. return false;
  978. }
  979. uint32_t flags;
  980. bx::read(_reader, flags);
  981. if ( (flags & (DDSD_CAPS|DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT) ) != (DDSD_CAPS|DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT) )
  982. {
  983. return false;
  984. }
  985. uint32_t height;
  986. bx::read(_reader, height);
  987. uint32_t width;
  988. bx::read(_reader, width);
  989. uint32_t pitch;
  990. bx::read(_reader, pitch);
  991. uint32_t depth;
  992. bx::read(_reader, depth);
  993. uint32_t mips;
  994. bx::read(_reader, mips);
  995. bx::skip(_reader, 44); // reserved
  996. uint32_t pixelFormatSize;
  997. bx::read(_reader, pixelFormatSize);
  998. uint32_t pixelFlags;
  999. bx::read(_reader, pixelFlags);
  1000. uint32_t fourcc;
  1001. bx::read(_reader, fourcc);
  1002. uint32_t rgbCount;
  1003. bx::read(_reader, rgbCount);
  1004. uint32_t rbitmask;
  1005. bx::read(_reader, rbitmask);
  1006. uint32_t gbitmask;
  1007. bx::read(_reader, gbitmask);
  1008. uint32_t bbitmask;
  1009. bx::read(_reader, bbitmask);
  1010. uint32_t abitmask;
  1011. bx::read(_reader, abitmask);
  1012. uint32_t caps[4];
  1013. bx::read(_reader, caps);
  1014. bx::skip(_reader, 4); // reserved
  1015. uint32_t dxgiFormat = 0;
  1016. if (DDPF_FOURCC == pixelFlags
  1017. && DDS_DX10 == fourcc)
  1018. {
  1019. bx::read(_reader, dxgiFormat);
  1020. uint32_t dims;
  1021. bx::read(_reader, dims);
  1022. uint32_t miscFlags;
  1023. bx::read(_reader, miscFlags);
  1024. uint32_t arraySize;
  1025. bx::read(_reader, arraySize);
  1026. uint32_t miscFlags2;
  1027. bx::read(_reader, miscFlags2);
  1028. }
  1029. if ( (caps[0] & DDSCAPS_TEXTURE) == 0)
  1030. {
  1031. return false;
  1032. }
  1033. bool cubeMap = 0 != (caps[1] & DDSCAPS2_CUBEMAP);
  1034. if (cubeMap)
  1035. {
  1036. if ( (caps[1] & DDS_CUBEMAP_ALLFACES) != DDS_CUBEMAP_ALLFACES)
  1037. {
  1038. // parital cube map is not supported.
  1039. return false;
  1040. }
  1041. }
  1042. TextureFormat::Enum format = TextureFormat::Unknown;
  1043. bool hasAlpha = pixelFlags & DDPF_ALPHAPIXELS;
  1044. if (dxgiFormat == 0)
  1045. {
  1046. uint32_t ddsFormat = pixelFlags & DDPF_FOURCC ? fourcc : pixelFlags;
  1047. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateDdsFormat); ++ii)
  1048. {
  1049. if (s_translateDdsFormat[ii].m_format == ddsFormat)
  1050. {
  1051. format = s_translateDdsFormat[ii].m_textureFormat;
  1052. break;
  1053. }
  1054. }
  1055. }
  1056. else
  1057. {
  1058. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateDxgiFormat); ++ii)
  1059. {
  1060. if (s_translateDxgiFormat[ii].m_format == dxgiFormat)
  1061. {
  1062. format = s_translateDxgiFormat[ii].m_textureFormat;
  1063. break;
  1064. }
  1065. }
  1066. }
  1067. _imageContainer.m_data = NULL;
  1068. _imageContainer.m_size = 0;
  1069. _imageContainer.m_offset = (uint32_t)bx::seek(_reader);
  1070. _imageContainer.m_width = width;
  1071. _imageContainer.m_height = height;
  1072. _imageContainer.m_depth = depth;
  1073. _imageContainer.m_format = format;
  1074. _imageContainer.m_numMips = (caps[0] & DDSCAPS_MIPMAP) ? mips : 1;
  1075. _imageContainer.m_hasAlpha = hasAlpha;
  1076. _imageContainer.m_cubeMap = cubeMap;
  1077. _imageContainer.m_ktx = false;
  1078. return TextureFormat::Unknown != format;
  1079. }
  1080. // KTX
  1081. #define KTX_MAGIC BX_MAKEFOURCC(0xAB, 'K', 'T', 'X')
  1082. #define KTX_HEADER_SIZE 64
  1083. #define KTX_ETC1_RGB8_OES 0x8D64
  1084. #define KTX_COMPRESSED_R11_EAC 0x9270
  1085. #define KTX_COMPRESSED_SIGNED_R11_EAC 0x9271
  1086. #define KTX_COMPRESSED_RG11_EAC 0x9272
  1087. #define KTX_COMPRESSED_SIGNED_RG11_EAC 0x9273
  1088. #define KTX_COMPRESSED_RGB8_ETC2 0x9274
  1089. #define KTX_COMPRESSED_SRGB8_ETC2 0x9275
  1090. #define KTX_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2 0x9276
  1091. #define KTX_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2 0x9277
  1092. #define KTX_COMPRESSED_RGBA8_ETC2_EAC 0x9278
  1093. #define KTX_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC 0x9279
  1094. #define KTX_COMPRESSED_RGB_PVRTC_4BPPV1_IMG 0x8C00
  1095. #define KTX_COMPRESSED_RGB_PVRTC_2BPPV1_IMG 0x8C01
  1096. #define KTX_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG 0x8C02
  1097. #define KTX_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG 0x8C03
  1098. #define KTX_COMPRESSED_RGBA_PVRTC_2BPPV2_IMG 0x9137
  1099. #define KTX_COMPRESSED_RGBA_PVRTC_4BPPV2_IMG 0x9138
  1100. #define KTX_COMPRESSED_RGBA_S3TC_DXT1_EXT 0x83F1
  1101. #define KTX_COMPRESSED_RGBA_S3TC_DXT3_EXT 0x83F2
  1102. #define KTX_COMPRESSED_RGBA_S3TC_DXT5_EXT 0x83F3
  1103. #define KTX_COMPRESSED_LUMINANCE_LATC1_EXT 0x8C70
  1104. #define KTX_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT 0x8C72
  1105. #define KTX_COMPRESSED_RGBA_BPTC_UNORM_ARB 0x8E8C
  1106. #define KTX_COMPRESSED_SRGB_ALPHA_BPTC_UNORM_ARB 0x8E8D
  1107. #define KTX_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB 0x8E8E
  1108. #define KTX_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT_ARB 0x8E8F
  1109. #define KTX_R8 0x8229
  1110. #define KTX_R16 0x822A
  1111. #define KTX_RG8 0x822B
  1112. #define KTX_RG16 0x822C
  1113. #define KTX_R16F 0x822D
  1114. #define KTX_R32F 0x822E
  1115. #define KTX_RG16F 0x822F
  1116. #define KTX_RG32F 0x8230
  1117. #define KTX_RGBA16 0x805B
  1118. #define KTX_RGBA16F 0x881A
  1119. #define KTX_R32UI 0x8236
  1120. #define KTX_RG32UI 0x823C
  1121. #define KTX_RGBA32UI 0x8D70
  1122. #define KTX_BGRA 0x80E1
  1123. #define KTX_RGBA32F 0x8814
  1124. #define KTX_RGB565 0x8D62
  1125. #define KTX_RGBA4 0x8056
  1126. #define KTX_RGB5_A1 0x8057
  1127. #define KTX_RGB10_A2 0x8059
  1128. static struct TranslateKtxFormat
  1129. {
  1130. uint32_t m_format;
  1131. TextureFormat::Enum m_textureFormat;
  1132. } s_translateKtxFormat[] =
  1133. {
  1134. { KTX_COMPRESSED_RGBA_S3TC_DXT1_EXT, TextureFormat::BC1 },
  1135. { KTX_COMPRESSED_RGBA_S3TC_DXT3_EXT, TextureFormat::BC2 },
  1136. { KTX_COMPRESSED_RGBA_S3TC_DXT5_EXT, TextureFormat::BC3 },
  1137. { KTX_COMPRESSED_LUMINANCE_LATC1_EXT, TextureFormat::BC4 },
  1138. { KTX_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT, TextureFormat::BC5 },
  1139. { KTX_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB, TextureFormat::BC6H },
  1140. { KTX_COMPRESSED_RGBA_BPTC_UNORM_ARB, TextureFormat::BC7 },
  1141. { KTX_ETC1_RGB8_OES, TextureFormat::ETC1 },
  1142. { KTX_COMPRESSED_RGB8_ETC2, TextureFormat::ETC2 },
  1143. { KTX_COMPRESSED_RGBA8_ETC2_EAC, TextureFormat::ETC2A },
  1144. { KTX_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2, TextureFormat::ETC2A1 },
  1145. { KTX_COMPRESSED_RGB_PVRTC_2BPPV1_IMG, TextureFormat::PTC12 },
  1146. { KTX_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG, TextureFormat::PTC12A },
  1147. { KTX_COMPRESSED_RGB_PVRTC_4BPPV1_IMG, TextureFormat::PTC14 },
  1148. { KTX_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG, TextureFormat::PTC14A },
  1149. { KTX_COMPRESSED_RGBA_PVRTC_2BPPV2_IMG, TextureFormat::PTC22 },
  1150. { KTX_COMPRESSED_RGBA_PVRTC_4BPPV2_IMG, TextureFormat::PTC24 },
  1151. { KTX_R8, TextureFormat::R8 },
  1152. { KTX_RGBA16, TextureFormat::RGBA16 },
  1153. { KTX_RGBA16F, TextureFormat::RGBA16F },
  1154. { KTX_R32UI, TextureFormat::R32 },
  1155. { KTX_R32F, TextureFormat::R32F },
  1156. { KTX_RG8, TextureFormat::RG8 },
  1157. { KTX_RG16, TextureFormat::RG16 },
  1158. { KTX_RG16F, TextureFormat::RG16F },
  1159. { KTX_RG32UI, TextureFormat::RG32 },
  1160. { KTX_RG32F, TextureFormat::RG32F },
  1161. { KTX_BGRA, TextureFormat::BGRA8 },
  1162. { KTX_RGBA16, TextureFormat::RGBA16 },
  1163. { KTX_RGBA16F, TextureFormat::RGBA16F },
  1164. { KTX_RGBA32UI, TextureFormat::RGBA32 },
  1165. { KTX_RGBA32F, TextureFormat::RGBA32F },
  1166. { KTX_RGB565, TextureFormat::R5G6B5 },
  1167. { KTX_RGBA4, TextureFormat::RGBA4 },
  1168. { KTX_RGB5_A1, TextureFormat::RGB5A1 },
  1169. { KTX_RGB10_A2, TextureFormat::RGB10A2 },
  1170. };
  1171. bool imageParseKtx(ImageContainer& _imageContainer, bx::ReaderSeekerI* _reader)
  1172. {
  1173. uint8_t identifier[8];
  1174. bx::read(_reader, identifier);
  1175. if (identifier[1] != '1'
  1176. && identifier[2] != '1')
  1177. {
  1178. return false;
  1179. }
  1180. uint32_t endianness;
  1181. bx::read(_reader, endianness);
  1182. bool fromLittleEndian = 0x04030201 == endianness;
  1183. uint32_t glType;
  1184. bx::readHE(_reader, glType, fromLittleEndian);
  1185. uint32_t glTypeSize;
  1186. bx::readHE(_reader, glTypeSize, fromLittleEndian);
  1187. uint32_t glFormat;
  1188. bx::readHE(_reader, glFormat, fromLittleEndian);
  1189. uint32_t glInternalFormat;
  1190. bx::readHE(_reader, glInternalFormat, fromLittleEndian);
  1191. uint32_t glBaseInternalFormat;
  1192. bx::readHE(_reader, glBaseInternalFormat, fromLittleEndian);
  1193. uint32_t width;
  1194. bx::readHE(_reader, width, fromLittleEndian);
  1195. uint32_t height;
  1196. bx::readHE(_reader, height, fromLittleEndian);
  1197. uint32_t depth;
  1198. bx::readHE(_reader, depth, fromLittleEndian);
  1199. uint32_t numberOfArrayElements;
  1200. bx::readHE(_reader, numberOfArrayElements, fromLittleEndian);
  1201. uint32_t numFaces;
  1202. bx::readHE(_reader, numFaces, fromLittleEndian);
  1203. uint32_t numMips;
  1204. bx::readHE(_reader, numMips, fromLittleEndian);
  1205. uint32_t metaDataSize;
  1206. bx::readHE(_reader, metaDataSize, fromLittleEndian);
  1207. // skip meta garbage...
  1208. int64_t offset = bx::skip(_reader, metaDataSize);
  1209. TextureFormat::Enum format = TextureFormat::Unknown;
  1210. bool hasAlpha = false;
  1211. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateKtxFormat); ++ii)
  1212. {
  1213. if (s_translateKtxFormat[ii].m_format == glInternalFormat)
  1214. {
  1215. format = s_translateKtxFormat[ii].m_textureFormat;
  1216. break;
  1217. }
  1218. }
  1219. _imageContainer.m_data = NULL;
  1220. _imageContainer.m_size = 0;
  1221. _imageContainer.m_offset = (uint32_t)offset;
  1222. _imageContainer.m_width = width;
  1223. _imageContainer.m_height = height;
  1224. _imageContainer.m_depth = depth;
  1225. _imageContainer.m_format = format;
  1226. _imageContainer.m_numMips = numMips;
  1227. _imageContainer.m_hasAlpha = hasAlpha;
  1228. _imageContainer.m_cubeMap = numFaces > 1;
  1229. _imageContainer.m_ktx = true;
  1230. return TextureFormat::Unknown != format;
  1231. }
  1232. // PVR3
  1233. #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) )
  1234. #define PVR3_MAGIC BX_MAKEFOURCC('P', 'V', 'R', 3)
  1235. #define PVR3_HEADER_SIZE 52
  1236. #define PVR3_PVRTC1_2BPP_RGB 0
  1237. #define PVR3_PVRTC1_2BPP_RGBA 1
  1238. #define PVR3_PVRTC1_4BPP_RGB 2
  1239. #define PVR3_PVRTC1_4BPP_RGBA 3
  1240. #define PVR3_PVRTC2_2BPP_RGBA 4
  1241. #define PVR3_PVRTC2_4BPP_RGBA 5
  1242. #define PVR3_ETC1 6
  1243. #define PVR3_DXT1 7
  1244. #define PVR3_DXT2 8
  1245. #define PVR3_DXT3 9
  1246. #define PVR3_DXT4 10
  1247. #define PVR3_DXT5 11
  1248. #define PVR3_BC4 12
  1249. #define PVR3_BC5 13
  1250. #define PVR3_R8 PVR3_MAKE8CC('r', 0, 0, 0, 8, 0, 0, 0)
  1251. #define PVR3_R16 PVR3_MAKE8CC('r', 0, 0, 0, 16, 0, 0, 0)
  1252. #define PVR3_R32 PVR3_MAKE8CC('r', 0, 0, 0, 32, 0, 0, 0)
  1253. #define PVR3_RG8 PVR3_MAKE8CC('r', 'g', 0, 0, 8, 8, 0, 0)
  1254. #define PVR3_RG16 PVR3_MAKE8CC('r', 'g', 0, 0, 16, 16, 0, 0)
  1255. #define PVR3_RG32 PVR3_MAKE8CC('r', 'g', 0, 0, 32, 32, 0, 0)
  1256. #define PVR3_BGRA8 PVR3_MAKE8CC('b', 'g', 'r', 'a', 8, 8, 8, 8)
  1257. #define PVR3_RGBA16 PVR3_MAKE8CC('r', 'g', 'b', 'a', 16, 16, 16, 16)
  1258. #define PVR3_RGBA32 PVR3_MAKE8CC('r', 'g', 'b', 'a', 32, 32, 32, 32)
  1259. #define PVR3_RGB565 PVR3_MAKE8CC('r', 'g', 'b', 0, 5, 6, 5, 0)
  1260. #define PVR3_RGBA4 PVR3_MAKE8CC('r', 'g', 'b', 'a', 4, 4, 4, 4)
  1261. #define PVR3_RGBA51 PVR3_MAKE8CC('r', 'g', 'b', 'a', 5, 5, 5, 1)
  1262. #define PVR3_RGB10A2 PVR3_MAKE8CC('r', 'g', 'b', 'a', 10, 10, 10, 2)
  1263. #define PVR3_CHANNEL_TYPE_ANY UINT32_MAX
  1264. #define PVR3_CHANNEL_TYPE_FLOAT UINT32_C(12)
  1265. static struct TranslatePvr3Format
  1266. {
  1267. uint64_t m_format;
  1268. uint32_t m_channelTypeMask;
  1269. TextureFormat::Enum m_textureFormat;
  1270. } s_translatePvr3Format[] =
  1271. {
  1272. { PVR3_PVRTC1_2BPP_RGB, PVR3_CHANNEL_TYPE_ANY, TextureFormat::PTC12 },
  1273. { PVR3_PVRTC1_2BPP_RGBA, PVR3_CHANNEL_TYPE_ANY, TextureFormat::PTC12A },
  1274. { PVR3_PVRTC1_4BPP_RGB, PVR3_CHANNEL_TYPE_ANY, TextureFormat::PTC14 },
  1275. { PVR3_PVRTC1_4BPP_RGBA, PVR3_CHANNEL_TYPE_ANY, TextureFormat::PTC14A },
  1276. { PVR3_PVRTC2_2BPP_RGBA, PVR3_CHANNEL_TYPE_ANY, TextureFormat::PTC22 },
  1277. { PVR3_PVRTC2_4BPP_RGBA, PVR3_CHANNEL_TYPE_ANY, TextureFormat::PTC24 },
  1278. { PVR3_ETC1, PVR3_CHANNEL_TYPE_ANY, TextureFormat::ETC1 },
  1279. { PVR3_DXT1, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC1 },
  1280. { PVR3_DXT2, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC2 },
  1281. { PVR3_DXT3, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC2 },
  1282. { PVR3_DXT4, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC3 },
  1283. { PVR3_DXT5, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC3 },
  1284. { PVR3_BC4, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC4 },
  1285. { PVR3_BC5, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC5 },
  1286. { PVR3_R8, PVR3_CHANNEL_TYPE_ANY, TextureFormat::R8 },
  1287. { PVR3_R16, PVR3_CHANNEL_TYPE_ANY, TextureFormat::R16 },
  1288. { PVR3_R16, PVR3_CHANNEL_TYPE_FLOAT, TextureFormat::R16F },
  1289. { PVR3_R32, PVR3_CHANNEL_TYPE_ANY, TextureFormat::R32 },
  1290. { PVR3_R32, PVR3_CHANNEL_TYPE_FLOAT, TextureFormat::R32F },
  1291. { PVR3_RG8, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RG8 },
  1292. { PVR3_RG16, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RG16 },
  1293. { PVR3_RG16, PVR3_CHANNEL_TYPE_FLOAT, TextureFormat::RG16F },
  1294. { PVR3_RG32, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RG16 },
  1295. { PVR3_RG32, PVR3_CHANNEL_TYPE_FLOAT, TextureFormat::RG32F },
  1296. { PVR3_BGRA8, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BGRA8 },
  1297. { PVR3_RGBA16, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RGBA16 },
  1298. { PVR3_RGBA16, PVR3_CHANNEL_TYPE_FLOAT, TextureFormat::RGBA16F },
  1299. { PVR3_RGBA32, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RGBA32 },
  1300. { PVR3_RGBA32, PVR3_CHANNEL_TYPE_FLOAT, TextureFormat::RGBA32F },
  1301. { PVR3_RGB565, PVR3_CHANNEL_TYPE_ANY, TextureFormat::R5G6B5 },
  1302. { PVR3_RGBA4, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RGBA4 },
  1303. { PVR3_RGBA51, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RGB5A1 },
  1304. { PVR3_RGB10A2, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RGB10A2 },
  1305. };
  1306. bool imageParsePvr3(ImageContainer& _imageContainer, bx::ReaderSeekerI* _reader)
  1307. {
  1308. uint32_t flags;
  1309. bx::read(_reader, flags);
  1310. uint64_t pixelFormat;
  1311. bx::read(_reader, pixelFormat);
  1312. uint32_t colorSpace;
  1313. bx::read(_reader, colorSpace); // 0 - linearRGB, 1 - sRGB
  1314. uint32_t channelType;
  1315. bx::read(_reader, channelType);
  1316. uint32_t height;
  1317. bx::read(_reader, height);
  1318. uint32_t width;
  1319. bx::read(_reader, width);
  1320. uint32_t depth;
  1321. bx::read(_reader, depth);
  1322. uint32_t numSurfaces;
  1323. bx::read(_reader, numSurfaces);
  1324. uint32_t numFaces;
  1325. bx::read(_reader, numFaces);
  1326. uint32_t numMips;
  1327. bx::read(_reader, numMips);
  1328. uint32_t metaDataSize;
  1329. bx::read(_reader, metaDataSize);
  1330. // skip meta garbage...
  1331. int64_t offset = bx::skip(_reader, metaDataSize);
  1332. TextureFormat::Enum format = TextureFormat::Unknown;
  1333. bool hasAlpha = false;
  1334. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translatePvr3Format); ++ii)
  1335. {
  1336. if (s_translatePvr3Format[ii].m_format == pixelFormat
  1337. && channelType == (s_translatePvr3Format[ii].m_channelTypeMask & channelType) )
  1338. {
  1339. format = s_translatePvr3Format[ii].m_textureFormat;
  1340. break;
  1341. }
  1342. }
  1343. _imageContainer.m_data = NULL;
  1344. _imageContainer.m_size = 0;
  1345. _imageContainer.m_offset = (uint32_t)offset;
  1346. _imageContainer.m_width = width;
  1347. _imageContainer.m_height = height;
  1348. _imageContainer.m_depth = depth;
  1349. _imageContainer.m_format = format;
  1350. _imageContainer.m_numMips = numMips;
  1351. _imageContainer.m_hasAlpha = hasAlpha;
  1352. _imageContainer.m_cubeMap = numFaces > 1;
  1353. _imageContainer.m_ktx = false;
  1354. return TextureFormat::Unknown != format;
  1355. }
  1356. bool imageParse(ImageContainer& _imageContainer, bx::ReaderSeekerI* _reader)
  1357. {
  1358. uint32_t magic;
  1359. bx::read(_reader, magic);
  1360. if (DDS_MAGIC == magic)
  1361. {
  1362. return imageParseDds(_imageContainer, _reader);
  1363. }
  1364. else if (KTX_MAGIC == magic)
  1365. {
  1366. return imageParseKtx(_imageContainer, _reader);
  1367. }
  1368. else if (PVR3_MAGIC == magic)
  1369. {
  1370. return imageParsePvr3(_imageContainer, _reader);
  1371. }
  1372. else if (BGFX_CHUNK_MAGIC_TEX == magic)
  1373. {
  1374. TextureCreate tc;
  1375. bx::read(_reader, tc);
  1376. _imageContainer.m_format = tc.m_format;
  1377. _imageContainer.m_offset = UINT32_MAX;
  1378. if (NULL == tc.m_mem)
  1379. {
  1380. _imageContainer.m_data = NULL;
  1381. _imageContainer.m_size = 0;
  1382. }
  1383. else
  1384. {
  1385. _imageContainer.m_data = tc.m_mem->data;
  1386. _imageContainer.m_size = tc.m_mem->size;
  1387. }
  1388. _imageContainer.m_width = tc.m_width;
  1389. _imageContainer.m_height = tc.m_height;
  1390. _imageContainer.m_depth = tc.m_depth;
  1391. _imageContainer.m_numMips = tc.m_numMips;
  1392. _imageContainer.m_hasAlpha = false;
  1393. _imageContainer.m_cubeMap = tc.m_cubeMap;
  1394. _imageContainer.m_ktx = false;
  1395. return true;
  1396. }
  1397. return false;
  1398. }
  1399. bool imageParse(ImageContainer& _imageContainer, const void* _data, uint32_t _size)
  1400. {
  1401. bx::MemoryReader reader(_data, _size);
  1402. return imageParse(_imageContainer, &reader);
  1403. }
  1404. void imageDecodeToBgra8(uint8_t* _dst, const uint8_t* _src, uint32_t _width, uint32_t _height, uint32_t _pitch, uint8_t _type)
  1405. {
  1406. const uint8_t* src = _src;
  1407. uint32_t width = _width/4;
  1408. uint32_t height = _height/4;
  1409. uint8_t temp[16*4];
  1410. switch (_type)
  1411. {
  1412. case TextureFormat::BC1:
  1413. for (uint32_t yy = 0; yy < height; ++yy)
  1414. {
  1415. for (uint32_t xx = 0; xx < width; ++xx)
  1416. {
  1417. decodeBlockDxt1(temp, src);
  1418. src += 8;
  1419. uint8_t* dst = &_dst[(yy*_pitch+xx*4)*4];
  1420. memcpy(&dst[0*_pitch], &temp[ 0], 16);
  1421. memcpy(&dst[1*_pitch], &temp[16], 16);
  1422. memcpy(&dst[2*_pitch], &temp[32], 16);
  1423. memcpy(&dst[3*_pitch], &temp[48], 16);
  1424. }
  1425. }
  1426. break;
  1427. case TextureFormat::BC2:
  1428. for (uint32_t yy = 0; yy < height; ++yy)
  1429. {
  1430. for (uint32_t xx = 0; xx < width; ++xx)
  1431. {
  1432. decodeBlockDxt23A(temp+3, src);
  1433. src += 8;
  1434. decodeBlockDxt(temp, src);
  1435. src += 8;
  1436. uint8_t* dst = &_dst[(yy*_pitch+xx*4)*4];
  1437. memcpy(&dst[0*_pitch], &temp[ 0], 16);
  1438. memcpy(&dst[1*_pitch], &temp[16], 16);
  1439. memcpy(&dst[2*_pitch], &temp[32], 16);
  1440. memcpy(&dst[3*_pitch], &temp[48], 16);
  1441. }
  1442. }
  1443. break;
  1444. case TextureFormat::BC3:
  1445. for (uint32_t yy = 0; yy < height; ++yy)
  1446. {
  1447. for (uint32_t xx = 0; xx < width; ++xx)
  1448. {
  1449. decodeBlockDxt45A(temp+3, src);
  1450. src += 8;
  1451. decodeBlockDxt(temp, src);
  1452. src += 8;
  1453. uint8_t* dst = &_dst[(yy*_pitch+xx*4)*4];
  1454. memcpy(&dst[0*_pitch], &temp[ 0], 16);
  1455. memcpy(&dst[1*_pitch], &temp[16], 16);
  1456. memcpy(&dst[2*_pitch], &temp[32], 16);
  1457. memcpy(&dst[3*_pitch], &temp[48], 16);
  1458. }
  1459. }
  1460. break;
  1461. case TextureFormat::BC4:
  1462. for (uint32_t yy = 0; yy < height; ++yy)
  1463. {
  1464. for (uint32_t xx = 0; xx < width; ++xx)
  1465. {
  1466. decodeBlockDxt45A(temp, src);
  1467. src += 8;
  1468. uint8_t* dst = &_dst[(yy*_pitch+xx*4)*4];
  1469. memcpy(&dst[0*_pitch], &temp[ 0], 16);
  1470. memcpy(&dst[1*_pitch], &temp[16], 16);
  1471. memcpy(&dst[2*_pitch], &temp[32], 16);
  1472. memcpy(&dst[3*_pitch], &temp[48], 16);
  1473. }
  1474. }
  1475. break;
  1476. case TextureFormat::BC5:
  1477. for (uint32_t yy = 0; yy < height; ++yy)
  1478. {
  1479. for (uint32_t xx = 0; xx < width; ++xx)
  1480. {
  1481. decodeBlockDxt45A(temp+1, src);
  1482. src += 8;
  1483. decodeBlockDxt45A(temp+2, src);
  1484. src += 8;
  1485. for (uint32_t ii = 0; ii < 16; ++ii)
  1486. {
  1487. float nx = temp[ii*4+2]*2.0f/255.0f - 1.0f;
  1488. float ny = temp[ii*4+1]*2.0f/255.0f - 1.0f;
  1489. float nz = sqrtf(1.0f - nx*nx - ny*ny);
  1490. temp[ii*4+0] = uint8_t( (nz + 1.0f)*255.0f/2.0f);
  1491. temp[ii*4+3] = 0;
  1492. }
  1493. uint8_t* dst = &_dst[(yy*_pitch+xx*4)*4];
  1494. memcpy(&dst[0*_pitch], &temp[ 0], 16);
  1495. memcpy(&dst[1*_pitch], &temp[16], 16);
  1496. memcpy(&dst[2*_pitch], &temp[32], 16);
  1497. memcpy(&dst[3*_pitch], &temp[48], 16);
  1498. }
  1499. }
  1500. break;
  1501. case TextureFormat::ETC1:
  1502. case TextureFormat::ETC2:
  1503. for (uint32_t yy = 0; yy < height; ++yy)
  1504. {
  1505. for (uint32_t xx = 0; xx < width; ++xx)
  1506. {
  1507. decodeBlockEtc12(temp, src);
  1508. src += 8;
  1509. uint8_t* dst = &_dst[(yy*_pitch+xx*4)*4];
  1510. memcpy(&dst[0*_pitch], &temp[ 0], 16);
  1511. memcpy(&dst[1*_pitch], &temp[16], 16);
  1512. memcpy(&dst[2*_pitch], &temp[32], 16);
  1513. memcpy(&dst[3*_pitch], &temp[48], 16);
  1514. }
  1515. }
  1516. break;
  1517. case TextureFormat::ETC2A:
  1518. BX_WARN(false, "ETC2A decoder is not implemented.");
  1519. imageCheckerboard(_width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00), _dst);
  1520. break;
  1521. case TextureFormat::ETC2A1:
  1522. BX_WARN(false, "ETC2A1 decoder is not implemented.");
  1523. imageCheckerboard(_width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xffff0000), _dst);
  1524. break;
  1525. case TextureFormat::PTC12:
  1526. BX_WARN(false, "PTC12 decoder is not implemented.");
  1527. imageCheckerboard(_width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xffff00ff), _dst);
  1528. break;
  1529. case TextureFormat::PTC12A:
  1530. BX_WARN(false, "PTC12A decoder is not implemented.");
  1531. imageCheckerboard(_width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xffffff00), _dst);
  1532. break;
  1533. case TextureFormat::PTC14:
  1534. BX_WARN(false, "PTC14 decoder is not implemented.");
  1535. imageCheckerboard(_width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ffff), _dst);
  1536. break;
  1537. case TextureFormat::PTC14A:
  1538. BX_WARN(false, "PTC14A decoder is not implemented.");
  1539. imageCheckerboard(_width, _height, 16, UINT32_C(0xffff0000), UINT32_C(0xff0000ff), _dst);
  1540. break;
  1541. case TextureFormat::PTC22:
  1542. BX_WARN(false, "PTC22 decoder is not implemented.");
  1543. imageCheckerboard(_width, _height, 16, UINT32_C(0xff00ff00), UINT32_C(0xff0000ff), _dst);
  1544. break;
  1545. case TextureFormat::PTC24:
  1546. BX_WARN(false, "PTC24 decoder is not implemented.");
  1547. imageCheckerboard(_width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xffffffff), _dst);
  1548. break;
  1549. default:
  1550. // Decompression not implemented... Make ugly red-yellow checkerboard texture.
  1551. imageCheckerboard(_width, _height, 16, UINT32_C(0xffff0000), UINT32_C(0xffffff00), _dst);
  1552. break;
  1553. }
  1554. }
  1555. bool imageGetRawData(const ImageContainer& _imageContainer, uint8_t _side, uint8_t _lod, const void* _data, uint32_t _size, ImageMip& _mip)
  1556. {
  1557. uint32_t offset = _imageContainer.m_offset;
  1558. TextureFormat::Enum type = TextureFormat::Enum(_imageContainer.m_format);
  1559. bool hasAlpha = _imageContainer.m_hasAlpha;
  1560. const ImageBlockInfo& blockInfo = s_imageBlockInfo[type];
  1561. const uint8_t bpp = blockInfo.bitsPerPixel;
  1562. const uint32_t blockSize = blockInfo.blockSize;
  1563. const uint32_t blockWidth = blockInfo.blockWidth;
  1564. const uint32_t blockHeight = blockInfo.blockHeight;
  1565. if (UINT32_MAX == _imageContainer.m_offset)
  1566. {
  1567. if (NULL == _imageContainer.m_data)
  1568. {
  1569. return false;
  1570. }
  1571. offset = 0;
  1572. _data = _imageContainer.m_data;
  1573. _size = _imageContainer.m_size;
  1574. }
  1575. for (uint8_t side = 0, numSides = _imageContainer.m_cubeMap ? 6 : 1; side < numSides; ++side)
  1576. {
  1577. uint32_t width = _imageContainer.m_width;
  1578. uint32_t height = _imageContainer.m_height;
  1579. uint32_t depth = _imageContainer.m_depth;
  1580. for (uint8_t lod = 0, num = _imageContainer.m_numMips; lod < num; ++lod)
  1581. {
  1582. // skip imageSize in KTX format.
  1583. offset += _imageContainer.m_ktx ? sizeof(uint32_t) : 0;
  1584. width = bx::uint32_max(blockWidth, ( (width +blockWidth -1)/blockWidth )*blockWidth);
  1585. height = bx::uint32_max(blockHeight, ( (height+blockHeight-1)/blockHeight)*blockHeight);
  1586. depth = bx::uint32_max(1, depth);
  1587. uint32_t size = width*height*depth*bpp/8;
  1588. if (side == _side
  1589. && lod == _lod)
  1590. {
  1591. _mip.m_width = width;
  1592. _mip.m_height = height;
  1593. _mip.m_blockSize = blockSize;
  1594. _mip.m_size = size;
  1595. _mip.m_data = (const uint8_t*)_data + offset;
  1596. _mip.m_bpp = bpp;
  1597. _mip.m_format = type;
  1598. _mip.m_hasAlpha = hasAlpha;
  1599. return true;
  1600. }
  1601. offset += size;
  1602. BX_CHECK(offset <= _size, "Reading past size of data buffer! (offset %d, size %d)", offset, _size);
  1603. BX_UNUSED(_size);
  1604. width >>= 1;
  1605. height >>= 1;
  1606. depth >>= 1;
  1607. }
  1608. }
  1609. return false;
  1610. }
  1611. } // namespace bgfx