image.cpp 76 KB

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