image.cpp 93 KB

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