image.cpp 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166
  1. /*
  2. * Copyright 2011-2015 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, 1, 1 }, // BC1
  13. { 8, 4, 4, 16, 1, 1 }, // BC2
  14. { 8, 4, 4, 16, 1, 1 }, // BC3
  15. { 4, 4, 4, 8, 1, 1 }, // BC4
  16. { 8, 4, 4, 16, 1, 1 }, // BC5
  17. { 8, 4, 4, 16, 1, 1 }, // BC6H
  18. { 8, 4, 4, 16, 1, 1 }, // BC7
  19. { 4, 4, 4, 8, 1, 1 }, // ETC1
  20. { 4, 4, 4, 8, 1, 1 }, // ETC2
  21. { 8, 4, 4, 16, 1, 1 }, // ETC2A
  22. { 4, 4, 4, 8, 1, 1 }, // ETC2A1
  23. { 2, 8, 4, 8, 2, 2 }, // PTC12
  24. { 4, 4, 4, 8, 2, 2 }, // PTC14
  25. { 2, 8, 4, 8, 2, 2 }, // PTC12A
  26. { 4, 4, 4, 8, 2, 2 }, // PTC14A
  27. { 2, 8, 4, 8, 2, 2 }, // PTC22
  28. { 4, 4, 4, 8, 2, 2 }, // PTC24
  29. { 0, 0, 0, 0, 1, 1 }, // Unknown
  30. { 1, 8, 1, 1, 1, 1 }, // R1
  31. { 8, 1, 1, 1, 1, 1 }, // R8
  32. { 16, 1, 1, 2, 1, 1 }, // R16
  33. { 16, 1, 1, 2, 1, 1 }, // R16F
  34. { 32, 1, 1, 4, 1, 1 }, // R32
  35. { 32, 1, 1, 4, 1, 1 }, // R32F
  36. { 16, 1, 1, 2, 1, 1 }, // RG8
  37. { 32, 1, 1, 4, 1, 1 }, // RG16
  38. { 32, 1, 1, 4, 1, 1 }, // RG16F
  39. { 64, 1, 1, 8, 1, 1 }, // RG32
  40. { 64, 1, 1, 8, 1, 1 }, // RG32F
  41. { 32, 1, 1, 4, 1, 1 }, // BGRA8
  42. { 64, 1, 1, 8, 1, 1 }, // RGBA16
  43. { 64, 1, 1, 8, 1, 1 }, // RGBA16F
  44. { 128, 1, 1, 16, 1, 1 }, // RGBA32
  45. { 128, 1, 1, 16, 1, 1 }, // RGBA32F
  46. { 16, 1, 1, 2, 1, 1 }, // R5G6B5
  47. { 16, 1, 1, 2, 1, 1 }, // RGBA4
  48. { 16, 1, 1, 2, 1, 1 }, // RGB5A1
  49. { 32, 1, 1, 4, 1, 1 }, // RGB10A2
  50. { 32, 1, 1, 4, 1, 1 }, // R11G11B10F
  51. { 0, 0, 0, 0, 1, 1 }, // UnknownDepth
  52. { 16, 1, 1, 2, 1, 1 }, // D16
  53. { 24, 1, 1, 3, 1, 1 }, // D24
  54. { 32, 1, 1, 4, 1, 1 }, // D24S8
  55. { 32, 1, 1, 4, 1, 1 }, // D32
  56. { 16, 1, 1, 2, 1, 1 }, // D16F
  57. { 24, 1, 1, 3, 1, 1 }, // D24F
  58. { 32, 1, 1, 4, 1, 1 }, // D32F
  59. { 8, 1, 1, 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. static const uint8_t s_pvrtcFactors[16][4] =
  822. {
  823. { 4, 4, 4, 4 },
  824. { 2, 6, 2, 6 },
  825. { 8, 0, 8, 0 },
  826. { 6, 2, 6, 2 },
  827. { 2, 2, 6, 6 },
  828. { 1, 3, 3, 9 },
  829. { 4, 0, 12, 0 },
  830. { 3, 1, 9, 3 },
  831. { 8, 8, 0, 0 },
  832. { 4, 12, 0, 0 },
  833. { 16, 0, 0, 0 },
  834. { 12, 4, 0, 0 },
  835. { 6, 6, 2, 2 },
  836. { 3, 9, 1, 3 },
  837. { 12, 0, 4, 0 },
  838. { 9, 3, 3, 1 },
  839. };
  840. static const uint8_t s_pvrtcWeights[8][4] =
  841. {
  842. { 8, 0, 8, 0 },
  843. { 5, 3, 5, 3 },
  844. { 3, 5, 3, 5 },
  845. { 0, 8, 0, 8 },
  846. { 8, 0, 8, 0 },
  847. { 4, 4, 4, 4 },
  848. { 4, 4, 0, 0 },
  849. { 0, 8, 0, 8 },
  850. };
  851. uint32_t morton2d(uint16_t _x, uint16_t _y)
  852. {
  853. using namespace bx;
  854. const uint32_t tmpx = uint32_part1by1(_x);
  855. const uint32_t xbits = uint32_sll(tmpx, 1);
  856. const uint32_t ybits = uint32_part1by1(_y);
  857. const uint32_t result = uint32_or(xbits, ybits);
  858. return result;
  859. }
  860. uint32_t getColor(const uint8_t _src[8])
  861. {
  862. return 0
  863. | _src[7]<<24
  864. | _src[6]<<16
  865. | _src[5]<<8
  866. | _src[4]
  867. ;
  868. }
  869. void decodeBlockPtc14RgbAddA(uint32_t _block, uint32_t* _r, uint32_t* _g, uint32_t* _b, uint8_t _factor)
  870. {
  871. if (0 != (_block & (1<<15) ) )
  872. {
  873. *_r += bitRangeConvert( (_block >> 10) & 0x1f, 5, 8) * _factor;
  874. *_g += bitRangeConvert( (_block >> 5) & 0x1f, 5, 8) * _factor;
  875. *_b += bitRangeConvert( (_block >> 1) & 0x0f, 4, 8) * _factor;
  876. }
  877. else
  878. {
  879. *_r += bitRangeConvert( (_block >> 8) & 0xf, 4, 8) * _factor;
  880. *_g += bitRangeConvert( (_block >> 4) & 0xf, 4, 8) * _factor;
  881. *_b += bitRangeConvert( (_block >> 1) & 0x7, 3, 8) * _factor;
  882. }
  883. }
  884. void decodeBlockPtc14RgbAddB(uint32_t _block, uint32_t* _r, uint32_t* _g, uint32_t* _b, uint8_t _factor)
  885. {
  886. if (0 != (_block & (1<<31) ) )
  887. {
  888. *_r += bitRangeConvert( (_block >> 26) & 0x1f, 5, 8) * _factor;
  889. *_g += bitRangeConvert( (_block >> 21) & 0x1f, 5, 8) * _factor;
  890. *_b += bitRangeConvert( (_block >> 16) & 0x1f, 5, 8) * _factor;
  891. }
  892. else
  893. {
  894. *_r += bitRangeConvert( (_block >> 24) & 0xf, 4, 8) * _factor;
  895. *_g += bitRangeConvert( (_block >> 20) & 0xf, 4, 8) * _factor;
  896. *_b += bitRangeConvert( (_block >> 16) & 0xf, 4, 8) * _factor;
  897. }
  898. }
  899. void decodeBlockPtc14(uint8_t _dst[16*4], const uint8_t* _src, uint32_t _x, uint32_t _y, uint32_t _width, uint32_t _height)
  900. {
  901. // 0 1 2 3 4 5 6 7
  902. // 7654321076543210765432107654321076543210765432107654321076543210
  903. // mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmyrrrrrgggggbbbbbxrrrrrgggggbbbbp
  904. // ^ ^^ ^^ ^
  905. // +-- modulation data |+- B color |+- A color |
  906. // +-- B opaque +-- A opaque |
  907. // alpha punchthrough --+
  908. const uint8_t* bc = &_src[morton2d(_x, _y) * 8];
  909. uint32_t mod = 0
  910. | bc[3]<<24
  911. | bc[2]<<16
  912. | bc[1]<<8
  913. | bc[0]
  914. ;
  915. const bool punchthrough = !!(bc[7] & 1);
  916. const uint8_t* weightTable = s_pvrtcWeights[4 * punchthrough];
  917. const uint8_t* factorTable = s_pvrtcFactors[0];
  918. for (int yy = 0; yy < 4; ++yy)
  919. {
  920. const uint32_t yOffset = (yy < 2) ? -1 : 0;
  921. const uint32_t y0 = (_y + yOffset) % _height;
  922. const uint32_t y1 = (y0 + 1) % _height;
  923. for (int xx = 0; xx < 4; ++xx)
  924. {
  925. const uint32_t xOffset = (xx < 2) ? -1 : 0;
  926. const uint32_t x0 = (_x + xOffset) % _width;
  927. const uint32_t x1 = (x0 + 1) % _width;
  928. const uint32_t bc0 = getColor(&_src[morton2d(x0, y0) * 8]);
  929. const uint32_t bc1 = getColor(&_src[morton2d(x1, y0) * 8]);
  930. const uint32_t bc2 = getColor(&_src[morton2d(x0, y1) * 8]);
  931. const uint32_t bc3 = getColor(&_src[morton2d(x1, y1) * 8]);
  932. const uint8_t f0 = factorTable[0];
  933. const uint8_t f1 = factorTable[1];
  934. const uint8_t f2 = factorTable[2];
  935. const uint8_t f3 = factorTable[3];
  936. uint32_t ar = 0, ag = 0, ab = 0;
  937. decodeBlockPtc14RgbAddA(bc0, &ar, &ag, &ab, f0);
  938. decodeBlockPtc14RgbAddA(bc1, &ar, &ag, &ab, f1);
  939. decodeBlockPtc14RgbAddA(bc2, &ar, &ag, &ab, f2);
  940. decodeBlockPtc14RgbAddA(bc3, &ar, &ag, &ab, f3);
  941. uint32_t br = 0, bg = 0, bb = 0;
  942. decodeBlockPtc14RgbAddB(bc0, &br, &bg, &bb, f0);
  943. decodeBlockPtc14RgbAddB(bc1, &br, &bg, &bb, f1);
  944. decodeBlockPtc14RgbAddB(bc2, &br, &bg, &bb, f2);
  945. decodeBlockPtc14RgbAddB(bc3, &br, &bg, &bb, f3);
  946. const uint8_t* weight = &weightTable[(mod & 3)*4];
  947. const uint8_t wa = weight[0];
  948. const uint8_t wb = weight[1];
  949. _dst[(yy*4 + xx)*4+0] = (ab * wa + bb * wb) >> 7;
  950. _dst[(yy*4 + xx)*4+1] = (ag * wa + bg * wb) >> 7;
  951. _dst[(yy*4 + xx)*4+2] = (ar * wa + br * wb) >> 7;
  952. _dst[(yy*4 + xx)*4+3] = 255;
  953. mod >>= 2;
  954. factorTable += 4;
  955. }
  956. }
  957. }
  958. void decodeBlockPtc14ARgbaAddA(uint32_t _block, uint32_t* _r, uint32_t* _g, uint32_t* _b, uint32_t* _a, uint8_t _factor)
  959. {
  960. if (0 != (_block & (1<<15) ) )
  961. {
  962. *_r += bitRangeConvert( (_block >> 10) & 0x1f, 5, 8) * _factor;
  963. *_g += bitRangeConvert( (_block >> 5) & 0x1f, 5, 8) * _factor;
  964. *_b += bitRangeConvert( (_block >> 1) & 0x0f, 4, 8) * _factor;
  965. *_a += 255;
  966. }
  967. else
  968. {
  969. *_r += bitRangeConvert( (_block >> 8) & 0xf, 4, 8) * _factor;
  970. *_g += bitRangeConvert( (_block >> 4) & 0xf, 4, 8) * _factor;
  971. *_b += bitRangeConvert( (_block >> 1) & 0x7, 3, 8) * _factor;
  972. *_a += bitRangeConvert( (_block >> 12) & 0x7, 3, 8) * _factor;
  973. }
  974. }
  975. void decodeBlockPtc14ARgbaAddB(uint32_t _block, uint32_t* _r, uint32_t* _g, uint32_t* _b, uint32_t* _a, uint8_t _factor)
  976. {
  977. if (0 != (_block & (1<<31) ) )
  978. {
  979. *_r += bitRangeConvert( (_block >> 26) & 0x1f, 5, 8) * _factor;
  980. *_g += bitRangeConvert( (_block >> 21) & 0x1f, 5, 8) * _factor;
  981. *_b += bitRangeConvert( (_block >> 16) & 0x1f, 5, 8) * _factor;
  982. *_a += 255;
  983. }
  984. else
  985. {
  986. *_r += bitRangeConvert( (_block >> 24) & 0xf, 4, 8) * _factor;
  987. *_g += bitRangeConvert( (_block >> 20) & 0xf, 4, 8) * _factor;
  988. *_b += bitRangeConvert( (_block >> 16) & 0xf, 4, 8) * _factor;
  989. *_a += bitRangeConvert( (_block >> 28) & 0x7, 3, 8) * _factor;
  990. }
  991. }
  992. void decodeBlockPtc14A(uint8_t _dst[16*4], const uint8_t* _src, uint32_t _x, uint32_t _y, uint32_t _width, uint32_t _height)
  993. {
  994. // 0 1 2 3 4 5 6 7
  995. // 7654321076543210765432107654321076543210765432107654321076543210
  996. // mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmyrrrrrgggggbbbbbxrrrrrgggggbbbbp
  997. // ^ ^^ ^^ ^
  998. // +-- modulation data |+- B color |+- A color |
  999. // +-- B opaque +-- A opaque |
  1000. // alpha punchthrough --+
  1001. const uint8_t* bc = &_src[morton2d(_x, _y) * 8];
  1002. uint32_t mod = 0
  1003. | bc[3]<<24
  1004. | bc[2]<<16
  1005. | bc[1]<<8
  1006. | bc[0]
  1007. ;
  1008. const bool punchthrough = !!(bc[7] & 1);
  1009. const uint8_t* weightTable = s_pvrtcWeights[4 * punchthrough];
  1010. const uint8_t* factorTable = s_pvrtcFactors[0];
  1011. for (int yy = 0; yy < 4; ++yy)
  1012. {
  1013. const uint32_t yOffset = (yy < 2) ? -1 : 0;
  1014. const uint32_t y0 = (_y + yOffset) % _height;
  1015. const uint32_t y1 = (y0 + 1) % _height;
  1016. for (int xx = 0; xx < 4; ++xx)
  1017. {
  1018. const uint32_t xOffset = (xx < 2) ? -1 : 0;
  1019. const uint32_t x0 = (_x + xOffset) % _width;
  1020. const uint32_t x1 = (x0 + 1) % _width;
  1021. const uint32_t bc0 = getColor(&_src[morton2d(x0, y0) * 8]);
  1022. const uint32_t bc1 = getColor(&_src[morton2d(x1, y0) * 8]);
  1023. const uint32_t bc2 = getColor(&_src[morton2d(x0, y1) * 8]);
  1024. const uint32_t bc3 = getColor(&_src[morton2d(x1, y1) * 8]);
  1025. const uint8_t f0 = factorTable[0];
  1026. const uint8_t f1 = factorTable[1];
  1027. const uint8_t f2 = factorTable[2];
  1028. const uint8_t f3 = factorTable[3];
  1029. uint32_t ar = 0, ag = 0, ab = 0, aa = 0;
  1030. decodeBlockPtc14ARgbaAddA(bc0, &ar, &ag, &ab, &aa, f0);
  1031. decodeBlockPtc14ARgbaAddA(bc1, &ar, &ag, &ab, &aa, f1);
  1032. decodeBlockPtc14ARgbaAddA(bc2, &ar, &ag, &ab, &aa, f2);
  1033. decodeBlockPtc14ARgbaAddA(bc3, &ar, &ag, &ab, &aa, f3);
  1034. uint32_t br = 0, bg = 0, bb = 0, ba = 0;
  1035. decodeBlockPtc14ARgbaAddB(bc0, &br, &bg, &bb, &ba, f0);
  1036. decodeBlockPtc14ARgbaAddB(bc1, &br, &bg, &bb, &ba, f1);
  1037. decodeBlockPtc14ARgbaAddB(bc2, &br, &bg, &bb, &ba, f2);
  1038. decodeBlockPtc14ARgbaAddB(bc3, &br, &bg, &bb, &ba, f3);
  1039. const uint8_t* weight = &weightTable[(mod & 3)*4];
  1040. const uint8_t wa = weight[0];
  1041. const uint8_t wb = weight[1];
  1042. const uint8_t wc = weight[2];
  1043. const uint8_t wd = weight[3];
  1044. _dst[(yy*4 + xx)*4+0] = (ab * wa + bb * wb) >> 7;
  1045. _dst[(yy*4 + xx)*4+1] = (ag * wa + bg * wb) >> 7;
  1046. _dst[(yy*4 + xx)*4+2] = (ar * wa + br * wb) >> 7;
  1047. _dst[(yy*4 + xx)*4+3] = (aa * wc + ba * wd) >> 7;
  1048. mod >>= 2;
  1049. factorTable += 4;
  1050. }
  1051. }
  1052. }
  1053. // DDS
  1054. #define DDS_MAGIC BX_MAKEFOURCC('D', 'D', 'S', ' ')
  1055. #define DDS_HEADER_SIZE 124
  1056. #define DDS_DXT1 BX_MAKEFOURCC('D', 'X', 'T', '1')
  1057. #define DDS_DXT2 BX_MAKEFOURCC('D', 'X', 'T', '2')
  1058. #define DDS_DXT3 BX_MAKEFOURCC('D', 'X', 'T', '3')
  1059. #define DDS_DXT4 BX_MAKEFOURCC('D', 'X', 'T', '4')
  1060. #define DDS_DXT5 BX_MAKEFOURCC('D', 'X', 'T', '5')
  1061. #define DDS_ATI1 BX_MAKEFOURCC('A', 'T', 'I', '1')
  1062. #define DDS_BC4U BX_MAKEFOURCC('B', 'C', '4', 'U')
  1063. #define DDS_ATI2 BX_MAKEFOURCC('A', 'T', 'I', '2')
  1064. #define DDS_BC5U BX_MAKEFOURCC('B', 'C', '5', 'U')
  1065. #define DDS_DX10 BX_MAKEFOURCC('D', 'X', '1', '0')
  1066. #define D3DFMT_A8R8G8B8 21
  1067. #define D3DFMT_R5G6B5 23
  1068. #define D3DFMT_A1R5G5B5 25
  1069. #define D3DFMT_A4R4G4B4 26
  1070. #define D3DFMT_A2B10G10R10 31
  1071. #define D3DFMT_G16R16 34
  1072. #define D3DFMT_A2R10G10B10 35
  1073. #define D3DFMT_A16B16G16R16 36
  1074. #define D3DFMT_A8L8 51
  1075. #define D3DFMT_R16F 111
  1076. #define D3DFMT_G16R16F 112
  1077. #define D3DFMT_A16B16G16R16F 113
  1078. #define D3DFMT_R32F 114
  1079. #define D3DFMT_G32R32F 115
  1080. #define D3DFMT_A32B32G32R32F 116
  1081. #define DXGI_FORMAT_R32G32B32A32_FLOAT 2
  1082. #define DXGI_FORMAT_R32G32B32A32_UINT 3
  1083. #define DXGI_FORMAT_R16G16B16A16_FLOAT 10
  1084. #define DXGI_FORMAT_R16G16B16A16_UNORM 11
  1085. #define DXGI_FORMAT_R16G16B16A16_UINT 12
  1086. #define DXGI_FORMAT_R32G32_FLOAT 16
  1087. #define DXGI_FORMAT_R32G32_UINT 17
  1088. #define DXGI_FORMAT_R10G10B10A2_UNORM 24
  1089. #define DXGI_FORMAT_R16G16_FLOAT 34
  1090. #define DXGI_FORMAT_R16G16_UNORM 35
  1091. #define DXGI_FORMAT_R32_FLOAT 41
  1092. #define DXGI_FORMAT_R32_UINT 42
  1093. #define DXGI_FORMAT_R8G8_UNORM 49
  1094. #define DXGI_FORMAT_R16_FLOAT 54
  1095. #define DXGI_FORMAT_R16_UNORM 56
  1096. #define DXGI_FORMAT_R8_UNORM 61
  1097. #define DXGI_FORMAT_BC1_UNORM 71
  1098. #define DXGI_FORMAT_BC2_UNORM 74
  1099. #define DXGI_FORMAT_BC3_UNORM 77
  1100. #define DXGI_FORMAT_BC4_UNORM 80
  1101. #define DXGI_FORMAT_BC5_UNORM 83
  1102. #define DXGI_FORMAT_B5G6R5_UNORM 85
  1103. #define DXGI_FORMAT_B5G5R5A1_UNORM 86
  1104. #define DXGI_FORMAT_B8G8R8A8_UNORM 87
  1105. #define DXGI_FORMAT_BC6H_SF16 96
  1106. #define DXGI_FORMAT_BC7_UNORM 98
  1107. #define DXGI_FORMAT_B4G4R4A4_UNORM 115
  1108. #define DDSD_CAPS 0x00000001
  1109. #define DDSD_HEIGHT 0x00000002
  1110. #define DDSD_WIDTH 0x00000004
  1111. #define DDSD_PITCH 0x00000008
  1112. #define DDSD_PIXELFORMAT 0x00001000
  1113. #define DDSD_MIPMAPCOUNT 0x00020000
  1114. #define DDSD_LINEARSIZE 0x00080000
  1115. #define DDSD_DEPTH 0x00800000
  1116. #define DDPF_ALPHAPIXELS 0x00000001
  1117. #define DDPF_ALPHA 0x00000002
  1118. #define DDPF_FOURCC 0x00000004
  1119. #define DDPF_INDEXED 0x00000020
  1120. #define DDPF_RGB 0x00000040
  1121. #define DDPF_YUV 0x00000200
  1122. #define DDPF_LUMINANCE 0x00020000
  1123. #define DDSCAPS_COMPLEX 0x00000008
  1124. #define DDSCAPS_TEXTURE 0x00001000
  1125. #define DDSCAPS_MIPMAP 0x00400000
  1126. #define DDSCAPS2_CUBEMAP 0x00000200
  1127. #define DDSCAPS2_CUBEMAP_POSITIVEX 0x00000400
  1128. #define DDSCAPS2_CUBEMAP_NEGATIVEX 0x00000800
  1129. #define DDSCAPS2_CUBEMAP_POSITIVEY 0x00001000
  1130. #define DDSCAPS2_CUBEMAP_NEGATIVEY 0x00002000
  1131. #define DDSCAPS2_CUBEMAP_POSITIVEZ 0x00004000
  1132. #define DDSCAPS2_CUBEMAP_NEGATIVEZ 0x00008000
  1133. #define DDS_CUBEMAP_ALLFACES (DDSCAPS2_CUBEMAP_POSITIVEX|DDSCAPS2_CUBEMAP_NEGATIVEX \
  1134. |DDSCAPS2_CUBEMAP_POSITIVEY|DDSCAPS2_CUBEMAP_NEGATIVEY \
  1135. |DDSCAPS2_CUBEMAP_POSITIVEZ|DDSCAPS2_CUBEMAP_NEGATIVEZ)
  1136. #define DDSCAPS2_VOLUME 0x00200000
  1137. struct TranslateDdsFormat
  1138. {
  1139. uint32_t m_format;
  1140. TextureFormat::Enum m_textureFormat;
  1141. };
  1142. static TranslateDdsFormat s_translateDdsFormat[] =
  1143. {
  1144. { DDS_DXT1, TextureFormat::BC1 },
  1145. { DDS_DXT2, TextureFormat::BC2 },
  1146. { DDS_DXT3, TextureFormat::BC2 },
  1147. { DDS_DXT4, TextureFormat::BC3 },
  1148. { DDS_DXT5, TextureFormat::BC3 },
  1149. { DDS_ATI1, TextureFormat::BC4 },
  1150. { DDS_BC4U, TextureFormat::BC4 },
  1151. { DDS_ATI2, TextureFormat::BC5 },
  1152. { DDS_BC5U, TextureFormat::BC5 },
  1153. { D3DFMT_A16B16G16R16, TextureFormat::RGBA16 },
  1154. { D3DFMT_A16B16G16R16F, TextureFormat::RGBA16F },
  1155. { DDPF_RGB|DDPF_ALPHAPIXELS, TextureFormat::BGRA8 },
  1156. { DDPF_INDEXED, TextureFormat::R8 },
  1157. { DDPF_LUMINANCE, TextureFormat::R8 },
  1158. { DDPF_ALPHA, TextureFormat::R8 },
  1159. { D3DFMT_R16F, TextureFormat::R16F },
  1160. { D3DFMT_R32F, TextureFormat::R32F },
  1161. { D3DFMT_A8L8, TextureFormat::RG8 },
  1162. { D3DFMT_G16R16, TextureFormat::RG16 },
  1163. { D3DFMT_G16R16F, TextureFormat::RG16F },
  1164. { D3DFMT_G32R32F, TextureFormat::RG32F },
  1165. { D3DFMT_A8R8G8B8, TextureFormat::BGRA8 },
  1166. { D3DFMT_A16B16G16R16, TextureFormat::RGBA16 },
  1167. { D3DFMT_A16B16G16R16F, TextureFormat::RGBA16F },
  1168. { D3DFMT_A32B32G32R32F, TextureFormat::RGBA32F },
  1169. { D3DFMT_R5G6B5, TextureFormat::R5G6B5 },
  1170. { D3DFMT_A4R4G4B4, TextureFormat::RGBA4 },
  1171. { D3DFMT_A1R5G5B5, TextureFormat::RGB5A1 },
  1172. { D3DFMT_A2B10G10R10, TextureFormat::RGB10A2 },
  1173. };
  1174. static TranslateDdsFormat s_translateDxgiFormat[] =
  1175. {
  1176. { DXGI_FORMAT_BC1_UNORM, TextureFormat::BC1 },
  1177. { DXGI_FORMAT_BC2_UNORM, TextureFormat::BC2 },
  1178. { DXGI_FORMAT_BC3_UNORM, TextureFormat::BC3 },
  1179. { DXGI_FORMAT_BC4_UNORM, TextureFormat::BC4 },
  1180. { DXGI_FORMAT_BC5_UNORM, TextureFormat::BC5 },
  1181. { DXGI_FORMAT_BC6H_SF16, TextureFormat::BC6H },
  1182. { DXGI_FORMAT_BC7_UNORM, TextureFormat::BC7 },
  1183. { DXGI_FORMAT_R8_UNORM, TextureFormat::R8 },
  1184. { DXGI_FORMAT_R16_UNORM, TextureFormat::R16 },
  1185. { DXGI_FORMAT_R16_FLOAT, TextureFormat::R16F },
  1186. { DXGI_FORMAT_R32_UINT, TextureFormat::R32 },
  1187. { DXGI_FORMAT_R32_FLOAT, TextureFormat::R32F },
  1188. { DXGI_FORMAT_R8G8_UNORM, TextureFormat::RG8 },
  1189. { DXGI_FORMAT_R16G16_UNORM, TextureFormat::RG16 },
  1190. { DXGI_FORMAT_R16G16_FLOAT, TextureFormat::RG16F },
  1191. { DXGI_FORMAT_R32G32_UINT, TextureFormat::RG32 },
  1192. { DXGI_FORMAT_R32G32_FLOAT, TextureFormat::RG32F },
  1193. { DXGI_FORMAT_B8G8R8A8_UNORM, TextureFormat::BGRA8 },
  1194. { DXGI_FORMAT_R16G16B16A16_UNORM, TextureFormat::RGBA16 },
  1195. { DXGI_FORMAT_R16G16B16A16_FLOAT, TextureFormat::RGBA16F },
  1196. { DXGI_FORMAT_R32G32B32A32_UINT, TextureFormat::RGBA32 },
  1197. { DXGI_FORMAT_R32G32B32A32_FLOAT, TextureFormat::RGBA32F },
  1198. { DXGI_FORMAT_B5G6R5_UNORM, TextureFormat::R5G6B5 },
  1199. { DXGI_FORMAT_B4G4R4A4_UNORM, TextureFormat::RGBA4 },
  1200. { DXGI_FORMAT_B5G5R5A1_UNORM, TextureFormat::RGB5A1 },
  1201. { DXGI_FORMAT_R10G10B10A2_UNORM, TextureFormat::RGB10A2 },
  1202. };
  1203. bool imageParseDds(ImageContainer& _imageContainer, bx::ReaderSeekerI* _reader)
  1204. {
  1205. uint32_t headerSize;
  1206. bx::read(_reader, headerSize);
  1207. if (headerSize < DDS_HEADER_SIZE)
  1208. {
  1209. return false;
  1210. }
  1211. uint32_t flags;
  1212. bx::read(_reader, flags);
  1213. if ( (flags & (DDSD_CAPS|DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT) ) != (DDSD_CAPS|DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT) )
  1214. {
  1215. return false;
  1216. }
  1217. uint32_t height;
  1218. bx::read(_reader, height);
  1219. uint32_t width;
  1220. bx::read(_reader, width);
  1221. uint32_t pitch;
  1222. bx::read(_reader, pitch);
  1223. uint32_t depth;
  1224. bx::read(_reader, depth);
  1225. uint32_t mips;
  1226. bx::read(_reader, mips);
  1227. bx::skip(_reader, 44); // reserved
  1228. uint32_t pixelFormatSize;
  1229. bx::read(_reader, pixelFormatSize);
  1230. uint32_t pixelFlags;
  1231. bx::read(_reader, pixelFlags);
  1232. uint32_t fourcc;
  1233. bx::read(_reader, fourcc);
  1234. uint32_t rgbCount;
  1235. bx::read(_reader, rgbCount);
  1236. uint32_t rbitmask;
  1237. bx::read(_reader, rbitmask);
  1238. uint32_t gbitmask;
  1239. bx::read(_reader, gbitmask);
  1240. uint32_t bbitmask;
  1241. bx::read(_reader, bbitmask);
  1242. uint32_t abitmask;
  1243. bx::read(_reader, abitmask);
  1244. uint32_t caps[4];
  1245. bx::read(_reader, caps);
  1246. bx::skip(_reader, 4); // reserved
  1247. uint32_t dxgiFormat = 0;
  1248. if (DDPF_FOURCC == pixelFlags
  1249. && DDS_DX10 == fourcc)
  1250. {
  1251. bx::read(_reader, dxgiFormat);
  1252. uint32_t dims;
  1253. bx::read(_reader, dims);
  1254. uint32_t miscFlags;
  1255. bx::read(_reader, miscFlags);
  1256. uint32_t arraySize;
  1257. bx::read(_reader, arraySize);
  1258. uint32_t miscFlags2;
  1259. bx::read(_reader, miscFlags2);
  1260. }
  1261. if ( (caps[0] & DDSCAPS_TEXTURE) == 0)
  1262. {
  1263. return false;
  1264. }
  1265. bool cubeMap = 0 != (caps[1] & DDSCAPS2_CUBEMAP);
  1266. if (cubeMap)
  1267. {
  1268. if ( (caps[1] & DDS_CUBEMAP_ALLFACES) != DDS_CUBEMAP_ALLFACES)
  1269. {
  1270. // parital cube map is not supported.
  1271. return false;
  1272. }
  1273. }
  1274. TextureFormat::Enum format = TextureFormat::Unknown;
  1275. bool hasAlpha = pixelFlags & DDPF_ALPHAPIXELS;
  1276. if (dxgiFormat == 0)
  1277. {
  1278. uint32_t ddsFormat = pixelFlags & DDPF_FOURCC ? fourcc : pixelFlags;
  1279. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateDdsFormat); ++ii)
  1280. {
  1281. if (s_translateDdsFormat[ii].m_format == ddsFormat)
  1282. {
  1283. format = s_translateDdsFormat[ii].m_textureFormat;
  1284. break;
  1285. }
  1286. }
  1287. }
  1288. else
  1289. {
  1290. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateDxgiFormat); ++ii)
  1291. {
  1292. if (s_translateDxgiFormat[ii].m_format == dxgiFormat)
  1293. {
  1294. format = s_translateDxgiFormat[ii].m_textureFormat;
  1295. break;
  1296. }
  1297. }
  1298. }
  1299. _imageContainer.m_data = NULL;
  1300. _imageContainer.m_size = 0;
  1301. _imageContainer.m_offset = (uint32_t)bx::seek(_reader);
  1302. _imageContainer.m_width = width;
  1303. _imageContainer.m_height = height;
  1304. _imageContainer.m_depth = depth;
  1305. _imageContainer.m_format = format;
  1306. _imageContainer.m_numMips = (caps[0] & DDSCAPS_MIPMAP) ? mips : 1;
  1307. _imageContainer.m_hasAlpha = hasAlpha;
  1308. _imageContainer.m_cubeMap = cubeMap;
  1309. _imageContainer.m_ktx = false;
  1310. return TextureFormat::Unknown != format;
  1311. }
  1312. // KTX
  1313. #define KTX_MAGIC BX_MAKEFOURCC(0xAB, 'K', 'T', 'X')
  1314. #define KTX_HEADER_SIZE 64
  1315. #define KTX_ETC1_RGB8_OES 0x8D64
  1316. #define KTX_COMPRESSED_R11_EAC 0x9270
  1317. #define KTX_COMPRESSED_SIGNED_R11_EAC 0x9271
  1318. #define KTX_COMPRESSED_RG11_EAC 0x9272
  1319. #define KTX_COMPRESSED_SIGNED_RG11_EAC 0x9273
  1320. #define KTX_COMPRESSED_RGB8_ETC2 0x9274
  1321. #define KTX_COMPRESSED_SRGB8_ETC2 0x9275
  1322. #define KTX_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2 0x9276
  1323. #define KTX_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2 0x9277
  1324. #define KTX_COMPRESSED_RGBA8_ETC2_EAC 0x9278
  1325. #define KTX_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC 0x9279
  1326. #define KTX_COMPRESSED_RGB_PVRTC_4BPPV1_IMG 0x8C00
  1327. #define KTX_COMPRESSED_RGB_PVRTC_2BPPV1_IMG 0x8C01
  1328. #define KTX_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG 0x8C02
  1329. #define KTX_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG 0x8C03
  1330. #define KTX_COMPRESSED_RGBA_PVRTC_2BPPV2_IMG 0x9137
  1331. #define KTX_COMPRESSED_RGBA_PVRTC_4BPPV2_IMG 0x9138
  1332. #define KTX_COMPRESSED_RGBA_S3TC_DXT1_EXT 0x83F1
  1333. #define KTX_COMPRESSED_RGBA_S3TC_DXT3_EXT 0x83F2
  1334. #define KTX_COMPRESSED_RGBA_S3TC_DXT5_EXT 0x83F3
  1335. #define KTX_COMPRESSED_LUMINANCE_LATC1_EXT 0x8C70
  1336. #define KTX_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT 0x8C72
  1337. #define KTX_COMPRESSED_RGBA_BPTC_UNORM_ARB 0x8E8C
  1338. #define KTX_COMPRESSED_SRGB_ALPHA_BPTC_UNORM_ARB 0x8E8D
  1339. #define KTX_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB 0x8E8E
  1340. #define KTX_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT_ARB 0x8E8F
  1341. #define KTX_R8 0x8229
  1342. #define KTX_R16 0x822A
  1343. #define KTX_RG8 0x822B
  1344. #define KTX_RG16 0x822C
  1345. #define KTX_R16F 0x822D
  1346. #define KTX_R32F 0x822E
  1347. #define KTX_RG16F 0x822F
  1348. #define KTX_RG32F 0x8230
  1349. #define KTX_RGBA16 0x805B
  1350. #define KTX_RGBA16F 0x881A
  1351. #define KTX_R32UI 0x8236
  1352. #define KTX_RG32UI 0x823C
  1353. #define KTX_RGBA32UI 0x8D70
  1354. #define KTX_BGRA 0x80E1
  1355. #define KTX_RGBA32F 0x8814
  1356. #define KTX_RGB565 0x8D62
  1357. #define KTX_RGBA4 0x8056
  1358. #define KTX_RGB5_A1 0x8057
  1359. #define KTX_RGB10_A2 0x8059
  1360. static struct TranslateKtxFormat
  1361. {
  1362. uint32_t m_format;
  1363. TextureFormat::Enum m_textureFormat;
  1364. } s_translateKtxFormat[] =
  1365. {
  1366. { KTX_COMPRESSED_RGBA_S3TC_DXT1_EXT, TextureFormat::BC1 },
  1367. { KTX_COMPRESSED_RGBA_S3TC_DXT3_EXT, TextureFormat::BC2 },
  1368. { KTX_COMPRESSED_RGBA_S3TC_DXT5_EXT, TextureFormat::BC3 },
  1369. { KTX_COMPRESSED_LUMINANCE_LATC1_EXT, TextureFormat::BC4 },
  1370. { KTX_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT, TextureFormat::BC5 },
  1371. { KTX_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB, TextureFormat::BC6H },
  1372. { KTX_COMPRESSED_RGBA_BPTC_UNORM_ARB, TextureFormat::BC7 },
  1373. { KTX_ETC1_RGB8_OES, TextureFormat::ETC1 },
  1374. { KTX_COMPRESSED_RGB8_ETC2, TextureFormat::ETC2 },
  1375. { KTX_COMPRESSED_RGBA8_ETC2_EAC, TextureFormat::ETC2A },
  1376. { KTX_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2, TextureFormat::ETC2A1 },
  1377. { KTX_COMPRESSED_RGB_PVRTC_2BPPV1_IMG, TextureFormat::PTC12 },
  1378. { KTX_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG, TextureFormat::PTC12A },
  1379. { KTX_COMPRESSED_RGB_PVRTC_4BPPV1_IMG, TextureFormat::PTC14 },
  1380. { KTX_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG, TextureFormat::PTC14A },
  1381. { KTX_COMPRESSED_RGBA_PVRTC_2BPPV2_IMG, TextureFormat::PTC22 },
  1382. { KTX_COMPRESSED_RGBA_PVRTC_4BPPV2_IMG, TextureFormat::PTC24 },
  1383. { KTX_R8, TextureFormat::R8 },
  1384. { KTX_RGBA16, TextureFormat::RGBA16 },
  1385. { KTX_RGBA16F, TextureFormat::RGBA16F },
  1386. { KTX_R32UI, TextureFormat::R32 },
  1387. { KTX_R32F, TextureFormat::R32F },
  1388. { KTX_RG8, TextureFormat::RG8 },
  1389. { KTX_RG16, TextureFormat::RG16 },
  1390. { KTX_RG16F, TextureFormat::RG16F },
  1391. { KTX_RG32UI, TextureFormat::RG32 },
  1392. { KTX_RG32F, TextureFormat::RG32F },
  1393. { KTX_BGRA, TextureFormat::BGRA8 },
  1394. { KTX_RGBA16, TextureFormat::RGBA16 },
  1395. { KTX_RGBA16F, TextureFormat::RGBA16F },
  1396. { KTX_RGBA32UI, TextureFormat::RGBA32 },
  1397. { KTX_RGBA32F, TextureFormat::RGBA32F },
  1398. { KTX_RGB565, TextureFormat::R5G6B5 },
  1399. { KTX_RGBA4, TextureFormat::RGBA4 },
  1400. { KTX_RGB5_A1, TextureFormat::RGB5A1 },
  1401. { KTX_RGB10_A2, TextureFormat::RGB10A2 },
  1402. };
  1403. bool imageParseKtx(ImageContainer& _imageContainer, bx::ReaderSeekerI* _reader)
  1404. {
  1405. uint8_t identifier[8];
  1406. bx::read(_reader, identifier);
  1407. if (identifier[1] != '1'
  1408. && identifier[2] != '1')
  1409. {
  1410. return false;
  1411. }
  1412. uint32_t endianness;
  1413. bx::read(_reader, endianness);
  1414. bool fromLittleEndian = 0x04030201 == endianness;
  1415. uint32_t glType;
  1416. bx::readHE(_reader, glType, fromLittleEndian);
  1417. uint32_t glTypeSize;
  1418. bx::readHE(_reader, glTypeSize, fromLittleEndian);
  1419. uint32_t glFormat;
  1420. bx::readHE(_reader, glFormat, fromLittleEndian);
  1421. uint32_t glInternalFormat;
  1422. bx::readHE(_reader, glInternalFormat, fromLittleEndian);
  1423. uint32_t glBaseInternalFormat;
  1424. bx::readHE(_reader, glBaseInternalFormat, fromLittleEndian);
  1425. uint32_t width;
  1426. bx::readHE(_reader, width, fromLittleEndian);
  1427. uint32_t height;
  1428. bx::readHE(_reader, height, fromLittleEndian);
  1429. uint32_t depth;
  1430. bx::readHE(_reader, depth, fromLittleEndian);
  1431. uint32_t numberOfArrayElements;
  1432. bx::readHE(_reader, numberOfArrayElements, fromLittleEndian);
  1433. uint32_t numFaces;
  1434. bx::readHE(_reader, numFaces, fromLittleEndian);
  1435. uint32_t numMips;
  1436. bx::readHE(_reader, numMips, fromLittleEndian);
  1437. uint32_t metaDataSize;
  1438. bx::readHE(_reader, metaDataSize, fromLittleEndian);
  1439. // skip meta garbage...
  1440. int64_t offset = bx::skip(_reader, metaDataSize);
  1441. TextureFormat::Enum format = TextureFormat::Unknown;
  1442. bool hasAlpha = false;
  1443. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateKtxFormat); ++ii)
  1444. {
  1445. if (s_translateKtxFormat[ii].m_format == glInternalFormat)
  1446. {
  1447. format = s_translateKtxFormat[ii].m_textureFormat;
  1448. break;
  1449. }
  1450. }
  1451. _imageContainer.m_data = NULL;
  1452. _imageContainer.m_size = 0;
  1453. _imageContainer.m_offset = (uint32_t)offset;
  1454. _imageContainer.m_width = width;
  1455. _imageContainer.m_height = height;
  1456. _imageContainer.m_depth = depth;
  1457. _imageContainer.m_format = format;
  1458. _imageContainer.m_numMips = numMips;
  1459. _imageContainer.m_hasAlpha = hasAlpha;
  1460. _imageContainer.m_cubeMap = numFaces > 1;
  1461. _imageContainer.m_ktx = true;
  1462. return TextureFormat::Unknown != format;
  1463. }
  1464. // PVR3
  1465. #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) )
  1466. #define PVR3_MAGIC BX_MAKEFOURCC('P', 'V', 'R', 3)
  1467. #define PVR3_HEADER_SIZE 52
  1468. #define PVR3_PVRTC1_2BPP_RGB 0
  1469. #define PVR3_PVRTC1_2BPP_RGBA 1
  1470. #define PVR3_PVRTC1_4BPP_RGB 2
  1471. #define PVR3_PVRTC1_4BPP_RGBA 3
  1472. #define PVR3_PVRTC2_2BPP_RGBA 4
  1473. #define PVR3_PVRTC2_4BPP_RGBA 5
  1474. #define PVR3_ETC1 6
  1475. #define PVR3_DXT1 7
  1476. #define PVR3_DXT2 8
  1477. #define PVR3_DXT3 9
  1478. #define PVR3_DXT4 10
  1479. #define PVR3_DXT5 11
  1480. #define PVR3_BC4 12
  1481. #define PVR3_BC5 13
  1482. #define PVR3_R8 PVR3_MAKE8CC('r', 0, 0, 0, 8, 0, 0, 0)
  1483. #define PVR3_R16 PVR3_MAKE8CC('r', 0, 0, 0, 16, 0, 0, 0)
  1484. #define PVR3_R32 PVR3_MAKE8CC('r', 0, 0, 0, 32, 0, 0, 0)
  1485. #define PVR3_RG8 PVR3_MAKE8CC('r', 'g', 0, 0, 8, 8, 0, 0)
  1486. #define PVR3_RG16 PVR3_MAKE8CC('r', 'g', 0, 0, 16, 16, 0, 0)
  1487. #define PVR3_RG32 PVR3_MAKE8CC('r', 'g', 0, 0, 32, 32, 0, 0)
  1488. #define PVR3_BGRA8 PVR3_MAKE8CC('b', 'g', 'r', 'a', 8, 8, 8, 8)
  1489. #define PVR3_RGBA16 PVR3_MAKE8CC('r', 'g', 'b', 'a', 16, 16, 16, 16)
  1490. #define PVR3_RGBA32 PVR3_MAKE8CC('r', 'g', 'b', 'a', 32, 32, 32, 32)
  1491. #define PVR3_RGB565 PVR3_MAKE8CC('r', 'g', 'b', 0, 5, 6, 5, 0)
  1492. #define PVR3_RGBA4 PVR3_MAKE8CC('r', 'g', 'b', 'a', 4, 4, 4, 4)
  1493. #define PVR3_RGBA51 PVR3_MAKE8CC('r', 'g', 'b', 'a', 5, 5, 5, 1)
  1494. #define PVR3_RGB10A2 PVR3_MAKE8CC('r', 'g', 'b', 'a', 10, 10, 10, 2)
  1495. #define PVR3_CHANNEL_TYPE_ANY UINT32_MAX
  1496. #define PVR3_CHANNEL_TYPE_FLOAT UINT32_C(12)
  1497. static struct TranslatePvr3Format
  1498. {
  1499. uint64_t m_format;
  1500. uint32_t m_channelTypeMask;
  1501. TextureFormat::Enum m_textureFormat;
  1502. } s_translatePvr3Format[] =
  1503. {
  1504. { PVR3_PVRTC1_2BPP_RGB, PVR3_CHANNEL_TYPE_ANY, TextureFormat::PTC12 },
  1505. { PVR3_PVRTC1_2BPP_RGBA, PVR3_CHANNEL_TYPE_ANY, TextureFormat::PTC12A },
  1506. { PVR3_PVRTC1_4BPP_RGB, PVR3_CHANNEL_TYPE_ANY, TextureFormat::PTC14 },
  1507. { PVR3_PVRTC1_4BPP_RGBA, PVR3_CHANNEL_TYPE_ANY, TextureFormat::PTC14A },
  1508. { PVR3_PVRTC2_2BPP_RGBA, PVR3_CHANNEL_TYPE_ANY, TextureFormat::PTC22 },
  1509. { PVR3_PVRTC2_4BPP_RGBA, PVR3_CHANNEL_TYPE_ANY, TextureFormat::PTC24 },
  1510. { PVR3_ETC1, PVR3_CHANNEL_TYPE_ANY, TextureFormat::ETC1 },
  1511. { PVR3_DXT1, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC1 },
  1512. { PVR3_DXT2, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC2 },
  1513. { PVR3_DXT3, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC2 },
  1514. { PVR3_DXT4, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC3 },
  1515. { PVR3_DXT5, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC3 },
  1516. { PVR3_BC4, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC4 },
  1517. { PVR3_BC5, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC5 },
  1518. { PVR3_R8, PVR3_CHANNEL_TYPE_ANY, TextureFormat::R8 },
  1519. { PVR3_R16, PVR3_CHANNEL_TYPE_ANY, TextureFormat::R16 },
  1520. { PVR3_R16, PVR3_CHANNEL_TYPE_FLOAT, TextureFormat::R16F },
  1521. { PVR3_R32, PVR3_CHANNEL_TYPE_ANY, TextureFormat::R32 },
  1522. { PVR3_R32, PVR3_CHANNEL_TYPE_FLOAT, TextureFormat::R32F },
  1523. { PVR3_RG8, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RG8 },
  1524. { PVR3_RG16, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RG16 },
  1525. { PVR3_RG16, PVR3_CHANNEL_TYPE_FLOAT, TextureFormat::RG16F },
  1526. { PVR3_RG32, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RG16 },
  1527. { PVR3_RG32, PVR3_CHANNEL_TYPE_FLOAT, TextureFormat::RG32F },
  1528. { PVR3_BGRA8, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BGRA8 },
  1529. { PVR3_RGBA16, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RGBA16 },
  1530. { PVR3_RGBA16, PVR3_CHANNEL_TYPE_FLOAT, TextureFormat::RGBA16F },
  1531. { PVR3_RGBA32, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RGBA32 },
  1532. { PVR3_RGBA32, PVR3_CHANNEL_TYPE_FLOAT, TextureFormat::RGBA32F },
  1533. { PVR3_RGB565, PVR3_CHANNEL_TYPE_ANY, TextureFormat::R5G6B5 },
  1534. { PVR3_RGBA4, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RGBA4 },
  1535. { PVR3_RGBA51, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RGB5A1 },
  1536. { PVR3_RGB10A2, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RGB10A2 },
  1537. };
  1538. bool imageParsePvr3(ImageContainer& _imageContainer, bx::ReaderSeekerI* _reader)
  1539. {
  1540. uint32_t flags;
  1541. bx::read(_reader, flags);
  1542. uint64_t pixelFormat;
  1543. bx::read(_reader, pixelFormat);
  1544. uint32_t colorSpace;
  1545. bx::read(_reader, colorSpace); // 0 - linearRGB, 1 - sRGB
  1546. uint32_t channelType;
  1547. bx::read(_reader, channelType);
  1548. uint32_t height;
  1549. bx::read(_reader, height);
  1550. uint32_t width;
  1551. bx::read(_reader, width);
  1552. uint32_t depth;
  1553. bx::read(_reader, depth);
  1554. uint32_t numSurfaces;
  1555. bx::read(_reader, numSurfaces);
  1556. uint32_t numFaces;
  1557. bx::read(_reader, numFaces);
  1558. uint32_t numMips;
  1559. bx::read(_reader, numMips);
  1560. uint32_t metaDataSize;
  1561. bx::read(_reader, metaDataSize);
  1562. // skip meta garbage...
  1563. int64_t offset = bx::skip(_reader, metaDataSize);
  1564. TextureFormat::Enum format = TextureFormat::Unknown;
  1565. bool hasAlpha = false;
  1566. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translatePvr3Format); ++ii)
  1567. {
  1568. if (s_translatePvr3Format[ii].m_format == pixelFormat
  1569. && channelType == (s_translatePvr3Format[ii].m_channelTypeMask & channelType) )
  1570. {
  1571. format = s_translatePvr3Format[ii].m_textureFormat;
  1572. break;
  1573. }
  1574. }
  1575. _imageContainer.m_data = NULL;
  1576. _imageContainer.m_size = 0;
  1577. _imageContainer.m_offset = (uint32_t)offset;
  1578. _imageContainer.m_width = width;
  1579. _imageContainer.m_height = height;
  1580. _imageContainer.m_depth = depth;
  1581. _imageContainer.m_format = format;
  1582. _imageContainer.m_numMips = numMips;
  1583. _imageContainer.m_hasAlpha = hasAlpha;
  1584. _imageContainer.m_cubeMap = numFaces > 1;
  1585. _imageContainer.m_ktx = false;
  1586. return TextureFormat::Unknown != format;
  1587. }
  1588. bool imageParse(ImageContainer& _imageContainer, bx::ReaderSeekerI* _reader)
  1589. {
  1590. uint32_t magic;
  1591. bx::read(_reader, magic);
  1592. if (DDS_MAGIC == magic)
  1593. {
  1594. return imageParseDds(_imageContainer, _reader);
  1595. }
  1596. else if (KTX_MAGIC == magic)
  1597. {
  1598. return imageParseKtx(_imageContainer, _reader);
  1599. }
  1600. else if (PVR3_MAGIC == magic)
  1601. {
  1602. return imageParsePvr3(_imageContainer, _reader);
  1603. }
  1604. else if (BGFX_CHUNK_MAGIC_TEX == magic)
  1605. {
  1606. TextureCreate tc;
  1607. bx::read(_reader, tc);
  1608. _imageContainer.m_format = tc.m_format;
  1609. _imageContainer.m_offset = UINT32_MAX;
  1610. if (NULL == tc.m_mem)
  1611. {
  1612. _imageContainer.m_data = NULL;
  1613. _imageContainer.m_size = 0;
  1614. }
  1615. else
  1616. {
  1617. _imageContainer.m_data = tc.m_mem->data;
  1618. _imageContainer.m_size = tc.m_mem->size;
  1619. }
  1620. _imageContainer.m_width = tc.m_width;
  1621. _imageContainer.m_height = tc.m_height;
  1622. _imageContainer.m_depth = tc.m_depth;
  1623. _imageContainer.m_numMips = tc.m_numMips;
  1624. _imageContainer.m_hasAlpha = false;
  1625. _imageContainer.m_cubeMap = tc.m_cubeMap;
  1626. _imageContainer.m_ktx = false;
  1627. return true;
  1628. }
  1629. return false;
  1630. }
  1631. bool imageParse(ImageContainer& _imageContainer, const void* _data, uint32_t _size)
  1632. {
  1633. bx::MemoryReader reader(_data, _size);
  1634. return imageParse(_imageContainer, &reader);
  1635. }
  1636. void imageDecodeToBgra8(uint8_t* _dst, const uint8_t* _src, uint32_t _width, uint32_t _height, uint32_t _pitch, uint8_t _type)
  1637. {
  1638. const uint8_t* src = _src;
  1639. uint32_t width = _width/4;
  1640. uint32_t height = _height/4;
  1641. uint8_t temp[16*4];
  1642. switch (_type)
  1643. {
  1644. case TextureFormat::BC1:
  1645. for (uint32_t yy = 0; yy < height; ++yy)
  1646. {
  1647. for (uint32_t xx = 0; xx < width; ++xx)
  1648. {
  1649. decodeBlockDxt1(temp, src);
  1650. src += 8;
  1651. uint8_t* dst = &_dst[(yy*_pitch+xx*4)*4];
  1652. memcpy(&dst[0*_pitch], &temp[ 0], 16);
  1653. memcpy(&dst[1*_pitch], &temp[16], 16);
  1654. memcpy(&dst[2*_pitch], &temp[32], 16);
  1655. memcpy(&dst[3*_pitch], &temp[48], 16);
  1656. }
  1657. }
  1658. break;
  1659. case TextureFormat::BC2:
  1660. for (uint32_t yy = 0; yy < height; ++yy)
  1661. {
  1662. for (uint32_t xx = 0; xx < width; ++xx)
  1663. {
  1664. decodeBlockDxt23A(temp+3, src);
  1665. src += 8;
  1666. decodeBlockDxt(temp, src);
  1667. src += 8;
  1668. uint8_t* dst = &_dst[(yy*_pitch+xx*4)*4];
  1669. memcpy(&dst[0*_pitch], &temp[ 0], 16);
  1670. memcpy(&dst[1*_pitch], &temp[16], 16);
  1671. memcpy(&dst[2*_pitch], &temp[32], 16);
  1672. memcpy(&dst[3*_pitch], &temp[48], 16);
  1673. }
  1674. }
  1675. break;
  1676. case TextureFormat::BC3:
  1677. for (uint32_t yy = 0; yy < height; ++yy)
  1678. {
  1679. for (uint32_t xx = 0; xx < width; ++xx)
  1680. {
  1681. decodeBlockDxt45A(temp+3, src);
  1682. src += 8;
  1683. decodeBlockDxt(temp, src);
  1684. src += 8;
  1685. uint8_t* dst = &_dst[(yy*_pitch+xx*4)*4];
  1686. memcpy(&dst[0*_pitch], &temp[ 0], 16);
  1687. memcpy(&dst[1*_pitch], &temp[16], 16);
  1688. memcpy(&dst[2*_pitch], &temp[32], 16);
  1689. memcpy(&dst[3*_pitch], &temp[48], 16);
  1690. }
  1691. }
  1692. break;
  1693. case TextureFormat::BC4:
  1694. for (uint32_t yy = 0; yy < height; ++yy)
  1695. {
  1696. for (uint32_t xx = 0; xx < width; ++xx)
  1697. {
  1698. decodeBlockDxt45A(temp, src);
  1699. src += 8;
  1700. uint8_t* dst = &_dst[(yy*_pitch+xx*4)*4];
  1701. memcpy(&dst[0*_pitch], &temp[ 0], 16);
  1702. memcpy(&dst[1*_pitch], &temp[16], 16);
  1703. memcpy(&dst[2*_pitch], &temp[32], 16);
  1704. memcpy(&dst[3*_pitch], &temp[48], 16);
  1705. }
  1706. }
  1707. break;
  1708. case TextureFormat::BC5:
  1709. for (uint32_t yy = 0; yy < height; ++yy)
  1710. {
  1711. for (uint32_t xx = 0; xx < width; ++xx)
  1712. {
  1713. decodeBlockDxt45A(temp+1, src);
  1714. src += 8;
  1715. decodeBlockDxt45A(temp+2, src);
  1716. src += 8;
  1717. for (uint32_t ii = 0; ii < 16; ++ii)
  1718. {
  1719. float nx = temp[ii*4+2]*2.0f/255.0f - 1.0f;
  1720. float ny = temp[ii*4+1]*2.0f/255.0f - 1.0f;
  1721. float nz = sqrtf(1.0f - nx*nx - ny*ny);
  1722. temp[ii*4+0] = uint8_t( (nz + 1.0f)*255.0f/2.0f);
  1723. temp[ii*4+3] = 0;
  1724. }
  1725. uint8_t* dst = &_dst[(yy*_pitch+xx*4)*4];
  1726. memcpy(&dst[0*_pitch], &temp[ 0], 16);
  1727. memcpy(&dst[1*_pitch], &temp[16], 16);
  1728. memcpy(&dst[2*_pitch], &temp[32], 16);
  1729. memcpy(&dst[3*_pitch], &temp[48], 16);
  1730. }
  1731. }
  1732. break;
  1733. case TextureFormat::ETC1:
  1734. case TextureFormat::ETC2:
  1735. for (uint32_t yy = 0; yy < height; ++yy)
  1736. {
  1737. for (uint32_t xx = 0; xx < width; ++xx)
  1738. {
  1739. decodeBlockEtc12(temp, src);
  1740. src += 8;
  1741. uint8_t* dst = &_dst[(yy*_pitch+xx*4)*4];
  1742. memcpy(&dst[0*_pitch], &temp[ 0], 16);
  1743. memcpy(&dst[1*_pitch], &temp[16], 16);
  1744. memcpy(&dst[2*_pitch], &temp[32], 16);
  1745. memcpy(&dst[3*_pitch], &temp[48], 16);
  1746. }
  1747. }
  1748. break;
  1749. case TextureFormat::ETC2A:
  1750. BX_WARN(false, "ETC2A decoder is not implemented.");
  1751. imageCheckerboard(_width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00), _dst);
  1752. break;
  1753. case TextureFormat::ETC2A1:
  1754. BX_WARN(false, "ETC2A1 decoder is not implemented.");
  1755. imageCheckerboard(_width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xffff0000), _dst);
  1756. break;
  1757. case TextureFormat::PTC12:
  1758. BX_WARN(false, "PTC12 decoder is not implemented.");
  1759. imageCheckerboard(_width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xffff00ff), _dst);
  1760. break;
  1761. case TextureFormat::PTC12A:
  1762. BX_WARN(false, "PTC12A decoder is not implemented.");
  1763. imageCheckerboard(_width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xffffff00), _dst);
  1764. break;
  1765. case TextureFormat::PTC14:
  1766. for (uint32_t yy = 0; yy < height; ++yy)
  1767. {
  1768. for (uint32_t xx = 0; xx < width; ++xx)
  1769. {
  1770. decodeBlockPtc14(temp, src, xx, yy, width, height);
  1771. uint8_t* dst = &_dst[(yy*_pitch+xx*4)*4];
  1772. memcpy(&dst[0*_pitch], &temp[ 0], 16);
  1773. memcpy(&dst[1*_pitch], &temp[16], 16);
  1774. memcpy(&dst[2*_pitch], &temp[32], 16);
  1775. memcpy(&dst[3*_pitch], &temp[48], 16);
  1776. }
  1777. }
  1778. break;
  1779. case TextureFormat::PTC14A:
  1780. for (uint32_t yy = 0; yy < height; ++yy)
  1781. {
  1782. for (uint32_t xx = 0; xx < width; ++xx)
  1783. {
  1784. decodeBlockPtc14A(temp, src, xx, yy, width, height);
  1785. uint8_t* dst = &_dst[(yy*_pitch+xx*4)*4];
  1786. memcpy(&dst[0*_pitch], &temp[ 0], 16);
  1787. memcpy(&dst[1*_pitch], &temp[16], 16);
  1788. memcpy(&dst[2*_pitch], &temp[32], 16);
  1789. memcpy(&dst[3*_pitch], &temp[48], 16);
  1790. }
  1791. }
  1792. break;
  1793. case TextureFormat::PTC22:
  1794. BX_WARN(false, "PTC22 decoder is not implemented.");
  1795. imageCheckerboard(_width, _height, 16, UINT32_C(0xff00ff00), UINT32_C(0xff0000ff), _dst);
  1796. break;
  1797. case TextureFormat::PTC24:
  1798. BX_WARN(false, "PTC24 decoder is not implemented.");
  1799. imageCheckerboard(_width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xffffffff), _dst);
  1800. break;
  1801. default:
  1802. // Decompression not implemented... Make ugly red-yellow checkerboard texture.
  1803. imageCheckerboard(_width, _height, 16, UINT32_C(0xffff0000), UINT32_C(0xffffff00), _dst);
  1804. break;
  1805. }
  1806. }
  1807. bool imageGetRawData(const ImageContainer& _imageContainer, uint8_t _side, uint8_t _lod, const void* _data, uint32_t _size, ImageMip& _mip)
  1808. {
  1809. uint32_t offset = _imageContainer.m_offset;
  1810. TextureFormat::Enum type = TextureFormat::Enum(_imageContainer.m_format);
  1811. bool hasAlpha = _imageContainer.m_hasAlpha;
  1812. const ImageBlockInfo& blockInfo = s_imageBlockInfo[type];
  1813. const uint8_t bpp = blockInfo.bitsPerPixel;
  1814. const uint32_t blockSize = blockInfo.blockSize;
  1815. const uint32_t blockWidth = blockInfo.blockWidth;
  1816. const uint32_t blockHeight = blockInfo.blockHeight;
  1817. const uint32_t minBlockX = blockInfo.minBlockX;
  1818. const uint32_t minBlockY = blockInfo.minBlockY;
  1819. if (UINT32_MAX == _imageContainer.m_offset)
  1820. {
  1821. if (NULL == _imageContainer.m_data)
  1822. {
  1823. return false;
  1824. }
  1825. offset = 0;
  1826. _data = _imageContainer.m_data;
  1827. _size = _imageContainer.m_size;
  1828. }
  1829. for (uint8_t side = 0, numSides = _imageContainer.m_cubeMap ? 6 : 1; side < numSides; ++side)
  1830. {
  1831. uint32_t width = _imageContainer.m_width;
  1832. uint32_t height = _imageContainer.m_height;
  1833. uint32_t depth = _imageContainer.m_depth;
  1834. for (uint8_t lod = 0, num = _imageContainer.m_numMips; lod < num; ++lod)
  1835. {
  1836. // skip imageSize in KTX format.
  1837. offset += _imageContainer.m_ktx ? sizeof(uint32_t) : 0;
  1838. width = bx::uint32_max(blockWidth * minBlockX, ( (width + blockWidth - 1) / blockWidth )*blockWidth);
  1839. height = bx::uint32_max(blockHeight * minBlockY, ( (height + blockHeight - 1) / blockHeight)*blockHeight);
  1840. depth = bx::uint32_max(1, depth);
  1841. uint32_t size = width*height*depth*bpp/8;
  1842. if (side == _side
  1843. && lod == _lod)
  1844. {
  1845. _mip.m_width = width;
  1846. _mip.m_height = height;
  1847. _mip.m_blockSize = blockSize;
  1848. _mip.m_size = size;
  1849. _mip.m_data = (const uint8_t*)_data + offset;
  1850. _mip.m_bpp = bpp;
  1851. _mip.m_format = type;
  1852. _mip.m_hasAlpha = hasAlpha;
  1853. return true;
  1854. }
  1855. offset += size;
  1856. BX_CHECK(offset <= _size, "Reading past size of data buffer! (offset %d, size %d)", offset, _size);
  1857. BX_UNUSED(_size);
  1858. width >>= 1;
  1859. height >>= 1;
  1860. depth >>= 1;
  1861. }
  1862. }
  1863. return false;
  1864. }
  1865. } // namespace bgfx