image.cpp 72 KB

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