image.cpp 72 KB

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