image.cpp 128 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554
  1. /*
  2. * Copyright 2011-2017 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  4. */
  5. #include "bimg_p.h"
  6. namespace bimg
  7. {
  8. static const ImageBlockInfo s_imageBlockInfo[] =
  9. {
  10. // +-------------------------------------------- bits per pixel
  11. // | +----------------------------------------- block width
  12. // | | +-------------------------------------- block height
  13. // | | | +---------------------------------- block size
  14. // | | | | +------------------------------- min blocks x
  15. // | | | | | +---------------------------- min blocks y
  16. // | | | | | | +------------------------ depth bits
  17. // | | | | | | | +--------------------- stencil bits
  18. // | | | | | | | | +---+---+---+----- r, g, b, a bits
  19. // | | | | | | | | r g b a +-- encoding type
  20. // | | | | | | | | | | | | |
  21. { 4, 4, 4, 8, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // BC1
  22. { 8, 4, 4, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // BC2
  23. { 8, 4, 4, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // BC3
  24. { 4, 4, 4, 8, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // BC4
  25. { 8, 4, 4, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // BC5
  26. { 8, 4, 4, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // BC6H
  27. { 8, 4, 4, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // BC7
  28. { 4, 4, 4, 8, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ETC1
  29. { 4, 4, 4, 8, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ETC2
  30. { 8, 4, 4, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ETC2A
  31. { 4, 4, 4, 8, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ETC2A1
  32. { 2, 8, 4, 8, 2, 2, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // PTC12
  33. { 4, 4, 4, 8, 2, 2, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // PTC14
  34. { 2, 8, 4, 8, 2, 2, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // PTC12A
  35. { 4, 4, 4, 8, 2, 2, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // PTC14A
  36. { 2, 8, 4, 8, 2, 2, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // PTC22
  37. { 4, 4, 4, 8, 2, 2, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // PTC24
  38. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Count) }, // Unknown
  39. { 1, 8, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // R1
  40. { 8, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 8, uint8_t(bx::EncodingType::Unorm) }, // A8
  41. { 8, 1, 1, 1, 1, 1, 0, 0, 8, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // R8
  42. { 8, 1, 1, 1, 1, 1, 0, 0, 8, 0, 0, 0, uint8_t(bx::EncodingType::Int ) }, // R8I
  43. { 8, 1, 1, 1, 1, 1, 0, 0, 8, 0, 0, 0, uint8_t(bx::EncodingType::Uint ) }, // R8U
  44. { 8, 1, 1, 1, 1, 1, 0, 0, 8, 0, 0, 0, uint8_t(bx::EncodingType::Snorm) }, // R8S
  45. { 16, 1, 1, 2, 1, 1, 0, 0, 16, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // R16
  46. { 16, 1, 1, 2, 1, 1, 0, 0, 16, 0, 0, 0, uint8_t(bx::EncodingType::Int ) }, // R16I
  47. { 16, 1, 1, 2, 1, 1, 0, 0, 16, 0, 0, 0, uint8_t(bx::EncodingType::Uint ) }, // R16U
  48. { 16, 1, 1, 2, 1, 1, 0, 0, 16, 0, 0, 0, uint8_t(bx::EncodingType::Float) }, // R16F
  49. { 16, 1, 1, 2, 1, 1, 0, 0, 16, 0, 0, 0, uint8_t(bx::EncodingType::Snorm) }, // R16S
  50. { 32, 1, 1, 4, 1, 1, 0, 0, 32, 0, 0, 0, uint8_t(bx::EncodingType::Int ) }, // R32I
  51. { 32, 1, 1, 4, 1, 1, 0, 0, 32, 0, 0, 0, uint8_t(bx::EncodingType::Uint ) }, // R32U
  52. { 32, 1, 1, 4, 1, 1, 0, 0, 32, 0, 0, 0, uint8_t(bx::EncodingType::Float) }, // R32F
  53. { 16, 1, 1, 2, 1, 1, 0, 0, 8, 8, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // RG8
  54. { 16, 1, 1, 2, 1, 1, 0, 0, 8, 8, 0, 0, uint8_t(bx::EncodingType::Int ) }, // RG8I
  55. { 16, 1, 1, 2, 1, 1, 0, 0, 8, 8, 0, 0, uint8_t(bx::EncodingType::Uint ) }, // RG8U
  56. { 16, 1, 1, 2, 1, 1, 0, 0, 8, 8, 0, 0, uint8_t(bx::EncodingType::Snorm) }, // RG8S
  57. { 32, 1, 1, 4, 1, 1, 0, 0, 16, 16, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // RG16
  58. { 32, 1, 1, 4, 1, 1, 0, 0, 16, 16, 0, 0, uint8_t(bx::EncodingType::Int ) }, // RG16I
  59. { 32, 1, 1, 4, 1, 1, 0, 0, 16, 16, 0, 0, uint8_t(bx::EncodingType::Uint ) }, // RG16U
  60. { 32, 1, 1, 4, 1, 1, 0, 0, 16, 16, 0, 0, uint8_t(bx::EncodingType::Float) }, // RG16F
  61. { 32, 1, 1, 4, 1, 1, 0, 0, 16, 16, 0, 0, uint8_t(bx::EncodingType::Snorm) }, // RG16S
  62. { 64, 1, 1, 8, 1, 1, 0, 0, 32, 32, 0, 0, uint8_t(bx::EncodingType::Int ) }, // RG32I
  63. { 64, 1, 1, 8, 1, 1, 0, 0, 32, 32, 0, 0, uint8_t(bx::EncodingType::Uint ) }, // RG32U
  64. { 64, 1, 1, 8, 1, 1, 0, 0, 32, 32, 0, 0, uint8_t(bx::EncodingType::Float) }, // RG32F
  65. { 24, 1, 1, 3, 1, 1, 0, 0, 8, 8, 8, 0, uint8_t(bx::EncodingType::Unorm) }, // RGB8
  66. { 24, 1, 1, 3, 1, 1, 0, 0, 8, 8, 8, 0, uint8_t(bx::EncodingType::Int ) }, // RGB8I
  67. { 24, 1, 1, 3, 1, 1, 0, 0, 8, 8, 8, 0, uint8_t(bx::EncodingType::Uint ) }, // RGB8U
  68. { 24, 1, 1, 3, 1, 1, 0, 0, 8, 8, 8, 0, uint8_t(bx::EncodingType::Snorm) }, // RGB8S
  69. { 32, 1, 1, 4, 1, 1, 0, 0, 9, 9, 9, 5, uint8_t(bx::EncodingType::Float) }, // RGB9E5F
  70. { 32, 1, 1, 4, 1, 1, 0, 0, 8, 8, 8, 8, uint8_t(bx::EncodingType::Unorm) }, // BGRA8
  71. { 32, 1, 1, 4, 1, 1, 0, 0, 8, 8, 8, 8, uint8_t(bx::EncodingType::Unorm) }, // RGBA8
  72. { 32, 1, 1, 4, 1, 1, 0, 0, 8, 8, 8, 8, uint8_t(bx::EncodingType::Int ) }, // RGBA8I
  73. { 32, 1, 1, 4, 1, 1, 0, 0, 8, 8, 8, 8, uint8_t(bx::EncodingType::Uint ) }, // RGBA8U
  74. { 32, 1, 1, 4, 1, 1, 0, 0, 8, 8, 8, 8, uint8_t(bx::EncodingType::Snorm) }, // RGBA8S
  75. { 64, 1, 1, 8, 1, 1, 0, 0, 16, 16, 16, 16, uint8_t(bx::EncodingType::Unorm) }, // RGBA16
  76. { 64, 1, 1, 8, 1, 1, 0, 0, 16, 16, 16, 16, uint8_t(bx::EncodingType::Int ) }, // RGBA16I
  77. { 64, 1, 1, 8, 1, 1, 0, 0, 16, 16, 16, 16, uint8_t(bx::EncodingType::Uint ) }, // RGBA16U
  78. { 64, 1, 1, 8, 1, 1, 0, 0, 16, 16, 16, 16, uint8_t(bx::EncodingType::Float) }, // RGBA16F
  79. { 64, 1, 1, 8, 1, 1, 0, 0, 16, 16, 16, 16, uint8_t(bx::EncodingType::Snorm) }, // RGBA16S
  80. { 128, 1, 1, 16, 1, 1, 0, 0, 32, 32, 32, 32, uint8_t(bx::EncodingType::Int ) }, // RGBA32I
  81. { 128, 1, 1, 16, 1, 1, 0, 0, 32, 32, 32, 32, uint8_t(bx::EncodingType::Uint ) }, // RGBA32U
  82. { 128, 1, 1, 16, 1, 1, 0, 0, 32, 32, 32, 32, uint8_t(bx::EncodingType::Float) }, // RGBA32F
  83. { 16, 1, 1, 2, 1, 1, 0, 0, 5, 6, 5, 0, uint8_t(bx::EncodingType::Unorm) }, // R5G6B5
  84. { 16, 1, 1, 2, 1, 1, 0, 0, 4, 4, 4, 4, uint8_t(bx::EncodingType::Unorm) }, // RGBA4
  85. { 16, 1, 1, 2, 1, 1, 0, 0, 5, 5, 5, 1, uint8_t(bx::EncodingType::Unorm) }, // RGB5A1
  86. { 32, 1, 1, 4, 1, 1, 0, 0, 10, 10, 10, 2, uint8_t(bx::EncodingType::Unorm) }, // RGB10A2
  87. { 32, 1, 1, 4, 1, 1, 0, 0, 11, 11, 10, 0, uint8_t(bx::EncodingType::Unorm) }, // RG11B10F
  88. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Count) }, // UnknownDepth
  89. { 16, 1, 1, 2, 1, 1, 16, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // D16
  90. { 24, 1, 1, 3, 1, 1, 24, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // D24
  91. { 32, 1, 1, 4, 1, 1, 24, 8, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // D24S8
  92. { 32, 1, 1, 4, 1, 1, 32, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // D32
  93. { 16, 1, 1, 2, 1, 1, 16, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Float) }, // D16F
  94. { 24, 1, 1, 3, 1, 1, 24, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Float) }, // D24F
  95. { 32, 1, 1, 4, 1, 1, 32, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Float) }, // D32F
  96. { 8, 1, 1, 1, 1, 1, 0, 8, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // D0S8
  97. };
  98. BX_STATIC_ASSERT(TextureFormat::Count == BX_COUNTOF(s_imageBlockInfo) );
  99. static const char* s_textureFormatName[] =
  100. {
  101. "BC1", // BC1
  102. "BC2", // BC2
  103. "BC3", // BC3
  104. "BC4", // BC4
  105. "BC5", // BC5
  106. "BC6H", // BC6H
  107. "BC7", // BC7
  108. "ETC1", // ETC1
  109. "ETC2", // ETC2
  110. "ETC2A", // ETC2A
  111. "ETC2A1", // ETC2A1
  112. "PTC12", // PTC12
  113. "PTC14", // PTC14
  114. "PTC12A", // PTC12A
  115. "PTC14A", // PTC14A
  116. "PTC22", // PTC22
  117. "PTC24", // PTC24
  118. "<unknown>", // Unknown
  119. "R1", // R1
  120. "A8", // A8
  121. "R8", // R8
  122. "R8I", // R8I
  123. "R8U", // R8U
  124. "R8S", // R8S
  125. "R16", // R16
  126. "R16I", // R16I
  127. "R16U", // R16U
  128. "R16F", // R16F
  129. "R16S", // R16S
  130. "R32I", // R32I
  131. "R32U", // R32U
  132. "R32F", // R32F
  133. "RG8", // RG8
  134. "RG8I", // RG8I
  135. "RG8U", // RG8U
  136. "RG8S", // RG8S
  137. "RG16", // RG16
  138. "RG16I", // RG16I
  139. "RG16U", // RG16U
  140. "RG16F", // RG16F
  141. "RG16S", // RG16S
  142. "RG32I", // RG32I
  143. "RG32U", // RG32U
  144. "RG32F", // RG32F
  145. "RGB8", // RGB8
  146. "RGB8I", // RGB8I
  147. "RGB8U", // RGB8U
  148. "RGB8S", // RGB8S
  149. "RGB9E5", // RGB9E5F
  150. "BGRA8", // BGRA8
  151. "RGBA8", // RGBA8
  152. "RGBA8I", // RGBA8I
  153. "RGBA8U", // RGBA8U
  154. "RGBA8S", // RGBA8S
  155. "RGBA16", // RGBA16
  156. "RGBA16I", // RGBA16I
  157. "RGBA16U", // RGBA16U
  158. "RGBA16F", // RGBA16F
  159. "RGBA16S", // RGBA16S
  160. "RGBA32I", // RGBA32I
  161. "RGBA32U", // RGBA32U
  162. "RGBA32F", // RGBA32F
  163. "R5G6B5", // R5G6B5
  164. "RGBA4", // RGBA4
  165. "RGB5A1", // RGB5A1
  166. "RGB10A2", // RGB10A2
  167. "RG11B10F", // RG11B10F
  168. "<unknown>", // UnknownDepth
  169. "D16", // D16
  170. "D24", // D24
  171. "D24S8", // D24S8
  172. "D32", // D32
  173. "D16F", // D16F
  174. "D24F", // D24F
  175. "D32F", // D32F
  176. "D0S8", // D0S8
  177. };
  178. BX_STATIC_ASSERT(TextureFormat::Count == BX_COUNTOF(s_textureFormatName) );
  179. bool isCompressed(TextureFormat::Enum _format)
  180. {
  181. return _format < TextureFormat::Unknown;
  182. }
  183. bool isColor(TextureFormat::Enum _format)
  184. {
  185. return _format > TextureFormat::Unknown
  186. && _format < TextureFormat::UnknownDepth
  187. ;
  188. }
  189. bool isDepth(TextureFormat::Enum _format)
  190. {
  191. return _format > TextureFormat::UnknownDepth
  192. && _format < TextureFormat::Count
  193. ;
  194. }
  195. bool isValid(TextureFormat::Enum _format)
  196. {
  197. return _format != TextureFormat::Unknown
  198. && _format != TextureFormat::UnknownDepth
  199. && _format != TextureFormat::Count
  200. ;
  201. }
  202. uint8_t getBitsPerPixel(TextureFormat::Enum _format)
  203. {
  204. return s_imageBlockInfo[_format].bitsPerPixel;
  205. }
  206. const ImageBlockInfo& getBlockInfo(TextureFormat::Enum _format)
  207. {
  208. return s_imageBlockInfo[_format];
  209. }
  210. uint8_t getBlockSize(TextureFormat::Enum _format)
  211. {
  212. return s_imageBlockInfo[_format].blockSize;
  213. }
  214. const char* getName(TextureFormat::Enum _format)
  215. {
  216. return s_textureFormatName[_format];
  217. }
  218. TextureFormat::Enum getFormat(const char* _name)
  219. {
  220. for (uint32_t ii = 0; ii < TextureFormat::Count; ++ii)
  221. {
  222. const TextureFormat::Enum fmt = TextureFormat::Enum(ii);
  223. if (isValid(fmt) )
  224. {
  225. if (0 == bx::strCmpI(s_textureFormatName[ii], _name) )
  226. {
  227. return fmt;
  228. }
  229. }
  230. }
  231. return TextureFormat::Unknown;
  232. }
  233. uint8_t imageGetNumMips(TextureFormat::Enum _format, uint16_t _width, uint16_t _height, uint16_t _depth)
  234. {
  235. const ImageBlockInfo& blockInfo = getBlockInfo(_format);
  236. const uint16_t blockWidth = blockInfo.blockWidth;
  237. const uint16_t blockHeight = blockInfo.blockHeight;
  238. const uint16_t minBlockX = blockInfo.minBlockX;
  239. const uint16_t minBlockY = blockInfo.minBlockY;
  240. _width = bx::uint16_max(blockWidth * minBlockX, ( (_width + blockWidth - 1) / blockWidth )*blockWidth);
  241. _height = bx::uint16_max(blockHeight * minBlockY, ( (_height + blockHeight - 1) / blockHeight)*blockHeight);
  242. _depth = bx::uint16_max(1, _depth);
  243. uint8_t numMips = calcNumMips(true, _width, _height, _depth);
  244. return numMips;
  245. }
  246. uint32_t imageGetSize(TextureInfo* _info, uint16_t _width, uint16_t _height, uint16_t _depth, bool _cubeMap, bool _hasMips, uint16_t _numLayers, TextureFormat::Enum _format)
  247. {
  248. const ImageBlockInfo& blockInfo = getBlockInfo(_format);
  249. const uint8_t bpp = blockInfo.bitsPerPixel;
  250. const uint16_t blockWidth = blockInfo.blockWidth;
  251. const uint16_t blockHeight = blockInfo.blockHeight;
  252. const uint16_t minBlockX = blockInfo.minBlockX;
  253. const uint16_t minBlockY = blockInfo.minBlockY;
  254. _width = bx::uint16_max(blockWidth * minBlockX, ( (_width + blockWidth - 1) / blockWidth)*blockWidth);
  255. _height = bx::uint16_max(blockHeight * minBlockY, ( (_height + blockHeight - 1) / blockHeight)*blockHeight);
  256. _depth = bx::uint16_max(1, _depth);
  257. const uint8_t numMips = calcNumMips(_hasMips, _width, _height, _depth);
  258. const uint32_t sides = _cubeMap ? 6 : 1;
  259. uint32_t width = _width;
  260. uint32_t height = _height;
  261. uint32_t depth = _depth;
  262. uint32_t size = 0;
  263. for (uint32_t lod = 0; lod < numMips; ++lod)
  264. {
  265. width = bx::uint32_max(blockWidth * minBlockX, ( (width + blockWidth - 1) / blockWidth )*blockWidth);
  266. height = bx::uint32_max(blockHeight * minBlockY, ( (height + blockHeight - 1) / blockHeight)*blockHeight);
  267. depth = bx::uint32_max(1, depth);
  268. size += width*height*depth*bpp/8 * sides;
  269. width >>= 1;
  270. height >>= 1;
  271. depth >>= 1;
  272. }
  273. size *= _numLayers;
  274. if (NULL != _info)
  275. {
  276. _info->format = _format;
  277. _info->width = _width;
  278. _info->height = _height;
  279. _info->depth = _depth;
  280. _info->numMips = numMips;
  281. _info->numLayers = _numLayers;
  282. _info->cubeMap = _cubeMap;
  283. _info->storageSize = size;
  284. _info->bitsPerPixel = bpp;
  285. }
  286. return size;
  287. }
  288. void imageSolid(void* _dst, uint32_t _width, uint32_t _height, uint32_t _solid)
  289. {
  290. uint32_t* dst = (uint32_t*)_dst;
  291. for (uint32_t ii = 0, num = _width*_height; ii < num; ++ii)
  292. {
  293. *dst++ = _solid;
  294. }
  295. }
  296. void imageCheckerboard(void* _dst, uint32_t _width, uint32_t _height, uint32_t _step, uint32_t _0, uint32_t _1)
  297. {
  298. uint32_t* dst = (uint32_t*)_dst;
  299. for (uint32_t yy = 0; yy < _height; ++yy)
  300. {
  301. for (uint32_t xx = 0; xx < _width; ++xx)
  302. {
  303. uint32_t abgr = ( (xx/_step)&1) ^ ( (yy/_step)&1) ? _1 : _0;
  304. *dst++ = abgr;
  305. }
  306. }
  307. }
  308. void imageRgba8Downsample2x2Ref(void* _dst, uint32_t _width, uint32_t _height, uint32_t _srcPitch, const void* _src)
  309. {
  310. const uint32_t dstWidth = _width/2;
  311. const uint32_t dstHeight = _height/2;
  312. if (0 == dstWidth
  313. || 0 == dstHeight)
  314. {
  315. return;
  316. }
  317. uint8_t* dst = (uint8_t*)_dst;
  318. const uint8_t* src = (const uint8_t*)_src;
  319. for (uint32_t yy = 0, ystep = _srcPitch*2; yy < dstHeight; ++yy, src += ystep)
  320. {
  321. const uint8_t* rgba = src;
  322. for (uint32_t xx = 0; xx < dstWidth; ++xx, rgba += 8, dst += 4)
  323. {
  324. float rr = bx::fpow(rgba[ 0], 2.2f);
  325. float gg = bx::fpow(rgba[ 1], 2.2f);
  326. float bb = bx::fpow(rgba[ 2], 2.2f);
  327. float aa = rgba[ 3];
  328. rr += bx::fpow(rgba[ 4], 2.2f);
  329. gg += bx::fpow(rgba[ 5], 2.2f);
  330. bb += bx::fpow(rgba[ 6], 2.2f);
  331. aa += rgba[ 7];
  332. rr += bx::fpow(rgba[_srcPitch+0], 2.2f);
  333. gg += bx::fpow(rgba[_srcPitch+1], 2.2f);
  334. bb += bx::fpow(rgba[_srcPitch+2], 2.2f);
  335. aa += rgba[_srcPitch+3];
  336. rr += bx::fpow(rgba[_srcPitch+4], 2.2f);
  337. gg += bx::fpow(rgba[_srcPitch+5], 2.2f);
  338. bb += bx::fpow(rgba[_srcPitch+6], 2.2f);
  339. aa += rgba[_srcPitch+7];
  340. rr *= 0.25f;
  341. gg *= 0.25f;
  342. bb *= 0.25f;
  343. aa *= 0.25f;
  344. rr = bx::fpow(rr, 1.0f/2.2f);
  345. gg = bx::fpow(gg, 1.0f/2.2f);
  346. bb = bx::fpow(bb, 1.0f/2.2f);
  347. dst[0] = (uint8_t)rr;
  348. dst[1] = (uint8_t)gg;
  349. dst[2] = (uint8_t)bb;
  350. dst[3] = (uint8_t)aa;
  351. }
  352. }
  353. }
  354. void imageRgba8Downsample2x2(void* _dst, uint32_t _width, uint32_t _height, uint32_t _srcPitch, const void* _src)
  355. {
  356. const uint32_t dstWidth = _width/2;
  357. const uint32_t dstHeight = _height/2;
  358. if (0 == dstWidth
  359. || 0 == dstHeight)
  360. {
  361. return;
  362. }
  363. uint8_t* dst = (uint8_t*)_dst;
  364. const uint8_t* src = (const uint8_t*)_src;
  365. using namespace bx;
  366. const simd128_t unpack = simd_ld(1.0f, 1.0f/256.0f, 1.0f/65536.0f, 1.0f/16777216.0f);
  367. const simd128_t pack = simd_ld(1.0f, 256.0f*0.5f, 65536.0f, 16777216.0f*0.5f);
  368. const simd128_t umask = simd_ild(0xff, 0xff00, 0xff0000, 0xff000000);
  369. const simd128_t pmask = simd_ild(0xff, 0x7f80, 0xff0000, 0x7f800000);
  370. const simd128_t wflip = simd_ild(0, 0, 0, 0x80000000);
  371. const simd128_t wadd = simd_ld(0.0f, 0.0f, 0.0f, 32768.0f*65536.0f);
  372. const simd128_t gamma = simd_ld(1.0f/2.2f, 1.0f/2.2f, 1.0f/2.2f, 1.0f);
  373. const simd128_t linear = simd_ld(2.2f, 2.2f, 2.2f, 1.0f);
  374. const simd128_t quater = simd_splat(0.25f);
  375. for (uint32_t yy = 0, ystep = _srcPitch*2; yy < dstHeight; ++yy, src += ystep)
  376. {
  377. const uint8_t* rgba = src;
  378. for (uint32_t xx = 0; xx < dstWidth; ++xx, rgba += 8, dst += 4)
  379. {
  380. const simd128_t abgr0 = simd_splat(rgba);
  381. const simd128_t abgr1 = simd_splat(rgba+4);
  382. const simd128_t abgr2 = simd_splat(rgba+_srcPitch);
  383. const simd128_t abgr3 = simd_splat(rgba+_srcPitch+4);
  384. const simd128_t abgr0m = simd_and(abgr0, umask);
  385. const simd128_t abgr1m = simd_and(abgr1, umask);
  386. const simd128_t abgr2m = simd_and(abgr2, umask);
  387. const simd128_t abgr3m = simd_and(abgr3, umask);
  388. const simd128_t abgr0x = simd_xor(abgr0m, wflip);
  389. const simd128_t abgr1x = simd_xor(abgr1m, wflip);
  390. const simd128_t abgr2x = simd_xor(abgr2m, wflip);
  391. const simd128_t abgr3x = simd_xor(abgr3m, wflip);
  392. const simd128_t abgr0f = simd_itof(abgr0x);
  393. const simd128_t abgr1f = simd_itof(abgr1x);
  394. const simd128_t abgr2f = simd_itof(abgr2x);
  395. const simd128_t abgr3f = simd_itof(abgr3x);
  396. const simd128_t abgr0c = simd_add(abgr0f, wadd);
  397. const simd128_t abgr1c = simd_add(abgr1f, wadd);
  398. const simd128_t abgr2c = simd_add(abgr2f, wadd);
  399. const simd128_t abgr3c = simd_add(abgr3f, wadd);
  400. const simd128_t abgr0n = simd_mul(abgr0c, unpack);
  401. const simd128_t abgr1n = simd_mul(abgr1c, unpack);
  402. const simd128_t abgr2n = simd_mul(abgr2c, unpack);
  403. const simd128_t abgr3n = simd_mul(abgr3c, unpack);
  404. const simd128_t abgr0l = simd_pow(abgr0n, linear);
  405. const simd128_t abgr1l = simd_pow(abgr1n, linear);
  406. const simd128_t abgr2l = simd_pow(abgr2n, linear);
  407. const simd128_t abgr3l = simd_pow(abgr3n, linear);
  408. const simd128_t sum0 = simd_add(abgr0l, abgr1l);
  409. const simd128_t sum1 = simd_add(abgr2l, abgr3l);
  410. const simd128_t sum2 = simd_add(sum0, sum1);
  411. const simd128_t avg0 = simd_mul(sum2, quater);
  412. const simd128_t avg1 = simd_pow(avg0, gamma);
  413. const simd128_t avg2 = simd_mul(avg1, pack);
  414. const simd128_t ftoi0 = simd_ftoi(avg2);
  415. const simd128_t ftoi1 = simd_and(ftoi0, pmask);
  416. const simd128_t zwxy = simd_swiz_zwxy(ftoi1);
  417. const simd128_t tmp0 = simd_or(ftoi1, zwxy);
  418. const simd128_t yyyy = simd_swiz_yyyy(tmp0);
  419. const simd128_t tmp1 = simd_iadd(yyyy, yyyy);
  420. const simd128_t result = simd_or(tmp0, tmp1);
  421. simd_stx(dst, result);
  422. }
  423. }
  424. }
  425. void imageRgba32fToLinear(void* _dst, uint32_t _width, uint32_t _height, uint32_t _srcPitch, const void* _src)
  426. {
  427. uint8_t* dst = ( uint8_t*)_dst;
  428. const uint8_t* src = (const uint8_t*)_src;
  429. for (uint32_t yy = 0; yy < _height; ++yy, src += _srcPitch, dst += _width*16)
  430. {
  431. for (uint32_t xx = 0; xx < _width; ++xx)
  432. {
  433. const uint32_t offset = xx * 16;
  434. float* fd = ( float*)(dst + offset);
  435. const float* fs = (const float*)(src + offset);
  436. fd[0] = bx::fpow(fs[0], 1.0f/2.2f);
  437. fd[1] = bx::fpow(fs[1], 1.0f/2.2f);
  438. fd[2] = bx::fpow(fs[2], 1.0f/2.2f);
  439. fd[3] = fs[3];
  440. }
  441. }
  442. }
  443. void imageRgba32fToGamma(void* _dst, uint32_t _width, uint32_t _height, uint32_t _srcPitch, const void* _src)
  444. {
  445. uint8_t* dst = ( uint8_t*)_dst;
  446. const uint8_t* src = (const uint8_t*)_src;
  447. for (uint32_t yy = 0; yy < _height; ++yy, src += _srcPitch, dst += _width*16)
  448. {
  449. for (uint32_t xx = 0; xx < _width; ++xx)
  450. {
  451. const uint32_t offset = xx * 16;
  452. float* fd = ( float*)(dst + offset);
  453. const float* fs = (const float*)(src + offset);
  454. fd[0] = bx::fpow(fs[0], 2.2f);
  455. fd[1] = bx::fpow(fs[1], 2.2f);
  456. fd[2] = bx::fpow(fs[2], 2.2f);
  457. fd[3] = fs[3];
  458. }
  459. }
  460. }
  461. void imageRgba32fLinearDownsample2x2Ref(void* _dst, uint32_t _width, uint32_t _height, uint32_t _srcPitch, const void* _src)
  462. {
  463. const uint32_t dstWidth = _width/2;
  464. const uint32_t dstHeight = _height/2;
  465. if (0 == dstWidth
  466. || 0 == dstHeight)
  467. {
  468. return;
  469. }
  470. const uint8_t* src = (const uint8_t*)_src;
  471. uint8_t* dst = (uint8_t*)_dst;
  472. for (uint32_t yy = 0, ystep = _srcPitch*2; yy < dstHeight; ++yy, src += ystep)
  473. {
  474. const float* rgba0 = (const float*)&src[0];
  475. const float* rgba1 = (const float*)&src[_srcPitch];
  476. for (uint32_t xx = 0; xx < dstWidth; ++xx, rgba0 += 8, rgba1 += 8, dst += 16)
  477. {
  478. float xyz[4];
  479. xyz[0] = rgba0[0];
  480. xyz[1] = rgba0[1];
  481. xyz[2] = rgba0[2];
  482. xyz[3] = rgba0[3];
  483. xyz[0] += rgba0[4];
  484. xyz[1] += rgba0[5];
  485. xyz[2] += rgba0[6];
  486. xyz[3] += rgba0[7];
  487. xyz[0] += rgba1[0];
  488. xyz[1] += rgba1[1];
  489. xyz[2] += rgba1[2];
  490. xyz[3] += rgba1[3];
  491. xyz[0] += rgba1[4];
  492. xyz[1] += rgba1[5];
  493. xyz[2] += rgba1[6];
  494. xyz[3] += rgba1[7];
  495. xyz[0] *= 0.25f;
  496. xyz[1] *= 0.25f;
  497. xyz[2] *= 0.25f;
  498. xyz[3] *= 0.25f;
  499. bx::packRgba32F(dst, xyz);
  500. }
  501. }
  502. }
  503. void imageRgba32fLinearDownsample2x2(void* _dst, uint32_t _width, uint32_t _height, uint32_t _srcPitch, const void* _src)
  504. {
  505. imageRgba32fLinearDownsample2x2Ref(_dst, _width, _height, _srcPitch, _src);
  506. }
  507. void imageRgba32fDownsample2x2NormalMapRef(void* _dst, uint32_t _width, uint32_t _height, uint32_t _srcPitch, const void* _src)
  508. {
  509. const uint32_t dstWidth = _width/2;
  510. const uint32_t dstHeight = _height/2;
  511. if (0 == dstWidth
  512. || 0 == dstHeight)
  513. {
  514. return;
  515. }
  516. const uint8_t* src = (const uint8_t*)_src;
  517. uint8_t* dst = (uint8_t*)_dst;
  518. for (uint32_t yy = 0, ystep = _srcPitch*2; yy < dstHeight; ++yy, src += ystep)
  519. {
  520. const float* rgba0 = (const float*)&src[0];
  521. const float* rgba1 = (const float*)&src[_srcPitch];
  522. for (uint32_t xx = 0; xx < dstWidth; ++xx, rgba0 += 8, rgba1 += 8, dst += 16)
  523. {
  524. float xyz[3];
  525. xyz[0] = rgba0[0];
  526. xyz[1] = rgba0[1];
  527. xyz[2] = rgba0[2];
  528. xyz[0] += rgba0[4];
  529. xyz[1] += rgba0[5];
  530. xyz[2] += rgba0[6];
  531. xyz[0] += rgba1[0];
  532. xyz[1] += rgba1[1];
  533. xyz[2] += rgba1[2];
  534. xyz[0] += rgba1[4];
  535. xyz[1] += rgba1[5];
  536. xyz[2] += rgba1[6];
  537. bx::vec3Norm( (float*)dst, xyz);
  538. }
  539. }
  540. }
  541. void imageRgba32fDownsample2x2NormalMap(void* _dst, uint32_t _width, uint32_t _height, uint32_t _srcPitch, const void* _src)
  542. {
  543. imageRgba32fDownsample2x2NormalMapRef(_dst, _width, _height, _srcPitch, _src);
  544. }
  545. void imageSwizzleBgra8Ref(void* _dst, uint32_t _width, uint32_t _height, uint32_t _srcPitch, const void* _src)
  546. {
  547. const uint8_t* src = (uint8_t*) _src;
  548. const uint8_t* next = src + _srcPitch;
  549. uint8_t* dst = (uint8_t*)_dst;
  550. for (uint32_t yy = 0; yy < _height; ++yy, src = next, next += _srcPitch)
  551. {
  552. for (uint32_t xx = 0; xx < _width; ++xx, src += 4, dst += 4)
  553. {
  554. uint8_t rr = src[0];
  555. uint8_t gg = src[1];
  556. uint8_t bb = src[2];
  557. uint8_t aa = src[3];
  558. dst[0] = bb;
  559. dst[1] = gg;
  560. dst[2] = rr;
  561. dst[3] = aa;
  562. }
  563. }
  564. }
  565. void imageSwizzleBgra8(void* _dst, uint32_t _width, uint32_t _height, uint32_t _srcPitch, const void* _src)
  566. {
  567. // Test can we do four 4-byte pixels at the time.
  568. if (0 != (_width&0x3)
  569. || _width < 4
  570. || !bx::isAligned(_src, 16)
  571. || !bx::isAligned(_dst, 16) )
  572. {
  573. BX_WARN(false, "Image swizzle is taking slow path.");
  574. BX_WARN(bx::isAligned(_src, 16), "Source %p is not 16-byte aligned.", _src);
  575. BX_WARN(bx::isAligned(_dst, 16), "Destination %p is not 16-byte aligned.", _dst);
  576. BX_WARN(_width < 4, "Image width must be multiple of 4 (width %d).", _width);
  577. imageSwizzleBgra8Ref(_dst, _width, _height, _srcPitch, _src);
  578. return;
  579. }
  580. using namespace bx;
  581. const simd128_t mf0f0 = simd_isplat(0xff00ff00);
  582. const simd128_t m0f0f = simd_isplat(0x00ff00ff);
  583. const uint8_t* src = (uint8_t*) _src;
  584. const uint8_t* next = src + _srcPitch;
  585. uint8_t* dst = (uint8_t*)_dst;
  586. const uint32_t width = _width/4;
  587. for (uint32_t yy = 0; yy < _height; ++yy, src = next, next += _srcPitch)
  588. {
  589. for (uint32_t xx = 0; xx < width; ++xx, src += 16, dst += 16)
  590. {
  591. const simd128_t tabgr = simd_ld(src);
  592. const simd128_t t00ab = simd_srl(tabgr, 16);
  593. const simd128_t tgr00 = simd_sll(tabgr, 16);
  594. const simd128_t tgrab = simd_or(t00ab, tgr00);
  595. const simd128_t ta0g0 = simd_and(tabgr, mf0f0);
  596. const simd128_t t0r0b = simd_and(tgrab, m0f0f);
  597. const simd128_t targb = simd_or(ta0g0, t0r0b);
  598. simd_st(dst, targb);
  599. }
  600. }
  601. }
  602. void imageCopy(void* _dst, uint32_t _height, uint32_t _srcPitch, const void* _src, uint32_t _dstPitch)
  603. {
  604. const uint32_t pitch = bx::uint32_min(_srcPitch, _dstPitch);
  605. const uint8_t* src = (uint8_t*)_src;
  606. uint8_t* dst = (uint8_t*)_dst;
  607. bx::memCopy(dst, src, pitch, _height, _srcPitch, _dstPitch);
  608. }
  609. void imageCopy(void* _dst, uint32_t _width, uint32_t _height, uint32_t _bpp, uint32_t _srcPitch, const void* _src)
  610. {
  611. const uint32_t dstPitch = _width*_bpp/8;
  612. imageCopy(_dst, _height, _srcPitch, _src, dstPitch);
  613. }
  614. struct PackUnpack
  615. {
  616. PackFn pack;
  617. UnpackFn unpack;
  618. };
  619. static const PackUnpack s_packUnpack[] =
  620. {
  621. { NULL, NULL }, // BC1
  622. { NULL, NULL }, // BC2
  623. { NULL, NULL }, // BC3
  624. { NULL, NULL }, // BC4
  625. { NULL, NULL }, // BC5
  626. { NULL, NULL }, // BC6H
  627. { NULL, NULL }, // BC7
  628. { NULL, NULL }, // ETC1
  629. { NULL, NULL }, // ETC2
  630. { NULL, NULL }, // ETC2A
  631. { NULL, NULL }, // ETC2A1
  632. { NULL, NULL }, // PTC12
  633. { NULL, NULL }, // PTC14
  634. { NULL, NULL }, // PTC12A
  635. { NULL, NULL }, // PTC14A
  636. { NULL, NULL }, // PTC22
  637. { NULL, NULL }, // PTC24
  638. { NULL, NULL }, // Unknown
  639. { NULL, NULL }, // R1
  640. { bx::packR8, bx::unpackR8 }, // A8
  641. { bx::packR8, bx::unpackR8 }, // R8
  642. { bx::packR8I, bx::unpackR8I }, // R8I
  643. { bx::packR8U, bx::unpackR8U }, // R8U
  644. { bx::packR8S, bx::unpackR8S }, // R8S
  645. { bx::packR16, bx::unpackR16 }, // R16
  646. { bx::packR16I, bx::unpackR16I }, // R16I
  647. { bx::packR16U, bx::unpackR16U }, // R16U
  648. { bx::packR16F, bx::unpackR16F }, // R16F
  649. { bx::packR16S, bx::unpackR16S }, // R16S
  650. { bx::packR32I, bx::unpackR32I }, // R32I
  651. { bx::packR32U, bx::unpackR32U }, // R32U
  652. { bx::packR32F, bx::unpackR32F }, // R32F
  653. { bx::packRg8, bx::unpackRg8 }, // RG8
  654. { bx::packRg8I, bx::unpackRg8I }, // RG8I
  655. { bx::packRg8U, bx::unpackRg8U }, // RG8U
  656. { bx::packRg8S, bx::unpackRg8S }, // RG8S
  657. { bx::packRg16, bx::unpackRg16 }, // RG16
  658. { bx::packRg16I, bx::unpackRg16I }, // RG16I
  659. { bx::packRg16U, bx::unpackRg16U }, // RG16U
  660. { bx::packRg16F, bx::unpackRg16F }, // RG16F
  661. { bx::packRg16S, bx::unpackRg16S }, // RG16S
  662. { bx::packRg32I, bx::unpackRg32I }, // RG32I
  663. { bx::packRg32U, bx::unpackRg32U }, // RG32U
  664. { bx::packRg32F, bx::unpackRg32F }, // RG32F
  665. { bx::packRgb8, bx::unpackRgb8 }, // RGB8
  666. { bx::packRgb8S, bx::unpackRgb8S }, // RGB8S
  667. { bx::packRgb8I, bx::unpackRgb8I }, // RGB8I
  668. { bx::packRgb8U, bx::unpackRgb8U }, // RGB8U
  669. { bx::packRgb9E5F, bx::unpackRgb9E5F }, // RGB9E5F
  670. { bx::packBgra8, bx::unpackBgra8 }, // BGRA8
  671. { bx::packRgba8, bx::unpackRgba8 }, // RGBA8
  672. { bx::packRgba8I, bx::unpackRgba8I }, // RGBA8I
  673. { bx::packRgba8U, bx::unpackRgba8U }, // RGBA8U
  674. { bx::packRgba8S, bx::unpackRgba8S }, // RGBA8S
  675. { bx::packRgba16, bx::unpackRgba16 }, // RGBA16
  676. { bx::packRgba16I, bx::unpackRgba16I }, // RGBA16I
  677. { bx::packRgba16U, bx::unpackRgba16U }, // RGBA16U
  678. { bx::packRgba16F, bx::unpackRgba16F }, // RGBA16F
  679. { bx::packRgba16S, bx::unpackRgba16S }, // RGBA16S
  680. { bx::packRgba32I, bx::unpackRgba32I }, // RGBA32I
  681. { bx::packRgba32U, bx::unpackRgba32U }, // RGBA32U
  682. { bx::packRgba32F, bx::unpackRgba32F }, // RGBA32F
  683. { bx::packR5G6B5, bx::unpackR5G6B5 }, // R5G6B5
  684. { bx::packRgba4, bx::unpackRgba4 }, // RGBA4
  685. { bx::packRgb5a1, bx::unpackRgb5a1 }, // RGB5A1
  686. { bx::packRgb10A2, bx::unpackRgb10A2 }, // RGB10A2
  687. { bx::packRG11B10F, bx::unpackRG11B10F }, // RG11B10F
  688. { NULL, NULL }, // UnknownDepth
  689. { bx::packR16, bx::unpackR16 }, // D16
  690. { bx::packR24, bx::unpackR24 }, // D24
  691. { bx::packR24G8, bx::unpackR24G8 }, // D24S8
  692. { NULL, NULL }, // D32
  693. { bx::packR16F, bx::unpackR16F }, // D16F
  694. { NULL, NULL }, // D24F
  695. { bx::packR32F, bx::unpackR32F }, // D32F
  696. { bx::packR8, bx::unpackR8 }, // D0S8
  697. };
  698. BX_STATIC_ASSERT(TextureFormat::Count == BX_COUNTOF(s_packUnpack) );
  699. PackFn getPack(TextureFormat::Enum _format)
  700. {
  701. return s_packUnpack[_format].pack;
  702. }
  703. UnpackFn getUnpack(TextureFormat::Enum _format)
  704. {
  705. return s_packUnpack[_format].unpack;
  706. }
  707. bool imageConvert(TextureFormat::Enum _dstFormat, TextureFormat::Enum _srcFormat)
  708. {
  709. UnpackFn unpack = s_packUnpack[_srcFormat].unpack;
  710. PackFn pack = s_packUnpack[_dstFormat].pack;
  711. return NULL != pack
  712. && NULL != unpack
  713. ;
  714. }
  715. void imageConvert(void* _dst, uint32_t _bpp, PackFn _pack, const void* _src, UnpackFn _unpack, uint32_t _size)
  716. {
  717. const uint8_t* src = (uint8_t*)_src;
  718. uint8_t* dst = (uint8_t*)_dst;
  719. const uint32_t size = _size * 8 / _bpp;
  720. for (uint32_t ii = 0; ii < size; ++ii)
  721. {
  722. float rgba[4];
  723. _unpack(rgba, &src[ii*_bpp/8]);
  724. _pack(&dst[ii*_bpp/8], rgba);
  725. }
  726. }
  727. void imageConvert(void* _dst, uint32_t _dstBpp, PackFn _pack, const void* _src, uint32_t _srcBpp, UnpackFn _unpack, uint32_t _width, uint32_t _height, uint32_t _srcPitch)
  728. {
  729. const uint8_t* src = (uint8_t*)_src;
  730. uint8_t* dst = (uint8_t*)_dst;
  731. const uint32_t dstPitch = _width * _dstBpp / 8;
  732. for (uint32_t yy = 0; yy < _height; ++yy, src += _srcPitch, dst += dstPitch)
  733. {
  734. for (uint32_t xx = 0; xx < _width; ++xx)
  735. {
  736. float rgba[4];
  737. _unpack(rgba, &src[xx*_srcBpp/8]);
  738. _pack(&dst[xx*_dstBpp/8], rgba);
  739. }
  740. }
  741. }
  742. bool imageConvert(void* _dst, TextureFormat::Enum _dstFormat, const void* _src, TextureFormat::Enum _srcFormat, uint32_t _width, uint32_t _height, uint32_t _srcPitch)
  743. {
  744. UnpackFn unpack = s_packUnpack[_srcFormat].unpack;
  745. PackFn pack = s_packUnpack[_dstFormat].pack;
  746. if (NULL == pack
  747. || NULL == unpack)
  748. {
  749. return false;
  750. }
  751. const uint32_t srcBpp = s_imageBlockInfo[_srcFormat].bitsPerPixel;
  752. const uint32_t dstBpp = s_imageBlockInfo[_dstFormat].bitsPerPixel;
  753. imageConvert(_dst, dstBpp, pack, _src, srcBpp, unpack, _width, _height, _srcPitch);
  754. return true;
  755. }
  756. bool imageConvert(void* _dst, TextureFormat::Enum _dstFormat, const void* _src, TextureFormat::Enum _srcFormat, uint32_t _width, uint32_t _height)
  757. {
  758. const uint32_t srcBpp = s_imageBlockInfo[_srcFormat].bitsPerPixel;
  759. if (_dstFormat == _srcFormat)
  760. {
  761. bx::memCopy(_dst, _src, _width*_height*srcBpp/8);
  762. return true;
  763. }
  764. return imageConvert(_dst, _dstFormat, _src, _srcFormat, _width, _height, _width*srcBpp/8);
  765. }
  766. ImageContainer* imageConvert(bx::AllocatorI* _allocator, TextureFormat::Enum _dstFormat, const ImageContainer& _input)
  767. {
  768. ImageContainer* output = imageAlloc(_allocator
  769. , _dstFormat
  770. , uint16_t(_input.m_width)
  771. , uint16_t(_input.m_height)
  772. , uint16_t(_input.m_depth)
  773. , _input.m_numLayers
  774. , _input.m_cubeMap
  775. , 1 < _input.m_numMips
  776. );
  777. const uint16_t numSides = _input.m_numLayers * (_input.m_cubeMap ? 6 : 1);
  778. for (uint16_t side = 0; side < numSides; ++side)
  779. {
  780. for (uint8_t lod = 0, num = _input.m_numMips; lod < num; ++lod)
  781. {
  782. ImageMip mip;
  783. if (imageGetRawData(_input, side, lod, _input.m_data, _input.m_size, mip) )
  784. {
  785. ImageMip dstMip;
  786. imageGetRawData(*output, side, lod, output->m_data, output->m_size, dstMip);
  787. uint8_t* dstData = const_cast<uint8_t*>(dstMip.m_data);
  788. bool ok = imageConvert(dstData
  789. , _dstFormat
  790. , mip.m_data
  791. , mip.m_format
  792. , mip.m_width
  793. , mip.m_height
  794. );
  795. BX_CHECK(ok, "Conversion from %s to %s failed!"
  796. , getName(_input.m_format)
  797. , getName(output->m_format)
  798. );
  799. BX_UNUSED(ok);
  800. }
  801. }
  802. }
  803. return output;
  804. }
  805. typedef bool (*ParseFn)(ImageContainer&, bx::ReaderSeekerI*, bx::Error*);
  806. template<uint32_t magicT, ParseFn parseFnT>
  807. ImageContainer* imageParseT(bx::AllocatorI* _allocator, const void* _src, uint32_t _size, bx::Error* _err)
  808. {
  809. bx::MemoryReader reader(_src, _size);
  810. uint32_t magic;
  811. bx::read(&reader, magic);
  812. ImageContainer imageContainer;
  813. if (magicT != magic
  814. || !parseFnT(imageContainer, &reader, _err) )
  815. {
  816. return NULL;
  817. }
  818. ImageContainer* output = imageAlloc(_allocator
  819. , imageContainer.m_format
  820. , uint16_t(imageContainer.m_width)
  821. , uint16_t(imageContainer.m_height)
  822. , uint16_t(imageContainer.m_depth)
  823. , imageContainer.m_numLayers
  824. , imageContainer.m_cubeMap
  825. , 1 < imageContainer.m_numMips
  826. );
  827. const uint16_t numSides = imageContainer.m_numLayers * (imageContainer.m_cubeMap ? 6 : 1);
  828. for (uint16_t side = 0; side < numSides; ++side)
  829. {
  830. for (uint8_t lod = 0, num = imageContainer.m_numMips; lod < num; ++lod)
  831. {
  832. ImageMip dstMip;
  833. if (imageGetRawData(*output, side, lod, output->m_data, output->m_size, dstMip) )
  834. {
  835. ImageMip mip;
  836. if (imageGetRawData(imageContainer, side, lod, _src, _size, mip) )
  837. {
  838. uint8_t* dstData = const_cast<uint8_t*>(dstMip.m_data);
  839. bx::memCopy(dstData, mip.m_data, mip.m_size);
  840. }
  841. }
  842. }
  843. }
  844. return output;
  845. }
  846. uint8_t bitRangeConvert(uint32_t _in, uint32_t _from, uint32_t _to)
  847. {
  848. using namespace bx;
  849. uint32_t tmp0 = uint32_sll(1, _to);
  850. uint32_t tmp1 = uint32_sll(1, _from);
  851. uint32_t tmp2 = uint32_dec(tmp0);
  852. uint32_t tmp3 = uint32_dec(tmp1);
  853. uint32_t tmp4 = uint32_mul(_in, tmp2);
  854. uint32_t tmp5 = uint32_add(tmp3, tmp4);
  855. uint32_t tmp6 = uint32_srl(tmp5, _from);
  856. uint32_t tmp7 = uint32_add(tmp5, tmp6);
  857. uint32_t result = uint32_srl(tmp7, _from);
  858. return uint8_t(result);
  859. }
  860. void decodeBlockDxt(uint8_t _dst[16*4], const uint8_t _src[8])
  861. {
  862. uint8_t colors[4*3];
  863. uint32_t c0 = _src[0] | (_src[1] << 8);
  864. colors[0] = bitRangeConvert( (c0>> 0)&0x1f, 5, 8);
  865. colors[1] = bitRangeConvert( (c0>> 5)&0x3f, 6, 8);
  866. colors[2] = bitRangeConvert( (c0>>11)&0x1f, 5, 8);
  867. uint32_t c1 = _src[2] | (_src[3] << 8);
  868. colors[3] = bitRangeConvert( (c1>> 0)&0x1f, 5, 8);
  869. colors[4] = bitRangeConvert( (c1>> 5)&0x3f, 6, 8);
  870. colors[5] = bitRangeConvert( (c1>>11)&0x1f, 5, 8);
  871. colors[6] = (2*colors[0] + colors[3]) / 3;
  872. colors[7] = (2*colors[1] + colors[4]) / 3;
  873. colors[8] = (2*colors[2] + colors[5]) / 3;
  874. colors[ 9] = (colors[0] + 2*colors[3]) / 3;
  875. colors[10] = (colors[1] + 2*colors[4]) / 3;
  876. colors[11] = (colors[2] + 2*colors[5]) / 3;
  877. for (uint32_t ii = 0, next = 8*4; ii < 16*4; ii += 4, next += 2)
  878. {
  879. int idx = ( (_src[next>>3] >> (next & 7) ) & 3) * 3;
  880. _dst[ii+0] = colors[idx+0];
  881. _dst[ii+1] = colors[idx+1];
  882. _dst[ii+2] = colors[idx+2];
  883. }
  884. }
  885. void decodeBlockDxt1(uint8_t _dst[16*4], const uint8_t _src[8])
  886. {
  887. uint8_t colors[4*4];
  888. uint32_t c0 = _src[0] | (_src[1] << 8);
  889. colors[0] = bitRangeConvert( (c0>> 0)&0x1f, 5, 8);
  890. colors[1] = bitRangeConvert( (c0>> 5)&0x3f, 6, 8);
  891. colors[2] = bitRangeConvert( (c0>>11)&0x1f, 5, 8);
  892. colors[3] = 255;
  893. uint32_t c1 = _src[2] | (_src[3] << 8);
  894. colors[4] = bitRangeConvert( (c1>> 0)&0x1f, 5, 8);
  895. colors[5] = bitRangeConvert( (c1>> 5)&0x3f, 6, 8);
  896. colors[6] = bitRangeConvert( (c1>>11)&0x1f, 5, 8);
  897. colors[7] = 255;
  898. if (c0 > c1)
  899. {
  900. colors[ 8] = (2*colors[0] + colors[4]) / 3;
  901. colors[ 9] = (2*colors[1] + colors[5]) / 3;
  902. colors[10] = (2*colors[2] + colors[6]) / 3;
  903. colors[11] = 255;
  904. colors[12] = (colors[0] + 2*colors[4]) / 3;
  905. colors[13] = (colors[1] + 2*colors[5]) / 3;
  906. colors[14] = (colors[2] + 2*colors[6]) / 3;
  907. colors[15] = 255;
  908. }
  909. else
  910. {
  911. colors[ 8] = (colors[0] + colors[4]) / 2;
  912. colors[ 9] = (colors[1] + colors[5]) / 2;
  913. colors[10] = (colors[2] + colors[6]) / 2;
  914. colors[11] = 255;
  915. colors[12] = 0;
  916. colors[13] = 0;
  917. colors[14] = 0;
  918. colors[15] = 0;
  919. }
  920. for (uint32_t ii = 0, next = 8*4; ii < 16*4; ii += 4, next += 2)
  921. {
  922. int idx = ( (_src[next>>3] >> (next & 7) ) & 3) * 4;
  923. _dst[ii+0] = colors[idx+0];
  924. _dst[ii+1] = colors[idx+1];
  925. _dst[ii+2] = colors[idx+2];
  926. _dst[ii+3] = colors[idx+3];
  927. }
  928. }
  929. void decodeBlockDxt23A(uint8_t _dst[16*4], const uint8_t _src[8])
  930. {
  931. for (uint32_t ii = 0, next = 0; ii < 16*4; ii += 4, next += 4)
  932. {
  933. uint32_t c0 = (_src[next>>3] >> (next&7) ) & 0xf;
  934. _dst[ii] = bitRangeConvert(c0, 4, 8);
  935. }
  936. }
  937. void decodeBlockDxt45A(uint8_t _dst[16*4], const uint8_t _src[8])
  938. {
  939. uint8_t alpha[8];
  940. alpha[0] = _src[0];
  941. alpha[1] = _src[1];
  942. if (alpha[0] > alpha[1])
  943. {
  944. alpha[2] = (6*alpha[0] + 1*alpha[1]) / 7;
  945. alpha[3] = (5*alpha[0] + 2*alpha[1]) / 7;
  946. alpha[4] = (4*alpha[0] + 3*alpha[1]) / 7;
  947. alpha[5] = (3*alpha[0] + 4*alpha[1]) / 7;
  948. alpha[6] = (2*alpha[0] + 5*alpha[1]) / 7;
  949. alpha[7] = (1*alpha[0] + 6*alpha[1]) / 7;
  950. }
  951. else
  952. {
  953. alpha[2] = (4*alpha[0] + 1*alpha[1]) / 5;
  954. alpha[3] = (3*alpha[0] + 2*alpha[1]) / 5;
  955. alpha[4] = (2*alpha[0] + 3*alpha[1]) / 5;
  956. alpha[5] = (1*alpha[0] + 4*alpha[1]) / 5;
  957. alpha[6] = 0;
  958. alpha[7] = 255;
  959. }
  960. uint32_t idx0 = _src[2];
  961. uint32_t idx1 = _src[5];
  962. idx0 |= uint32_t(_src[3])<<8;
  963. idx1 |= uint32_t(_src[6])<<8;
  964. idx0 |= uint32_t(_src[4])<<16;
  965. idx1 |= uint32_t(_src[7])<<16;
  966. for (uint32_t ii = 0; ii < 8*4; ii += 4)
  967. {
  968. _dst[ii] = alpha[idx0&7];
  969. _dst[ii+32] = alpha[idx1&7];
  970. idx0 >>= 3;
  971. idx1 >>= 3;
  972. }
  973. }
  974. static const int32_t s_etc1Mod[8][4] =
  975. {
  976. { 2, 8, -2, -8},
  977. { 5, 17, -5, -17},
  978. { 9, 29, -9, -29},
  979. { 13, 42, -13, -42},
  980. { 18, 60, -18, -60},
  981. { 24, 80, -24, -80},
  982. { 33, 106, -33, -106},
  983. { 47, 183, -47, -183},
  984. };
  985. static const uint8_t s_etc2Mod[8] = { 3, 6, 11, 16, 23, 32, 41, 64 };
  986. uint8_t uint8_sat(int32_t _a)
  987. {
  988. using namespace bx;
  989. const uint32_t min = uint32_imin(_a, 255);
  990. const uint32_t result = uint32_imax(min, 0);
  991. return (uint8_t)result;
  992. }
  993. uint8_t uint8_satadd(int32_t _a, int32_t _b)
  994. {
  995. const int32_t add = _a + _b;
  996. return uint8_sat(add);
  997. }
  998. void decodeBlockEtc2ModeT(uint8_t _dst[16*4], const uint8_t _src[8])
  999. {
  1000. uint8_t rgb[16];
  1001. // 0 1 2 3 4 5 6 7
  1002. // 7654321076543210765432107654321076543210765432107654321076543210
  1003. // ...rr.rrggggbbbbrrrrggggbbbbDDD.mmmmmmmmmmmmmmmmllllllllllllllll
  1004. // ^ ^ ^ ^ ^
  1005. // +-- c0 +-- c1 | +-- msb +-- lsb
  1006. // +-- dist
  1007. rgb[ 0] = ( (_src[0] >> 1) & 0xc)
  1008. | (_src[0] & 0x3)
  1009. ;
  1010. rgb[ 1] = _src[1] >> 4;
  1011. rgb[ 2] = _src[1] & 0xf;
  1012. rgb[ 8] = _src[2] >> 4;
  1013. rgb[ 9] = _src[2] & 0xf;
  1014. rgb[10] = _src[3] >> 4;
  1015. rgb[ 0] = bitRangeConvert(rgb[ 0], 4, 8);
  1016. rgb[ 1] = bitRangeConvert(rgb[ 1], 4, 8);
  1017. rgb[ 2] = bitRangeConvert(rgb[ 2], 4, 8);
  1018. rgb[ 8] = bitRangeConvert(rgb[ 8], 4, 8);
  1019. rgb[ 9] = bitRangeConvert(rgb[ 9], 4, 8);
  1020. rgb[10] = bitRangeConvert(rgb[10], 4, 8);
  1021. uint8_t dist = (_src[3] >> 1) & 0x7;
  1022. int32_t mod = s_etc2Mod[dist];
  1023. rgb[ 4] = uint8_satadd(rgb[ 8], mod);
  1024. rgb[ 5] = uint8_satadd(rgb[ 9], mod);
  1025. rgb[ 6] = uint8_satadd(rgb[10], mod);
  1026. rgb[12] = uint8_satadd(rgb[ 8], -mod);
  1027. rgb[13] = uint8_satadd(rgb[ 9], -mod);
  1028. rgb[14] = uint8_satadd(rgb[10], -mod);
  1029. uint32_t indexMsb = (_src[4]<<8) | _src[5];
  1030. uint32_t indexLsb = (_src[6]<<8) | _src[7];
  1031. for (uint32_t ii = 0; ii < 16; ++ii)
  1032. {
  1033. const uint32_t idx = (ii&0xc) | ( (ii & 0x3)<<4);
  1034. const uint32_t lsbi = indexLsb & 1;
  1035. const uint32_t msbi = (indexMsb & 1)<<1;
  1036. const uint32_t pal = (lsbi | msbi)<<2;
  1037. _dst[idx + 0] = rgb[pal+2];
  1038. _dst[idx + 1] = rgb[pal+1];
  1039. _dst[idx + 2] = rgb[pal+0];
  1040. _dst[idx + 3] = 255;
  1041. indexLsb >>= 1;
  1042. indexMsb >>= 1;
  1043. }
  1044. }
  1045. void decodeBlockEtc2ModeH(uint8_t _dst[16*4], const uint8_t _src[8])
  1046. {
  1047. uint8_t rgb[16];
  1048. // 0 1 2 3 4 5 6 7
  1049. // 7654321076543210765432107654321076543210765432107654321076543210
  1050. // .rrrrggg...gb.bbbrrrrggggbbbbDD.mmmmmmmmmmmmmmmmllllllllllllllll
  1051. // ^ ^ ^ ^ ^
  1052. // +-- c0 +-- c1 | +-- msb +-- lsb
  1053. // +-- dist
  1054. rgb[ 0] = (_src[0] >> 3) & 0xf;
  1055. rgb[ 1] = ( (_src[0] << 1) & 0xe)
  1056. | ( (_src[1] >> 4) & 0x1)
  1057. ;
  1058. rgb[ 2] = (_src[1] & 0x8)
  1059. | ( (_src[1] << 1) & 0x6)
  1060. | (_src[2] >> 7)
  1061. ;
  1062. rgb[ 8] = (_src[2] >> 3) & 0xf;
  1063. rgb[ 9] = ( (_src[2] << 1) & 0xe)
  1064. | (_src[3] >> 7)
  1065. ;
  1066. rgb[10] = (_src[2] >> 3) & 0xf;
  1067. rgb[ 0] = bitRangeConvert(rgb[ 0], 4, 8);
  1068. rgb[ 1] = bitRangeConvert(rgb[ 1], 4, 8);
  1069. rgb[ 2] = bitRangeConvert(rgb[ 2], 4, 8);
  1070. rgb[ 8] = bitRangeConvert(rgb[ 8], 4, 8);
  1071. rgb[ 9] = bitRangeConvert(rgb[ 9], 4, 8);
  1072. rgb[10] = bitRangeConvert(rgb[10], 4, 8);
  1073. uint32_t col0 = uint32_t(rgb[0]<<16) | uint32_t(rgb[1]<<8) | uint32_t(rgb[ 2]);
  1074. uint32_t col1 = uint32_t(rgb[8]<<16) | uint32_t(rgb[9]<<8) | uint32_t(rgb[10]);
  1075. uint8_t dist = (_src[3] & 0x6) | (col0 >= col1);
  1076. int32_t mod = s_etc2Mod[dist];
  1077. rgb[ 4] = uint8_satadd(rgb[ 0], -mod);
  1078. rgb[ 5] = uint8_satadd(rgb[ 1], -mod);
  1079. rgb[ 6] = uint8_satadd(rgb[ 2], -mod);
  1080. rgb[ 0] = uint8_satadd(rgb[ 0], mod);
  1081. rgb[ 1] = uint8_satadd(rgb[ 1], mod);
  1082. rgb[ 2] = uint8_satadd(rgb[ 2], mod);
  1083. rgb[12] = uint8_satadd(rgb[ 8], -mod);
  1084. rgb[13] = uint8_satadd(rgb[ 9], -mod);
  1085. rgb[14] = uint8_satadd(rgb[10], -mod);
  1086. rgb[ 8] = uint8_satadd(rgb[ 8], mod);
  1087. rgb[ 9] = uint8_satadd(rgb[ 9], mod);
  1088. rgb[10] = uint8_satadd(rgb[10], mod);
  1089. uint32_t indexMsb = (_src[4]<<8) | _src[5];
  1090. uint32_t indexLsb = (_src[6]<<8) | _src[7];
  1091. for (uint32_t ii = 0; ii < 16; ++ii)
  1092. {
  1093. const uint32_t idx = (ii&0xc) | ( (ii & 0x3)<<4);
  1094. const uint32_t lsbi = indexLsb & 1;
  1095. const uint32_t msbi = (indexMsb & 1)<<1;
  1096. const uint32_t pal = (lsbi | msbi)<<2;
  1097. _dst[idx + 0] = rgb[pal+2];
  1098. _dst[idx + 1] = rgb[pal+1];
  1099. _dst[idx + 2] = rgb[pal+0];
  1100. _dst[idx + 3] = 255;
  1101. indexLsb >>= 1;
  1102. indexMsb >>= 1;
  1103. }
  1104. }
  1105. void decodeBlockEtc2ModePlanar(uint8_t _dst[16*4], const uint8_t _src[8])
  1106. {
  1107. // 0 1 2 3 4 5 6 7
  1108. // 7654321076543210765432107654321076543210765432107654321076543210
  1109. // .rrrrrrg.ggggggb...bb.bbbrrrrr.rgggggggbbbbbbrrrrrrgggggggbbbbbb
  1110. // ^ ^ ^
  1111. // +-- c0 +-- cH +-- cV
  1112. uint8_t c0[3];
  1113. uint8_t cH[3];
  1114. uint8_t cV[3];
  1115. c0[0] = (_src[0] >> 1) & 0x3f;
  1116. c0[1] = ( (_src[0] & 1) << 6)
  1117. | ( (_src[1] >> 1) & 0x3f)
  1118. ;
  1119. c0[2] = ( (_src[1] & 1) << 5)
  1120. | ( (_src[2] & 0x18) )
  1121. | ( (_src[2] << 1) & 6)
  1122. | ( (_src[3] >> 7) )
  1123. ;
  1124. cH[0] = ( (_src[3] >> 1) & 0x3e)
  1125. | (_src[3] & 1)
  1126. ;
  1127. cH[1] = _src[4] >> 1;
  1128. cH[2] = ( (_src[4] & 1) << 5)
  1129. | (_src[5] >> 3)
  1130. ;
  1131. cV[0] = ( (_src[5] & 0x7) << 3)
  1132. | (_src[6] >> 5)
  1133. ;
  1134. cV[1] = ( (_src[6] & 0x1f) << 2)
  1135. | (_src[7] >> 5)
  1136. ;
  1137. cV[2] = _src[7] & 0x3f;
  1138. c0[0] = bitRangeConvert(c0[0], 6, 8);
  1139. c0[1] = bitRangeConvert(c0[1], 7, 8);
  1140. c0[2] = bitRangeConvert(c0[2], 6, 8);
  1141. cH[0] = bitRangeConvert(cH[0], 6, 8);
  1142. cH[1] = bitRangeConvert(cH[1], 7, 8);
  1143. cH[2] = bitRangeConvert(cH[2], 6, 8);
  1144. cV[0] = bitRangeConvert(cV[0], 6, 8);
  1145. cV[1] = bitRangeConvert(cV[1], 7, 8);
  1146. cV[2] = bitRangeConvert(cV[2], 6, 8);
  1147. int16_t dy[3];
  1148. dy[0] = cV[0] - c0[0];
  1149. dy[1] = cV[1] - c0[1];
  1150. dy[2] = cV[2] - c0[2];
  1151. int16_t sx[3];
  1152. sx[0] = int16_t(c0[0])<<2;
  1153. sx[1] = int16_t(c0[1])<<2;
  1154. sx[2] = int16_t(c0[2])<<2;
  1155. int16_t ex[3];
  1156. ex[0] = int16_t(cH[0])<<2;
  1157. ex[1] = int16_t(cH[1])<<2;
  1158. ex[2] = int16_t(cH[2])<<2;
  1159. for (int32_t vv = 0; vv < 4; ++vv)
  1160. {
  1161. int16_t dx[3];
  1162. dx[0] = (ex[0] - sx[0])>>2;
  1163. dx[1] = (ex[1] - sx[1])>>2;
  1164. dx[2] = (ex[2] - sx[2])>>2;
  1165. for (int32_t hh = 0; hh < 4; ++hh)
  1166. {
  1167. const uint32_t idx = (vv<<4) + (hh<<2);
  1168. _dst[idx + 0] = uint8_sat( (sx[2] + dx[2]*hh)>>2);
  1169. _dst[idx + 1] = uint8_sat( (sx[1] + dx[1]*hh)>>2);
  1170. _dst[idx + 2] = uint8_sat( (sx[0] + dx[0]*hh)>>2);
  1171. _dst[idx + 3] = 255;
  1172. }
  1173. sx[0] += dy[0];
  1174. sx[1] += dy[1];
  1175. sx[2] += dy[2];
  1176. ex[0] += dy[0];
  1177. ex[1] += dy[1];
  1178. ex[2] += dy[2];
  1179. }
  1180. }
  1181. void decodeBlockEtc12(uint8_t _dst[16*4], const uint8_t _src[8])
  1182. {
  1183. bool flipBit = 0 != (_src[3] & 0x1);
  1184. bool diffBit = 0 != (_src[3] & 0x2);
  1185. uint8_t rgb[8];
  1186. if (diffBit)
  1187. {
  1188. rgb[0] = _src[0] >> 3;
  1189. rgb[1] = _src[1] >> 3;
  1190. rgb[2] = _src[2] >> 3;
  1191. int8_t diff[3];
  1192. diff[0] = int8_t( (_src[0] & 0x7)<<5)>>5;
  1193. diff[1] = int8_t( (_src[1] & 0x7)<<5)>>5;
  1194. diff[2] = int8_t( (_src[2] & 0x7)<<5)>>5;
  1195. int8_t rr = rgb[0] + diff[0];
  1196. int8_t gg = rgb[1] + diff[1];
  1197. int8_t bb = rgb[2] + diff[2];
  1198. // Etc2 3-modes
  1199. if (rr < 0 || rr > 31)
  1200. {
  1201. decodeBlockEtc2ModeT(_dst, _src);
  1202. return;
  1203. }
  1204. if (gg < 0 || gg > 31)
  1205. {
  1206. decodeBlockEtc2ModeH(_dst, _src);
  1207. return;
  1208. }
  1209. if (bb < 0 || bb > 31)
  1210. {
  1211. decodeBlockEtc2ModePlanar(_dst, _src);
  1212. return;
  1213. }
  1214. // Etc1
  1215. rgb[0] = bitRangeConvert(rgb[0], 5, 8);
  1216. rgb[1] = bitRangeConvert(rgb[1], 5, 8);
  1217. rgb[2] = bitRangeConvert(rgb[2], 5, 8);
  1218. rgb[4] = bitRangeConvert(rr, 5, 8);
  1219. rgb[5] = bitRangeConvert(gg, 5, 8);
  1220. rgb[6] = bitRangeConvert(bb, 5, 8);
  1221. }
  1222. else
  1223. {
  1224. rgb[0] = _src[0] >> 4;
  1225. rgb[1] = _src[1] >> 4;
  1226. rgb[2] = _src[2] >> 4;
  1227. rgb[4] = _src[0] & 0xf;
  1228. rgb[5] = _src[1] & 0xf;
  1229. rgb[6] = _src[2] & 0xf;
  1230. rgb[0] = bitRangeConvert(rgb[0], 4, 8);
  1231. rgb[1] = bitRangeConvert(rgb[1], 4, 8);
  1232. rgb[2] = bitRangeConvert(rgb[2], 4, 8);
  1233. rgb[4] = bitRangeConvert(rgb[4], 4, 8);
  1234. rgb[5] = bitRangeConvert(rgb[5], 4, 8);
  1235. rgb[6] = bitRangeConvert(rgb[6], 4, 8);
  1236. }
  1237. uint32_t table[2];
  1238. table[0] = (_src[3] >> 5) & 0x7;
  1239. table[1] = (_src[3] >> 2) & 0x7;
  1240. uint32_t indexMsb = (_src[4]<<8) | _src[5];
  1241. uint32_t indexLsb = (_src[6]<<8) | _src[7];
  1242. if (flipBit)
  1243. {
  1244. for (uint32_t ii = 0; ii < 16; ++ii)
  1245. {
  1246. const uint32_t block = (ii>>1)&1;
  1247. const uint32_t color = block<<2;
  1248. const uint32_t idx = (ii&0xc) | ( (ii & 0x3)<<4);
  1249. const uint32_t lsbi = indexLsb & 1;
  1250. const uint32_t msbi = (indexMsb & 1)<<1;
  1251. const int32_t mod = s_etc1Mod[table[block] ][lsbi | msbi];
  1252. _dst[idx + 0] = uint8_satadd(rgb[color+2], mod);
  1253. _dst[idx + 1] = uint8_satadd(rgb[color+1], mod);
  1254. _dst[idx + 2] = uint8_satadd(rgb[color+0], mod);
  1255. _dst[idx + 3] = 255;
  1256. indexLsb >>= 1;
  1257. indexMsb >>= 1;
  1258. }
  1259. }
  1260. else
  1261. {
  1262. for (uint32_t ii = 0; ii < 16; ++ii)
  1263. {
  1264. const uint32_t block = ii>>3;
  1265. const uint32_t color = block<<2;
  1266. const uint32_t idx = (ii&0xc) | ( (ii & 0x3)<<4);
  1267. const uint32_t lsbi = indexLsb & 1;
  1268. const uint32_t msbi = (indexMsb & 1)<<1;
  1269. const int32_t mod = s_etc1Mod[table[block] ][lsbi | msbi];
  1270. _dst[idx + 0] = uint8_satadd(rgb[color+2], mod);
  1271. _dst[idx + 1] = uint8_satadd(rgb[color+1], mod);
  1272. _dst[idx + 2] = uint8_satadd(rgb[color+0], mod);
  1273. _dst[idx + 3] = 255;
  1274. indexLsb >>= 1;
  1275. indexMsb >>= 1;
  1276. }
  1277. }
  1278. }
  1279. static const uint8_t s_pvrtcFactors[16][4] =
  1280. {
  1281. { 4, 4, 4, 4 },
  1282. { 2, 6, 2, 6 },
  1283. { 8, 0, 8, 0 },
  1284. { 6, 2, 6, 2 },
  1285. { 2, 2, 6, 6 },
  1286. { 1, 3, 3, 9 },
  1287. { 4, 0, 12, 0 },
  1288. { 3, 1, 9, 3 },
  1289. { 8, 8, 0, 0 },
  1290. { 4, 12, 0, 0 },
  1291. { 16, 0, 0, 0 },
  1292. { 12, 4, 0, 0 },
  1293. { 6, 6, 2, 2 },
  1294. { 3, 9, 1, 3 },
  1295. { 12, 0, 4, 0 },
  1296. { 9, 3, 3, 1 },
  1297. };
  1298. static const uint8_t s_pvrtcWeights[8][4] =
  1299. {
  1300. { 8, 0, 8, 0 },
  1301. { 5, 3, 5, 3 },
  1302. { 3, 5, 3, 5 },
  1303. { 0, 8, 0, 8 },
  1304. { 8, 0, 8, 0 },
  1305. { 4, 4, 4, 4 },
  1306. { 4, 4, 4, 4 },
  1307. { 0, 8, 0, 8 },
  1308. };
  1309. uint32_t morton2d(uint32_t _x, uint32_t _y)
  1310. {
  1311. using namespace bx;
  1312. const uint32_t tmpx = uint32_part1by1(_x);
  1313. const uint32_t xbits = uint32_sll(tmpx, 1);
  1314. const uint32_t ybits = uint32_part1by1(_y);
  1315. const uint32_t result = uint32_or(xbits, ybits);
  1316. return result;
  1317. }
  1318. uint32_t getColor(const uint8_t _src[8])
  1319. {
  1320. return 0
  1321. | _src[7]<<24
  1322. | _src[6]<<16
  1323. | _src[5]<<8
  1324. | _src[4]
  1325. ;
  1326. }
  1327. void decodeBlockPtc14RgbAddA(uint32_t _block, uint32_t* _r, uint32_t* _g, uint32_t* _b, uint8_t _factor)
  1328. {
  1329. if (0 != (_block & (1<<15) ) )
  1330. {
  1331. *_r += bitRangeConvert( (_block >> 10) & 0x1f, 5, 8) * _factor;
  1332. *_g += bitRangeConvert( (_block >> 5) & 0x1f, 5, 8) * _factor;
  1333. *_b += bitRangeConvert( (_block >> 1) & 0x0f, 4, 8) * _factor;
  1334. }
  1335. else
  1336. {
  1337. *_r += bitRangeConvert( (_block >> 8) & 0xf, 4, 8) * _factor;
  1338. *_g += bitRangeConvert( (_block >> 4) & 0xf, 4, 8) * _factor;
  1339. *_b += bitRangeConvert( (_block >> 1) & 0x7, 3, 8) * _factor;
  1340. }
  1341. }
  1342. void decodeBlockPtc14RgbAddB(uint32_t _block, uint32_t* _r, uint32_t* _g, uint32_t* _b, uint8_t _factor)
  1343. {
  1344. if (0 != (_block & (1<<31) ) )
  1345. {
  1346. *_r += bitRangeConvert( (_block >> 26) & 0x1f, 5, 8) * _factor;
  1347. *_g += bitRangeConvert( (_block >> 21) & 0x1f, 5, 8) * _factor;
  1348. *_b += bitRangeConvert( (_block >> 16) & 0x1f, 5, 8) * _factor;
  1349. }
  1350. else
  1351. {
  1352. *_r += bitRangeConvert( (_block >> 24) & 0xf, 4, 8) * _factor;
  1353. *_g += bitRangeConvert( (_block >> 20) & 0xf, 4, 8) * _factor;
  1354. *_b += bitRangeConvert( (_block >> 16) & 0xf, 4, 8) * _factor;
  1355. }
  1356. }
  1357. void decodeBlockPtc14(uint8_t _dst[16*4], const uint8_t* _src, uint32_t _x, uint32_t _y, uint32_t _width, uint32_t _height)
  1358. {
  1359. // 0 1 2 3 4 5 6 7
  1360. // 7654321076543210765432107654321076543210765432107654321076543210
  1361. // mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmyrrrrrgggggbbbbbxrrrrrgggggbbbbp
  1362. // ^ ^^ ^^ ^
  1363. // +-- modulation data |+- B color |+- A color |
  1364. // +-- B opaque +-- A opaque |
  1365. // alpha punchthrough --+
  1366. const uint8_t* bc = &_src[morton2d(_x, _y) * 8];
  1367. uint32_t mod = 0
  1368. | bc[3]<<24
  1369. | bc[2]<<16
  1370. | bc[1]<<8
  1371. | bc[0]
  1372. ;
  1373. const bool punchthrough = !!(bc[7] & 1);
  1374. const uint8_t* weightTable = s_pvrtcWeights[4 * punchthrough];
  1375. const uint8_t* factorTable = s_pvrtcFactors[0];
  1376. for (int yy = 0; yy < 4; ++yy)
  1377. {
  1378. const uint32_t yOffset = (yy < 2) ? -1 : 0;
  1379. const uint32_t y0 = (_y + yOffset) % _height;
  1380. const uint32_t y1 = (y0 + 1) % _height;
  1381. for (int xx = 0; xx < 4; ++xx)
  1382. {
  1383. const uint32_t xOffset = (xx < 2) ? -1 : 0;
  1384. const uint32_t x0 = (_x + xOffset) % _width;
  1385. const uint32_t x1 = (x0 + 1) % _width;
  1386. const uint32_t bc0 = getColor(&_src[morton2d(x0, y0) * 8]);
  1387. const uint32_t bc1 = getColor(&_src[morton2d(x1, y0) * 8]);
  1388. const uint32_t bc2 = getColor(&_src[morton2d(x0, y1) * 8]);
  1389. const uint32_t bc3 = getColor(&_src[morton2d(x1, y1) * 8]);
  1390. const uint8_t f0 = factorTable[0];
  1391. const uint8_t f1 = factorTable[1];
  1392. const uint8_t f2 = factorTable[2];
  1393. const uint8_t f3 = factorTable[3];
  1394. uint32_t ar = 0, ag = 0, ab = 0;
  1395. decodeBlockPtc14RgbAddA(bc0, &ar, &ag, &ab, f0);
  1396. decodeBlockPtc14RgbAddA(bc1, &ar, &ag, &ab, f1);
  1397. decodeBlockPtc14RgbAddA(bc2, &ar, &ag, &ab, f2);
  1398. decodeBlockPtc14RgbAddA(bc3, &ar, &ag, &ab, f3);
  1399. uint32_t br = 0, bg = 0, bb = 0;
  1400. decodeBlockPtc14RgbAddB(bc0, &br, &bg, &bb, f0);
  1401. decodeBlockPtc14RgbAddB(bc1, &br, &bg, &bb, f1);
  1402. decodeBlockPtc14RgbAddB(bc2, &br, &bg, &bb, f2);
  1403. decodeBlockPtc14RgbAddB(bc3, &br, &bg, &bb, f3);
  1404. const uint8_t* weight = &weightTable[(mod & 3)*4];
  1405. const uint8_t wa = weight[0];
  1406. const uint8_t wb = weight[1];
  1407. _dst[(yy*4 + xx)*4+0] = uint8_t( (ab * wa + bb * wb) >> 7);
  1408. _dst[(yy*4 + xx)*4+1] = uint8_t( (ag * wa + bg * wb) >> 7);
  1409. _dst[(yy*4 + xx)*4+2] = uint8_t( (ar * wa + br * wb) >> 7);
  1410. _dst[(yy*4 + xx)*4+3] = 255;
  1411. mod >>= 2;
  1412. factorTable += 4;
  1413. }
  1414. }
  1415. }
  1416. void decodeBlockPtc14ARgbaAddA(uint32_t _block, uint32_t* _r, uint32_t* _g, uint32_t* _b, uint32_t* _a, uint8_t _factor)
  1417. {
  1418. if (0 != (_block & (1<<15) ) )
  1419. {
  1420. *_r += bitRangeConvert( (_block >> 10) & 0x1f, 5, 8) * _factor;
  1421. *_g += bitRangeConvert( (_block >> 5) & 0x1f, 5, 8) * _factor;
  1422. *_b += bitRangeConvert( (_block >> 1) & 0x0f, 4, 8) * _factor;
  1423. *_a += 255 * _factor;
  1424. }
  1425. else
  1426. {
  1427. *_r += bitRangeConvert( (_block >> 8) & 0xf, 4, 8) * _factor;
  1428. *_g += bitRangeConvert( (_block >> 4) & 0xf, 4, 8) * _factor;
  1429. *_b += bitRangeConvert( (_block >> 1) & 0x7, 3, 8) * _factor;
  1430. *_a += bitRangeConvert( (_block >> 12) & 0x7, 3, 8) * _factor;
  1431. }
  1432. }
  1433. void decodeBlockPtc14ARgbaAddB(uint32_t _block, uint32_t* _r, uint32_t* _g, uint32_t* _b, uint32_t* _a, uint8_t _factor)
  1434. {
  1435. if (0 != (_block & (1<<31) ) )
  1436. {
  1437. *_r += bitRangeConvert( (_block >> 26) & 0x1f, 5, 8) * _factor;
  1438. *_g += bitRangeConvert( (_block >> 21) & 0x1f, 5, 8) * _factor;
  1439. *_b += bitRangeConvert( (_block >> 16) & 0x1f, 5, 8) * _factor;
  1440. *_a += 255 * _factor;
  1441. }
  1442. else
  1443. {
  1444. *_r += bitRangeConvert( (_block >> 24) & 0xf, 4, 8) * _factor;
  1445. *_g += bitRangeConvert( (_block >> 20) & 0xf, 4, 8) * _factor;
  1446. *_b += bitRangeConvert( (_block >> 16) & 0xf, 4, 8) * _factor;
  1447. *_a += bitRangeConvert( (_block >> 28) & 0x7, 3, 8) * _factor;
  1448. }
  1449. }
  1450. void decodeBlockPtc14A(uint8_t _dst[16*4], const uint8_t* _src, uint32_t _x, uint32_t _y, uint32_t _width, uint32_t _height)
  1451. {
  1452. // 0 1 2 3 4 5 6 7
  1453. // 7654321076543210765432107654321076543210765432107654321076543210
  1454. // mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmyrrrrrgggggbbbbbxrrrrrgggggbbbbp
  1455. // ^ ^^ ^^ ^
  1456. // +-- modulation data |+- B color |+- A color |
  1457. // +-- B opaque +-- A opaque |
  1458. // alpha punchthrough --+
  1459. const uint8_t* bc = &_src[morton2d(_x, _y) * 8];
  1460. uint32_t mod = 0
  1461. | bc[3]<<24
  1462. | bc[2]<<16
  1463. | bc[1]<<8
  1464. | bc[0]
  1465. ;
  1466. const bool punchthrough = !!(bc[7] & 1);
  1467. const uint8_t* weightTable = s_pvrtcWeights[4 * punchthrough];
  1468. const uint8_t* factorTable = s_pvrtcFactors[0];
  1469. for (int yy = 0; yy < 4; ++yy)
  1470. {
  1471. const uint32_t yOffset = (yy < 2) ? -1 : 0;
  1472. const uint32_t y0 = (_y + yOffset) % _height;
  1473. const uint32_t y1 = (y0 + 1) % _height;
  1474. for (int xx = 0; xx < 4; ++xx)
  1475. {
  1476. const uint32_t xOffset = (xx < 2) ? -1 : 0;
  1477. const uint32_t x0 = (_x + xOffset) % _width;
  1478. const uint32_t x1 = (x0 + 1) % _width;
  1479. const uint32_t bc0 = getColor(&_src[morton2d(x0, y0) * 8]);
  1480. const uint32_t bc1 = getColor(&_src[morton2d(x1, y0) * 8]);
  1481. const uint32_t bc2 = getColor(&_src[morton2d(x0, y1) * 8]);
  1482. const uint32_t bc3 = getColor(&_src[morton2d(x1, y1) * 8]);
  1483. const uint8_t f0 = factorTable[0];
  1484. const uint8_t f1 = factorTable[1];
  1485. const uint8_t f2 = factorTable[2];
  1486. const uint8_t f3 = factorTable[3];
  1487. uint32_t ar = 0, ag = 0, ab = 0, aa = 0;
  1488. decodeBlockPtc14ARgbaAddA(bc0, &ar, &ag, &ab, &aa, f0);
  1489. decodeBlockPtc14ARgbaAddA(bc1, &ar, &ag, &ab, &aa, f1);
  1490. decodeBlockPtc14ARgbaAddA(bc2, &ar, &ag, &ab, &aa, f2);
  1491. decodeBlockPtc14ARgbaAddA(bc3, &ar, &ag, &ab, &aa, f3);
  1492. uint32_t br = 0, bg = 0, bb = 0, ba = 0;
  1493. decodeBlockPtc14ARgbaAddB(bc0, &br, &bg, &bb, &ba, f0);
  1494. decodeBlockPtc14ARgbaAddB(bc1, &br, &bg, &bb, &ba, f1);
  1495. decodeBlockPtc14ARgbaAddB(bc2, &br, &bg, &bb, &ba, f2);
  1496. decodeBlockPtc14ARgbaAddB(bc3, &br, &bg, &bb, &ba, f3);
  1497. const uint8_t* weight = &weightTable[(mod & 3)*4];
  1498. const uint8_t wa = weight[0];
  1499. const uint8_t wb = weight[1];
  1500. const uint8_t wc = weight[2];
  1501. const uint8_t wd = weight[3];
  1502. _dst[(yy*4 + xx)*4+0] = uint8_t( (ab * wa + bb * wb) >> 7);
  1503. _dst[(yy*4 + xx)*4+1] = uint8_t( (ag * wa + bg * wb) >> 7);
  1504. _dst[(yy*4 + xx)*4+2] = uint8_t( (ar * wa + br * wb) >> 7);
  1505. _dst[(yy*4 + xx)*4+3] = uint8_t( (aa * wc + ba * wd) >> 7);
  1506. mod >>= 2;
  1507. factorTable += 4;
  1508. }
  1509. }
  1510. }
  1511. ImageContainer* imageAlloc(bx::AllocatorI* _allocator, TextureFormat::Enum _format, uint16_t _width, uint16_t _height, uint16_t _depth, uint16_t _numLayers, bool _cubeMap, bool _hasMips, const void* _data)
  1512. {
  1513. const ImageBlockInfo& blockInfo = getBlockInfo(_format);
  1514. const uint16_t blockWidth = blockInfo.blockWidth;
  1515. const uint16_t blockHeight = blockInfo.blockHeight;
  1516. const uint16_t minBlockX = blockInfo.minBlockX;
  1517. const uint16_t minBlockY = blockInfo.minBlockY;
  1518. _width = bx::uint16_max(blockWidth * minBlockX, ( (_width + blockWidth - 1) / blockWidth)*blockWidth);
  1519. _height = bx::uint16_max(blockHeight * minBlockY, ( (_height + blockHeight - 1) / blockHeight)*blockHeight);
  1520. _depth = bx::uint16_max(1, _depth);
  1521. _numLayers = bx::uint16_max(1, _numLayers);
  1522. const uint8_t numMips = _hasMips ? imageGetNumMips(_format, _width, _height, _depth) : 1;
  1523. uint32_t size = imageGetSize(NULL, _width, _height, _depth, _cubeMap, _hasMips, _numLayers, _format);
  1524. ImageContainer* imageContainer = (ImageContainer*)BX_ALLOC(_allocator, size + sizeof(ImageContainer) );
  1525. imageContainer->m_allocator = _allocator;
  1526. imageContainer->m_data = imageContainer + 1;
  1527. imageContainer->m_format = _format;
  1528. imageContainer->m_size = size;
  1529. imageContainer->m_offset = 0;
  1530. imageContainer->m_width = _width;
  1531. imageContainer->m_height = _height;
  1532. imageContainer->m_depth = _depth;
  1533. imageContainer->m_numLayers = _numLayers;
  1534. imageContainer->m_numMips = numMips;
  1535. imageContainer->m_hasAlpha = false;
  1536. imageContainer->m_cubeMap = _cubeMap;
  1537. imageContainer->m_ktx = false;
  1538. imageContainer->m_ktxLE = false;
  1539. imageContainer->m_srgb = false;
  1540. if (NULL != _data)
  1541. {
  1542. bx::memCopy(imageContainer->m_data, _data, imageContainer->m_size);
  1543. }
  1544. return imageContainer;
  1545. }
  1546. void imageFree(ImageContainer* _imageContainer)
  1547. {
  1548. BX_FREE(_imageContainer->m_allocator, _imageContainer);
  1549. }
  1550. // DDS
  1551. #define DDS_MAGIC BX_MAKEFOURCC('D', 'D', 'S', ' ')
  1552. #define DDS_HEADER_SIZE 124
  1553. #define DDS_DXT1 BX_MAKEFOURCC('D', 'X', 'T', '1')
  1554. #define DDS_DXT2 BX_MAKEFOURCC('D', 'X', 'T', '2')
  1555. #define DDS_DXT3 BX_MAKEFOURCC('D', 'X', 'T', '3')
  1556. #define DDS_DXT4 BX_MAKEFOURCC('D', 'X', 'T', '4')
  1557. #define DDS_DXT5 BX_MAKEFOURCC('D', 'X', 'T', '5')
  1558. #define DDS_ATI1 BX_MAKEFOURCC('A', 'T', 'I', '1')
  1559. #define DDS_BC4U BX_MAKEFOURCC('B', 'C', '4', 'U')
  1560. #define DDS_ATI2 BX_MAKEFOURCC('A', 'T', 'I', '2')
  1561. #define DDS_BC5U BX_MAKEFOURCC('B', 'C', '5', 'U')
  1562. #define DDS_DX10 BX_MAKEFOURCC('D', 'X', '1', '0')
  1563. #define DDS_A8R8G8B8 21
  1564. #define DDS_R5G6B5 23
  1565. #define DDS_A1R5G5B5 25
  1566. #define DDS_A4R4G4B4 26
  1567. #define DDS_A2B10G10R10 31
  1568. #define DDS_G16R16 34
  1569. #define DDS_A2R10G10B10 35
  1570. #define DDS_A16B16G16R16 36
  1571. #define DDS_A8L8 51
  1572. #define DDS_R16F 111
  1573. #define DDS_G16R16F 112
  1574. #define DDS_A16B16G16R16F 113
  1575. #define DDS_R32F 114
  1576. #define DDS_G32R32F 115
  1577. #define DDS_A32B32G32R32F 116
  1578. #define DDS_FORMAT_R32G32B32A32_FLOAT 2
  1579. #define DDS_FORMAT_R32G32B32A32_UINT 3
  1580. #define DDS_FORMAT_R16G16B16A16_FLOAT 10
  1581. #define DDS_FORMAT_R16G16B16A16_UNORM 11
  1582. #define DDS_FORMAT_R16G16B16A16_UINT 12
  1583. #define DDS_FORMAT_R32G32_FLOAT 16
  1584. #define DDS_FORMAT_R32G32_UINT 17
  1585. #define DDS_FORMAT_R10G10B10A2_UNORM 24
  1586. #define DDS_FORMAT_R11G11B10_FLOAT 26
  1587. #define DDS_FORMAT_R8G8B8A8_UNORM 28
  1588. #define DDS_FORMAT_R8G8B8A8_UNORM_SRGB 29
  1589. #define DDS_FORMAT_R16G16_FLOAT 34
  1590. #define DDS_FORMAT_R16G16_UNORM 35
  1591. #define DDS_FORMAT_R32_FLOAT 41
  1592. #define DDS_FORMAT_R32_UINT 42
  1593. #define DDS_FORMAT_R8G8_UNORM 49
  1594. #define DDS_FORMAT_R16_FLOAT 54
  1595. #define DDS_FORMAT_R16_UNORM 56
  1596. #define DDS_FORMAT_R8_UNORM 61
  1597. #define DDS_FORMAT_R1_UNORM 66
  1598. #define DDS_FORMAT_BC1_UNORM 71
  1599. #define DDS_FORMAT_BC1_UNORM_SRGB 72
  1600. #define DDS_FORMAT_BC2_UNORM 74
  1601. #define DDS_FORMAT_BC2_UNORM_SRGB 75
  1602. #define DDS_FORMAT_BC3_UNORM 77
  1603. #define DDS_FORMAT_BC3_UNORM_SRGB 78
  1604. #define DDS_FORMAT_BC4_UNORM 80
  1605. #define DDS_FORMAT_BC5_UNORM 83
  1606. #define DDS_FORMAT_B5G6R5_UNORM 85
  1607. #define DDS_FORMAT_B5G5R5A1_UNORM 86
  1608. #define DDS_FORMAT_B8G8R8A8_UNORM 87
  1609. #define DDS_FORMAT_B8G8R8A8_UNORM_SRGB 91
  1610. #define DDS_FORMAT_BC6H_SF16 96
  1611. #define DDS_FORMAT_BC7_UNORM 98
  1612. #define DDS_FORMAT_BC7_UNORM_SRGB 99
  1613. #define DDS_FORMAT_B4G4R4A4_UNORM 115
  1614. #define DDS_DX10_DIMENSION_TEXTURE2D 3
  1615. #define DDS_DX10_DIMENSION_TEXTURE3D 4
  1616. #define DDS_DX10_MISC_TEXTURECUBE 4
  1617. #define DDSD_CAPS 0x00000001
  1618. #define DDSD_HEIGHT 0x00000002
  1619. #define DDSD_WIDTH 0x00000004
  1620. #define DDSD_PITCH 0x00000008
  1621. #define DDSD_PIXELFORMAT 0x00001000
  1622. #define DDSD_MIPMAPCOUNT 0x00020000
  1623. #define DDSD_LINEARSIZE 0x00080000
  1624. #define DDSD_DEPTH 0x00800000
  1625. #define DDPF_ALPHAPIXELS 0x00000001
  1626. #define DDPF_ALPHA 0x00000002
  1627. #define DDPF_FOURCC 0x00000004
  1628. #define DDPF_INDEXED 0x00000020
  1629. #define DDPF_RGB 0x00000040
  1630. #define DDPF_YUV 0x00000200
  1631. #define DDPF_LUMINANCE 0x00020000
  1632. #define DDSCAPS_COMPLEX 0x00000008
  1633. #define DDSCAPS_TEXTURE 0x00001000
  1634. #define DDSCAPS_MIPMAP 0x00400000
  1635. #define DDSCAPS2_VOLUME 0x00200000
  1636. #define DDSCAPS2_CUBEMAP 0x00000200
  1637. #define DDSCAPS2_CUBEMAP_POSITIVEX 0x00000400
  1638. #define DDSCAPS2_CUBEMAP_NEGATIVEX 0x00000800
  1639. #define DDSCAPS2_CUBEMAP_POSITIVEY 0x00001000
  1640. #define DDSCAPS2_CUBEMAP_NEGATIVEY 0x00002000
  1641. #define DDSCAPS2_CUBEMAP_POSITIVEZ 0x00004000
  1642. #define DDSCAPS2_CUBEMAP_NEGATIVEZ 0x00008000
  1643. #define DSCAPS2_CUBEMAP_ALLSIDES (0 \
  1644. | DDSCAPS2_CUBEMAP_POSITIVEX \
  1645. | DDSCAPS2_CUBEMAP_NEGATIVEX \
  1646. | DDSCAPS2_CUBEMAP_POSITIVEY \
  1647. | DDSCAPS2_CUBEMAP_NEGATIVEY \
  1648. | DDSCAPS2_CUBEMAP_POSITIVEZ \
  1649. | DDSCAPS2_CUBEMAP_NEGATIVEZ \
  1650. )
  1651. struct TranslateDdsFormat
  1652. {
  1653. uint32_t m_format;
  1654. TextureFormat::Enum m_textureFormat;
  1655. bool m_srgb;
  1656. };
  1657. static const TranslateDdsFormat s_translateDdsFourccFormat[] =
  1658. {
  1659. { DDS_DXT1, TextureFormat::BC1, false },
  1660. { DDS_DXT2, TextureFormat::BC2, false },
  1661. { DDS_DXT3, TextureFormat::BC2, false },
  1662. { DDS_DXT4, TextureFormat::BC3, false },
  1663. { DDS_DXT5, TextureFormat::BC3, false },
  1664. { DDS_ATI1, TextureFormat::BC4, false },
  1665. { DDS_BC4U, TextureFormat::BC4, false },
  1666. { DDS_ATI2, TextureFormat::BC5, false },
  1667. { DDS_BC5U, TextureFormat::BC5, false },
  1668. { DDS_A16B16G16R16, TextureFormat::RGBA16, false },
  1669. { DDS_A16B16G16R16F, TextureFormat::RGBA16F, false },
  1670. { DDPF_RGB|DDPF_ALPHAPIXELS, TextureFormat::BGRA8, false },
  1671. { DDPF_INDEXED, TextureFormat::R8, false },
  1672. { DDPF_LUMINANCE, TextureFormat::R8, false },
  1673. { DDPF_ALPHA, TextureFormat::R8, false },
  1674. { DDS_R16F, TextureFormat::R16F, false },
  1675. { DDS_R32F, TextureFormat::R32F, false },
  1676. { DDS_A8L8, TextureFormat::RG8, false },
  1677. { DDS_G16R16, TextureFormat::RG16, false },
  1678. { DDS_G16R16F, TextureFormat::RG16F, false },
  1679. { DDS_G32R32F, TextureFormat::RG32F, false },
  1680. { DDS_A8R8G8B8, TextureFormat::BGRA8, false },
  1681. { DDS_A16B16G16R16, TextureFormat::RGBA16, false },
  1682. { DDS_A16B16G16R16F, TextureFormat::RGBA16F, false },
  1683. { DDS_A32B32G32R32F, TextureFormat::RGBA32F, false },
  1684. { DDS_R5G6B5, TextureFormat::R5G6B5, false },
  1685. { DDS_A4R4G4B4, TextureFormat::RGBA4, false },
  1686. { DDS_A1R5G5B5, TextureFormat::RGB5A1, false },
  1687. { DDS_A2B10G10R10, TextureFormat::RGB10A2, false },
  1688. };
  1689. static const TranslateDdsFormat s_translateDxgiFormat[] =
  1690. {
  1691. { DDS_FORMAT_BC1_UNORM, TextureFormat::BC1, false },
  1692. { DDS_FORMAT_BC1_UNORM_SRGB, TextureFormat::BC1, true },
  1693. { DDS_FORMAT_BC2_UNORM, TextureFormat::BC2, false },
  1694. { DDS_FORMAT_BC2_UNORM_SRGB, TextureFormat::BC2, true },
  1695. { DDS_FORMAT_BC3_UNORM, TextureFormat::BC3, false },
  1696. { DDS_FORMAT_BC3_UNORM_SRGB, TextureFormat::BC3, true },
  1697. { DDS_FORMAT_BC4_UNORM, TextureFormat::BC4, false },
  1698. { DDS_FORMAT_BC5_UNORM, TextureFormat::BC5, false },
  1699. { DDS_FORMAT_BC6H_SF16, TextureFormat::BC6H, false },
  1700. { DDS_FORMAT_BC7_UNORM, TextureFormat::BC7, false },
  1701. { DDS_FORMAT_BC7_UNORM_SRGB, TextureFormat::BC7, true },
  1702. { DDS_FORMAT_R1_UNORM, TextureFormat::R1, false },
  1703. { DDS_FORMAT_R8_UNORM, TextureFormat::R8, false },
  1704. { DDS_FORMAT_R16_UNORM, TextureFormat::R16, false },
  1705. { DDS_FORMAT_R16_FLOAT, TextureFormat::R16F, false },
  1706. { DDS_FORMAT_R32_UINT, TextureFormat::R32U, false },
  1707. { DDS_FORMAT_R32_FLOAT, TextureFormat::R32F, false },
  1708. { DDS_FORMAT_R8G8_UNORM, TextureFormat::RG8, false },
  1709. { DDS_FORMAT_R16G16_UNORM, TextureFormat::RG16, false },
  1710. { DDS_FORMAT_R16G16_FLOAT, TextureFormat::RG16F, false },
  1711. { DDS_FORMAT_R32G32_UINT, TextureFormat::RG32U, false },
  1712. { DDS_FORMAT_R32G32_FLOAT, TextureFormat::RG32F, false },
  1713. { DDS_FORMAT_B8G8R8A8_UNORM, TextureFormat::BGRA8, false },
  1714. { DDS_FORMAT_B8G8R8A8_UNORM_SRGB, TextureFormat::BGRA8, true },
  1715. { DDS_FORMAT_R8G8B8A8_UNORM, TextureFormat::RGBA8, false },
  1716. { DDS_FORMAT_R8G8B8A8_UNORM_SRGB, TextureFormat::RGBA8, true },
  1717. { DDS_FORMAT_R16G16B16A16_UNORM, TextureFormat::RGBA16, false },
  1718. { DDS_FORMAT_R16G16B16A16_FLOAT, TextureFormat::RGBA16F, false },
  1719. { DDS_FORMAT_R32G32B32A32_UINT, TextureFormat::RGBA32U, false },
  1720. { DDS_FORMAT_R32G32B32A32_FLOAT, TextureFormat::RGBA32F, false },
  1721. { DDS_FORMAT_B5G6R5_UNORM, TextureFormat::R5G6B5, false },
  1722. { DDS_FORMAT_B4G4R4A4_UNORM, TextureFormat::RGBA4, false },
  1723. { DDS_FORMAT_B5G5R5A1_UNORM, TextureFormat::RGB5A1, false },
  1724. { DDS_FORMAT_R10G10B10A2_UNORM, TextureFormat::RGB10A2, false },
  1725. { DDS_FORMAT_R11G11B10_FLOAT, TextureFormat::RG11B10F, false },
  1726. };
  1727. struct TranslateDdsPixelFormat
  1728. {
  1729. uint32_t m_bitCount;
  1730. uint32_t m_flags;
  1731. uint32_t m_bitmask[4];
  1732. TextureFormat::Enum m_textureFormat;
  1733. };
  1734. static const TranslateDdsPixelFormat s_translateDdsPixelFormat[] =
  1735. {
  1736. { 8, DDPF_LUMINANCE, { 0x000000ff, 0x00000000, 0x00000000, 0x00000000 }, TextureFormat::R8 },
  1737. { 16, DDPF_RGB, { 0x0000ffff, 0x00000000, 0x00000000, 0x00000000 }, TextureFormat::R16U },
  1738. { 16, DDPF_RGB|DDPF_ALPHAPIXELS, { 0x00000f00, 0x000000f0, 0x0000000f, 0x0000f000 }, TextureFormat::RGBA4 },
  1739. { 16, DDPF_RGB, { 0x0000f800, 0x000007e0, 0x0000001f, 0x00000000 }, TextureFormat::R5G6B5 },
  1740. { 16, DDPF_RGB, { 0x00007c00, 0x000003e0, 0x0000001f, 0x00008000 }, TextureFormat::RGB5A1 },
  1741. { 24, DDPF_RGB, { 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000 }, TextureFormat::RGB8 },
  1742. { 32, DDPF_RGB|DDPF_ALPHAPIXELS, { 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000 }, TextureFormat::RGBA8 },
  1743. { 32, DDPF_RGB|DDPF_ALPHAPIXELS, { 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000 }, TextureFormat::BGRA8 }, // D3DFMT_A8R8G8B8
  1744. { 32, DDPF_RGB|DDPF_ALPHAPIXELS, { 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000 }, TextureFormat::BGRA8 }, // D3DFMT_X8R8G8B8
  1745. { 32, DDPF_RGB|DDPF_ALPHAPIXELS, { 0x000003ff, 0x000ffc00, 0x3ff00000, 0xc0000000 }, TextureFormat::RGB10A2 },
  1746. { 32, DDPF_RGB, { 0x0000ffff, 0xffff0000, 0x00000000, 0x00000000 }, TextureFormat::RG16 },
  1747. { 32, DDPF_RGB, { 0xffffffff, 0x00000000, 0x00000000, 0x00000000 }, TextureFormat::R32U },
  1748. };
  1749. bool imageParseDds(ImageContainer& _imageContainer, bx::ReaderSeekerI* _reader, bx::Error* _err)
  1750. {
  1751. BX_ERROR_SCOPE(_err);
  1752. int32_t total = 0;
  1753. uint32_t headerSize;
  1754. total += bx::read(_reader, headerSize, _err);
  1755. if (!_err->isOk()
  1756. || headerSize < DDS_HEADER_SIZE)
  1757. {
  1758. return false;
  1759. }
  1760. uint32_t flags;
  1761. total += bx::read(_reader, flags, _err);
  1762. if (!_err->isOk() )
  1763. {
  1764. return false;
  1765. }
  1766. if ( (flags & (DDSD_CAPS|DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT) ) != (DDSD_CAPS|DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT) )
  1767. {
  1768. BX_ERROR_SET(_err, BIMG_ERROR, "DDS: Invalid flags.");
  1769. return false;
  1770. }
  1771. uint32_t height;
  1772. total += bx::read(_reader, height, _err);
  1773. uint32_t width;
  1774. total += bx::read(_reader, width, _err);
  1775. uint32_t pitch;
  1776. total += bx::read(_reader, pitch, _err);
  1777. uint32_t depth;
  1778. total += bx::read(_reader, depth, _err);
  1779. uint32_t mips;
  1780. total += bx::read(_reader, mips, _err);
  1781. bx::skip(_reader, 44); // reserved
  1782. total += 44;
  1783. uint32_t pixelFormatSize;
  1784. total += bx::read(_reader, pixelFormatSize, _err);
  1785. uint32_t pixelFlags;
  1786. total += bx::read(_reader, pixelFlags, _err);
  1787. uint32_t fourcc;
  1788. total += bx::read(_reader, fourcc, _err);
  1789. uint32_t bitCount;
  1790. total += bx::read(_reader, bitCount, _err);
  1791. uint32_t bitmask[4];
  1792. total += bx::read(_reader, bitmask, sizeof(bitmask), _err);
  1793. uint32_t caps[4];
  1794. total += bx::read(_reader, caps, _err);
  1795. bx::skip(_reader, 4);
  1796. total += 4; // reserved
  1797. if (!_err->isOk() )
  1798. {
  1799. return false;
  1800. }
  1801. uint32_t dxgiFormat = 0;
  1802. uint32_t arraySize = 1;
  1803. if (DDPF_FOURCC == pixelFlags
  1804. && DDS_DX10 == fourcc)
  1805. {
  1806. total += bx::read(_reader, dxgiFormat, _err);
  1807. uint32_t dims;
  1808. total += bx::read(_reader, dims, _err);
  1809. uint32_t miscFlags;
  1810. total += bx::read(_reader, miscFlags, _err);
  1811. total += bx::read(_reader, arraySize, _err);
  1812. uint32_t miscFlags2;
  1813. total += bx::read(_reader, miscFlags2, _err);
  1814. }
  1815. if (!_err->isOk() )
  1816. {
  1817. return false;
  1818. }
  1819. if ( (caps[0] & DDSCAPS_TEXTURE) == 0)
  1820. {
  1821. BX_ERROR_SET(_err, BIMG_ERROR, "DDS: Unsupported caps.");
  1822. return false;
  1823. }
  1824. bool cubeMap = 0 != (caps[1] & DDSCAPS2_CUBEMAP);
  1825. if (cubeMap)
  1826. {
  1827. if ( (caps[1] & DSCAPS2_CUBEMAP_ALLSIDES) != DSCAPS2_CUBEMAP_ALLSIDES)
  1828. {
  1829. // partial cube map is not supported.
  1830. BX_ERROR_SET(_err, BIMG_ERROR, "DDS: Incomplete cubemap.");
  1831. return false;
  1832. }
  1833. }
  1834. TextureFormat::Enum format = TextureFormat::Unknown;
  1835. bool hasAlpha = pixelFlags & DDPF_ALPHAPIXELS;
  1836. bool srgb = false;
  1837. if (dxgiFormat == 0)
  1838. {
  1839. if (DDPF_FOURCC == (pixelFlags & DDPF_FOURCC) )
  1840. {
  1841. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateDdsFourccFormat); ++ii)
  1842. {
  1843. if (s_translateDdsFourccFormat[ii].m_format == fourcc)
  1844. {
  1845. format = s_translateDdsFourccFormat[ii].m_textureFormat;
  1846. break;
  1847. }
  1848. }
  1849. }
  1850. else
  1851. {
  1852. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateDdsPixelFormat); ++ii)
  1853. {
  1854. const TranslateDdsPixelFormat& pf = s_translateDdsPixelFormat[ii];
  1855. if (pf.m_bitCount == bitCount
  1856. && pf.m_bitmask[0] == bitmask[0]
  1857. && pf.m_bitmask[1] == bitmask[1]
  1858. && pf.m_bitmask[2] == bitmask[2]
  1859. && pf.m_bitmask[3] == bitmask[3])
  1860. {
  1861. format = pf.m_textureFormat;
  1862. break;
  1863. }
  1864. }
  1865. }
  1866. }
  1867. else
  1868. {
  1869. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateDxgiFormat); ++ii)
  1870. {
  1871. if (s_translateDxgiFormat[ii].m_format == dxgiFormat)
  1872. {
  1873. format = s_translateDxgiFormat[ii].m_textureFormat;
  1874. srgb = s_translateDxgiFormat[ii].m_srgb;
  1875. break;
  1876. }
  1877. }
  1878. }
  1879. if (TextureFormat::Unknown == format)
  1880. {
  1881. BX_ERROR_SET(_err, BIMG_ERROR, "DDS: Unknown texture format.");
  1882. return false;
  1883. }
  1884. _imageContainer.m_allocator = NULL;
  1885. _imageContainer.m_data = NULL;
  1886. _imageContainer.m_size = 0;
  1887. _imageContainer.m_offset = (uint32_t)bx::seek(_reader);
  1888. _imageContainer.m_width = width;
  1889. _imageContainer.m_height = height;
  1890. _imageContainer.m_depth = depth;
  1891. _imageContainer.m_format = format;
  1892. _imageContainer.m_numLayers = uint16_t(arraySize);
  1893. _imageContainer.m_numMips = uint8_t( (caps[0] & DDSCAPS_MIPMAP) ? mips : 1);
  1894. _imageContainer.m_hasAlpha = hasAlpha;
  1895. _imageContainer.m_cubeMap = cubeMap;
  1896. _imageContainer.m_ktx = false;
  1897. _imageContainer.m_ktxLE = false;
  1898. _imageContainer.m_srgb = srgb;
  1899. return true;
  1900. }
  1901. ImageContainer* imageParseDds(bx::AllocatorI* _allocator, const void* _src, uint32_t _size, bx::Error* _err)
  1902. {
  1903. return imageParseT<DDS_MAGIC, imageParseDds>(_allocator, _src, _size, _err);
  1904. }
  1905. // KTX
  1906. #define KTX_MAGIC BX_MAKEFOURCC(0xAB, 'K', 'T', 'X')
  1907. #define KTX_HEADER_SIZE 64
  1908. #define KTX_ETC1_RGB8_OES 0x8D64
  1909. #define KTX_COMPRESSED_R11_EAC 0x9270
  1910. #define KTX_COMPRESSED_SIGNED_R11_EAC 0x9271
  1911. #define KTX_COMPRESSED_RG11_EAC 0x9272
  1912. #define KTX_COMPRESSED_SIGNED_RG11_EAC 0x9273
  1913. #define KTX_COMPRESSED_RGB8_ETC2 0x9274
  1914. #define KTX_COMPRESSED_SRGB8_ETC2 0x9275
  1915. #define KTX_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2 0x9276
  1916. #define KTX_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2 0x9277
  1917. #define KTX_COMPRESSED_RGBA8_ETC2_EAC 0x9278
  1918. #define KTX_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC 0x9279
  1919. #define KTX_COMPRESSED_RGB_PVRTC_4BPPV1_IMG 0x8C00
  1920. #define KTX_COMPRESSED_RGB_PVRTC_2BPPV1_IMG 0x8C01
  1921. #define KTX_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG 0x8C02
  1922. #define KTX_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG 0x8C03
  1923. #define KTX_COMPRESSED_RGBA_PVRTC_2BPPV2_IMG 0x9137
  1924. #define KTX_COMPRESSED_RGBA_PVRTC_4BPPV2_IMG 0x9138
  1925. #define KTX_COMPRESSED_RGB_S3TC_DXT1_EXT 0x83F0
  1926. #define KTX_COMPRESSED_RGBA_S3TC_DXT1_EXT 0x83F1
  1927. #define KTX_COMPRESSED_RGBA_S3TC_DXT3_EXT 0x83F2
  1928. #define KTX_COMPRESSED_RGBA_S3TC_DXT5_EXT 0x83F3
  1929. #define KTX_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT 0x8C4D
  1930. #define KTX_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT 0x8C4E
  1931. #define KTX_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT 0x8C4F
  1932. #define KTX_COMPRESSED_LUMINANCE_LATC1_EXT 0x8C70
  1933. #define KTX_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT 0x8C72
  1934. #define KTX_COMPRESSED_RGBA_BPTC_UNORM_ARB 0x8E8C
  1935. #define KTX_COMPRESSED_SRGB_ALPHA_BPTC_UNORM_ARB 0x8E8D
  1936. #define KTX_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB 0x8E8E
  1937. #define KTX_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT_ARB 0x8E8F
  1938. #define KTX_COMPRESSED_SRGB_PVRTC_2BPPV1_EXT 0x8A54
  1939. #define KTX_COMPRESSED_SRGB_PVRTC_4BPPV1_EXT 0x8A55
  1940. #define KTX_COMPRESSED_SRGB_ALPHA_PVRTC_2BPPV1_EXT 0x8A56
  1941. #define KTX_COMPRESSED_SRGB_ALPHA_PVRTC_4BPPV1_EXT 0x8A57
  1942. #define KTX_A8 0x803C
  1943. #define KTX_R8 0x8229
  1944. #define KTX_R16 0x822A
  1945. #define KTX_RG8 0x822B
  1946. #define KTX_RG16 0x822C
  1947. #define KTX_R16F 0x822D
  1948. #define KTX_R32F 0x822E
  1949. #define KTX_RG16F 0x822F
  1950. #define KTX_RG32F 0x8230
  1951. #define KTX_RGBA8 0x8058
  1952. #define KTX_RGBA16 0x805B
  1953. #define KTX_RGBA16F 0x881A
  1954. #define KTX_R32UI 0x8236
  1955. #define KTX_RG32UI 0x823C
  1956. #define KTX_RGBA32UI 0x8D70
  1957. #define KTX_RGBA32F 0x8814
  1958. #define KTX_RGB565 0x8D62
  1959. #define KTX_RGBA4 0x8056
  1960. #define KTX_RGB5_A1 0x8057
  1961. #define KTX_RGB10_A2 0x8059
  1962. #define KTX_R8I 0x8231
  1963. #define KTX_R8UI 0x8232
  1964. #define KTX_R16I 0x8233
  1965. #define KTX_R16UI 0x8234
  1966. #define KTX_R32I 0x8235
  1967. #define KTX_R32UI 0x8236
  1968. #define KTX_RG8I 0x8237
  1969. #define KTX_RG8UI 0x8238
  1970. #define KTX_RG16I 0x8239
  1971. #define KTX_RG16UI 0x823A
  1972. #define KTX_RG32I 0x823B
  1973. #define KTX_RG32UI 0x823C
  1974. #define KTX_R8_SNORM 0x8F94
  1975. #define KTX_RG8_SNORM 0x8F95
  1976. #define KTX_RGB8_SNORM 0x8F96
  1977. #define KTX_RGBA8_SNORM 0x8F97
  1978. #define KTX_R16_SNORM 0x8F98
  1979. #define KTX_RG16_SNORM 0x8F99
  1980. #define KTX_RGB16_SNORM 0x8F9A
  1981. #define KTX_RGBA16_SNORM 0x8F9B
  1982. #define KTX_SRGB8 0x8C41
  1983. #define KTX_SRGB8_ALPHA8 0x8C43
  1984. #define KTX_RGBA32UI 0x8D70
  1985. #define KTX_RGB32UI 0x8D71
  1986. #define KTX_RGBA16UI 0x8D76
  1987. #define KTX_RGB16UI 0x8D77
  1988. #define KTX_RGBA8UI 0x8D7C
  1989. #define KTX_RGB8UI 0x8D7D
  1990. #define KTX_RGBA32I 0x8D82
  1991. #define KTX_RGB32I 0x8D83
  1992. #define KTX_RGBA16I 0x8D88
  1993. #define KTX_RGB16I 0x8D89
  1994. #define KTX_RGBA8I 0x8D8E
  1995. #define KTX_RGB8 0x8051
  1996. #define KTX_RGB8I 0x8D8F
  1997. #define KTX_RGB9_E5 0x8C3D
  1998. #define KTX_R11F_G11F_B10F 0x8C3A
  1999. #define KTX_ZERO 0
  2000. #define KTX_RED 0x1903
  2001. #define KTX_ALPHA 0x1906
  2002. #define KTX_RGB 0x1907
  2003. #define KTX_RGBA 0x1908
  2004. #define KTX_BGRA 0x80E1
  2005. #define KTX_RG 0x8227
  2006. #define KTX_BYTE 0x1400
  2007. #define KTX_UNSIGNED_BYTE 0x1401
  2008. #define KTX_SHORT 0x1402
  2009. #define KTX_UNSIGNED_SHORT 0x1403
  2010. #define KTX_INT 0x1404
  2011. #define KTX_UNSIGNED_INT 0x1405
  2012. #define KTX_FLOAT 0x1406
  2013. #define KTX_HALF_FLOAT 0x140B
  2014. #define KTX_UNSIGNED_INT_5_9_9_9_REV 0x8C3E
  2015. #define KTX_UNSIGNED_SHORT_5_6_5 0x8363
  2016. #define KTX_UNSIGNED_SHORT_4_4_4_4 0x8033
  2017. #define KTX_UNSIGNED_SHORT_5_5_5_1 0x8034
  2018. #define KTX_UNSIGNED_INT_2_10_10_10_REV 0x8368
  2019. #define KTX_UNSIGNED_INT_10F_11F_11F_REV 0x8C3B
  2020. struct KtxFormatInfo
  2021. {
  2022. uint32_t m_internalFmt;
  2023. uint32_t m_internalFmtSrgb;
  2024. uint32_t m_fmt;
  2025. uint32_t m_type;
  2026. };
  2027. static const KtxFormatInfo s_translateKtxFormat[] =
  2028. {
  2029. { KTX_COMPRESSED_RGBA_S3TC_DXT1_EXT, KTX_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT, KTX_COMPRESSED_RGBA_S3TC_DXT1_EXT, KTX_ZERO, }, // BC1
  2030. { KTX_COMPRESSED_RGBA_S3TC_DXT3_EXT, KTX_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT, KTX_COMPRESSED_RGBA_S3TC_DXT3_EXT, KTX_ZERO, }, // BC2
  2031. { KTX_COMPRESSED_RGBA_S3TC_DXT5_EXT, KTX_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT, KTX_COMPRESSED_RGBA_S3TC_DXT5_EXT, KTX_ZERO, }, // BC3
  2032. { KTX_COMPRESSED_LUMINANCE_LATC1_EXT, KTX_ZERO, KTX_COMPRESSED_LUMINANCE_LATC1_EXT, KTX_ZERO, }, // BC4
  2033. { KTX_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT, KTX_ZERO, KTX_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT, KTX_ZERO, }, // BC5
  2034. { KTX_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB, KTX_ZERO, KTX_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB, KTX_ZERO, }, // BC6H
  2035. { KTX_COMPRESSED_RGBA_BPTC_UNORM_ARB, KTX_ZERO, KTX_COMPRESSED_RGBA_BPTC_UNORM_ARB, KTX_ZERO, }, // BC7
  2036. { KTX_ETC1_RGB8_OES, KTX_ZERO, KTX_ETC1_RGB8_OES, KTX_ZERO, }, // ETC1
  2037. { KTX_COMPRESSED_RGB8_ETC2, KTX_ZERO, KTX_COMPRESSED_RGB8_ETC2, KTX_ZERO, }, // ETC2
  2038. { KTX_COMPRESSED_RGBA8_ETC2_EAC, KTX_COMPRESSED_SRGB8_ETC2, KTX_COMPRESSED_RGBA8_ETC2_EAC, KTX_ZERO, }, // ETC2A
  2039. { KTX_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2, KTX_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2, KTX_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2, KTX_ZERO, }, // ETC2A1
  2040. { KTX_COMPRESSED_RGB_PVRTC_2BPPV1_IMG, KTX_COMPRESSED_SRGB_PVRTC_2BPPV1_EXT, KTX_COMPRESSED_RGB_PVRTC_2BPPV1_IMG, KTX_ZERO, }, // PTC12
  2041. { KTX_COMPRESSED_RGB_PVRTC_4BPPV1_IMG, KTX_COMPRESSED_SRGB_PVRTC_4BPPV1_EXT, KTX_COMPRESSED_RGB_PVRTC_4BPPV1_IMG, KTX_ZERO, }, // PTC14
  2042. { KTX_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG, KTX_COMPRESSED_SRGB_ALPHA_PVRTC_2BPPV1_EXT, KTX_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG, KTX_ZERO, }, // PTC12A
  2043. { KTX_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG, KTX_COMPRESSED_SRGB_ALPHA_PVRTC_4BPPV1_EXT, KTX_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG, KTX_ZERO, }, // PTC14A
  2044. { KTX_COMPRESSED_RGBA_PVRTC_2BPPV2_IMG, KTX_ZERO, KTX_COMPRESSED_RGBA_PVRTC_2BPPV2_IMG, KTX_ZERO, }, // PTC22
  2045. { KTX_COMPRESSED_RGBA_PVRTC_4BPPV2_IMG, KTX_ZERO, KTX_COMPRESSED_RGBA_PVRTC_4BPPV2_IMG, KTX_ZERO, }, // PTC24
  2046. { KTX_ZERO, KTX_ZERO, KTX_ZERO, KTX_ZERO, }, // Unknown
  2047. { KTX_ZERO, KTX_ZERO, KTX_ZERO, KTX_ZERO, }, // R1
  2048. { KTX_ALPHA, KTX_ZERO, KTX_ALPHA, KTX_UNSIGNED_BYTE, }, // A8
  2049. { KTX_R8, KTX_ZERO, KTX_RED, KTX_UNSIGNED_BYTE, }, // R8
  2050. { KTX_R8I, KTX_ZERO, KTX_RED, KTX_BYTE, }, // R8S
  2051. { KTX_R8UI, KTX_ZERO, KTX_RED, KTX_UNSIGNED_BYTE, }, // R8S
  2052. { KTX_R8_SNORM, KTX_ZERO, KTX_RED, KTX_BYTE, }, // R8S
  2053. { KTX_R16, KTX_ZERO, KTX_RED, KTX_UNSIGNED_SHORT, }, // R16
  2054. { KTX_R16I, KTX_ZERO, KTX_RED, KTX_SHORT, }, // R16I
  2055. { KTX_R16UI, KTX_ZERO, KTX_RED, KTX_UNSIGNED_SHORT, }, // R16U
  2056. { KTX_R16F, KTX_ZERO, KTX_RED, KTX_HALF_FLOAT, }, // R16F
  2057. { KTX_R16_SNORM, KTX_ZERO, KTX_RED, KTX_SHORT, }, // R16S
  2058. { KTX_R32I, KTX_ZERO, KTX_RED, KTX_INT, }, // R32I
  2059. { KTX_R32UI, KTX_ZERO, KTX_RED, KTX_UNSIGNED_INT, }, // R32U
  2060. { KTX_R32F, KTX_ZERO, KTX_RED, KTX_FLOAT, }, // R32F
  2061. { KTX_RG8, KTX_ZERO, KTX_RG, KTX_UNSIGNED_BYTE, }, // RG8
  2062. { KTX_RG8I, KTX_ZERO, KTX_RG, KTX_BYTE, }, // RG8I
  2063. { KTX_RG8UI, KTX_ZERO, KTX_RG, KTX_UNSIGNED_BYTE, }, // RG8U
  2064. { KTX_RG8_SNORM, KTX_ZERO, KTX_RG, KTX_BYTE, }, // RG8S
  2065. { KTX_RG16, KTX_ZERO, KTX_RG, KTX_UNSIGNED_SHORT, }, // RG16
  2066. { KTX_RG16I, KTX_ZERO, KTX_RG, KTX_SHORT, }, // RG16
  2067. { KTX_RG16UI, KTX_ZERO, KTX_RG, KTX_UNSIGNED_SHORT, }, // RG16
  2068. { KTX_RG16F, KTX_ZERO, KTX_RG, KTX_FLOAT, }, // RG16F
  2069. { KTX_RG16_SNORM, KTX_ZERO, KTX_RG, KTX_SHORT, }, // RG16S
  2070. { KTX_RG32I, KTX_ZERO, KTX_RG, KTX_INT, }, // RG32I
  2071. { KTX_RG32UI, KTX_ZERO, KTX_RG, KTX_UNSIGNED_INT, }, // RG32U
  2072. { KTX_RG32F, KTX_ZERO, KTX_RG, KTX_FLOAT, }, // RG32F
  2073. { KTX_RGB8, KTX_SRGB8, KTX_RGB, KTX_UNSIGNED_BYTE, }, // RGB8
  2074. { KTX_RGB8I, KTX_ZERO, KTX_RGB, KTX_BYTE, }, // RGB8I
  2075. { KTX_RGB8UI, KTX_ZERO, KTX_RGB, KTX_UNSIGNED_BYTE, }, // RGB8U
  2076. { KTX_RGB8_SNORM, KTX_ZERO, KTX_RGB, KTX_BYTE, }, // RGB8S
  2077. { KTX_RGB9_E5, KTX_ZERO, KTX_RGB, KTX_UNSIGNED_INT_5_9_9_9_REV, }, // RGB9E5F
  2078. { KTX_BGRA, KTX_SRGB8_ALPHA8, KTX_BGRA, KTX_UNSIGNED_BYTE, }, // BGRA8
  2079. { KTX_RGBA8, KTX_SRGB8_ALPHA8, KTX_RGBA, KTX_UNSIGNED_BYTE, }, // RGBA8
  2080. { KTX_RGBA8I, KTX_ZERO, KTX_RGBA, KTX_BYTE, }, // RGBA8I
  2081. { KTX_RGBA8UI, KTX_ZERO, KTX_RGBA, KTX_UNSIGNED_BYTE, }, // RGBA8U
  2082. { KTX_RGBA8_SNORM, KTX_ZERO, KTX_RGBA, KTX_BYTE, }, // RGBA8S
  2083. { KTX_RGBA16, KTX_ZERO, KTX_RGBA, KTX_UNSIGNED_SHORT, }, // RGBA16
  2084. { KTX_RGBA16I, KTX_ZERO, KTX_RGBA, KTX_SHORT, }, // RGBA16I
  2085. { KTX_RGBA16UI, KTX_ZERO, KTX_RGBA, KTX_UNSIGNED_SHORT, }, // RGBA16U
  2086. { KTX_RGBA16F, KTX_ZERO, KTX_RGBA, KTX_HALF_FLOAT, }, // RGBA16F
  2087. { KTX_RGBA16_SNORM, KTX_ZERO, KTX_RGBA, KTX_SHORT, }, // RGBA16S
  2088. { KTX_RGBA32I, KTX_ZERO, KTX_RGBA, KTX_INT, }, // RGBA32I
  2089. { KTX_RGBA32UI, KTX_ZERO, KTX_RGBA, KTX_UNSIGNED_INT, }, // RGBA32U
  2090. { KTX_RGBA32F, KTX_ZERO, KTX_RGBA, KTX_FLOAT, }, // RGBA32F
  2091. { KTX_RGB565, KTX_ZERO, KTX_RGB, KTX_UNSIGNED_SHORT_5_6_5, }, // R5G6B5
  2092. { KTX_RGBA4, KTX_ZERO, KTX_RGBA, KTX_UNSIGNED_SHORT_4_4_4_4, }, // RGBA4
  2093. { KTX_RGB5_A1, KTX_ZERO, KTX_RGBA, KTX_UNSIGNED_SHORT_5_5_5_1, }, // RGB5A1
  2094. { KTX_RGB10_A2, KTX_ZERO, KTX_RGBA, KTX_UNSIGNED_INT_2_10_10_10_REV, }, // RGB10A2
  2095. { KTX_R11F_G11F_B10F, KTX_ZERO, KTX_RGB, KTX_UNSIGNED_INT_10F_11F_11F_REV, }, // RG11B10F
  2096. };
  2097. BX_STATIC_ASSERT(TextureFormat::UnknownDepth == BX_COUNTOF(s_translateKtxFormat) );
  2098. struct KtxFormatInfo2
  2099. {
  2100. uint32_t m_internalFmt;
  2101. TextureFormat::Enum m_format;
  2102. };
  2103. static const KtxFormatInfo2 s_translateKtxFormat2[] =
  2104. {
  2105. { KTX_A8, TextureFormat::A8 },
  2106. { KTX_RED, TextureFormat::R8 },
  2107. { KTX_RGB, TextureFormat::RGB8 },
  2108. { KTX_RGBA, TextureFormat::RGBA8 },
  2109. { KTX_COMPRESSED_RGB_S3TC_DXT1_EXT, TextureFormat::BC1 },
  2110. };
  2111. bool imageParseKtx(ImageContainer& _imageContainer, bx::ReaderSeekerI* _reader, bx::Error* _err)
  2112. {
  2113. BX_ERROR_SCOPE(_err);
  2114. uint8_t identifier[8];
  2115. bx::read(_reader, identifier);
  2116. if (identifier[1] != '1'
  2117. && identifier[2] != '1')
  2118. {
  2119. return false;
  2120. }
  2121. uint32_t endianness;
  2122. bx::read(_reader, endianness);
  2123. bool fromLittleEndian = 0x04030201 == endianness;
  2124. uint32_t glType;
  2125. bx::readHE(_reader, glType, fromLittleEndian);
  2126. uint32_t glTypeSize;
  2127. bx::readHE(_reader, glTypeSize, fromLittleEndian);
  2128. uint32_t glFormat;
  2129. bx::readHE(_reader, glFormat, fromLittleEndian);
  2130. uint32_t glInternalFormat;
  2131. bx::readHE(_reader, glInternalFormat, fromLittleEndian);
  2132. uint32_t glBaseInternalFormat;
  2133. bx::readHE(_reader, glBaseInternalFormat, fromLittleEndian);
  2134. uint32_t width;
  2135. bx::readHE(_reader, width, fromLittleEndian);
  2136. uint32_t height;
  2137. bx::readHE(_reader, height, fromLittleEndian);
  2138. uint32_t depth;
  2139. bx::readHE(_reader, depth, fromLittleEndian);
  2140. uint32_t numberOfArrayElements;
  2141. bx::readHE(_reader, numberOfArrayElements, fromLittleEndian);
  2142. uint32_t numFaces;
  2143. bx::readHE(_reader, numFaces, fromLittleEndian);
  2144. uint32_t numMips;
  2145. bx::readHE(_reader, numMips, fromLittleEndian);
  2146. uint32_t metaDataSize;
  2147. bx::readHE(_reader, metaDataSize, fromLittleEndian);
  2148. // skip meta garbage...
  2149. int64_t offset = bx::skip(_reader, metaDataSize);
  2150. TextureFormat::Enum format = TextureFormat::Unknown;
  2151. bool hasAlpha = false;
  2152. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateKtxFormat); ++ii)
  2153. {
  2154. if (s_translateKtxFormat[ii].m_internalFmt == glInternalFormat)
  2155. {
  2156. format = TextureFormat::Enum(ii);
  2157. break;
  2158. }
  2159. }
  2160. if (TextureFormat::Unknown == format)
  2161. {
  2162. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateKtxFormat2); ++ii)
  2163. {
  2164. if (s_translateKtxFormat2[ii].m_internalFmt == glInternalFormat)
  2165. {
  2166. format = s_translateKtxFormat2[ii].m_format;
  2167. break;
  2168. }
  2169. }
  2170. }
  2171. _imageContainer.m_allocator = NULL;
  2172. _imageContainer.m_data = NULL;
  2173. _imageContainer.m_size = 0;
  2174. _imageContainer.m_offset = (uint32_t)offset;
  2175. _imageContainer.m_width = width;
  2176. _imageContainer.m_height = height;
  2177. _imageContainer.m_depth = depth;
  2178. _imageContainer.m_format = format;
  2179. _imageContainer.m_numLayers = uint16_t(bx::uint32_max(numberOfArrayElements, 1) );
  2180. _imageContainer.m_numMips = uint8_t(bx::uint32_max(numMips, 1) );
  2181. _imageContainer.m_hasAlpha = hasAlpha;
  2182. _imageContainer.m_cubeMap = numFaces > 1;
  2183. _imageContainer.m_ktx = true;
  2184. _imageContainer.m_ktxLE = fromLittleEndian;
  2185. _imageContainer.m_srgb = false;
  2186. if (TextureFormat::Unknown == format)
  2187. {
  2188. BX_ERROR_SET(_err, BIMG_ERROR, "Unrecognized image format.");
  2189. return false;
  2190. }
  2191. return true;
  2192. }
  2193. ImageContainer* imageParseKtx(bx::AllocatorI* _allocator, const void* _src, uint32_t _size, bx::Error* _err)
  2194. {
  2195. return imageParseT<KTX_MAGIC, imageParseKtx>(_allocator, _src, _size, _err);
  2196. }
  2197. // PVR3
  2198. #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) )
  2199. #define PVR3_MAGIC BX_MAKEFOURCC('P', 'V', 'R', 3)
  2200. #define PVR3_HEADER_SIZE 52
  2201. #define PVR3_PVRTC1_2BPP_RGB 0
  2202. #define PVR3_PVRTC1_2BPP_RGBA 1
  2203. #define PVR3_PVRTC1_4BPP_RGB 2
  2204. #define PVR3_PVRTC1_4BPP_RGBA 3
  2205. #define PVR3_PVRTC2_2BPP_RGBA 4
  2206. #define PVR3_PVRTC2_4BPP_RGBA 5
  2207. #define PVR3_ETC1 6
  2208. #define PVR3_DXT1 7
  2209. #define PVR3_DXT2 8
  2210. #define PVR3_DXT3 9
  2211. #define PVR3_DXT4 10
  2212. #define PVR3_DXT5 11
  2213. #define PVR3_BC4 12
  2214. #define PVR3_BC5 13
  2215. #define PVR3_R8 PVR3_MAKE8CC('r', 0, 0, 0, 8, 0, 0, 0)
  2216. #define PVR3_R16 PVR3_MAKE8CC('r', 0, 0, 0, 16, 0, 0, 0)
  2217. #define PVR3_R32 PVR3_MAKE8CC('r', 0, 0, 0, 32, 0, 0, 0)
  2218. #define PVR3_RG8 PVR3_MAKE8CC('r', 'g', 0, 0, 8, 8, 0, 0)
  2219. #define PVR3_RG16 PVR3_MAKE8CC('r', 'g', 0, 0, 16, 16, 0, 0)
  2220. #define PVR3_RG32 PVR3_MAKE8CC('r', 'g', 0, 0, 32, 32, 0, 0)
  2221. #define PVR3_BGRA8 PVR3_MAKE8CC('b', 'g', 'r', 'a', 8, 8, 8, 8)
  2222. #define PVR3_RGBA16 PVR3_MAKE8CC('r', 'g', 'b', 'a', 16, 16, 16, 16)
  2223. #define PVR3_RGBA32 PVR3_MAKE8CC('r', 'g', 'b', 'a', 32, 32, 32, 32)
  2224. #define PVR3_RGB565 PVR3_MAKE8CC('r', 'g', 'b', 0, 5, 6, 5, 0)
  2225. #define PVR3_RGBA4 PVR3_MAKE8CC('r', 'g', 'b', 'a', 4, 4, 4, 4)
  2226. #define PVR3_RGBA51 PVR3_MAKE8CC('r', 'g', 'b', 'a', 5, 5, 5, 1)
  2227. #define PVR3_RGB10A2 PVR3_MAKE8CC('r', 'g', 'b', 'a', 10, 10, 10, 2)
  2228. #define PVR3_CHANNEL_TYPE_ANY UINT32_MAX
  2229. #define PVR3_CHANNEL_TYPE_FLOAT UINT32_C(12)
  2230. struct TranslatePvr3Format
  2231. {
  2232. uint64_t m_format;
  2233. uint32_t m_channelTypeMask;
  2234. TextureFormat::Enum m_textureFormat;
  2235. };
  2236. static const TranslatePvr3Format s_translatePvr3Format[] =
  2237. {
  2238. { PVR3_PVRTC1_2BPP_RGB, PVR3_CHANNEL_TYPE_ANY, TextureFormat::PTC12 },
  2239. { PVR3_PVRTC1_2BPP_RGBA, PVR3_CHANNEL_TYPE_ANY, TextureFormat::PTC12A },
  2240. { PVR3_PVRTC1_4BPP_RGB, PVR3_CHANNEL_TYPE_ANY, TextureFormat::PTC14 },
  2241. { PVR3_PVRTC1_4BPP_RGBA, PVR3_CHANNEL_TYPE_ANY, TextureFormat::PTC14A },
  2242. { PVR3_PVRTC2_2BPP_RGBA, PVR3_CHANNEL_TYPE_ANY, TextureFormat::PTC22 },
  2243. { PVR3_PVRTC2_4BPP_RGBA, PVR3_CHANNEL_TYPE_ANY, TextureFormat::PTC24 },
  2244. { PVR3_ETC1, PVR3_CHANNEL_TYPE_ANY, TextureFormat::ETC1 },
  2245. { PVR3_DXT1, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC1 },
  2246. { PVR3_DXT2, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC2 },
  2247. { PVR3_DXT3, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC2 },
  2248. { PVR3_DXT4, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC3 },
  2249. { PVR3_DXT5, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC3 },
  2250. { PVR3_BC4, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC4 },
  2251. { PVR3_BC5, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC5 },
  2252. { PVR3_R8, PVR3_CHANNEL_TYPE_ANY, TextureFormat::R8 },
  2253. { PVR3_R16, PVR3_CHANNEL_TYPE_ANY, TextureFormat::R16U },
  2254. { PVR3_R16, PVR3_CHANNEL_TYPE_FLOAT, TextureFormat::R16F },
  2255. { PVR3_R32, PVR3_CHANNEL_TYPE_ANY, TextureFormat::R32U },
  2256. { PVR3_R32, PVR3_CHANNEL_TYPE_FLOAT, TextureFormat::R32F },
  2257. { PVR3_RG8, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RG8 },
  2258. { PVR3_RG16, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RG16 },
  2259. { PVR3_RG16, PVR3_CHANNEL_TYPE_FLOAT, TextureFormat::RG16F },
  2260. { PVR3_RG32, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RG16 },
  2261. { PVR3_RG32, PVR3_CHANNEL_TYPE_FLOAT, TextureFormat::RG32F },
  2262. { PVR3_BGRA8, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BGRA8 },
  2263. { PVR3_RGBA16, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RGBA16 },
  2264. { PVR3_RGBA16, PVR3_CHANNEL_TYPE_FLOAT, TextureFormat::RGBA16F },
  2265. { PVR3_RGBA32, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RGBA32U },
  2266. { PVR3_RGBA32, PVR3_CHANNEL_TYPE_FLOAT, TextureFormat::RGBA32F },
  2267. { PVR3_RGB565, PVR3_CHANNEL_TYPE_ANY, TextureFormat::R5G6B5 },
  2268. { PVR3_RGBA4, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RGBA4 },
  2269. { PVR3_RGBA51, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RGB5A1 },
  2270. { PVR3_RGB10A2, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RGB10A2 },
  2271. };
  2272. bool imageParsePvr3(ImageContainer& _imageContainer, bx::ReaderSeekerI* _reader, bx::Error* _err)
  2273. {
  2274. BX_ERROR_SCOPE(_err);
  2275. uint32_t flags;
  2276. bx::read(_reader, flags);
  2277. uint64_t pixelFormat;
  2278. bx::read(_reader, pixelFormat);
  2279. uint32_t colorSpace;
  2280. bx::read(_reader, colorSpace); // 0 - linearRGB, 1 - sRGB
  2281. uint32_t channelType;
  2282. bx::read(_reader, channelType);
  2283. uint32_t height;
  2284. bx::read(_reader, height);
  2285. uint32_t width;
  2286. bx::read(_reader, width);
  2287. uint32_t depth;
  2288. bx::read(_reader, depth);
  2289. uint32_t numSurfaces;
  2290. bx::read(_reader, numSurfaces);
  2291. uint32_t numFaces;
  2292. bx::read(_reader, numFaces);
  2293. uint32_t numMips;
  2294. bx::read(_reader, numMips);
  2295. uint32_t metaDataSize;
  2296. bx::read(_reader, metaDataSize);
  2297. // skip meta garbage...
  2298. int64_t offset = bx::skip(_reader, metaDataSize);
  2299. TextureFormat::Enum format = TextureFormat::Unknown;
  2300. bool hasAlpha = false;
  2301. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translatePvr3Format); ++ii)
  2302. {
  2303. if (s_translatePvr3Format[ii].m_format == pixelFormat
  2304. && channelType == (s_translatePvr3Format[ii].m_channelTypeMask & channelType) )
  2305. {
  2306. format = s_translatePvr3Format[ii].m_textureFormat;
  2307. break;
  2308. }
  2309. }
  2310. _imageContainer.m_allocator = NULL;
  2311. _imageContainer.m_data = NULL;
  2312. _imageContainer.m_size = 0;
  2313. _imageContainer.m_offset = (uint32_t)offset;
  2314. _imageContainer.m_width = width;
  2315. _imageContainer.m_height = height;
  2316. _imageContainer.m_depth = depth;
  2317. _imageContainer.m_format = format;
  2318. _imageContainer.m_numLayers = 1;
  2319. _imageContainer.m_numMips = uint8_t(bx::uint32_max(numMips, 1) );
  2320. _imageContainer.m_hasAlpha = hasAlpha;
  2321. _imageContainer.m_cubeMap = numFaces > 1;
  2322. _imageContainer.m_ktx = false;
  2323. _imageContainer.m_ktxLE = false;
  2324. _imageContainer.m_srgb = colorSpace > 0;
  2325. return TextureFormat::Unknown != format;
  2326. }
  2327. ImageContainer* imageParsePvr3(bx::AllocatorI* _allocator, const void* _src, uint32_t _size, bx::Error* _err)
  2328. {
  2329. return imageParseT<PVR3_MAGIC, imageParsePvr3>(_allocator, _src, _size, _err);
  2330. }
  2331. bool imageParse(ImageContainer& _imageContainer, bx::ReaderSeekerI* _reader, bx::Error* _err)
  2332. {
  2333. BX_ERROR_SCOPE(_err);
  2334. uint32_t magic;
  2335. bx::read(_reader, magic, _err);
  2336. if (DDS_MAGIC == magic)
  2337. {
  2338. return imageParseDds(_imageContainer, _reader, _err);
  2339. }
  2340. else if (KTX_MAGIC == magic)
  2341. {
  2342. return imageParseKtx(_imageContainer, _reader, _err);
  2343. }
  2344. else if (PVR3_MAGIC == magic)
  2345. {
  2346. return imageParsePvr3(_imageContainer, _reader, _err);
  2347. }
  2348. else if (BIMG_CHUNK_MAGIC_TEX == magic)
  2349. {
  2350. TextureCreate tc;
  2351. bx::read(_reader, tc);
  2352. _imageContainer.m_format = tc.m_format;
  2353. _imageContainer.m_offset = UINT32_MAX;
  2354. _imageContainer.m_allocator = NULL;
  2355. if (NULL == tc.m_mem)
  2356. {
  2357. _imageContainer.m_data = NULL;
  2358. _imageContainer.m_size = 0;
  2359. }
  2360. else
  2361. {
  2362. _imageContainer.m_data = tc.m_mem->data;
  2363. _imageContainer.m_size = tc.m_mem->size;
  2364. }
  2365. _imageContainer.m_width = tc.m_width;
  2366. _imageContainer.m_height = tc.m_height;
  2367. _imageContainer.m_depth = tc.m_depth;
  2368. _imageContainer.m_numLayers = tc.m_numLayers;
  2369. _imageContainer.m_numMips = tc.m_numMips;
  2370. _imageContainer.m_hasAlpha = false;
  2371. _imageContainer.m_cubeMap = tc.m_cubeMap;
  2372. _imageContainer.m_ktx = false;
  2373. _imageContainer.m_ktxLE = false;
  2374. _imageContainer.m_srgb = false;
  2375. return _err->isOk();
  2376. }
  2377. BX_TRACE("Unrecognized image format (magic: 0x%08x)!", magic);
  2378. BX_ERROR_SET(_err, BIMG_ERROR, "Unrecognized image format.");
  2379. return false;
  2380. }
  2381. bool imageParse(ImageContainer& _imageContainer, const void* _data, uint32_t _size, bx::Error* _err)
  2382. {
  2383. BX_ERROR_SCOPE(_err);
  2384. bx::MemoryReader reader(_data, _size);
  2385. return imageParse(_imageContainer, &reader, _err);
  2386. }
  2387. void imageDecodeToBgra8(void* _dst, const void* _src, uint32_t _width, uint32_t _height, uint32_t _dstPitch, TextureFormat::Enum _srcFormat)
  2388. {
  2389. const uint8_t* src = (const uint8_t*)_src;
  2390. uint8_t* dst = (uint8_t*)_dst;
  2391. uint32_t width = _width/4;
  2392. uint32_t height = _height/4;
  2393. uint8_t temp[16*4];
  2394. switch (_srcFormat)
  2395. {
  2396. case TextureFormat::BC1:
  2397. for (uint32_t yy = 0; yy < height; ++yy)
  2398. {
  2399. for (uint32_t xx = 0; xx < width; ++xx)
  2400. {
  2401. decodeBlockDxt1(temp, src);
  2402. src += 8;
  2403. uint8_t* block = &dst[yy*_dstPitch*4 + xx*16];
  2404. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  2405. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  2406. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  2407. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  2408. }
  2409. }
  2410. break;
  2411. case TextureFormat::BC2:
  2412. for (uint32_t yy = 0; yy < height; ++yy)
  2413. {
  2414. for (uint32_t xx = 0; xx < width; ++xx)
  2415. {
  2416. decodeBlockDxt23A(temp+3, src);
  2417. src += 8;
  2418. decodeBlockDxt(temp, src);
  2419. src += 8;
  2420. uint8_t* block = &dst[yy*_dstPitch*4 + xx*16];
  2421. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  2422. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  2423. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  2424. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  2425. }
  2426. }
  2427. break;
  2428. case TextureFormat::BC3:
  2429. for (uint32_t yy = 0; yy < height; ++yy)
  2430. {
  2431. for (uint32_t xx = 0; xx < width; ++xx)
  2432. {
  2433. decodeBlockDxt45A(temp+3, src);
  2434. src += 8;
  2435. decodeBlockDxt(temp, src);
  2436. src += 8;
  2437. uint8_t* block = &dst[yy*_dstPitch*4 + xx*16];
  2438. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  2439. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  2440. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  2441. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  2442. }
  2443. }
  2444. break;
  2445. case TextureFormat::BC4:
  2446. for (uint32_t yy = 0; yy < height; ++yy)
  2447. {
  2448. for (uint32_t xx = 0; xx < width; ++xx)
  2449. {
  2450. decodeBlockDxt45A(temp, src);
  2451. src += 8;
  2452. uint8_t* block = &dst[yy*_dstPitch*4 + xx*16];
  2453. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  2454. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  2455. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  2456. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  2457. }
  2458. }
  2459. break;
  2460. case TextureFormat::BC5:
  2461. for (uint32_t yy = 0; yy < height; ++yy)
  2462. {
  2463. for (uint32_t xx = 0; xx < width; ++xx)
  2464. {
  2465. decodeBlockDxt45A(temp+2, src);
  2466. src += 8;
  2467. decodeBlockDxt45A(temp+1, src);
  2468. src += 8;
  2469. for (uint32_t ii = 0; ii < 16; ++ii)
  2470. {
  2471. float nx = temp[ii*4+2]*2.0f/255.0f - 1.0f;
  2472. float ny = temp[ii*4+1]*2.0f/255.0f - 1.0f;
  2473. float nz = bx::fsqrt(1.0f - nx*nx - ny*ny);
  2474. temp[ii*4+0] = uint8_t( (nz + 1.0f)*255.0f/2.0f);
  2475. temp[ii*4+3] = 0;
  2476. }
  2477. uint8_t* block = &dst[yy*_dstPitch*4 + xx*16];
  2478. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  2479. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  2480. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  2481. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  2482. }
  2483. }
  2484. break;
  2485. case TextureFormat::ETC1:
  2486. case TextureFormat::ETC2:
  2487. for (uint32_t yy = 0; yy < height; ++yy)
  2488. {
  2489. for (uint32_t xx = 0; xx < width; ++xx)
  2490. {
  2491. decodeBlockEtc12(temp, src);
  2492. src += 8;
  2493. uint8_t* block = &dst[yy*_dstPitch*4 + xx*16];
  2494. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  2495. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  2496. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  2497. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  2498. }
  2499. }
  2500. break;
  2501. case TextureFormat::ETC2A:
  2502. BX_WARN(false, "ETC2A decoder is not implemented.");
  2503. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00) );
  2504. break;
  2505. case TextureFormat::ETC2A1:
  2506. BX_WARN(false, "ETC2A1 decoder is not implemented.");
  2507. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xffff0000) );
  2508. break;
  2509. case TextureFormat::PTC12:
  2510. BX_WARN(false, "PTC12 decoder is not implemented.");
  2511. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xffff00ff) );
  2512. break;
  2513. case TextureFormat::PTC12A:
  2514. BX_WARN(false, "PTC12A decoder is not implemented.");
  2515. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xffffff00) );
  2516. break;
  2517. case TextureFormat::PTC14:
  2518. for (uint32_t yy = 0; yy < height; ++yy)
  2519. {
  2520. for (uint32_t xx = 0; xx < width; ++xx)
  2521. {
  2522. decodeBlockPtc14(temp, src, xx, yy, width, height);
  2523. uint8_t* block = &dst[yy*_dstPitch*4 + xx*16];
  2524. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  2525. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  2526. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  2527. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  2528. }
  2529. }
  2530. break;
  2531. case TextureFormat::PTC14A:
  2532. for (uint32_t yy = 0; yy < height; ++yy)
  2533. {
  2534. for (uint32_t xx = 0; xx < width; ++xx)
  2535. {
  2536. decodeBlockPtc14A(temp, src, xx, yy, width, height);
  2537. uint8_t* block = &dst[yy*_dstPitch*4 + xx*16];
  2538. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  2539. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  2540. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  2541. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  2542. }
  2543. }
  2544. break;
  2545. case TextureFormat::PTC22:
  2546. BX_WARN(false, "PTC22 decoder is not implemented.");
  2547. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff00ff00), UINT32_C(0xff0000ff) );
  2548. break;
  2549. case TextureFormat::PTC24:
  2550. BX_WARN(false, "PTC24 decoder is not implemented.");
  2551. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xffffffff) );
  2552. break;
  2553. case TextureFormat::RGBA8:
  2554. imageSwizzleBgra8(_dst, _width, _height, _dstPitch, _src);
  2555. break;
  2556. case TextureFormat::BGRA8:
  2557. bx::memCopy(_dst, _src, _dstPitch*_height);
  2558. break;
  2559. default:
  2560. {
  2561. const uint32_t srcBpp = s_imageBlockInfo[_srcFormat].bitsPerPixel;
  2562. const uint32_t srcPitch = _width * srcBpp / 8;
  2563. if (!imageConvert(_dst, TextureFormat::BGRA8, _src, _srcFormat, _width, _height, srcPitch) )
  2564. {
  2565. // Failed to convert, just make ugly red-yellow checkerboard texture.
  2566. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xffff0000), UINT32_C(0xffffff00) );
  2567. }
  2568. }
  2569. break;
  2570. }
  2571. }
  2572. void imageDecodeToRgba8(void* _dst, const void* _src, uint32_t _width, uint32_t _height, uint32_t _dstPitch, TextureFormat::Enum _srcFormat)
  2573. {
  2574. switch (_srcFormat)
  2575. {
  2576. case TextureFormat::RGBA8:
  2577. bx::memCopy(_dst, _src, _dstPitch*_height);
  2578. break;
  2579. case TextureFormat::BGRA8:
  2580. imageSwizzleBgra8(_dst, _width, _height, _dstPitch, _src);
  2581. break;
  2582. default:
  2583. imageDecodeToBgra8(_dst, _src, _width, _height, _dstPitch, _srcFormat);
  2584. imageSwizzleBgra8(_dst, _width, _height, _dstPitch, _dst);
  2585. break;
  2586. }
  2587. }
  2588. void imageRgba8ToRgba32fRef(void* _dst, uint32_t _width, uint32_t _height, uint32_t _srcPitch, const void* _src)
  2589. {
  2590. const uint32_t dstWidth = _width;
  2591. const uint32_t dstHeight = _height;
  2592. if (0 == dstWidth
  2593. || 0 == dstHeight)
  2594. {
  2595. return;
  2596. }
  2597. float* dst = (float*)_dst;
  2598. const uint8_t* src = (const uint8_t*)_src;
  2599. for (uint32_t yy = 0, ystep = _srcPitch; yy < dstHeight; ++yy, src += ystep)
  2600. {
  2601. const uint8_t* rgba = src;
  2602. for (uint32_t xx = 0; xx < dstWidth; ++xx, rgba += 4, dst += 4)
  2603. {
  2604. dst[0] = bx::fpow(rgba[0], 2.2f);
  2605. dst[1] = bx::fpow(rgba[1], 2.2f);
  2606. dst[2] = bx::fpow(rgba[2], 2.2f);
  2607. dst[3] = rgba[3];
  2608. }
  2609. }
  2610. }
  2611. void imageRgba8ToRgba32f(void* _dst, uint32_t _width, uint32_t _height, uint32_t _srcPitch, const void* _src)
  2612. {
  2613. const uint32_t dstWidth = _width;
  2614. const uint32_t dstHeight = _height;
  2615. if (0 == dstWidth
  2616. || 0 == dstHeight)
  2617. {
  2618. return;
  2619. }
  2620. float* dst = (float*)_dst;
  2621. const uint8_t* src = (const uint8_t*)_src;
  2622. using namespace bx;
  2623. const simd128_t unpack = simd_ld(1.0f, 1.0f/256.0f, 1.0f/65536.0f, 1.0f/16777216.0f);
  2624. const simd128_t umask = simd_ild(0xff, 0xff00, 0xff0000, 0xff000000);
  2625. const simd128_t wflip = simd_ild(0, 0, 0, 0x80000000);
  2626. const simd128_t wadd = simd_ld(0.0f, 0.0f, 0.0f, 32768.0f*65536.0f);
  2627. for (uint32_t yy = 0, ystep = _srcPitch; yy < dstHeight; ++yy, src += ystep)
  2628. {
  2629. const uint8_t* rgba = src;
  2630. for (uint32_t xx = 0; xx < dstWidth; ++xx, rgba += 4, dst += 4)
  2631. {
  2632. const simd128_t abgr0 = simd_splat(rgba);
  2633. const simd128_t abgr0m = simd_and(abgr0, umask);
  2634. const simd128_t abgr0x = simd_xor(abgr0m, wflip);
  2635. const simd128_t abgr0f = simd_itof(abgr0x);
  2636. const simd128_t abgr0c = simd_add(abgr0f, wadd);
  2637. const simd128_t abgr0n = simd_mul(abgr0c, unpack);
  2638. simd_st(dst, abgr0n);
  2639. }
  2640. }
  2641. }
  2642. void imageDecodeToRgba32f(bx::AllocatorI* _allocator, void* _dst, const void* _src, uint32_t _width, uint32_t _height, uint32_t _dstPitch, TextureFormat::Enum _format)
  2643. {
  2644. const uint8_t* src = (const uint8_t*)_src;
  2645. uint8_t* dst = (uint8_t*)_dst;
  2646. switch (_format)
  2647. {
  2648. case TextureFormat::BC5:
  2649. {
  2650. uint32_t width = _width/4;
  2651. uint32_t height = _height/4;
  2652. for (uint32_t yy = 0; yy < height; ++yy)
  2653. {
  2654. for (uint32_t xx = 0; xx < width; ++xx)
  2655. {
  2656. uint8_t temp[16*4];
  2657. decodeBlockDxt45A(temp+2, src);
  2658. src += 8;
  2659. decodeBlockDxt45A(temp+1, src);
  2660. src += 8;
  2661. for (uint32_t ii = 0; ii < 16; ++ii)
  2662. {
  2663. float nx = temp[ii*4+2]*2.0f/255.0f - 1.0f;
  2664. float ny = temp[ii*4+1]*2.0f/255.0f - 1.0f;
  2665. float nz = bx::fsqrt(1.0f - nx*nx - ny*ny);
  2666. const uint32_t offset = (yy*4 + ii/4)*_width*16 + (xx*4 + ii%4)*16;
  2667. float* block = (float*)&dst[offset];
  2668. block[0] = nx;
  2669. block[1] = ny;
  2670. block[2] = nz;
  2671. block[3] = 0.0f;
  2672. }
  2673. }
  2674. }
  2675. }
  2676. break;
  2677. case TextureFormat::RGBA32F:
  2678. bx::memCopy(_dst, _src, _dstPitch*_height);
  2679. break;
  2680. default:
  2681. if (isCompressed(_format) )
  2682. {
  2683. uint32_t size = imageGetSize(NULL, uint16_t(_width), uint16_t(_height), 0, false, false, 1, TextureFormat::RGBA8);
  2684. void* temp = BX_ALLOC(_allocator, size);
  2685. imageDecodeToRgba8(temp, _src, _width, _height, _width*4, _format);
  2686. imageRgba8ToRgba32f(_dst, _width, _height, _width*4, temp);
  2687. BX_FREE(_allocator, temp);
  2688. }
  2689. else
  2690. {
  2691. const uint32_t srcBpp = s_imageBlockInfo[_format].bitsPerPixel;
  2692. imageConvert(_dst, TextureFormat::RGBA32F, _src, _format, _width, _height, _width*srcBpp/8);
  2693. }
  2694. break;
  2695. }
  2696. }
  2697. bool imageGetRawData(const ImageContainer& _imageContainer, uint16_t _side, uint8_t _lod, const void* _data, uint32_t _size, ImageMip& _mip)
  2698. {
  2699. uint32_t offset = _imageContainer.m_offset;
  2700. TextureFormat::Enum format = TextureFormat::Enum(_imageContainer.m_format);
  2701. bool hasAlpha = _imageContainer.m_hasAlpha;
  2702. const ImageBlockInfo& blockInfo = s_imageBlockInfo[format];
  2703. const uint8_t bpp = blockInfo.bitsPerPixel;
  2704. const uint32_t blockSize = blockInfo.blockSize;
  2705. const uint32_t blockWidth = blockInfo.blockWidth;
  2706. const uint32_t blockHeight = blockInfo.blockHeight;
  2707. const uint32_t minBlockX = blockInfo.minBlockX;
  2708. const uint32_t minBlockY = blockInfo.minBlockY;
  2709. if (UINT32_MAX == _imageContainer.m_offset)
  2710. {
  2711. if (NULL == _imageContainer.m_data)
  2712. {
  2713. return false;
  2714. }
  2715. offset = 0;
  2716. _data = _imageContainer.m_data;
  2717. _size = _imageContainer.m_size;
  2718. }
  2719. const uint8_t* data = (const uint8_t*)_data;
  2720. const uint16_t numSides = _imageContainer.m_numLayers * (_imageContainer.m_cubeMap ? 6 : 1);
  2721. if (_imageContainer.m_ktx)
  2722. {
  2723. uint32_t width = _imageContainer.m_width;
  2724. uint32_t height = _imageContainer.m_height;
  2725. uint32_t depth = _imageContainer.m_depth;
  2726. for (uint8_t lod = 0, num = _imageContainer.m_numMips; lod < num; ++lod)
  2727. {
  2728. width = bx::uint32_max(blockWidth * minBlockX, ( (width + blockWidth - 1) / blockWidth )*blockWidth);
  2729. height = bx::uint32_max(blockHeight * minBlockY, ( (height + blockHeight - 1) / blockHeight)*blockHeight);
  2730. depth = bx::uint32_max(1, depth);
  2731. const uint32_t mipSize = width*height*depth*bpp/8;
  2732. const uint32_t size = mipSize*numSides;
  2733. uint32_t imageSize = bx::toHostEndian(*(const uint32_t*)&data[offset], _imageContainer.m_ktxLE);
  2734. BX_CHECK(size == imageSize, "KTX: Image size mismatch %d (expected %d).", size, imageSize);
  2735. BX_UNUSED(size, imageSize);
  2736. offset += sizeof(uint32_t);
  2737. for (uint16_t side = 0; side < numSides; ++side)
  2738. {
  2739. if (side == _side
  2740. && lod == _lod)
  2741. {
  2742. _mip.m_width = width;
  2743. _mip.m_height = height;
  2744. _mip.m_blockSize = blockSize;
  2745. _mip.m_size = mipSize;
  2746. _mip.m_data = &data[offset];
  2747. _mip.m_bpp = bpp;
  2748. _mip.m_format = format;
  2749. _mip.m_hasAlpha = hasAlpha;
  2750. return true;
  2751. }
  2752. offset += mipSize;
  2753. BX_CHECK(offset <= _size, "Reading past size of data buffer! (offset %d, size %d)", offset, _size);
  2754. BX_UNUSED(_size);
  2755. }
  2756. width >>= 1;
  2757. height >>= 1;
  2758. depth >>= 1;
  2759. }
  2760. }
  2761. else
  2762. {
  2763. for (uint16_t side = 0; side < numSides; ++side)
  2764. {
  2765. uint32_t width = _imageContainer.m_width;
  2766. uint32_t height = _imageContainer.m_height;
  2767. uint32_t depth = _imageContainer.m_depth;
  2768. for (uint8_t lod = 0, num = _imageContainer.m_numMips; lod < num; ++lod)
  2769. {
  2770. width = bx::uint32_max(blockWidth * minBlockX, ( (width + blockWidth - 1) / blockWidth )*blockWidth);
  2771. height = bx::uint32_max(blockHeight * minBlockY, ( (height + blockHeight - 1) / blockHeight)*blockHeight);
  2772. depth = bx::uint32_max(1, depth);
  2773. uint32_t size = width*height*depth*bpp/8;
  2774. if (side == _side
  2775. && lod == _lod)
  2776. {
  2777. _mip.m_width = width;
  2778. _mip.m_height = height;
  2779. _mip.m_blockSize = blockSize;
  2780. _mip.m_size = size;
  2781. _mip.m_data = &data[offset];
  2782. _mip.m_bpp = bpp;
  2783. _mip.m_format = format;
  2784. _mip.m_hasAlpha = hasAlpha;
  2785. return true;
  2786. }
  2787. offset += size;
  2788. BX_CHECK(offset <= _size, "Reading past size of data buffer! (offset %d, size %d)", offset, _size);
  2789. BX_UNUSED(_size);
  2790. width >>= 1;
  2791. height >>= 1;
  2792. depth >>= 1;
  2793. }
  2794. }
  2795. }
  2796. return false;
  2797. }
  2798. int32_t imageWriteTga(bx::WriterI* _writer, uint32_t _width, uint32_t _height, uint32_t _srcPitch, const void* _src, bool _grayscale, bool _yflip, bx::Error* _err)
  2799. {
  2800. BX_ERROR_SCOPE(_err);
  2801. uint8_t type = _grayscale ? 3 : 2;
  2802. uint8_t bpp = _grayscale ? 8 : 32;
  2803. uint8_t header[18] = {};
  2804. header[ 2] = type;
  2805. header[12] = _width &0xff;
  2806. header[13] = (_width >>8)&0xff;
  2807. header[14] = _height &0xff;
  2808. header[15] = (_height>>8)&0xff;
  2809. header[16] = bpp;
  2810. header[17] = 32;
  2811. int32_t total = 0;
  2812. total += bx::write(_writer, header, sizeof(header), _err);
  2813. uint32_t dstPitch = _width*bpp/8;
  2814. if (_yflip)
  2815. {
  2816. uint8_t* data = (uint8_t*)_src + _srcPitch*_height - _srcPitch;
  2817. for (uint32_t yy = 0; yy < _height && _err->isOk(); ++yy)
  2818. {
  2819. total += bx::write(_writer, data, dstPitch, _err);
  2820. data -= _srcPitch;
  2821. }
  2822. }
  2823. else if (_srcPitch == dstPitch)
  2824. {
  2825. total += bx::write(_writer, _src, _height*_srcPitch, _err);
  2826. }
  2827. else
  2828. {
  2829. uint8_t* data = (uint8_t*)_src;
  2830. for (uint32_t yy = 0; yy < _height && _err->isOk(); ++yy)
  2831. {
  2832. total += bx::write(_writer, data, dstPitch, _err);
  2833. data += _srcPitch;
  2834. }
  2835. }
  2836. return total;
  2837. }
  2838. static int32_t imageWriteDdsHeader(bx::WriterI* _writer, TextureFormat::Enum _format, bool _cubeMap, uint32_t _width, uint32_t _height, uint32_t _depth, uint8_t _numMips, bx::Error* _err)
  2839. {
  2840. BX_ERROR_SCOPE(_err);
  2841. uint32_t ddspf = UINT32_MAX;
  2842. uint32_t dxgiFormat = UINT32_MAX;
  2843. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateDdsPixelFormat); ++ii)
  2844. {
  2845. if (s_translateDdsPixelFormat[ii].m_textureFormat == _format)
  2846. {
  2847. ddspf = ii;
  2848. break;
  2849. }
  2850. }
  2851. if (UINT32_MAX == ddspf)
  2852. {
  2853. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateDxgiFormat); ++ii)
  2854. {
  2855. if (s_translateDxgiFormat[ii].m_textureFormat == _format)
  2856. {
  2857. dxgiFormat = s_translateDxgiFormat[ii].m_format;
  2858. break;
  2859. }
  2860. }
  2861. if (UINT32_MAX == dxgiFormat)
  2862. {
  2863. BX_ERROR_SET(_err, BIMG_ERROR, "DDS: DXGI format not supported.");
  2864. return 0;
  2865. }
  2866. }
  2867. const uint32_t bpp = getBitsPerPixel(_format);
  2868. uint32_t total = 0;
  2869. total += bx::write(_writer, uint32_t(DDS_MAGIC), _err);
  2870. uint32_t headerStart = total;
  2871. total += bx::write(_writer, uint32_t(DDS_HEADER_SIZE), _err);
  2872. total += bx::write(_writer, uint32_t(0
  2873. | DDSD_HEIGHT
  2874. | DDSD_WIDTH
  2875. | DDSD_PIXELFORMAT
  2876. | DDSD_CAPS
  2877. | (1 < _depth ? DDSD_DEPTH : 0)
  2878. | (1 < _numMips ? DDSD_MIPMAPCOUNT : 0)
  2879. | (isCompressed(_format) ? DDSD_LINEARSIZE : DDSD_PITCH)
  2880. )
  2881. , _err
  2882. );
  2883. const uint32_t pitchOrLinearSize = isCompressed(_format)
  2884. ? _width*_height*bpp/8
  2885. : _width*bpp/8
  2886. ;
  2887. total += bx::write(_writer, _height, _err);
  2888. total += bx::write(_writer, _width, _err);
  2889. total += bx::write(_writer, pitchOrLinearSize, _err);
  2890. total += bx::write(_writer, _depth, _err);
  2891. total += bx::write(_writer, uint32_t(_numMips), _err);
  2892. total += bx::writeRep(_writer, 0, 44, _err); // reserved1
  2893. if (UINT32_MAX != ddspf)
  2894. {
  2895. const TranslateDdsPixelFormat& pf = s_translateDdsPixelFormat[ddspf];
  2896. total += bx::write(_writer, uint32_t(8*sizeof(uint32_t) ), _err); // pixelFormatSize
  2897. total += bx::write(_writer, pf.m_flags, _err);
  2898. total += bx::write(_writer, uint32_t(0), _err);
  2899. total += bx::write(_writer, pf.m_bitCount, _err);
  2900. total += bx::write(_writer, pf.m_bitmask, _err);
  2901. }
  2902. else
  2903. {
  2904. total += bx::write(_writer, uint32_t(8*sizeof(uint32_t) ), _err); // pixelFormatSize
  2905. total += bx::write(_writer, uint32_t(DDPF_FOURCC), _err);
  2906. total += bx::write(_writer, uint32_t(DDS_DX10), _err);
  2907. total += bx::write(_writer, uint32_t(0), _err); // bitCount
  2908. total += bx::writeRep(_writer, 0, 4*sizeof(uint32_t), _err); // bitmask
  2909. }
  2910. uint32_t caps[4] =
  2911. {
  2912. uint32_t(DDSCAPS_TEXTURE | (1 < _numMips ? DDSCAPS_COMPLEX|DDSCAPS_MIPMAP : 0) ),
  2913. uint32_t(_cubeMap ? DDSCAPS2_CUBEMAP|DSCAPS2_CUBEMAP_ALLSIDES : 0),
  2914. 0,
  2915. 0,
  2916. };
  2917. total += bx::write(_writer, caps, sizeof(caps) );
  2918. total += bx::writeRep(_writer, 0, 4, _err); // reserved2
  2919. BX_WARN(total-headerStart == DDS_HEADER_SIZE
  2920. , "DDS: Failed to write header size %d (expected: %d)."
  2921. , total-headerStart
  2922. , DDS_HEADER_SIZE
  2923. );
  2924. if (UINT32_MAX != dxgiFormat)
  2925. {
  2926. total += bx::write(_writer, dxgiFormat);
  2927. total += bx::write(_writer, uint32_t(1 < _depth ? DDS_DX10_DIMENSION_TEXTURE3D : DDS_DX10_DIMENSION_TEXTURE2D), _err); // dims
  2928. total += bx::write(_writer, uint32_t(_cubeMap ? DDS_DX10_MISC_TEXTURECUBE : 0), _err); // miscFlags
  2929. total += bx::write(_writer, uint32_t(1), _err); // arraySize
  2930. total += bx::write(_writer, uint32_t(0), _err); // miscFlags2
  2931. BX_WARN(total-headerStart == DDS_HEADER_SIZE+20
  2932. , "DDS: Failed to write header size %d (expected: %d)."
  2933. , total-headerStart
  2934. , DDS_HEADER_SIZE+20
  2935. );
  2936. BX_UNUSED(headerStart);
  2937. }
  2938. return total;
  2939. }
  2940. int32_t imageWriteDds(bx::WriterI* _writer, ImageContainer& _imageContainer, const void* _data, uint32_t _size, bx::Error* _err)
  2941. {
  2942. BX_ERROR_SCOPE(_err);
  2943. int32_t total = 0;
  2944. total += imageWriteDdsHeader(_writer
  2945. , TextureFormat::Enum(_imageContainer.m_format)
  2946. , _imageContainer.m_cubeMap
  2947. , _imageContainer.m_width
  2948. , _imageContainer.m_height
  2949. , _imageContainer.m_depth
  2950. , _imageContainer.m_numMips
  2951. , _err
  2952. );
  2953. if (!_err->isOk() )
  2954. {
  2955. return total;
  2956. }
  2957. for (uint8_t side = 0, numSides = _imageContainer.m_cubeMap ? 6 : 1; side < numSides && _err->isOk(); ++side)
  2958. {
  2959. for (uint8_t lod = 0, num = _imageContainer.m_numMips; lod < num && _err->isOk(); ++lod)
  2960. {
  2961. ImageMip mip;
  2962. if (imageGetRawData(_imageContainer, side, lod, _data, _size, mip) )
  2963. {
  2964. total += bx::write(_writer, mip.m_data, mip.m_size, _err);
  2965. }
  2966. }
  2967. }
  2968. return total;
  2969. }
  2970. static int32_t imageWriteKtxHeader(bx::WriterI* _writer, TextureFormat::Enum _format, bool _cubeMap, uint32_t _width, uint32_t _height, uint32_t _depth, uint8_t _numMips, uint32_t _numLayers, bx::Error* _err)
  2971. {
  2972. BX_ERROR_SCOPE(_err);
  2973. const KtxFormatInfo& tfi = s_translateKtxFormat[_format];
  2974. int32_t total = 0;
  2975. total += bx::write(_writer, "\xabKTX 11\xbb\r\n\x1a\n", 12, _err);
  2976. total += bx::write(_writer, uint32_t(0x04030201), _err);
  2977. total += bx::write(_writer, uint32_t(0), _err); // glType
  2978. total += bx::write(_writer, uint32_t(1), _err); // glTypeSize
  2979. total += bx::write(_writer, uint32_t(0), _err); // glFormat
  2980. total += bx::write(_writer, tfi.m_internalFmt, _err); // glInternalFormat
  2981. total += bx::write(_writer, tfi.m_fmt, _err); // glBaseInternalFormat
  2982. total += bx::write(_writer, _width, _err);
  2983. total += bx::write(_writer, _height, _err);
  2984. total += bx::write(_writer, _depth, _err);
  2985. total += bx::write(_writer, _numLayers, _err); // numberOfArrayElements
  2986. total += bx::write(_writer, _cubeMap ? uint32_t(6) : uint32_t(0), _err);
  2987. total += bx::write(_writer, uint32_t(_numMips), _err);
  2988. total += bx::write(_writer, uint32_t(0), _err); // Meta-data size.
  2989. BX_WARN(total == 64, "KTX: Failed to write header size %d (expected: %d).", total, 64);
  2990. return total;
  2991. }
  2992. int32_t imageWriteKtx(bx::WriterI* _writer, TextureFormat::Enum _format, bool _cubeMap, uint32_t _width, uint32_t _height, uint32_t _depth, uint8_t _numMips, uint32_t _numLayers, const void* _src, bx::Error* _err)
  2993. {
  2994. BX_ERROR_SCOPE(_err);
  2995. int32_t total = 0;
  2996. total += imageWriteKtxHeader(_writer, _format, _cubeMap, _width, _height, _depth, _numMips, _numLayers, _err);
  2997. if (!_err->isOk() )
  2998. {
  2999. return total;
  3000. }
  3001. const ImageBlockInfo& blockInfo = s_imageBlockInfo[_format];
  3002. const uint8_t bpp = blockInfo.bitsPerPixel;
  3003. const uint32_t blockWidth = blockInfo.blockWidth;
  3004. const uint32_t blockHeight = blockInfo.blockHeight;
  3005. const uint32_t minBlockX = blockInfo.minBlockX;
  3006. const uint32_t minBlockY = blockInfo.minBlockY;
  3007. const uint8_t* src = (const uint8_t*)_src;
  3008. const uint32_t numLayers = bx::uint32_max(_numLayers, 1);
  3009. const uint32_t numSides = _cubeMap ? 6 : 1;
  3010. uint32_t width = _width;
  3011. uint32_t height = _height;
  3012. uint32_t depth = _depth;
  3013. for (uint8_t lod = 0; lod < _numMips && _err->isOk(); ++lod)
  3014. {
  3015. width = bx::uint32_max(blockWidth * minBlockX, ( (width + blockWidth - 1) / blockWidth )*blockWidth);
  3016. height = bx::uint32_max(blockHeight * minBlockY, ( (height + blockHeight - 1) / blockHeight)*blockHeight);
  3017. depth = bx::uint32_max(1, depth);
  3018. const uint32_t mipSize = width*height*depth*bpp/8;
  3019. const uint32_t size = mipSize*numLayers*numSides;
  3020. total += bx::write(_writer, size, _err);
  3021. for (uint32_t layer = 0; layer < numLayers && _err->isOk(); ++layer)
  3022. {
  3023. for (uint8_t side = 0; side < numSides && _err->isOk(); ++side)
  3024. {
  3025. total += bx::write(_writer, src, size, _err);
  3026. src += size;
  3027. }
  3028. }
  3029. width >>= 1;
  3030. height >>= 1;
  3031. depth >>= 1;
  3032. }
  3033. return total;
  3034. }
  3035. int32_t imageWriteKtx(bx::WriterI* _writer, ImageContainer& _imageContainer, const void* _data, uint32_t _size, bx::Error* _err)
  3036. {
  3037. BX_ERROR_SCOPE(_err);
  3038. int32_t total = 0;
  3039. total += imageWriteKtxHeader(_writer
  3040. , TextureFormat::Enum(_imageContainer.m_format)
  3041. , _imageContainer.m_cubeMap
  3042. , _imageContainer.m_width
  3043. , _imageContainer.m_height
  3044. , _imageContainer.m_depth
  3045. , _imageContainer.m_numMips
  3046. , _imageContainer.m_numLayers
  3047. , _err
  3048. );
  3049. if (!_err->isOk() )
  3050. {
  3051. return total;
  3052. }
  3053. const uint32_t numMips = _imageContainer.m_numMips;
  3054. const uint32_t numLayers = bx::uint32_max(_imageContainer.m_numLayers, 1);
  3055. const uint32_t numSides = _imageContainer.m_cubeMap ? 6 : 1;
  3056. for (uint8_t lod = 0; lod < numMips && _err->isOk(); ++lod)
  3057. {
  3058. ImageMip mip;
  3059. imageGetRawData(_imageContainer, 0, lod, _data, _size, mip);
  3060. const uint32_t size = mip.m_size*numSides*numLayers;
  3061. total += bx::write(_writer, size, _err);
  3062. for (uint32_t layer = 0; layer < numLayers && _err->isOk(); ++layer)
  3063. {
  3064. for (uint8_t side = 0; side < numSides && _err->isOk(); ++side)
  3065. {
  3066. if (imageGetRawData(_imageContainer, uint16_t(layer*numSides + side), lod, _data, _size, mip) )
  3067. {
  3068. total += bx::write(_writer, mip.m_data, mip.m_size, _err);
  3069. }
  3070. }
  3071. }
  3072. }
  3073. return total;
  3074. }
  3075. } // namespace bimg