image.cpp 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203
  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. uint32_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 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(uint16_t _x, uint16_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] = (ab * wa + bb * wb) >> 7;
  959. _dst[(yy*4 + xx)*4+1] = (ag * wa + bg * wb) >> 7;
  960. _dst[(yy*4 + xx)*4+2] = (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] = (ab * wa + bb * wb) >> 7;
  1054. _dst[(yy*4 + xx)*4+1] = (ag * wa + bg * wb) >> 7;
  1055. _dst[(yy*4 + xx)*4+2] = (ar * wa + br * wb) >> 7;
  1056. _dst[(yy*4 + xx)*4+3] = (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_R16G16_FLOAT 34
  1099. #define DDS_FORMAT_R16G16_UNORM 35
  1100. #define DDS_FORMAT_R32_FLOAT 41
  1101. #define DDS_FORMAT_R32_UINT 42
  1102. #define DDS_FORMAT_R8G8_UNORM 49
  1103. #define DDS_FORMAT_R16_FLOAT 54
  1104. #define DDS_FORMAT_R16_UNORM 56
  1105. #define DDS_FORMAT_R8_UNORM 61
  1106. #define DDS_FORMAT_BC1_UNORM 71
  1107. #define DDS_FORMAT_BC2_UNORM 74
  1108. #define DDS_FORMAT_BC3_UNORM 77
  1109. #define DDS_FORMAT_BC4_UNORM 80
  1110. #define DDS_FORMAT_BC5_UNORM 83
  1111. #define DDS_FORMAT_B5G6R5_UNORM 85
  1112. #define DDS_FORMAT_B5G5R5A1_UNORM 86
  1113. #define DDS_FORMAT_B8G8R8A8_UNORM 87
  1114. #define DDS_FORMAT_BC6H_SF16 96
  1115. #define DDS_FORMAT_BC7_UNORM 98
  1116. #define DDS_FORMAT_B4G4R4A4_UNORM 115
  1117. #define DDSD_CAPS 0x00000001
  1118. #define DDSD_HEIGHT 0x00000002
  1119. #define DDSD_WIDTH 0x00000004
  1120. #define DDSD_PITCH 0x00000008
  1121. #define DDSD_PIXELFORMAT 0x00001000
  1122. #define DDSD_MIPMAPCOUNT 0x00020000
  1123. #define DDSD_LINEARSIZE 0x00080000
  1124. #define DDSD_DEPTH 0x00800000
  1125. #define DDPF_ALPHAPIXELS 0x00000001
  1126. #define DDPF_ALPHA 0x00000002
  1127. #define DDPF_FOURCC 0x00000004
  1128. #define DDPF_INDEXED 0x00000020
  1129. #define DDPF_RGB 0x00000040
  1130. #define DDPF_YUV 0x00000200
  1131. #define DDPF_LUMINANCE 0x00020000
  1132. #define DDSCAPS_COMPLEX 0x00000008
  1133. #define DDSCAPS_TEXTURE 0x00001000
  1134. #define DDSCAPS_MIPMAP 0x00400000
  1135. #define DDSCAPS2_CUBEMAP 0x00000200
  1136. #define DDSCAPS2_CUBEMAP_POSITIVEX 0x00000400
  1137. #define DDSCAPS2_CUBEMAP_NEGATIVEX 0x00000800
  1138. #define DDSCAPS2_CUBEMAP_POSITIVEY 0x00001000
  1139. #define DDSCAPS2_CUBEMAP_NEGATIVEY 0x00002000
  1140. #define DDSCAPS2_CUBEMAP_POSITIVEZ 0x00004000
  1141. #define DDSCAPS2_CUBEMAP_NEGATIVEZ 0x00008000
  1142. #define DDS_CUBEMAP_ALLFACES (DDSCAPS2_CUBEMAP_POSITIVEX|DDSCAPS2_CUBEMAP_NEGATIVEX \
  1143. |DDSCAPS2_CUBEMAP_POSITIVEY|DDSCAPS2_CUBEMAP_NEGATIVEY \
  1144. |DDSCAPS2_CUBEMAP_POSITIVEZ|DDSCAPS2_CUBEMAP_NEGATIVEZ)
  1145. #define DDSCAPS2_VOLUME 0x00200000
  1146. struct TranslateDdsFormat
  1147. {
  1148. uint32_t m_format;
  1149. TextureFormat::Enum m_textureFormat;
  1150. };
  1151. static TranslateDdsFormat s_translateDdsFourccFormat[] =
  1152. {
  1153. { DDS_DXT1, TextureFormat::BC1 },
  1154. { DDS_DXT2, TextureFormat::BC2 },
  1155. { DDS_DXT3, TextureFormat::BC2 },
  1156. { DDS_DXT4, TextureFormat::BC3 },
  1157. { DDS_DXT5, TextureFormat::BC3 },
  1158. { DDS_ATI1, TextureFormat::BC4 },
  1159. { DDS_BC4U, TextureFormat::BC4 },
  1160. { DDS_ATI2, TextureFormat::BC5 },
  1161. { DDS_BC5U, TextureFormat::BC5 },
  1162. { DDS_A16B16G16R16, TextureFormat::RGBA16 },
  1163. { DDS_A16B16G16R16F, TextureFormat::RGBA16F },
  1164. { DDPF_RGB|DDPF_ALPHAPIXELS, TextureFormat::BGRA8 },
  1165. { DDPF_INDEXED, TextureFormat::R8 },
  1166. { DDPF_LUMINANCE, TextureFormat::R8 },
  1167. { DDPF_ALPHA, TextureFormat::R8 },
  1168. { DDS_R16F, TextureFormat::R16F },
  1169. { DDS_R32F, TextureFormat::R32F },
  1170. { DDS_A8L8, TextureFormat::RG8 },
  1171. { DDS_G16R16, TextureFormat::RG16 },
  1172. { DDS_G16R16F, TextureFormat::RG16F },
  1173. { DDS_G32R32F, TextureFormat::RG32F },
  1174. { DDS_A8R8G8B8, TextureFormat::BGRA8 },
  1175. { DDS_A16B16G16R16, TextureFormat::RGBA16 },
  1176. { DDS_A16B16G16R16F, TextureFormat::RGBA16F },
  1177. { DDS_A32B32G32R32F, TextureFormat::RGBA32F },
  1178. { DDS_R5G6B5, TextureFormat::R5G6B5 },
  1179. { DDS_A4R4G4B4, TextureFormat::RGBA4 },
  1180. { DDS_A1R5G5B5, TextureFormat::RGB5A1 },
  1181. { DDS_A2B10G10R10, TextureFormat::RGB10A2 },
  1182. };
  1183. static TranslateDdsFormat s_translateDxgiFormat[] =
  1184. {
  1185. { DDS_FORMAT_BC1_UNORM, TextureFormat::BC1 },
  1186. { DDS_FORMAT_BC2_UNORM, TextureFormat::BC2 },
  1187. { DDS_FORMAT_BC3_UNORM, TextureFormat::BC3 },
  1188. { DDS_FORMAT_BC4_UNORM, TextureFormat::BC4 },
  1189. { DDS_FORMAT_BC5_UNORM, TextureFormat::BC5 },
  1190. { DDS_FORMAT_BC6H_SF16, TextureFormat::BC6H },
  1191. { DDS_FORMAT_BC7_UNORM, TextureFormat::BC7 },
  1192. { DDS_FORMAT_R8_UNORM, TextureFormat::R8 },
  1193. { DDS_FORMAT_R16_UNORM, TextureFormat::R16 },
  1194. { DDS_FORMAT_R16_FLOAT, TextureFormat::R16F },
  1195. { DDS_FORMAT_R32_UINT, TextureFormat::R32 },
  1196. { DDS_FORMAT_R32_FLOAT, TextureFormat::R32F },
  1197. { DDS_FORMAT_R8G8_UNORM, TextureFormat::RG8 },
  1198. { DDS_FORMAT_R16G16_UNORM, TextureFormat::RG16 },
  1199. { DDS_FORMAT_R16G16_FLOAT, TextureFormat::RG16F },
  1200. { DDS_FORMAT_R32G32_UINT, TextureFormat::RG32 },
  1201. { DDS_FORMAT_R32G32_FLOAT, TextureFormat::RG32F },
  1202. { DDS_FORMAT_B8G8R8A8_UNORM, TextureFormat::BGRA8 },
  1203. { DDS_FORMAT_R16G16B16A16_UNORM, TextureFormat::RGBA16 },
  1204. { DDS_FORMAT_R16G16B16A16_FLOAT, TextureFormat::RGBA16F },
  1205. { DDS_FORMAT_R32G32B32A32_UINT, TextureFormat::RGBA32 },
  1206. { DDS_FORMAT_R32G32B32A32_FLOAT, TextureFormat::RGBA32F },
  1207. { DDS_FORMAT_B5G6R5_UNORM, TextureFormat::R5G6B5 },
  1208. { DDS_FORMAT_B4G4R4A4_UNORM, TextureFormat::RGBA4 },
  1209. { DDS_FORMAT_B5G5R5A1_UNORM, TextureFormat::RGB5A1 },
  1210. { DDS_FORMAT_R10G10B10A2_UNORM, TextureFormat::RGB10A2 },
  1211. };
  1212. struct TranslateDdsPixelFormat
  1213. {
  1214. uint32_t m_bitCount;
  1215. uint32_t m_bitmask[4];
  1216. TextureFormat::Enum m_textureFormat;
  1217. };
  1218. static TranslateDdsPixelFormat s_translateDdsPixelFormat[] =
  1219. {
  1220. { 8, { 0x000000ff, 0x00000000, 0x00000000, 0x00000000 }, TextureFormat::R8 },
  1221. { 16, { 0x0000ffff, 0x00000000, 0x00000000, 0x00000000 }, TextureFormat::R16 },
  1222. { 16, { 0x00000f00, 0x000000f0, 0x0000000f, 0x0000f000 }, TextureFormat::RGBA4 },
  1223. { 16, { 0x0000f800, 0x000007e0, 0x0000001f, 0x00000000 }, TextureFormat::R5G6B5 },
  1224. { 16, { 0x00007c00, 0x000003e0, 0x0000001f, 0x00008000 }, TextureFormat::RGB5A1 },
  1225. { 32, { 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000 }, TextureFormat::BGRA8 },
  1226. { 32, { 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000 }, TextureFormat::BGRA8 },
  1227. { 32, { 0x000003ff, 0x000ffc00, 0x3ff00000, 0xc0000000 }, TextureFormat::RGB10A2 },
  1228. { 32, { 0x0000ffff, 0xffff0000, 0x00000000, 0x00000000 }, TextureFormat::RG16 },
  1229. { 32, { 0xffffffff, 0x00000000, 0x00000000, 0x00000000 }, TextureFormat::R32 },
  1230. };
  1231. bool imageParseDds(ImageContainer& _imageContainer, bx::ReaderSeekerI* _reader)
  1232. {
  1233. uint32_t headerSize;
  1234. bx::read(_reader, headerSize);
  1235. if (headerSize < DDS_HEADER_SIZE)
  1236. {
  1237. return false;
  1238. }
  1239. uint32_t flags;
  1240. bx::read(_reader, flags);
  1241. if ( (flags & (DDSD_CAPS|DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT) ) != (DDSD_CAPS|DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT) )
  1242. {
  1243. return false;
  1244. }
  1245. uint32_t height;
  1246. bx::read(_reader, height);
  1247. uint32_t width;
  1248. bx::read(_reader, width);
  1249. uint32_t pitch;
  1250. bx::read(_reader, pitch);
  1251. uint32_t depth;
  1252. bx::read(_reader, depth);
  1253. uint32_t mips;
  1254. bx::read(_reader, mips);
  1255. bx::skip(_reader, 44); // reserved
  1256. uint32_t pixelFormatSize;
  1257. bx::read(_reader, pixelFormatSize);
  1258. uint32_t pixelFlags;
  1259. bx::read(_reader, pixelFlags);
  1260. uint32_t fourcc;
  1261. bx::read(_reader, fourcc);
  1262. uint32_t bitCount;
  1263. bx::read(_reader, bitCount);
  1264. uint32_t bitmask[4];
  1265. bx::read(_reader, bitmask, sizeof(bitmask) );
  1266. uint32_t caps[4];
  1267. bx::read(_reader, caps);
  1268. bx::skip(_reader, 4); // reserved
  1269. uint32_t dxgiFormat = 0;
  1270. if (DDPF_FOURCC == pixelFlags
  1271. && DDS_DX10 == fourcc)
  1272. {
  1273. bx::read(_reader, dxgiFormat);
  1274. uint32_t dims;
  1275. bx::read(_reader, dims);
  1276. uint32_t miscFlags;
  1277. bx::read(_reader, miscFlags);
  1278. uint32_t arraySize;
  1279. bx::read(_reader, arraySize);
  1280. uint32_t miscFlags2;
  1281. bx::read(_reader, miscFlags2);
  1282. }
  1283. if ( (caps[0] & DDSCAPS_TEXTURE) == 0)
  1284. {
  1285. return false;
  1286. }
  1287. bool cubeMap = 0 != (caps[1] & DDSCAPS2_CUBEMAP);
  1288. if (cubeMap)
  1289. {
  1290. if ( (caps[1] & DDS_CUBEMAP_ALLFACES) != DDS_CUBEMAP_ALLFACES)
  1291. {
  1292. // parital cube map is not supported.
  1293. return false;
  1294. }
  1295. }
  1296. TextureFormat::Enum format = TextureFormat::Unknown;
  1297. bool hasAlpha = pixelFlags & DDPF_ALPHAPIXELS;
  1298. if (dxgiFormat == 0)
  1299. {
  1300. if (DDPF_FOURCC == (pixelFlags & DDPF_FOURCC) )
  1301. {
  1302. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateDdsFourccFormat); ++ii)
  1303. {
  1304. if (s_translateDdsFourccFormat[ii].m_format == fourcc)
  1305. {
  1306. format = s_translateDdsFourccFormat[ii].m_textureFormat;
  1307. break;
  1308. }
  1309. }
  1310. }
  1311. else
  1312. {
  1313. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateDdsPixelFormat); ++ii)
  1314. {
  1315. const TranslateDdsPixelFormat& pf = s_translateDdsPixelFormat[ii];
  1316. if (pf.m_bitCount == bitCount
  1317. && pf.m_bitmask[0] == bitmask[0]
  1318. && pf.m_bitmask[1] == bitmask[1]
  1319. && pf.m_bitmask[2] == bitmask[2]
  1320. && pf.m_bitmask[3] == bitmask[3])
  1321. {
  1322. format = pf.m_textureFormat;
  1323. break;
  1324. }
  1325. }
  1326. }
  1327. }
  1328. else
  1329. {
  1330. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateDxgiFormat); ++ii)
  1331. {
  1332. if (s_translateDxgiFormat[ii].m_format == dxgiFormat)
  1333. {
  1334. format = s_translateDxgiFormat[ii].m_textureFormat;
  1335. break;
  1336. }
  1337. }
  1338. }
  1339. _imageContainer.m_data = NULL;
  1340. _imageContainer.m_size = 0;
  1341. _imageContainer.m_offset = (uint32_t)bx::seek(_reader);
  1342. _imageContainer.m_width = width;
  1343. _imageContainer.m_height = height;
  1344. _imageContainer.m_depth = depth;
  1345. _imageContainer.m_format = format;
  1346. _imageContainer.m_numMips = (caps[0] & DDSCAPS_MIPMAP) ? mips : 1;
  1347. _imageContainer.m_hasAlpha = hasAlpha;
  1348. _imageContainer.m_cubeMap = cubeMap;
  1349. _imageContainer.m_ktx = false;
  1350. return TextureFormat::Unknown != format;
  1351. }
  1352. // KTX
  1353. #define KTX_MAGIC BX_MAKEFOURCC(0xAB, 'K', 'T', 'X')
  1354. #define KTX_HEADER_SIZE 64
  1355. #define KTX_ETC1_RGB8_OES 0x8D64
  1356. #define KTX_COMPRESSED_R11_EAC 0x9270
  1357. #define KTX_COMPRESSED_SIGNED_R11_EAC 0x9271
  1358. #define KTX_COMPRESSED_RG11_EAC 0x9272
  1359. #define KTX_COMPRESSED_SIGNED_RG11_EAC 0x9273
  1360. #define KTX_COMPRESSED_RGB8_ETC2 0x9274
  1361. #define KTX_COMPRESSED_SRGB8_ETC2 0x9275
  1362. #define KTX_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2 0x9276
  1363. #define KTX_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2 0x9277
  1364. #define KTX_COMPRESSED_RGBA8_ETC2_EAC 0x9278
  1365. #define KTX_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC 0x9279
  1366. #define KTX_COMPRESSED_RGB_PVRTC_4BPPV1_IMG 0x8C00
  1367. #define KTX_COMPRESSED_RGB_PVRTC_2BPPV1_IMG 0x8C01
  1368. #define KTX_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG 0x8C02
  1369. #define KTX_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG 0x8C03
  1370. #define KTX_COMPRESSED_RGBA_PVRTC_2BPPV2_IMG 0x9137
  1371. #define KTX_COMPRESSED_RGBA_PVRTC_4BPPV2_IMG 0x9138
  1372. #define KTX_COMPRESSED_RGBA_S3TC_DXT1_EXT 0x83F1
  1373. #define KTX_COMPRESSED_RGBA_S3TC_DXT3_EXT 0x83F2
  1374. #define KTX_COMPRESSED_RGBA_S3TC_DXT5_EXT 0x83F3
  1375. #define KTX_COMPRESSED_LUMINANCE_LATC1_EXT 0x8C70
  1376. #define KTX_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT 0x8C72
  1377. #define KTX_COMPRESSED_RGBA_BPTC_UNORM_ARB 0x8E8C
  1378. #define KTX_COMPRESSED_SRGB_ALPHA_BPTC_UNORM_ARB 0x8E8D
  1379. #define KTX_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB 0x8E8E
  1380. #define KTX_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT_ARB 0x8E8F
  1381. #define KTX_R8 0x8229
  1382. #define KTX_R16 0x822A
  1383. #define KTX_RG8 0x822B
  1384. #define KTX_RG16 0x822C
  1385. #define KTX_R16F 0x822D
  1386. #define KTX_R32F 0x822E
  1387. #define KTX_RG16F 0x822F
  1388. #define KTX_RG32F 0x8230
  1389. #define KTX_RGBA16 0x805B
  1390. #define KTX_RGBA16F 0x881A
  1391. #define KTX_R32UI 0x8236
  1392. #define KTX_RG32UI 0x823C
  1393. #define KTX_RGBA32UI 0x8D70
  1394. #define KTX_BGRA 0x80E1
  1395. #define KTX_RGBA32F 0x8814
  1396. #define KTX_RGB565 0x8D62
  1397. #define KTX_RGBA4 0x8056
  1398. #define KTX_RGB5_A1 0x8057
  1399. #define KTX_RGB10_A2 0x8059
  1400. static struct TranslateKtxFormat
  1401. {
  1402. uint32_t m_format;
  1403. TextureFormat::Enum m_textureFormat;
  1404. } s_translateKtxFormat[] =
  1405. {
  1406. { KTX_COMPRESSED_RGBA_S3TC_DXT1_EXT, TextureFormat::BC1 },
  1407. { KTX_COMPRESSED_RGBA_S3TC_DXT3_EXT, TextureFormat::BC2 },
  1408. { KTX_COMPRESSED_RGBA_S3TC_DXT5_EXT, TextureFormat::BC3 },
  1409. { KTX_COMPRESSED_LUMINANCE_LATC1_EXT, TextureFormat::BC4 },
  1410. { KTX_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT, TextureFormat::BC5 },
  1411. { KTX_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB, TextureFormat::BC6H },
  1412. { KTX_COMPRESSED_RGBA_BPTC_UNORM_ARB, TextureFormat::BC7 },
  1413. { KTX_ETC1_RGB8_OES, TextureFormat::ETC1 },
  1414. { KTX_COMPRESSED_RGB8_ETC2, TextureFormat::ETC2 },
  1415. { KTX_COMPRESSED_RGBA8_ETC2_EAC, TextureFormat::ETC2A },
  1416. { KTX_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2, TextureFormat::ETC2A1 },
  1417. { KTX_COMPRESSED_RGB_PVRTC_2BPPV1_IMG, TextureFormat::PTC12 },
  1418. { KTX_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG, TextureFormat::PTC12A },
  1419. { KTX_COMPRESSED_RGB_PVRTC_4BPPV1_IMG, TextureFormat::PTC14 },
  1420. { KTX_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG, TextureFormat::PTC14A },
  1421. { KTX_COMPRESSED_RGBA_PVRTC_2BPPV2_IMG, TextureFormat::PTC22 },
  1422. { KTX_COMPRESSED_RGBA_PVRTC_4BPPV2_IMG, TextureFormat::PTC24 },
  1423. { KTX_R8, TextureFormat::R8 },
  1424. { KTX_RGBA16, TextureFormat::RGBA16 },
  1425. { KTX_RGBA16F, TextureFormat::RGBA16F },
  1426. { KTX_R32UI, TextureFormat::R32 },
  1427. { KTX_R32F, TextureFormat::R32F },
  1428. { KTX_RG8, TextureFormat::RG8 },
  1429. { KTX_RG16, TextureFormat::RG16 },
  1430. { KTX_RG16F, TextureFormat::RG16F },
  1431. { KTX_RG32UI, TextureFormat::RG32 },
  1432. { KTX_RG32F, TextureFormat::RG32F },
  1433. { KTX_BGRA, TextureFormat::BGRA8 },
  1434. { KTX_RGBA16, TextureFormat::RGBA16 },
  1435. { KTX_RGBA16F, TextureFormat::RGBA16F },
  1436. { KTX_RGBA32UI, TextureFormat::RGBA32 },
  1437. { KTX_RGBA32F, TextureFormat::RGBA32F },
  1438. { KTX_RGB565, TextureFormat::R5G6B5 },
  1439. { KTX_RGBA4, TextureFormat::RGBA4 },
  1440. { KTX_RGB5_A1, TextureFormat::RGB5A1 },
  1441. { KTX_RGB10_A2, TextureFormat::RGB10A2 },
  1442. };
  1443. bool imageParseKtx(ImageContainer& _imageContainer, bx::ReaderSeekerI* _reader)
  1444. {
  1445. uint8_t identifier[8];
  1446. bx::read(_reader, identifier);
  1447. if (identifier[1] != '1'
  1448. && identifier[2] != '1')
  1449. {
  1450. return false;
  1451. }
  1452. uint32_t endianness;
  1453. bx::read(_reader, endianness);
  1454. bool fromLittleEndian = 0x04030201 == endianness;
  1455. uint32_t glType;
  1456. bx::readHE(_reader, glType, fromLittleEndian);
  1457. uint32_t glTypeSize;
  1458. bx::readHE(_reader, glTypeSize, fromLittleEndian);
  1459. uint32_t glFormat;
  1460. bx::readHE(_reader, glFormat, fromLittleEndian);
  1461. uint32_t glInternalFormat;
  1462. bx::readHE(_reader, glInternalFormat, fromLittleEndian);
  1463. uint32_t glBaseInternalFormat;
  1464. bx::readHE(_reader, glBaseInternalFormat, fromLittleEndian);
  1465. uint32_t width;
  1466. bx::readHE(_reader, width, fromLittleEndian);
  1467. uint32_t height;
  1468. bx::readHE(_reader, height, fromLittleEndian);
  1469. uint32_t depth;
  1470. bx::readHE(_reader, depth, fromLittleEndian);
  1471. uint32_t numberOfArrayElements;
  1472. bx::readHE(_reader, numberOfArrayElements, fromLittleEndian);
  1473. uint32_t numFaces;
  1474. bx::readHE(_reader, numFaces, fromLittleEndian);
  1475. uint32_t numMips;
  1476. bx::readHE(_reader, numMips, fromLittleEndian);
  1477. uint32_t metaDataSize;
  1478. bx::readHE(_reader, metaDataSize, fromLittleEndian);
  1479. // skip meta garbage...
  1480. int64_t offset = bx::skip(_reader, metaDataSize);
  1481. TextureFormat::Enum format = TextureFormat::Unknown;
  1482. bool hasAlpha = false;
  1483. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateKtxFormat); ++ii)
  1484. {
  1485. if (s_translateKtxFormat[ii].m_format == glInternalFormat)
  1486. {
  1487. format = s_translateKtxFormat[ii].m_textureFormat;
  1488. break;
  1489. }
  1490. }
  1491. _imageContainer.m_data = NULL;
  1492. _imageContainer.m_size = 0;
  1493. _imageContainer.m_offset = (uint32_t)offset;
  1494. _imageContainer.m_width = width;
  1495. _imageContainer.m_height = height;
  1496. _imageContainer.m_depth = depth;
  1497. _imageContainer.m_format = format;
  1498. _imageContainer.m_numMips = numMips;
  1499. _imageContainer.m_hasAlpha = hasAlpha;
  1500. _imageContainer.m_cubeMap = numFaces > 1;
  1501. _imageContainer.m_ktx = true;
  1502. return TextureFormat::Unknown != format;
  1503. }
  1504. // PVR3
  1505. #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) )
  1506. #define PVR3_MAGIC BX_MAKEFOURCC('P', 'V', 'R', 3)
  1507. #define PVR3_HEADER_SIZE 52
  1508. #define PVR3_PVRTC1_2BPP_RGB 0
  1509. #define PVR3_PVRTC1_2BPP_RGBA 1
  1510. #define PVR3_PVRTC1_4BPP_RGB 2
  1511. #define PVR3_PVRTC1_4BPP_RGBA 3
  1512. #define PVR3_PVRTC2_2BPP_RGBA 4
  1513. #define PVR3_PVRTC2_4BPP_RGBA 5
  1514. #define PVR3_ETC1 6
  1515. #define PVR3_DXT1 7
  1516. #define PVR3_DXT2 8
  1517. #define PVR3_DXT3 9
  1518. #define PVR3_DXT4 10
  1519. #define PVR3_DXT5 11
  1520. #define PVR3_BC4 12
  1521. #define PVR3_BC5 13
  1522. #define PVR3_R8 PVR3_MAKE8CC('r', 0, 0, 0, 8, 0, 0, 0)
  1523. #define PVR3_R16 PVR3_MAKE8CC('r', 0, 0, 0, 16, 0, 0, 0)
  1524. #define PVR3_R32 PVR3_MAKE8CC('r', 0, 0, 0, 32, 0, 0, 0)
  1525. #define PVR3_RG8 PVR3_MAKE8CC('r', 'g', 0, 0, 8, 8, 0, 0)
  1526. #define PVR3_RG16 PVR3_MAKE8CC('r', 'g', 0, 0, 16, 16, 0, 0)
  1527. #define PVR3_RG32 PVR3_MAKE8CC('r', 'g', 0, 0, 32, 32, 0, 0)
  1528. #define PVR3_BGRA8 PVR3_MAKE8CC('b', 'g', 'r', 'a', 8, 8, 8, 8)
  1529. #define PVR3_RGBA16 PVR3_MAKE8CC('r', 'g', 'b', 'a', 16, 16, 16, 16)
  1530. #define PVR3_RGBA32 PVR3_MAKE8CC('r', 'g', 'b', 'a', 32, 32, 32, 32)
  1531. #define PVR3_RGB565 PVR3_MAKE8CC('r', 'g', 'b', 0, 5, 6, 5, 0)
  1532. #define PVR3_RGBA4 PVR3_MAKE8CC('r', 'g', 'b', 'a', 4, 4, 4, 4)
  1533. #define PVR3_RGBA51 PVR3_MAKE8CC('r', 'g', 'b', 'a', 5, 5, 5, 1)
  1534. #define PVR3_RGB10A2 PVR3_MAKE8CC('r', 'g', 'b', 'a', 10, 10, 10, 2)
  1535. #define PVR3_CHANNEL_TYPE_ANY UINT32_MAX
  1536. #define PVR3_CHANNEL_TYPE_FLOAT UINT32_C(12)
  1537. static struct TranslatePvr3Format
  1538. {
  1539. uint64_t m_format;
  1540. uint32_t m_channelTypeMask;
  1541. TextureFormat::Enum m_textureFormat;
  1542. } s_translatePvr3Format[] =
  1543. {
  1544. { PVR3_PVRTC1_2BPP_RGB, PVR3_CHANNEL_TYPE_ANY, TextureFormat::PTC12 },
  1545. { PVR3_PVRTC1_2BPP_RGBA, PVR3_CHANNEL_TYPE_ANY, TextureFormat::PTC12A },
  1546. { PVR3_PVRTC1_4BPP_RGB, PVR3_CHANNEL_TYPE_ANY, TextureFormat::PTC14 },
  1547. { PVR3_PVRTC1_4BPP_RGBA, PVR3_CHANNEL_TYPE_ANY, TextureFormat::PTC14A },
  1548. { PVR3_PVRTC2_2BPP_RGBA, PVR3_CHANNEL_TYPE_ANY, TextureFormat::PTC22 },
  1549. { PVR3_PVRTC2_4BPP_RGBA, PVR3_CHANNEL_TYPE_ANY, TextureFormat::PTC24 },
  1550. { PVR3_ETC1, PVR3_CHANNEL_TYPE_ANY, TextureFormat::ETC1 },
  1551. { PVR3_DXT1, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC1 },
  1552. { PVR3_DXT2, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC2 },
  1553. { PVR3_DXT3, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC2 },
  1554. { PVR3_DXT4, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC3 },
  1555. { PVR3_DXT5, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC3 },
  1556. { PVR3_BC4, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC4 },
  1557. { PVR3_BC5, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC5 },
  1558. { PVR3_R8, PVR3_CHANNEL_TYPE_ANY, TextureFormat::R8 },
  1559. { PVR3_R16, PVR3_CHANNEL_TYPE_ANY, TextureFormat::R16 },
  1560. { PVR3_R16, PVR3_CHANNEL_TYPE_FLOAT, TextureFormat::R16F },
  1561. { PVR3_R32, PVR3_CHANNEL_TYPE_ANY, TextureFormat::R32 },
  1562. { PVR3_R32, PVR3_CHANNEL_TYPE_FLOAT, TextureFormat::R32F },
  1563. { PVR3_RG8, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RG8 },
  1564. { PVR3_RG16, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RG16 },
  1565. { PVR3_RG16, PVR3_CHANNEL_TYPE_FLOAT, TextureFormat::RG16F },
  1566. { PVR3_RG32, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RG16 },
  1567. { PVR3_RG32, PVR3_CHANNEL_TYPE_FLOAT, TextureFormat::RG32F },
  1568. { PVR3_BGRA8, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BGRA8 },
  1569. { PVR3_RGBA16, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RGBA16 },
  1570. { PVR3_RGBA16, PVR3_CHANNEL_TYPE_FLOAT, TextureFormat::RGBA16F },
  1571. { PVR3_RGBA32, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RGBA32 },
  1572. { PVR3_RGBA32, PVR3_CHANNEL_TYPE_FLOAT, TextureFormat::RGBA32F },
  1573. { PVR3_RGB565, PVR3_CHANNEL_TYPE_ANY, TextureFormat::R5G6B5 },
  1574. { PVR3_RGBA4, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RGBA4 },
  1575. { PVR3_RGBA51, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RGB5A1 },
  1576. { PVR3_RGB10A2, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RGB10A2 },
  1577. };
  1578. bool imageParsePvr3(ImageContainer& _imageContainer, bx::ReaderSeekerI* _reader)
  1579. {
  1580. uint32_t flags;
  1581. bx::read(_reader, flags);
  1582. uint64_t pixelFormat;
  1583. bx::read(_reader, pixelFormat);
  1584. uint32_t colorSpace;
  1585. bx::read(_reader, colorSpace); // 0 - linearRGB, 1 - sRGB
  1586. uint32_t channelType;
  1587. bx::read(_reader, channelType);
  1588. uint32_t height;
  1589. bx::read(_reader, height);
  1590. uint32_t width;
  1591. bx::read(_reader, width);
  1592. uint32_t depth;
  1593. bx::read(_reader, depth);
  1594. uint32_t numSurfaces;
  1595. bx::read(_reader, numSurfaces);
  1596. uint32_t numFaces;
  1597. bx::read(_reader, numFaces);
  1598. uint32_t numMips;
  1599. bx::read(_reader, numMips);
  1600. uint32_t metaDataSize;
  1601. bx::read(_reader, metaDataSize);
  1602. // skip meta garbage...
  1603. int64_t offset = bx::skip(_reader, metaDataSize);
  1604. TextureFormat::Enum format = TextureFormat::Unknown;
  1605. bool hasAlpha = false;
  1606. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translatePvr3Format); ++ii)
  1607. {
  1608. if (s_translatePvr3Format[ii].m_format == pixelFormat
  1609. && channelType == (s_translatePvr3Format[ii].m_channelTypeMask & channelType) )
  1610. {
  1611. format = s_translatePvr3Format[ii].m_textureFormat;
  1612. break;
  1613. }
  1614. }
  1615. _imageContainer.m_data = NULL;
  1616. _imageContainer.m_size = 0;
  1617. _imageContainer.m_offset = (uint32_t)offset;
  1618. _imageContainer.m_width = width;
  1619. _imageContainer.m_height = height;
  1620. _imageContainer.m_depth = depth;
  1621. _imageContainer.m_format = format;
  1622. _imageContainer.m_numMips = numMips;
  1623. _imageContainer.m_hasAlpha = hasAlpha;
  1624. _imageContainer.m_cubeMap = numFaces > 1;
  1625. _imageContainer.m_ktx = false;
  1626. return TextureFormat::Unknown != format;
  1627. }
  1628. bool imageParse(ImageContainer& _imageContainer, bx::ReaderSeekerI* _reader)
  1629. {
  1630. uint32_t magic;
  1631. bx::read(_reader, magic);
  1632. if (DDS_MAGIC == magic)
  1633. {
  1634. return imageParseDds(_imageContainer, _reader);
  1635. }
  1636. else if (KTX_MAGIC == magic)
  1637. {
  1638. return imageParseKtx(_imageContainer, _reader);
  1639. }
  1640. else if (PVR3_MAGIC == magic)
  1641. {
  1642. return imageParsePvr3(_imageContainer, _reader);
  1643. }
  1644. else if (BGFX_CHUNK_MAGIC_TEX == magic)
  1645. {
  1646. TextureCreate tc;
  1647. bx::read(_reader, tc);
  1648. _imageContainer.m_format = tc.m_format;
  1649. _imageContainer.m_offset = UINT32_MAX;
  1650. if (NULL == tc.m_mem)
  1651. {
  1652. _imageContainer.m_data = NULL;
  1653. _imageContainer.m_size = 0;
  1654. }
  1655. else
  1656. {
  1657. _imageContainer.m_data = tc.m_mem->data;
  1658. _imageContainer.m_size = tc.m_mem->size;
  1659. }
  1660. _imageContainer.m_width = tc.m_width;
  1661. _imageContainer.m_height = tc.m_height;
  1662. _imageContainer.m_depth = tc.m_depth;
  1663. _imageContainer.m_numMips = tc.m_numMips;
  1664. _imageContainer.m_hasAlpha = false;
  1665. _imageContainer.m_cubeMap = tc.m_cubeMap;
  1666. _imageContainer.m_ktx = false;
  1667. return true;
  1668. }
  1669. return false;
  1670. }
  1671. bool imageParse(ImageContainer& _imageContainer, const void* _data, uint32_t _size)
  1672. {
  1673. bx::MemoryReader reader(_data, _size);
  1674. return imageParse(_imageContainer, &reader);
  1675. }
  1676. void imageDecodeToBgra8(uint8_t* _dst, const uint8_t* _src, uint32_t _width, uint32_t _height, uint32_t _pitch, uint8_t _type)
  1677. {
  1678. const uint8_t* src = _src;
  1679. uint32_t width = _width/4;
  1680. uint32_t height = _height/4;
  1681. uint8_t temp[16*4];
  1682. switch (_type)
  1683. {
  1684. case TextureFormat::BC1:
  1685. for (uint32_t yy = 0; yy < height; ++yy)
  1686. {
  1687. for (uint32_t xx = 0; xx < width; ++xx)
  1688. {
  1689. decodeBlockDxt1(temp, src);
  1690. src += 8;
  1691. uint8_t* dst = &_dst[(yy*_pitch+xx*4)*4];
  1692. memcpy(&dst[0*_pitch], &temp[ 0], 16);
  1693. memcpy(&dst[1*_pitch], &temp[16], 16);
  1694. memcpy(&dst[2*_pitch], &temp[32], 16);
  1695. memcpy(&dst[3*_pitch], &temp[48], 16);
  1696. }
  1697. }
  1698. break;
  1699. case TextureFormat::BC2:
  1700. for (uint32_t yy = 0; yy < height; ++yy)
  1701. {
  1702. for (uint32_t xx = 0; xx < width; ++xx)
  1703. {
  1704. decodeBlockDxt23A(temp+3, src);
  1705. src += 8;
  1706. decodeBlockDxt(temp, src);
  1707. src += 8;
  1708. uint8_t* dst = &_dst[(yy*_pitch+xx*4)*4];
  1709. memcpy(&dst[0*_pitch], &temp[ 0], 16);
  1710. memcpy(&dst[1*_pitch], &temp[16], 16);
  1711. memcpy(&dst[2*_pitch], &temp[32], 16);
  1712. memcpy(&dst[3*_pitch], &temp[48], 16);
  1713. }
  1714. }
  1715. break;
  1716. case TextureFormat::BC3:
  1717. for (uint32_t yy = 0; yy < height; ++yy)
  1718. {
  1719. for (uint32_t xx = 0; xx < width; ++xx)
  1720. {
  1721. decodeBlockDxt45A(temp+3, src);
  1722. src += 8;
  1723. decodeBlockDxt(temp, src);
  1724. src += 8;
  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::BC4:
  1734. for (uint32_t yy = 0; yy < height; ++yy)
  1735. {
  1736. for (uint32_t xx = 0; xx < width; ++xx)
  1737. {
  1738. decodeBlockDxt45A(temp, src);
  1739. src += 8;
  1740. uint8_t* dst = &_dst[(yy*_pitch+xx*4)*4];
  1741. memcpy(&dst[0*_pitch], &temp[ 0], 16);
  1742. memcpy(&dst[1*_pitch], &temp[16], 16);
  1743. memcpy(&dst[2*_pitch], &temp[32], 16);
  1744. memcpy(&dst[3*_pitch], &temp[48], 16);
  1745. }
  1746. }
  1747. break;
  1748. case TextureFormat::BC5:
  1749. for (uint32_t yy = 0; yy < height; ++yy)
  1750. {
  1751. for (uint32_t xx = 0; xx < width; ++xx)
  1752. {
  1753. decodeBlockDxt45A(temp+1, src);
  1754. src += 8;
  1755. decodeBlockDxt45A(temp+2, src);
  1756. src += 8;
  1757. for (uint32_t ii = 0; ii < 16; ++ii)
  1758. {
  1759. float nx = temp[ii*4+2]*2.0f/255.0f - 1.0f;
  1760. float ny = temp[ii*4+1]*2.0f/255.0f - 1.0f;
  1761. float nz = sqrtf(1.0f - nx*nx - ny*ny);
  1762. temp[ii*4+0] = uint8_t( (nz + 1.0f)*255.0f/2.0f);
  1763. temp[ii*4+3] = 0;
  1764. }
  1765. uint8_t* dst = &_dst[(yy*_pitch+xx*4)*4];
  1766. memcpy(&dst[0*_pitch], &temp[ 0], 16);
  1767. memcpy(&dst[1*_pitch], &temp[16], 16);
  1768. memcpy(&dst[2*_pitch], &temp[32], 16);
  1769. memcpy(&dst[3*_pitch], &temp[48], 16);
  1770. }
  1771. }
  1772. break;
  1773. case TextureFormat::ETC1:
  1774. case TextureFormat::ETC2:
  1775. for (uint32_t yy = 0; yy < height; ++yy)
  1776. {
  1777. for (uint32_t xx = 0; xx < width; ++xx)
  1778. {
  1779. decodeBlockEtc12(temp, src);
  1780. src += 8;
  1781. uint8_t* dst = &_dst[(yy*_pitch+xx*4)*4];
  1782. memcpy(&dst[0*_pitch], &temp[ 0], 16);
  1783. memcpy(&dst[1*_pitch], &temp[16], 16);
  1784. memcpy(&dst[2*_pitch], &temp[32], 16);
  1785. memcpy(&dst[3*_pitch], &temp[48], 16);
  1786. }
  1787. }
  1788. break;
  1789. case TextureFormat::ETC2A:
  1790. BX_WARN(false, "ETC2A decoder is not implemented.");
  1791. imageCheckerboard(_width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00), _dst);
  1792. break;
  1793. case TextureFormat::ETC2A1:
  1794. BX_WARN(false, "ETC2A1 decoder is not implemented.");
  1795. imageCheckerboard(_width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xffff0000), _dst);
  1796. break;
  1797. case TextureFormat::PTC12:
  1798. BX_WARN(false, "PTC12 decoder is not implemented.");
  1799. imageCheckerboard(_width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xffff00ff), _dst);
  1800. break;
  1801. case TextureFormat::PTC12A:
  1802. BX_WARN(false, "PTC12A decoder is not implemented.");
  1803. imageCheckerboard(_width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xffffff00), _dst);
  1804. break;
  1805. case TextureFormat::PTC14:
  1806. for (uint32_t yy = 0; yy < height; ++yy)
  1807. {
  1808. for (uint32_t xx = 0; xx < width; ++xx)
  1809. {
  1810. decodeBlockPtc14(temp, src, xx, yy, width, height);
  1811. uint8_t* dst = &_dst[(yy*_pitch+xx*4)*4];
  1812. memcpy(&dst[0*_pitch], &temp[ 0], 16);
  1813. memcpy(&dst[1*_pitch], &temp[16], 16);
  1814. memcpy(&dst[2*_pitch], &temp[32], 16);
  1815. memcpy(&dst[3*_pitch], &temp[48], 16);
  1816. }
  1817. }
  1818. break;
  1819. case TextureFormat::PTC14A:
  1820. for (uint32_t yy = 0; yy < height; ++yy)
  1821. {
  1822. for (uint32_t xx = 0; xx < width; ++xx)
  1823. {
  1824. decodeBlockPtc14A(temp, src, xx, yy, width, height);
  1825. uint8_t* dst = &_dst[(yy*_pitch+xx*4)*4];
  1826. memcpy(&dst[0*_pitch], &temp[ 0], 16);
  1827. memcpy(&dst[1*_pitch], &temp[16], 16);
  1828. memcpy(&dst[2*_pitch], &temp[32], 16);
  1829. memcpy(&dst[3*_pitch], &temp[48], 16);
  1830. }
  1831. }
  1832. break;
  1833. case TextureFormat::PTC22:
  1834. BX_WARN(false, "PTC22 decoder is not implemented.");
  1835. imageCheckerboard(_width, _height, 16, UINT32_C(0xff00ff00), UINT32_C(0xff0000ff), _dst);
  1836. break;
  1837. case TextureFormat::PTC24:
  1838. BX_WARN(false, "PTC24 decoder is not implemented.");
  1839. imageCheckerboard(_width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xffffffff), _dst);
  1840. break;
  1841. default:
  1842. // Decompression not implemented... Make ugly red-yellow checkerboard texture.
  1843. imageCheckerboard(_width, _height, 16, UINT32_C(0xffff0000), UINT32_C(0xffffff00), _dst);
  1844. break;
  1845. }
  1846. }
  1847. bool imageGetRawData(const ImageContainer& _imageContainer, uint8_t _side, uint8_t _lod, const void* _data, uint32_t _size, ImageMip& _mip)
  1848. {
  1849. uint32_t offset = _imageContainer.m_offset;
  1850. TextureFormat::Enum type = TextureFormat::Enum(_imageContainer.m_format);
  1851. bool hasAlpha = _imageContainer.m_hasAlpha;
  1852. const ImageBlockInfo& blockInfo = s_imageBlockInfo[type];
  1853. const uint8_t bpp = blockInfo.bitsPerPixel;
  1854. const uint32_t blockSize = blockInfo.blockSize;
  1855. const uint32_t blockWidth = blockInfo.blockWidth;
  1856. const uint32_t blockHeight = blockInfo.blockHeight;
  1857. const uint32_t minBlockX = blockInfo.minBlockX;
  1858. const uint32_t minBlockY = blockInfo.minBlockY;
  1859. if (UINT32_MAX == _imageContainer.m_offset)
  1860. {
  1861. if (NULL == _imageContainer.m_data)
  1862. {
  1863. return false;
  1864. }
  1865. offset = 0;
  1866. _data = _imageContainer.m_data;
  1867. _size = _imageContainer.m_size;
  1868. }
  1869. for (uint8_t side = 0, numSides = _imageContainer.m_cubeMap ? 6 : 1; side < numSides; ++side)
  1870. {
  1871. uint32_t width = _imageContainer.m_width;
  1872. uint32_t height = _imageContainer.m_height;
  1873. uint32_t depth = _imageContainer.m_depth;
  1874. for (uint8_t lod = 0, num = _imageContainer.m_numMips; lod < num; ++lod)
  1875. {
  1876. // skip imageSize in KTX format.
  1877. offset += _imageContainer.m_ktx ? sizeof(uint32_t) : 0;
  1878. width = bx::uint32_max(blockWidth * minBlockX, ( (width + blockWidth - 1) / blockWidth )*blockWidth);
  1879. height = bx::uint32_max(blockHeight * minBlockY, ( (height + blockHeight - 1) / blockHeight)*blockHeight);
  1880. depth = bx::uint32_max(1, depth);
  1881. uint32_t size = width*height*depth*bpp/8;
  1882. if (side == _side
  1883. && lod == _lod)
  1884. {
  1885. _mip.m_width = width;
  1886. _mip.m_height = height;
  1887. _mip.m_blockSize = blockSize;
  1888. _mip.m_size = size;
  1889. _mip.m_data = (const uint8_t*)_data + offset;
  1890. _mip.m_bpp = bpp;
  1891. _mip.m_format = type;
  1892. _mip.m_hasAlpha = hasAlpha;
  1893. return true;
  1894. }
  1895. offset += size;
  1896. BX_CHECK(offset <= _size, "Reading past size of data buffer! (offset %d, size %d)", offset, _size);
  1897. BX_UNUSED(_size);
  1898. width >>= 1;
  1899. height >>= 1;
  1900. depth >>= 1;
  1901. }
  1902. }
  1903. return false;
  1904. }
  1905. } // namespace bgfx