image.cpp 201 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799
  1. /*
  2. * Copyright 2011-2021 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bimg#license-bsd-2-clause
  4. */
  5. #include "bimg_p.h"
  6. #include <bx/hash.h>
  7. #include <astc-codec/astc-codec.h>
  8. #include <bx/debug.h>
  9. namespace bimg
  10. {
  11. static const ImageBlockInfo s_imageBlockInfo[] =
  12. {
  13. // +--------------------------------------------- bits per pixel
  14. // | +----------------------------------------- block width
  15. // | | +-------------------------------------- block height
  16. // | | | +---------------------------------- block size
  17. // | | | | +------------------------------- min blocks x
  18. // | | | | | +---------------------------- min blocks y
  19. // | | | | | | +------------------------ depth bits
  20. // | | | | | | | +--------------------- stencil bits
  21. // | | | | | | | | +---+---+---+----- r, g, b, a bits
  22. // | | | | | | | | r g b a +-- encoding type
  23. // | | | | | | | | | | | | |
  24. { 4, 4, 4, 8, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // BC1
  25. { 8, 4, 4, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // BC2
  26. { 8, 4, 4, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // BC3
  27. { 4, 4, 4, 8, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // BC4
  28. { 8, 4, 4, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // BC5
  29. { 8, 4, 4, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Float) }, // BC6H
  30. { 8, 4, 4, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // BC7
  31. { 4, 4, 4, 8, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ETC1
  32. { 4, 4, 4, 8, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ETC2
  33. { 8, 4, 4, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ETC2A
  34. { 4, 4, 4, 8, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ETC2A1
  35. { 2, 8, 4, 8, 2, 2, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // PTC12
  36. { 4, 4, 4, 8, 2, 2, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // PTC14
  37. { 2, 8, 4, 8, 2, 2, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // PTC12A
  38. { 4, 4, 4, 8, 2, 2, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // PTC14A
  39. { 2, 8, 4, 8, 2, 2, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // PTC22
  40. { 4, 4, 4, 8, 2, 2, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // PTC24
  41. { 4, 4, 4, 8, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ATC
  42. { 8, 4, 4, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ATCE
  43. { 8, 4, 4, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ATCI
  44. { 8, 4, 4, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ASTC4x4
  45. { 6, 5, 5, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ASTC5x5
  46. { 4, 6, 6, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ASTC6x6
  47. { 4, 8, 5, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ASTC8x5
  48. { 3, 8, 6, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ASTC8x6
  49. { 3, 10, 5, 16, 1, 1, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // ASTC10x5
  50. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Count) }, // Unknown
  51. { 1, 8, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // R1
  52. { 8, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 8, uint8_t(bx::EncodingType::Unorm) }, // A8
  53. { 8, 1, 1, 1, 1, 1, 0, 0, 8, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // R8
  54. { 8, 1, 1, 1, 1, 1, 0, 0, 8, 0, 0, 0, uint8_t(bx::EncodingType::Int ) }, // R8I
  55. { 8, 1, 1, 1, 1, 1, 0, 0, 8, 0, 0, 0, uint8_t(bx::EncodingType::Uint ) }, // R8U
  56. { 8, 1, 1, 1, 1, 1, 0, 0, 8, 0, 0, 0, uint8_t(bx::EncodingType::Snorm) }, // R8S
  57. { 16, 1, 1, 2, 1, 1, 0, 0, 16, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // R16
  58. { 16, 1, 1, 2, 1, 1, 0, 0, 16, 0, 0, 0, uint8_t(bx::EncodingType::Int ) }, // R16I
  59. { 16, 1, 1, 2, 1, 1, 0, 0, 16, 0, 0, 0, uint8_t(bx::EncodingType::Uint ) }, // R16U
  60. { 16, 1, 1, 2, 1, 1, 0, 0, 16, 0, 0, 0, uint8_t(bx::EncodingType::Float) }, // R16F
  61. { 16, 1, 1, 2, 1, 1, 0, 0, 16, 0, 0, 0, uint8_t(bx::EncodingType::Snorm) }, // R16S
  62. { 32, 1, 1, 4, 1, 1, 0, 0, 32, 0, 0, 0, uint8_t(bx::EncodingType::Int ) }, // R32I
  63. { 32, 1, 1, 4, 1, 1, 0, 0, 32, 0, 0, 0, uint8_t(bx::EncodingType::Uint ) }, // R32U
  64. { 32, 1, 1, 4, 1, 1, 0, 0, 32, 0, 0, 0, uint8_t(bx::EncodingType::Float) }, // R32F
  65. { 16, 1, 1, 2, 1, 1, 0, 0, 8, 8, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // RG8
  66. { 16, 1, 1, 2, 1, 1, 0, 0, 8, 8, 0, 0, uint8_t(bx::EncodingType::Int ) }, // RG8I
  67. { 16, 1, 1, 2, 1, 1, 0, 0, 8, 8, 0, 0, uint8_t(bx::EncodingType::Uint ) }, // RG8U
  68. { 16, 1, 1, 2, 1, 1, 0, 0, 8, 8, 0, 0, uint8_t(bx::EncodingType::Snorm) }, // RG8S
  69. { 32, 1, 1, 4, 1, 1, 0, 0, 16, 16, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // RG16
  70. { 32, 1, 1, 4, 1, 1, 0, 0, 16, 16, 0, 0, uint8_t(bx::EncodingType::Int ) }, // RG16I
  71. { 32, 1, 1, 4, 1, 1, 0, 0, 16, 16, 0, 0, uint8_t(bx::EncodingType::Uint ) }, // RG16U
  72. { 32, 1, 1, 4, 1, 1, 0, 0, 16, 16, 0, 0, uint8_t(bx::EncodingType::Float) }, // RG16F
  73. { 32, 1, 1, 4, 1, 1, 0, 0, 16, 16, 0, 0, uint8_t(bx::EncodingType::Snorm) }, // RG16S
  74. { 64, 1, 1, 8, 1, 1, 0, 0, 32, 32, 0, 0, uint8_t(bx::EncodingType::Int ) }, // RG32I
  75. { 64, 1, 1, 8, 1, 1, 0, 0, 32, 32, 0, 0, uint8_t(bx::EncodingType::Uint ) }, // RG32U
  76. { 64, 1, 1, 8, 1, 1, 0, 0, 32, 32, 0, 0, uint8_t(bx::EncodingType::Float) }, // RG32F
  77. { 24, 1, 1, 3, 1, 1, 0, 0, 8, 8, 8, 0, uint8_t(bx::EncodingType::Unorm) }, // RGB8
  78. { 24, 1, 1, 3, 1, 1, 0, 0, 8, 8, 8, 0, uint8_t(bx::EncodingType::Int ) }, // RGB8I
  79. { 24, 1, 1, 3, 1, 1, 0, 0, 8, 8, 8, 0, uint8_t(bx::EncodingType::Uint ) }, // RGB8U
  80. { 24, 1, 1, 3, 1, 1, 0, 0, 8, 8, 8, 0, uint8_t(bx::EncodingType::Snorm) }, // RGB8S
  81. { 32, 1, 1, 4, 1, 1, 0, 0, 9, 9, 9, 5, uint8_t(bx::EncodingType::Float) }, // RGB9E5F
  82. { 32, 1, 1, 4, 1, 1, 0, 0, 8, 8, 8, 8, uint8_t(bx::EncodingType::Unorm) }, // BGRA8
  83. { 32, 1, 1, 4, 1, 1, 0, 0, 8, 8, 8, 8, uint8_t(bx::EncodingType::Unorm) }, // RGBA8
  84. { 32, 1, 1, 4, 1, 1, 0, 0, 8, 8, 8, 8, uint8_t(bx::EncodingType::Int ) }, // RGBA8I
  85. { 32, 1, 1, 4, 1, 1, 0, 0, 8, 8, 8, 8, uint8_t(bx::EncodingType::Uint ) }, // RGBA8U
  86. { 32, 1, 1, 4, 1, 1, 0, 0, 8, 8, 8, 8, uint8_t(bx::EncodingType::Snorm) }, // RGBA8S
  87. { 64, 1, 1, 8, 1, 1, 0, 0, 16, 16, 16, 16, uint8_t(bx::EncodingType::Unorm) }, // RGBA16
  88. { 64, 1, 1, 8, 1, 1, 0, 0, 16, 16, 16, 16, uint8_t(bx::EncodingType::Int ) }, // RGBA16I
  89. { 64, 1, 1, 8, 1, 1, 0, 0, 16, 16, 16, 16, uint8_t(bx::EncodingType::Uint ) }, // RGBA16U
  90. { 64, 1, 1, 8, 1, 1, 0, 0, 16, 16, 16, 16, uint8_t(bx::EncodingType::Float) }, // RGBA16F
  91. { 64, 1, 1, 8, 1, 1, 0, 0, 16, 16, 16, 16, uint8_t(bx::EncodingType::Snorm) }, // RGBA16S
  92. { 128, 1, 1, 16, 1, 1, 0, 0, 32, 32, 32, 32, uint8_t(bx::EncodingType::Int ) }, // RGBA32I
  93. { 128, 1, 1, 16, 1, 1, 0, 0, 32, 32, 32, 32, uint8_t(bx::EncodingType::Uint ) }, // RGBA32U
  94. { 128, 1, 1, 16, 1, 1, 0, 0, 32, 32, 32, 32, uint8_t(bx::EncodingType::Float) }, // RGBA32F
  95. { 16, 1, 1, 2, 1, 1, 0, 0, 5, 6, 5, 0, uint8_t(bx::EncodingType::Unorm) }, // R5G6B5
  96. { 16, 1, 1, 2, 1, 1, 0, 0, 4, 4, 4, 4, uint8_t(bx::EncodingType::Unorm) }, // RGBA4
  97. { 16, 1, 1, 2, 1, 1, 0, 0, 5, 5, 5, 1, uint8_t(bx::EncodingType::Unorm) }, // RGB5A1
  98. { 32, 1, 1, 4, 1, 1, 0, 0, 10, 10, 10, 2, uint8_t(bx::EncodingType::Unorm) }, // RGB10A2
  99. { 32, 1, 1, 4, 1, 1, 0, 0, 11, 11, 10, 0, uint8_t(bx::EncodingType::Unorm) }, // RG11B10F
  100. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Count) }, // UnknownDepth
  101. { 16, 1, 1, 2, 1, 1, 16, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // D16
  102. { 24, 1, 1, 3, 1, 1, 24, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // D24
  103. { 32, 1, 1, 4, 1, 1, 24, 8, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // D24S8
  104. { 32, 1, 1, 4, 1, 1, 32, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // D32
  105. { 16, 1, 1, 2, 1, 1, 16, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Float) }, // D16F
  106. { 24, 1, 1, 3, 1, 1, 24, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Float) }, // D24F
  107. { 32, 1, 1, 4, 1, 1, 32, 0, 0, 0, 0, 0, uint8_t(bx::EncodingType::Float) }, // D32F
  108. { 8, 1, 1, 1, 1, 1, 0, 8, 0, 0, 0, 0, uint8_t(bx::EncodingType::Unorm) }, // D0S8
  109. };
  110. BX_STATIC_ASSERT(TextureFormat::Count == BX_COUNTOF(s_imageBlockInfo) );
  111. static const char* s_textureFormatName[] =
  112. {
  113. "BC1", // BC1
  114. "BC2", // BC2
  115. "BC3", // BC3
  116. "BC4", // BC4
  117. "BC5", // BC5
  118. "BC6H", // BC6H
  119. "BC7", // BC7
  120. "ETC1", // ETC1
  121. "ETC2", // ETC2
  122. "ETC2A", // ETC2A
  123. "ETC2A1", // ETC2A1
  124. "PTC12", // PTC12
  125. "PTC14", // PTC14
  126. "PTC12A", // PTC12A
  127. "PTC14A", // PTC14A
  128. "PTC22", // PTC22
  129. "PTC24", // PTC24
  130. "ATC", // ATC
  131. "ATCE", // ATCE
  132. "ATCI", // ATCI
  133. "ASTC4x4", // ASTC4x4
  134. "ASTC5x5", // ASTC5x5
  135. "ASTC6x6", // ASTC6x6
  136. "ASTC8x5", // ASTC8x5
  137. "ASTC8x6", // ASTC8x6
  138. "ASTC10x5", // ASTC10x5
  139. "<unknown>", // Unknown
  140. "R1", // R1
  141. "A8", // A8
  142. "R8", // R8
  143. "R8I", // R8I
  144. "R8U", // R8U
  145. "R8S", // R8S
  146. "R16", // R16
  147. "R16I", // R16I
  148. "R16U", // R16U
  149. "R16F", // R16F
  150. "R16S", // R16S
  151. "R32I", // R32I
  152. "R32U", // R32U
  153. "R32F", // R32F
  154. "RG8", // RG8
  155. "RG8I", // RG8I
  156. "RG8U", // RG8U
  157. "RG8S", // RG8S
  158. "RG16", // RG16
  159. "RG16I", // RG16I
  160. "RG16U", // RG16U
  161. "RG16F", // RG16F
  162. "RG16S", // RG16S
  163. "RG32I", // RG32I
  164. "RG32U", // RG32U
  165. "RG32F", // RG32F
  166. "RGB8", // RGB8
  167. "RGB8I", // RGB8I
  168. "RGB8U", // RGB8U
  169. "RGB8S", // RGB8S
  170. "RGB9E5", // RGB9E5F
  171. "BGRA8", // BGRA8
  172. "RGBA8", // RGBA8
  173. "RGBA8I", // RGBA8I
  174. "RGBA8U", // RGBA8U
  175. "RGBA8S", // RGBA8S
  176. "RGBA16", // RGBA16
  177. "RGBA16I", // RGBA16I
  178. "RGBA16U", // RGBA16U
  179. "RGBA16F", // RGBA16F
  180. "RGBA16S", // RGBA16S
  181. "RGBA32I", // RGBA32I
  182. "RGBA32U", // RGBA32U
  183. "RGBA32F", // RGBA32F
  184. "R5G6B5", // R5G6B5
  185. "RGBA4", // RGBA4
  186. "RGB5A1", // RGB5A1
  187. "RGB10A2", // RGB10A2
  188. "RG11B10F", // RG11B10F
  189. "<unknown>", // UnknownDepth
  190. "D16", // D16
  191. "D24", // D24
  192. "D24S8", // D24S8
  193. "D32", // D32
  194. "D16F", // D16F
  195. "D24F", // D24F
  196. "D32F", // D32F
  197. "D0S8", // D0S8
  198. };
  199. BX_STATIC_ASSERT(TextureFormat::Count == BX_COUNTOF(s_textureFormatName) );
  200. bool isCompressed(TextureFormat::Enum _format)
  201. {
  202. return _format < TextureFormat::Unknown;
  203. }
  204. bool isColor(TextureFormat::Enum _format)
  205. {
  206. return _format > TextureFormat::Unknown
  207. && _format < TextureFormat::UnknownDepth
  208. ;
  209. }
  210. bool isDepth(TextureFormat::Enum _format)
  211. {
  212. return _format > TextureFormat::UnknownDepth
  213. && _format < TextureFormat::Count
  214. ;
  215. }
  216. bool isValid(TextureFormat::Enum _format)
  217. {
  218. return _format != TextureFormat::Unknown
  219. && _format != TextureFormat::UnknownDepth
  220. && _format != TextureFormat::Count
  221. ;
  222. }
  223. bool isFloat(TextureFormat::Enum _format)
  224. {
  225. return uint8_t(bx::EncodingType::Float) == s_imageBlockInfo[_format].encoding;
  226. }
  227. uint8_t getBitsPerPixel(TextureFormat::Enum _format)
  228. {
  229. return s_imageBlockInfo[_format].bitsPerPixel;
  230. }
  231. const ImageBlockInfo& getBlockInfo(TextureFormat::Enum _format)
  232. {
  233. return s_imageBlockInfo[_format];
  234. }
  235. uint8_t getBlockSize(TextureFormat::Enum _format)
  236. {
  237. return s_imageBlockInfo[_format].blockSize;
  238. }
  239. const char* getName(TextureFormat::Enum _format)
  240. {
  241. if (_format >= TextureFormat::Count)
  242. {
  243. return "Unknown?!";
  244. }
  245. return s_textureFormatName[_format];
  246. }
  247. TextureFormat::Enum getFormat(const char* _name)
  248. {
  249. for (uint32_t ii = 0; ii < TextureFormat::Count; ++ii)
  250. {
  251. const TextureFormat::Enum fmt = TextureFormat::Enum(ii);
  252. if (isValid(fmt) )
  253. {
  254. if (0 == bx::strCmpI(s_textureFormatName[ii], _name) )
  255. {
  256. return fmt;
  257. }
  258. }
  259. }
  260. return TextureFormat::Unknown;
  261. }
  262. uint8_t imageGetNumMips(TextureFormat::Enum _format, uint16_t _width, uint16_t _height, uint16_t _depth)
  263. {
  264. const ImageBlockInfo& blockInfo = getBlockInfo(_format);
  265. const uint16_t blockWidth = blockInfo.blockWidth;
  266. const uint16_t blockHeight = blockInfo.blockHeight;
  267. const uint16_t minBlockX = blockInfo.minBlockX;
  268. const uint16_t minBlockY = blockInfo.minBlockY;
  269. _width = bx::max<uint16_t>(blockWidth * minBlockX, ( (_width + blockWidth - 1) / blockWidth )*blockWidth);
  270. _height = bx::max<uint16_t>(blockHeight * minBlockY, ( (_height + blockHeight - 1) / blockHeight)*blockHeight);
  271. _depth = bx::max<uint16_t>(1, _depth);
  272. uint8_t numMips = calcNumMips(true, _width, _height, _depth);
  273. return numMips;
  274. }
  275. uint32_t imageGetSize(TextureInfo* _info, uint16_t _width, uint16_t _height, uint16_t _depth, bool _cubeMap, bool _hasMips, uint16_t _numLayers, TextureFormat::Enum _format)
  276. {
  277. const ImageBlockInfo& blockInfo = getBlockInfo(_format);
  278. const uint8_t bpp = blockInfo.bitsPerPixel;
  279. const uint16_t blockWidth = blockInfo.blockWidth;
  280. const uint16_t blockHeight = blockInfo.blockHeight;
  281. const uint16_t minBlockX = blockInfo.minBlockX;
  282. const uint16_t minBlockY = blockInfo.minBlockY;
  283. const uint8_t blockSize = blockInfo.blockSize;
  284. _width = bx::max<uint16_t>(blockWidth * minBlockX, ( (_width + blockWidth - 1) / blockWidth)*blockWidth);
  285. _height = bx::max<uint16_t>(blockHeight * minBlockY, ( (_height + blockHeight - 1) / blockHeight)*blockHeight);
  286. _depth = bx::max<uint16_t>(1, _depth);
  287. const uint8_t numMips = calcNumMips(_hasMips, _width, _height, _depth);
  288. const uint32_t sides = _cubeMap ? 6 : 1;
  289. uint32_t width = _width;
  290. uint32_t height = _height;
  291. uint32_t depth = _depth;
  292. uint32_t size = 0;
  293. for (uint32_t lod = 0; lod < numMips; ++lod)
  294. {
  295. width = bx::max<uint32_t>(blockWidth * minBlockX, ( (width + blockWidth - 1) / blockWidth )*blockWidth);
  296. height = bx::max<uint32_t>(blockHeight * minBlockY, ( (height + blockHeight - 1) / blockHeight)*blockHeight);
  297. depth = bx::max<uint32_t>(1, depth);
  298. size += uint32_t(uint64_t(width/blockWidth * height/blockHeight * depth)*blockSize * sides);
  299. width >>= 1;
  300. height >>= 1;
  301. depth >>= 1;
  302. }
  303. size *= _numLayers;
  304. if (NULL != _info)
  305. {
  306. _info->format = _format;
  307. _info->width = _width;
  308. _info->height = _height;
  309. _info->depth = _depth;
  310. _info->numMips = numMips;
  311. _info->numLayers = _numLayers;
  312. _info->cubeMap = _cubeMap;
  313. _info->storageSize = size;
  314. _info->bitsPerPixel = bpp;
  315. }
  316. return size;
  317. }
  318. BX_NO_INLINE void imageSolid(void* _dst, uint32_t _width, uint32_t _height, uint32_t _solid)
  319. {
  320. uint32_t* dst = (uint32_t*)_dst;
  321. for (uint32_t ii = 0, num = _width*_height; ii < num; ++ii)
  322. {
  323. *dst++ = _solid;
  324. }
  325. }
  326. BX_NO_INLINE void imageCheckerboard(void* _dst, uint32_t _width, uint32_t _height, uint32_t _step, uint32_t _0, uint32_t _1)
  327. {
  328. uint32_t* dst = (uint32_t*)_dst;
  329. for (uint32_t yy = 0; yy < _height; ++yy)
  330. {
  331. for (uint32_t xx = 0; xx < _width; ++xx)
  332. {
  333. uint32_t abgr = ( (xx/_step)&1) ^ ( (yy/_step)&1) ? _1 : _0;
  334. *dst++ = abgr;
  335. }
  336. }
  337. }
  338. void imageRgba8Downsample2x2Ref(void* _dst, uint32_t _width, uint32_t _height, uint32_t _depth, uint32_t _srcPitch, uint32_t _dstPitch, const void* _src)
  339. {
  340. const uint32_t dstWidth = _width/2;
  341. const uint32_t dstHeight = _height/2;
  342. if (0 == dstWidth
  343. || 0 == dstHeight)
  344. {
  345. return;
  346. }
  347. const uint8_t* src = (const uint8_t*)_src;
  348. for (uint32_t zz = 0; zz < _depth; ++zz)
  349. {
  350. for (uint32_t yy = 0, ystep = _srcPitch*2; yy < dstHeight; ++yy, src += ystep)
  351. {
  352. uint8_t* dst = (uint8_t*)_dst + _dstPitch*yy;
  353. const uint8_t* rgba = src;
  354. for (uint32_t xx = 0; xx < dstWidth; ++xx, rgba += 8, dst += 4)
  355. {
  356. float rr = bx::toLinear(rgba[ 0]);
  357. float gg = bx::toLinear(rgba[ 1]);
  358. float bb = bx::toLinear(rgba[ 2]);
  359. float aa = rgba[ 3];
  360. rr += bx::toLinear(rgba[ 4]);
  361. gg += bx::toLinear(rgba[ 5]);
  362. bb += bx::toLinear(rgba[ 6]);
  363. aa += rgba[ 7];
  364. rr += bx::toLinear(rgba[_srcPitch+0]);
  365. gg += bx::toLinear(rgba[_srcPitch+1]);
  366. bb += bx::toLinear(rgba[_srcPitch+2]);
  367. aa += rgba[_srcPitch+3];
  368. rr += bx::toLinear(rgba[_srcPitch+4]);
  369. gg += bx::toLinear(rgba[_srcPitch+5]);
  370. bb += bx::toLinear(rgba[_srcPitch+6]);
  371. aa += rgba[_srcPitch+7];
  372. rr *= 0.25f;
  373. gg *= 0.25f;
  374. bb *= 0.25f;
  375. aa *= 0.25f;
  376. rr = bx::toGamma(rr);
  377. gg = bx::toGamma(gg);
  378. bb = bx::toGamma(bb);
  379. dst[0] = (uint8_t)rr;
  380. dst[1] = (uint8_t)gg;
  381. dst[2] = (uint8_t)bb;
  382. dst[3] = (uint8_t)aa;
  383. }
  384. }
  385. }
  386. }
  387. BX_SIMD_INLINE bx::simd128_t simd_to_linear(bx::simd128_t _a)
  388. {
  389. using namespace bx;
  390. const simd128_t f12_92 = simd_ld(12.92f, 12.92f, 12.92f, 1.0f);
  391. const simd128_t f0_055 = simd_ld(0.055f, 0.055f, 0.055f, 0.0f);
  392. const simd128_t f1_055 = simd_ld(1.055f, 1.055f, 1.055f, 1.0f);
  393. const simd128_t f2_4 = simd_ld(2.4f, 2.4f, 2.4f, 1.0f);
  394. const simd128_t f0_04045 = simd_ld(0.04045f, 0.04045f, 0.04045f, 0.0f);
  395. const simd128_t lo = simd_div(_a, f12_92);
  396. const simd128_t tmp0 = simd_add(_a, f0_055);
  397. const simd128_t tmp1 = simd_div(tmp0, f1_055);
  398. const simd128_t hi = simd_pow(tmp1, f2_4);
  399. const simd128_t mask = simd_cmple(_a, f0_04045);
  400. const simd128_t result = simd_selb(mask, hi, lo);
  401. return result;
  402. }
  403. BX_SIMD_INLINE bx::simd128_t simd_to_gamma(bx::simd128_t _a)
  404. {
  405. using namespace bx;
  406. const simd128_t f12_92 = simd_ld(12.92f, 12.92f, 12.92f, 1.0f);
  407. const simd128_t f0_055 = simd_ld(0.055f, 0.055f, 0.055f, 0.0f);
  408. const simd128_t f1_055 = simd_ld(1.055f, 1.055f, 1.055f, 1.0f);
  409. const simd128_t f1o2_4 = simd_ld(1.0f/2.4f, 1.0f/2.4f, 1.0f/2.4f, 1.0f);
  410. const simd128_t f0_0031308 = simd_ld(0.0031308f, 0.0031308f, 0.0031308f, 0.0f);
  411. const simd128_t lo = simd_mul(_a, f12_92);
  412. const simd128_t absa = simd_abs(_a);
  413. const simd128_t tmp0 = simd_pow(absa, f1o2_4);
  414. const simd128_t tmp1 = simd_mul(tmp0, f1_055);
  415. const simd128_t hi = simd_sub(tmp1, f0_055);
  416. const simd128_t mask = simd_cmple(_a, f0_0031308);
  417. const simd128_t result = simd_selb(mask, hi, lo);
  418. return result;
  419. }
  420. void imageRgba8Downsample2x2(void* _dst, uint32_t _width, uint32_t _height, uint32_t _depth, uint32_t _srcPitch, uint32_t _dstPitch, const void* _src)
  421. {
  422. const uint32_t dstWidth = _width/2;
  423. const uint32_t dstHeight = _height/2;
  424. if (0 == dstWidth
  425. || 0 == dstHeight)
  426. {
  427. return;
  428. }
  429. const uint8_t* src = (const uint8_t*)_src;
  430. using namespace bx;
  431. const simd128_t unpack = simd_ld(1.0f, 1.0f/256.0f, 1.0f/65536.0f, 1.0f/16777216.0f);
  432. const simd128_t pack = simd_ld(1.0f, 256.0f*0.5f, 65536.0f, 16777216.0f*0.5f);
  433. const simd128_t umask = simd_ild(0xff, 0xff00, 0xff0000, 0xff000000);
  434. const simd128_t pmask = simd_ild(0xff, 0x7f80, 0xff0000, 0x7f800000);
  435. const simd128_t wflip = simd_ild(0, 0, 0, 0x80000000);
  436. const simd128_t wadd = simd_ld(0.0f, 0.0f, 0.0f, 32768.0f*65536.0f);
  437. const simd128_t quater = simd_splat(0.25f);
  438. for (uint32_t zz = 0; zz < _depth; ++zz)
  439. {
  440. for (uint32_t yy = 0, ystep = _srcPitch*2; yy < dstHeight; ++yy, src += ystep)
  441. {
  442. uint8_t* dst = (uint8_t*)_dst + _dstPitch*yy;
  443. const uint8_t* rgba = src;
  444. for (uint32_t xx = 0; xx < dstWidth; ++xx, rgba += 8, dst += 4)
  445. {
  446. const simd128_t abgr0 = simd_splat(rgba);
  447. const simd128_t abgr1 = simd_splat(rgba+4);
  448. const simd128_t abgr2 = simd_splat(rgba+_srcPitch);
  449. const simd128_t abgr3 = simd_splat(rgba+_srcPitch+4);
  450. const simd128_t abgr0m = simd_and(abgr0, umask);
  451. const simd128_t abgr1m = simd_and(abgr1, umask);
  452. const simd128_t abgr2m = simd_and(abgr2, umask);
  453. const simd128_t abgr3m = simd_and(abgr3, umask);
  454. const simd128_t abgr0x = simd_xor(abgr0m, wflip);
  455. const simd128_t abgr1x = simd_xor(abgr1m, wflip);
  456. const simd128_t abgr2x = simd_xor(abgr2m, wflip);
  457. const simd128_t abgr3x = simd_xor(abgr3m, wflip);
  458. const simd128_t abgr0f = simd_itof(abgr0x);
  459. const simd128_t abgr1f = simd_itof(abgr1x);
  460. const simd128_t abgr2f = simd_itof(abgr2x);
  461. const simd128_t abgr3f = simd_itof(abgr3x);
  462. const simd128_t abgr0c = simd_add(abgr0f, wadd);
  463. const simd128_t abgr1c = simd_add(abgr1f, wadd);
  464. const simd128_t abgr2c = simd_add(abgr2f, wadd);
  465. const simd128_t abgr3c = simd_add(abgr3f, wadd);
  466. const simd128_t abgr0n = simd_mul(abgr0c, unpack);
  467. const simd128_t abgr1n = simd_mul(abgr1c, unpack);
  468. const simd128_t abgr2n = simd_mul(abgr2c, unpack);
  469. const simd128_t abgr3n = simd_mul(abgr3c, unpack);
  470. const simd128_t abgr0l = simd_to_linear(abgr0n);
  471. const simd128_t abgr1l = simd_to_linear(abgr1n);
  472. const simd128_t abgr2l = simd_to_linear(abgr2n);
  473. const simd128_t abgr3l = simd_to_linear(abgr3n);
  474. const simd128_t sum0 = simd_add(abgr0l, abgr1l);
  475. const simd128_t sum1 = simd_add(abgr2l, abgr3l);
  476. const simd128_t sum2 = simd_add(sum0, sum1);
  477. const simd128_t avg0 = simd_mul(sum2, quater);
  478. const simd128_t avg1 = simd_to_gamma(avg0);
  479. const simd128_t avg2 = simd_mul(avg1, pack);
  480. const simd128_t ftoi0 = simd_ftoi(avg2);
  481. const simd128_t ftoi1 = simd_and(ftoi0, pmask);
  482. const simd128_t zwxy = simd_swiz_zwxy(ftoi1);
  483. const simd128_t tmp0 = simd_or(ftoi1, zwxy);
  484. const simd128_t yyyy = simd_swiz_yyyy(tmp0);
  485. const simd128_t tmp1 = simd_iadd(yyyy, yyyy);
  486. const simd128_t result = simd_or(tmp0, tmp1);
  487. simd_stx(dst, result);
  488. }
  489. }
  490. }
  491. }
  492. void imageRgba32fToLinear(void* _dst, uint32_t _width, uint32_t _height, uint32_t _depth, uint32_t _srcPitch, const void* _src)
  493. {
  494. uint8_t* dst = ( uint8_t*)_dst;
  495. const uint8_t* src = (const uint8_t*)_src;
  496. for (uint32_t zz = 0; zz < _depth; ++zz)
  497. {
  498. for (uint32_t yy = 0; yy < _height; ++yy, src += _srcPitch, dst += _width*16)
  499. {
  500. for (uint32_t xx = 0; xx < _width; ++xx)
  501. {
  502. const uint32_t offset = xx * 16;
  503. float* fd = ( float*)(dst + offset);
  504. const float* fs = (const float*)(src + offset);
  505. fd[0] = bx::toLinear(fs[0]);
  506. fd[1] = bx::toLinear(fs[1]);
  507. fd[2] = bx::toLinear(fs[2]);
  508. fd[3] = fs[3];
  509. }
  510. }
  511. }
  512. }
  513. void imageRgba32fToLinear(ImageContainer* _imageContainer)
  514. {
  515. const uint16_t numSides = _imageContainer->m_numLayers * (_imageContainer->m_cubeMap ? 6 : 1);
  516. for (uint16_t side = 0; side < numSides; ++side)
  517. {
  518. ImageMip mip;
  519. imageGetRawData(*_imageContainer, side, 0, _imageContainer->m_data, _imageContainer->m_size, mip);
  520. const uint32_t pitch = _imageContainer->m_width*16;
  521. const uint32_t slice = _imageContainer->m_height*pitch;
  522. for (uint32_t zz = 0, depth = _imageContainer->m_depth; zz < depth; ++zz)
  523. {
  524. const uint32_t srcDataStep = uint32_t(bx::floor(zz * _imageContainer->m_depth / float(depth) ) );
  525. const uint8_t* srcData = &mip.m_data[srcDataStep*slice];
  526. imageRgba32fToLinear(const_cast<uint8_t*>(srcData), mip.m_width, mip.m_height, 1, pitch, srcData);
  527. }
  528. }
  529. }
  530. void imageRgba32fToGamma(void* _dst, uint32_t _width, uint32_t _height, uint32_t _depth, uint32_t _srcPitch, const void* _src)
  531. {
  532. uint8_t* dst = ( uint8_t*)_dst;
  533. const uint8_t* src = (const uint8_t*)_src;
  534. for (uint32_t zz = 0; zz < _depth; ++zz)
  535. {
  536. for (uint32_t yy = 0; yy < _height; ++yy, src += _srcPitch, dst += _width*16)
  537. {
  538. for (uint32_t xx = 0; xx < _width; ++xx)
  539. {
  540. const uint32_t offset = xx * 16;
  541. float* fd = ( float*)(dst + offset);
  542. const float* fs = (const float*)(src + offset);
  543. fd[0] = bx::toGamma(fs[0]);
  544. fd[1] = bx::toGamma(fs[1]);
  545. fd[2] = bx::toGamma(fs[2]);
  546. fd[3] = fs[3];
  547. }
  548. }
  549. }
  550. }
  551. void imageRgba32fToGamma(ImageContainer* _imageContainer)
  552. {
  553. const uint16_t numSides = _imageContainer->m_numLayers * (_imageContainer->m_cubeMap ? 6 : 1);
  554. for (uint16_t side = 0; side < numSides; ++side)
  555. {
  556. ImageMip mip;
  557. imageGetRawData(*_imageContainer, side, 0, _imageContainer->m_data, _imageContainer->m_size, mip);
  558. const uint32_t pitch = _imageContainer->m_width*16;
  559. const uint32_t slice = _imageContainer->m_height*pitch;
  560. for (uint32_t zz = 0, depth = _imageContainer->m_depth; zz < depth; ++zz)
  561. {
  562. const uint32_t srcDataStep = uint32_t(bx::floor(zz * _imageContainer->m_depth / float(depth) ) );
  563. const uint8_t* srcData = &mip.m_data[srcDataStep*slice];
  564. imageRgba32fToGamma(const_cast<uint8_t*>(srcData), mip.m_width, mip.m_height, 1, pitch, srcData);
  565. }
  566. }
  567. }
  568. void imageRgba32fLinearDownsample2x2Ref(void* _dst, uint32_t _width, uint32_t _height, uint32_t _depth, uint32_t _srcPitch, const void* _src)
  569. {
  570. const uint32_t dstWidth = _width/2;
  571. const uint32_t dstHeight = _height/2;
  572. const uint32_t dstDepth = _depth/2;
  573. if (0 == dstWidth
  574. || 0 == dstHeight)
  575. {
  576. return;
  577. }
  578. const uint8_t* src = (const uint8_t*)_src;
  579. uint8_t* dst = (uint8_t*)_dst;
  580. if (0 == dstDepth)
  581. {
  582. for (uint32_t yy = 0, ystep = _srcPitch*2; yy < dstHeight; ++yy, src += ystep)
  583. {
  584. const float* rgba0 = (const float*)&src[0];
  585. const float* rgba1 = (const float*)&src[_srcPitch];
  586. for (uint32_t xx = 0; xx < dstWidth; ++xx, rgba0 += 8, rgba1 += 8, dst += 16)
  587. {
  588. float xyz[4];
  589. xyz[0] = rgba0[0];
  590. xyz[1] = rgba0[1];
  591. xyz[2] = rgba0[2];
  592. xyz[3] = rgba0[3];
  593. xyz[0] += rgba0[4];
  594. xyz[1] += rgba0[5];
  595. xyz[2] += rgba0[6];
  596. xyz[3] += rgba0[7];
  597. xyz[0] += rgba1[0];
  598. xyz[1] += rgba1[1];
  599. xyz[2] += rgba1[2];
  600. xyz[3] += rgba1[3];
  601. xyz[0] += rgba1[4];
  602. xyz[1] += rgba1[5];
  603. xyz[2] += rgba1[6];
  604. xyz[3] += rgba1[7];
  605. xyz[0] *= 1.0f/4.0f;
  606. xyz[1] *= 1.0f/4.0f;
  607. xyz[2] *= 1.0f/4.0f;
  608. xyz[3] *= 1.0f/4.0f;
  609. bx::packRgba32F(dst, xyz);
  610. }
  611. }
  612. }
  613. else
  614. {
  615. const uint32_t slicePitch = _srcPitch*_height;
  616. for (uint32_t zz = 0; zz < dstDepth; ++zz, src += slicePitch)
  617. {
  618. for (uint32_t yy = 0, ystep = _srcPitch*2; yy < dstHeight; ++yy, src += ystep)
  619. {
  620. const float* rgba0 = (const float*)&src[0];
  621. const float* rgba1 = (const float*)&src[_srcPitch];
  622. const float* rgba2 = (const float*)&src[slicePitch];
  623. const float* rgba3 = (const float*)&src[slicePitch+_srcPitch];
  624. for (uint32_t xx = 0
  625. ; xx < dstWidth
  626. ; ++xx, rgba0 += 8, rgba1 += 8, rgba2 += 8, rgba3 += 8, dst += 16
  627. )
  628. {
  629. float xyz[4];
  630. xyz[0] = rgba0[0];
  631. xyz[1] = rgba0[1];
  632. xyz[2] = rgba0[2];
  633. xyz[3] = rgba0[3];
  634. xyz[0] += rgba0[4];
  635. xyz[1] += rgba0[5];
  636. xyz[2] += rgba0[6];
  637. xyz[3] += rgba0[7];
  638. xyz[0] += rgba1[0];
  639. xyz[1] += rgba1[1];
  640. xyz[2] += rgba1[2];
  641. xyz[3] += rgba1[3];
  642. xyz[0] += rgba1[4];
  643. xyz[1] += rgba1[5];
  644. xyz[2] += rgba1[6];
  645. xyz[3] += rgba1[7];
  646. xyz[0] += rgba2[0];
  647. xyz[1] += rgba2[1];
  648. xyz[2] += rgba2[2];
  649. xyz[3] += rgba2[3];
  650. xyz[0] += rgba2[4];
  651. xyz[1] += rgba2[5];
  652. xyz[2] += rgba2[6];
  653. xyz[3] += rgba2[7];
  654. xyz[0] += rgba3[0];
  655. xyz[1] += rgba3[1];
  656. xyz[2] += rgba3[2];
  657. xyz[3] += rgba3[3];
  658. xyz[0] += rgba3[4];
  659. xyz[1] += rgba3[5];
  660. xyz[2] += rgba3[6];
  661. xyz[3] += rgba3[7];
  662. xyz[0] *= 1.0f/8.0f;
  663. xyz[1] *= 1.0f/8.0f;
  664. xyz[2] *= 1.0f/8.0f;
  665. xyz[3] *= 1.0f/8.0f;
  666. bx::packRgba32F(dst, xyz);
  667. }
  668. }
  669. }
  670. }
  671. }
  672. void imageRgba32fLinearDownsample2x2(void* _dst, uint32_t _width, uint32_t _height, uint32_t _depth, uint32_t _srcPitch, const void* _src)
  673. {
  674. imageRgba32fLinearDownsample2x2Ref(_dst, _width, _height, _depth, _srcPitch, _src);
  675. }
  676. void imageRgba32fDownsample2x2Ref(void* _dst, uint32_t _width, uint32_t _height, uint32_t _depth, uint32_t _srcPitch, const void* _src)
  677. {
  678. const uint32_t dstWidth = _width/2;
  679. const uint32_t dstHeight = _height/2;
  680. const uint32_t dstDepth = _depth/2;
  681. if (0 == dstWidth
  682. || 0 == dstHeight)
  683. {
  684. return;
  685. }
  686. const uint8_t* src = (const uint8_t*)_src;
  687. uint8_t* dst = (uint8_t*)_dst;
  688. if (0 == dstDepth)
  689. {
  690. for (uint32_t yy = 0, ystep = _srcPitch*2; yy < dstHeight; ++yy, src += ystep)
  691. {
  692. const float* rgba0 = (const float*)&src[0];
  693. const float* rgba1 = (const float*)&src[_srcPitch];
  694. for (uint32_t xx = 0; xx < dstWidth; ++xx, rgba0 += 8, rgba1 += 8, dst += 16)
  695. {
  696. float xyz[4];
  697. xyz[0] = bx::toLinear(rgba0[0]);
  698. xyz[1] = bx::toLinear(rgba0[1]);
  699. xyz[2] = bx::toLinear(rgba0[2]);
  700. xyz[3] = rgba0[3];
  701. xyz[0] += bx::toLinear(rgba0[4]);
  702. xyz[1] += bx::toLinear(rgba0[5]);
  703. xyz[2] += bx::toLinear(rgba0[6]);
  704. xyz[3] += rgba0[7];
  705. xyz[0] += bx::toLinear(rgba1[0]);
  706. xyz[1] += bx::toLinear(rgba1[1]);
  707. xyz[2] += bx::toLinear(rgba1[2]);
  708. xyz[3] += rgba1[3];
  709. xyz[0] += bx::toLinear(rgba1[4]);
  710. xyz[1] += bx::toLinear(rgba1[5]);
  711. xyz[2] += bx::toLinear(rgba1[6]);
  712. xyz[3] += rgba1[7];
  713. xyz[0] = bx::toGamma(xyz[0]/4.0f);
  714. xyz[1] = bx::toGamma(xyz[1]/4.0f);
  715. xyz[2] = bx::toGamma(xyz[2]/4.0f);
  716. xyz[3] = xyz[3]/4.0f;
  717. bx::packRgba32F(dst, xyz);
  718. }
  719. }
  720. }
  721. else
  722. {
  723. const uint32_t slicePitch = _srcPitch*_height;
  724. for (uint32_t zz = 0; zz < dstDepth; ++zz, src += slicePitch)
  725. {
  726. for (uint32_t yy = 0, ystep = _srcPitch*2; yy < dstHeight; ++yy, src += ystep)
  727. {
  728. const float* rgba0 = (const float*)&src[0];
  729. const float* rgba1 = (const float*)&src[_srcPitch];
  730. const float* rgba2 = (const float*)&src[slicePitch];
  731. const float* rgba3 = (const float*)&src[slicePitch+_srcPitch];
  732. for (uint32_t xx = 0
  733. ; xx < dstWidth
  734. ; ++xx, rgba0 += 8, rgba1 += 8, rgba2 += 8, rgba3 += 8, dst += 16
  735. )
  736. {
  737. float xyz[4];
  738. xyz[0] = bx::toLinear(rgba0[0]);
  739. xyz[1] = bx::toLinear(rgba0[1]);
  740. xyz[2] = bx::toLinear(rgba0[2]);
  741. xyz[3] = rgba0[3];
  742. xyz[0] += bx::toLinear(rgba0[4]);
  743. xyz[1] += bx::toLinear(rgba0[5]);
  744. xyz[2] += bx::toLinear(rgba0[6]);
  745. xyz[3] += rgba0[7];
  746. xyz[0] += bx::toLinear(rgba1[0]);
  747. xyz[1] += bx::toLinear(rgba1[1]);
  748. xyz[2] += bx::toLinear(rgba1[2]);
  749. xyz[3] += rgba1[3];
  750. xyz[0] += bx::toLinear(rgba1[4]);
  751. xyz[1] += bx::toLinear(rgba1[5]);
  752. xyz[2] += bx::toLinear(rgba1[6]);
  753. xyz[3] += rgba1[7];
  754. xyz[0] += bx::toLinear(rgba2[0]);
  755. xyz[1] += bx::toLinear(rgba2[1]);
  756. xyz[2] += bx::toLinear(rgba2[2]);
  757. xyz[3] += rgba2[3];
  758. xyz[0] += bx::toLinear(rgba2[4]);
  759. xyz[1] += bx::toLinear(rgba2[5]);
  760. xyz[2] += bx::toLinear(rgba2[6]);
  761. xyz[3] += rgba2[7];
  762. xyz[0] += bx::toLinear(rgba3[0]);
  763. xyz[1] += bx::toLinear(rgba3[1]);
  764. xyz[2] += bx::toLinear(rgba3[2]);
  765. xyz[3] += rgba3[3];
  766. xyz[0] += bx::toLinear(rgba3[4]);
  767. xyz[1] += bx::toLinear(rgba3[5]);
  768. xyz[2] += bx::toLinear(rgba3[6]);
  769. xyz[3] += rgba3[7];
  770. xyz[0] = bx::toGamma(xyz[0]/8.0f);
  771. xyz[1] = bx::toGamma(xyz[1]/8.0f);
  772. xyz[2] = bx::toGamma(xyz[2]/8.0f);
  773. xyz[3] = xyz[3]/8.0f;
  774. bx::packRgba32F(dst, xyz);
  775. }
  776. }
  777. }
  778. }
  779. }
  780. void imageRgba32fDownsample2x2(void* _dst, uint32_t _width, uint32_t _height, uint32_t _depth, uint32_t _srcPitch, const void* _src)
  781. {
  782. imageRgba32fDownsample2x2Ref(_dst, _width, _height, _depth, _srcPitch, _src);
  783. }
  784. void imageRgba32fDownsample2x2NormalMapRef(void* _dst, uint32_t _width, uint32_t _height, uint32_t _srcPitch, uint32_t _dstPitch, const void* _src)
  785. {
  786. const uint32_t dstWidth = _width/2;
  787. const uint32_t dstHeight = _height/2;
  788. if (0 == dstWidth
  789. || 0 == dstHeight)
  790. {
  791. return;
  792. }
  793. const uint8_t* src = (const uint8_t*)_src;
  794. for (uint32_t yy = 0, ystep = _srcPitch*2; yy < dstHeight; ++yy, src += ystep)
  795. {
  796. const float* rgba0 = (const float*)&src[0];
  797. const float* rgba1 = (const float*)&src[_srcPitch];
  798. uint8_t* dst = (uint8_t*)_dst + _dstPitch*yy;
  799. for (uint32_t xx = 0; xx < dstWidth; ++xx, rgba0 += 8, rgba1 += 8, dst += 16)
  800. {
  801. float xyz[3];
  802. xyz[0] = rgba0[0];
  803. xyz[1] = rgba0[1];
  804. xyz[2] = rgba0[2];
  805. xyz[0] += rgba0[4];
  806. xyz[1] += rgba0[5];
  807. xyz[2] += rgba0[6];
  808. xyz[0] += rgba1[0];
  809. xyz[1] += rgba1[1];
  810. xyz[2] += rgba1[2];
  811. xyz[0] += rgba1[4];
  812. xyz[1] += rgba1[5];
  813. xyz[2] += rgba1[6];
  814. bx::store(dst, bx::normalize(bx::load<bx::Vec3>(xyz) ) );
  815. }
  816. }
  817. }
  818. void imageRgba32fDownsample2x2NormalMap(void* _dst, uint32_t _width, uint32_t _height, uint32_t _srcPitch, uint32_t _dstPitch, const void* _src)
  819. {
  820. imageRgba32fDownsample2x2NormalMapRef(_dst, _width, _height, _srcPitch, _dstPitch, _src);
  821. }
  822. void imageSwizzleBgra8Ref(void* _dst, uint32_t _dstPitch, uint32_t _width, uint32_t _height, const void* _src, uint32_t _srcPitch)
  823. {
  824. const uint8_t* srcData = (uint8_t*) _src;
  825. uint8_t* dstData = (uint8_t*)_dst;
  826. for (uint32_t yy = 0; yy < _height; ++yy, srcData += _srcPitch, dstData += _dstPitch)
  827. {
  828. const uint8_t* src = srcData;
  829. uint8_t* dst = dstData;
  830. for (uint32_t xx = 0; xx < _width; ++xx, src += 4, dst += 4)
  831. {
  832. uint8_t rr = src[0];
  833. uint8_t gg = src[1];
  834. uint8_t bb = src[2];
  835. uint8_t aa = src[3];
  836. dst[0] = bb;
  837. dst[1] = gg;
  838. dst[2] = rr;
  839. dst[3] = aa;
  840. }
  841. }
  842. }
  843. void imageSwizzleBgra8(void* _dst, uint32_t _dstPitch, uint32_t _width, uint32_t _height, const void* _src, uint32_t _srcPitch)
  844. {
  845. // Test can we do four 4-byte pixels at the time.
  846. if (0 != (_width&0x3)
  847. || _width < 4
  848. || !bx::isAligned(_src, 16)
  849. || !bx::isAligned(_dst, 16) )
  850. {
  851. BX_WARN(false, "Image swizzle is taking slow path.");
  852. BX_WARN(bx::isAligned(_src, 16), "Source %p is not 16-byte aligned.", _src);
  853. BX_WARN(bx::isAligned(_dst, 16), "Destination %p is not 16-byte aligned.", _dst);
  854. BX_WARN(_width < 4, "Image width must be multiple of 4 (width %d).", _width);
  855. imageSwizzleBgra8Ref(_dst, _dstPitch, _width, _height, _src, _srcPitch);
  856. return;
  857. }
  858. using namespace bx;
  859. const simd128_t mf0f0 = simd_isplat(0xff00ff00);
  860. const simd128_t m0f0f = simd_isplat(0x00ff00ff);
  861. const uint32_t width = _width/4;
  862. const uint8_t* srcData = (uint8_t*) _src;
  863. uint8_t* dstData = (uint8_t*)_dst;
  864. for (uint32_t yy = 0; yy < _height; ++yy, srcData += _srcPitch, dstData += _dstPitch)
  865. {
  866. const uint8_t* src = srcData;
  867. uint8_t* dst = dstData;
  868. for (uint32_t xx = 0; xx < width; ++xx, src += 16, dst += 16)
  869. {
  870. const simd128_t tabgr = simd_ld(src);
  871. const simd128_t t00ab = simd_srl(tabgr, 16);
  872. const simd128_t tgr00 = simd_sll(tabgr, 16);
  873. const simd128_t tgrab = simd_or(t00ab, tgr00);
  874. const simd128_t ta0g0 = simd_and(tabgr, mf0f0);
  875. const simd128_t t0r0b = simd_and(tgrab, m0f0f);
  876. const simd128_t targb = simd_or(ta0g0, t0r0b);
  877. simd_st(dst, targb);
  878. }
  879. }
  880. }
  881. void imageCopy(void* _dst, uint32_t _height, uint32_t _srcPitch, uint32_t _depth, const void* _src, uint32_t _dstPitch)
  882. {
  883. const uint32_t pitch = bx::uint32_min(_srcPitch, _dstPitch);
  884. const uint8_t* src = (uint8_t*)_src;
  885. uint8_t* dst = (uint8_t*)_dst;
  886. for (uint32_t zz = 0; zz < _depth; ++zz, src += _srcPitch*_height, dst += _dstPitch*_height)
  887. {
  888. bx::memCopy(dst, _dstPitch, src, _srcPitch, pitch, _height);
  889. }
  890. }
  891. void imageCopy(void* _dst, uint32_t _width, uint32_t _height, uint32_t _depth, uint32_t _bpp, uint32_t _srcPitch, const void* _src)
  892. {
  893. const uint32_t dstPitch = _width*_bpp/8;
  894. imageCopy(_dst, _height, _srcPitch, _depth, _src, dstPitch);
  895. }
  896. struct PackUnpack
  897. {
  898. PackFn pack;
  899. UnpackFn unpack;
  900. };
  901. static const PackUnpack s_packUnpack[] =
  902. {
  903. { NULL, NULL }, // BC1
  904. { NULL, NULL }, // BC2
  905. { NULL, NULL }, // BC3
  906. { NULL, NULL }, // BC4
  907. { NULL, NULL }, // BC5
  908. { NULL, NULL }, // BC6H
  909. { NULL, NULL }, // BC7
  910. { NULL, NULL }, // ETC1
  911. { NULL, NULL }, // ETC2
  912. { NULL, NULL }, // ETC2A
  913. { NULL, NULL }, // ETC2A1
  914. { NULL, NULL }, // PTC12
  915. { NULL, NULL }, // PTC14
  916. { NULL, NULL }, // PTC12A
  917. { NULL, NULL }, // PTC14A
  918. { NULL, NULL }, // PTC22
  919. { NULL, NULL }, // PTC24
  920. { NULL, NULL }, // ATC
  921. { NULL, NULL }, // ATCE
  922. { NULL, NULL }, // ATCI
  923. { NULL, NULL }, // ASTC4x4
  924. { NULL, NULL }, // ASTC5x5
  925. { NULL, NULL }, // ASTC6x6
  926. { NULL, NULL }, // ASTC8x5
  927. { NULL, NULL }, // ASTC8x6
  928. { NULL, NULL }, // ASTC10x5
  929. { NULL, NULL }, // Unknown
  930. { NULL, NULL }, // R1
  931. { bx::packA8, bx::unpackA8 }, // A8
  932. { bx::packR8, bx::unpackR8 }, // R8
  933. { bx::packR8I, bx::unpackR8I }, // R8I
  934. { bx::packR8U, bx::unpackR8U }, // R8U
  935. { bx::packR8S, bx::unpackR8S }, // R8S
  936. { bx::packR16, bx::unpackR16 }, // R16
  937. { bx::packR16I, bx::unpackR16I }, // R16I
  938. { bx::packR16U, bx::unpackR16U }, // R16U
  939. { bx::packR16F, bx::unpackR16F }, // R16F
  940. { bx::packR16S, bx::unpackR16S }, // R16S
  941. { bx::packR32I, bx::unpackR32I }, // R32I
  942. { bx::packR32U, bx::unpackR32U }, // R32U
  943. { bx::packR32F, bx::unpackR32F }, // R32F
  944. { bx::packRg8, bx::unpackRg8 }, // RG8
  945. { bx::packRg8I, bx::unpackRg8I }, // RG8I
  946. { bx::packRg8U, bx::unpackRg8U }, // RG8U
  947. { bx::packRg8S, bx::unpackRg8S }, // RG8S
  948. { bx::packRg16, bx::unpackRg16 }, // RG16
  949. { bx::packRg16I, bx::unpackRg16I }, // RG16I
  950. { bx::packRg16U, bx::unpackRg16U }, // RG16U
  951. { bx::packRg16F, bx::unpackRg16F }, // RG16F
  952. { bx::packRg16S, bx::unpackRg16S }, // RG16S
  953. { bx::packRg32I, bx::unpackRg32I }, // RG32I
  954. { bx::packRg32U, bx::unpackRg32U }, // RG32U
  955. { bx::packRg32F, bx::unpackRg32F }, // RG32F
  956. { bx::packRgb8, bx::unpackRgb8 }, // RGB8
  957. { bx::packRgb8S, bx::unpackRgb8S }, // RGB8S
  958. { bx::packRgb8I, bx::unpackRgb8I }, // RGB8I
  959. { bx::packRgb8U, bx::unpackRgb8U }, // RGB8U
  960. { bx::packRgb9E5F, bx::unpackRgb9E5F }, // RGB9E5F
  961. { bx::packBgra8, bx::unpackBgra8 }, // BGRA8
  962. { bx::packRgba8, bx::unpackRgba8 }, // RGBA8
  963. { bx::packRgba8I, bx::unpackRgba8I }, // RGBA8I
  964. { bx::packRgba8U, bx::unpackRgba8U }, // RGBA8U
  965. { bx::packRgba8S, bx::unpackRgba8S }, // RGBA8S
  966. { bx::packRgba16, bx::unpackRgba16 }, // RGBA16
  967. { bx::packRgba16I, bx::unpackRgba16I }, // RGBA16I
  968. { bx::packRgba16U, bx::unpackRgba16U }, // RGBA16U
  969. { bx::packRgba16F, bx::unpackRgba16F }, // RGBA16F
  970. { bx::packRgba16S, bx::unpackRgba16S }, // RGBA16S
  971. { bx::packRgba32I, bx::unpackRgba32I }, // RGBA32I
  972. { bx::packRgba32U, bx::unpackRgba32U }, // RGBA32U
  973. { bx::packRgba32F, bx::unpackRgba32F }, // RGBA32F
  974. { bx::packR5G6B5, bx::unpackR5G6B5 }, // R5G6B5
  975. { bx::packRgba4, bx::unpackRgba4 }, // RGBA4
  976. { bx::packRgb5a1, bx::unpackRgb5a1 }, // RGB5A1
  977. { bx::packRgb10A2, bx::unpackRgb10A2 }, // RGB10A2
  978. { bx::packRG11B10F, bx::unpackRG11B10F }, // RG11B10F
  979. { NULL, NULL }, // UnknownDepth
  980. { bx::packR16, bx::unpackR16 }, // D16
  981. { bx::packR24, bx::unpackR24 }, // D24
  982. { bx::packR24G8, bx::unpackR24G8 }, // D24S8
  983. { NULL, NULL }, // D32
  984. { bx::packR16F, bx::unpackR16F }, // D16F
  985. { NULL, NULL }, // D24F
  986. { bx::packR32F, bx::unpackR32F }, // D32F
  987. { bx::packR8, bx::unpackR8 }, // D0S8
  988. };
  989. BX_STATIC_ASSERT(TextureFormat::Count == BX_COUNTOF(s_packUnpack) );
  990. PackFn getPack(TextureFormat::Enum _format)
  991. {
  992. return s_packUnpack[_format].pack;
  993. }
  994. UnpackFn getUnpack(TextureFormat::Enum _format)
  995. {
  996. return s_packUnpack[_format].unpack;
  997. }
  998. bool imageConvert(TextureFormat::Enum _dstFormat, TextureFormat::Enum _srcFormat)
  999. {
  1000. UnpackFn unpack = s_packUnpack[_srcFormat].unpack;
  1001. PackFn pack = s_packUnpack[_dstFormat].pack;
  1002. return NULL != pack
  1003. && NULL != unpack
  1004. ;
  1005. }
  1006. void imageConvert(void* _dst, uint32_t _bpp, PackFn _pack, const void* _src, UnpackFn _unpack, uint32_t _size)
  1007. {
  1008. const uint8_t* src = (uint8_t*)_src;
  1009. uint8_t* dst = (uint8_t*)_dst;
  1010. const uint32_t size = _size * 8 / _bpp;
  1011. for (uint32_t ii = 0; ii < size; ++ii)
  1012. {
  1013. float rgba[4];
  1014. _unpack(rgba, &src[ii*_bpp/8]);
  1015. _pack(&dst[ii*_bpp/8], rgba);
  1016. }
  1017. }
  1018. 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 _depth, uint32_t _srcPitch, uint32_t _dstPitch)
  1019. {
  1020. const uint8_t* src = (uint8_t*)_src;
  1021. uint8_t* dst = (uint8_t*)_dst;
  1022. for (uint32_t zz = 0; zz < _depth; ++zz)
  1023. {
  1024. for (uint32_t yy = 0; yy < _height; ++yy, src += _srcPitch, dst += _dstPitch)
  1025. {
  1026. for (uint32_t xx = 0; xx < _width; ++xx)
  1027. {
  1028. float rgba[4];
  1029. _unpack(rgba, &src[xx*_srcBpp/8]);
  1030. _pack(&dst[xx*_dstBpp/8], rgba);
  1031. }
  1032. }
  1033. }
  1034. }
  1035. bool imageConvert(bx::AllocatorI* _allocator, void* _dst, TextureFormat::Enum _dstFormat, const void* _src, TextureFormat::Enum _srcFormat, uint32_t _width, uint32_t _height, uint32_t _depth, uint32_t _srcPitch, uint32_t _dstPitch)
  1036. {
  1037. UnpackFn unpack = s_packUnpack[_srcFormat].unpack;
  1038. PackFn pack = s_packUnpack[_dstFormat].pack;
  1039. if (NULL == pack
  1040. || NULL == unpack)
  1041. {
  1042. switch (_dstFormat)
  1043. {
  1044. case TextureFormat::RGBA8:
  1045. imageDecodeToRgba8(_allocator, _dst, _src, _width, _height, _width*4, _srcFormat);
  1046. return true;
  1047. case TextureFormat::BGRA8:
  1048. imageDecodeToBgra8(_allocator, _dst, _src, _width, _height, _width*4, _srcFormat);
  1049. return true;
  1050. case TextureFormat::RGBA32F:
  1051. imageDecodeToRgba32f(_allocator, _dst, _src, _width, _height, 1, _width*16, _srcFormat);
  1052. return true;
  1053. default:
  1054. break;
  1055. }
  1056. return false;
  1057. }
  1058. const uint32_t srcBpp = s_imageBlockInfo[_srcFormat].bitsPerPixel;
  1059. const uint32_t dstBpp = s_imageBlockInfo[_dstFormat].bitsPerPixel;
  1060. imageConvert(_dst, dstBpp, pack, _src, srcBpp, unpack, _width, _height, _depth, _srcPitch, _dstPitch);
  1061. return true;
  1062. }
  1063. bool imageConvert(bx::AllocatorI* _allocator, void* _dst, TextureFormat::Enum _dstFormat, const void* _src, TextureFormat::Enum _srcFormat, uint32_t _width, uint32_t _height, uint32_t _depth)
  1064. {
  1065. const uint32_t srcBpp = s_imageBlockInfo[_srcFormat].bitsPerPixel;
  1066. if (_dstFormat == _srcFormat)
  1067. {
  1068. bx::memCopy(_dst, _src, _width*_height*_depth*(srcBpp/8) );
  1069. return true;
  1070. }
  1071. const uint32_t dstBpp = s_imageBlockInfo[_dstFormat].bitsPerPixel;
  1072. const uint32_t dstPitch = _width * dstBpp / 8;
  1073. return imageConvert(_allocator, _dst, _dstFormat, _src, _srcFormat, _width, _height, _depth, _width*srcBpp/8, dstPitch);
  1074. }
  1075. ImageContainer* imageConvert(bx::AllocatorI* _allocator, TextureFormat::Enum _dstFormat, const ImageContainer& _input, bool _convertMips)
  1076. {
  1077. ImageContainer* output = imageAlloc(_allocator
  1078. , _dstFormat
  1079. , uint16_t(_input.m_width)
  1080. , uint16_t(_input.m_height)
  1081. , uint16_t(_input.m_depth)
  1082. , _input.m_numLayers
  1083. , _input.m_cubeMap
  1084. , _convertMips && 1 < _input.m_numMips
  1085. );
  1086. const uint16_t numSides = _input.m_numLayers * (_input.m_cubeMap ? 6 : 1);
  1087. for (uint16_t side = 0; side < numSides; ++side)
  1088. {
  1089. for (uint8_t lod = 0, num = _convertMips ? _input.m_numMips : 1; lod < num; ++lod)
  1090. {
  1091. ImageMip mip;
  1092. if (imageGetRawData(_input, side, lod, _input.m_data, _input.m_size, mip) )
  1093. {
  1094. ImageMip dstMip;
  1095. imageGetRawData(*output, side, lod, output->m_data, output->m_size, dstMip);
  1096. uint8_t* dstData = const_cast<uint8_t*>(dstMip.m_data);
  1097. bool ok = imageConvert(
  1098. _allocator
  1099. , dstData
  1100. , _dstFormat
  1101. , mip.m_data
  1102. , mip.m_format
  1103. , mip.m_width
  1104. , mip.m_height
  1105. , mip.m_depth
  1106. );
  1107. BX_ASSERT(ok, "Conversion from %s to %s failed!"
  1108. , getName(_input.m_format)
  1109. , getName(output->m_format)
  1110. );
  1111. BX_UNUSED(ok);
  1112. }
  1113. }
  1114. }
  1115. return output;
  1116. }
  1117. typedef bool (*ParseFn)(ImageContainer&, bx::ReaderSeekerI*, bx::Error*);
  1118. template<uint32_t magicT, ParseFn parseFnT>
  1119. ImageContainer* imageParseT(bx::AllocatorI* _allocator, const void* _src, uint32_t _size, bx::Error* _err)
  1120. {
  1121. bx::MemoryReader reader(_src, _size);
  1122. uint32_t magic;
  1123. bx::read(&reader, magic, bx::ErrorIgnore{});
  1124. ImageContainer imageContainer;
  1125. if (magicT != magic)
  1126. {
  1127. return NULL;
  1128. }
  1129. if (!parseFnT(imageContainer, &reader, _err) )
  1130. {
  1131. return NULL;
  1132. }
  1133. ImageContainer* output = imageAlloc(_allocator
  1134. , imageContainer.m_format
  1135. , uint16_t(imageContainer.m_width)
  1136. , uint16_t(imageContainer.m_height)
  1137. , uint16_t(imageContainer.m_depth)
  1138. , imageContainer.m_numLayers
  1139. , imageContainer.m_cubeMap
  1140. , 1 < imageContainer.m_numMips
  1141. );
  1142. const uint16_t numSides = imageContainer.m_numLayers * (imageContainer.m_cubeMap ? 6 : 1);
  1143. for (uint16_t side = 0; side < numSides; ++side)
  1144. {
  1145. for (uint8_t lod = 0, num = imageContainer.m_numMips; lod < num; ++lod)
  1146. {
  1147. ImageMip dstMip;
  1148. if (imageGetRawData(*output, side, lod, output->m_data, output->m_size, dstMip) )
  1149. {
  1150. ImageMip mip;
  1151. if (imageGetRawData(imageContainer, side, lod, _src, _size, mip) )
  1152. {
  1153. uint8_t* dstData = const_cast<uint8_t*>(dstMip.m_data);
  1154. bx::memCopy(dstData, mip.m_data, mip.m_size);
  1155. }
  1156. }
  1157. }
  1158. }
  1159. return output;
  1160. }
  1161. static uint8_t bitRangeConvert(uint32_t _in, uint32_t _from, uint32_t _to)
  1162. {
  1163. using namespace bx;
  1164. uint32_t tmp0 = uint32_sll(1, _to);
  1165. uint32_t tmp1 = uint32_sll(1, _from);
  1166. uint32_t tmp2 = uint32_dec(tmp0);
  1167. uint32_t tmp3 = uint32_dec(tmp1);
  1168. uint32_t tmp4 = uint32_mul(_in, tmp2);
  1169. uint32_t tmp5 = uint32_add(tmp3, tmp4);
  1170. uint32_t tmp6 = uint32_srl(tmp5, _from);
  1171. uint32_t tmp7 = uint32_add(tmp5, tmp6);
  1172. uint32_t result = uint32_srl(tmp7, _from);
  1173. return uint8_t(result);
  1174. }
  1175. static void decodeBlockDxt(uint8_t _dst[16*4], const uint8_t _src[8])
  1176. {
  1177. if (!BX_ENABLED(BIMG_DECODE_BC2 || BIMG_DECODE_BC3) )
  1178. {
  1179. return;
  1180. }
  1181. uint8_t colors[4*3];
  1182. uint32_t c0 = _src[0] | (_src[1] << 8);
  1183. colors[0] = bitRangeConvert( (c0>> 0)&0x1f, 5, 8);
  1184. colors[1] = bitRangeConvert( (c0>> 5)&0x3f, 6, 8);
  1185. colors[2] = bitRangeConvert( (c0>>11)&0x1f, 5, 8);
  1186. uint32_t c1 = _src[2] | (_src[3] << 8);
  1187. colors[3] = bitRangeConvert( (c1>> 0)&0x1f, 5, 8);
  1188. colors[4] = bitRangeConvert( (c1>> 5)&0x3f, 6, 8);
  1189. colors[5] = bitRangeConvert( (c1>>11)&0x1f, 5, 8);
  1190. colors[6] = (2*colors[0] + colors[3]) / 3;
  1191. colors[7] = (2*colors[1] + colors[4]) / 3;
  1192. colors[8] = (2*colors[2] + colors[5]) / 3;
  1193. colors[ 9] = (colors[0] + 2*colors[3]) / 3;
  1194. colors[10] = (colors[1] + 2*colors[4]) / 3;
  1195. colors[11] = (colors[2] + 2*colors[5]) / 3;
  1196. for (uint32_t ii = 0, next = 8*4; ii < 16*4; ii += 4, next += 2)
  1197. {
  1198. int idx = ( (_src[next>>3] >> (next & 7) ) & 3) * 3;
  1199. _dst[ii+0] = colors[idx+0];
  1200. _dst[ii+1] = colors[idx+1];
  1201. _dst[ii+2] = colors[idx+2];
  1202. }
  1203. }
  1204. static void decodeBlockDxt1(uint8_t _dst[16*4], const uint8_t _src[8])
  1205. {
  1206. if (!BX_ENABLED(BIMG_DECODE_BC1 || BIMG_DECODE_BC2 || BIMG_DECODE_BC3) )
  1207. {
  1208. return;
  1209. }
  1210. uint8_t colors[4*4];
  1211. uint32_t c0 = _src[0] | (_src[1] << 8);
  1212. colors[0] = bitRangeConvert( (c0>> 0)&0x1f, 5, 8);
  1213. colors[1] = bitRangeConvert( (c0>> 5)&0x3f, 6, 8);
  1214. colors[2] = bitRangeConvert( (c0>>11)&0x1f, 5, 8);
  1215. colors[3] = 255;
  1216. uint32_t c1 = _src[2] | (_src[3] << 8);
  1217. colors[4] = bitRangeConvert( (c1>> 0)&0x1f, 5, 8);
  1218. colors[5] = bitRangeConvert( (c1>> 5)&0x3f, 6, 8);
  1219. colors[6] = bitRangeConvert( (c1>>11)&0x1f, 5, 8);
  1220. colors[7] = 255;
  1221. if (c0 > c1)
  1222. {
  1223. colors[ 8] = (2*colors[0] + colors[4]) / 3;
  1224. colors[ 9] = (2*colors[1] + colors[5]) / 3;
  1225. colors[10] = (2*colors[2] + colors[6]) / 3;
  1226. colors[11] = 255;
  1227. colors[12] = (colors[0] + 2*colors[4]) / 3;
  1228. colors[13] = (colors[1] + 2*colors[5]) / 3;
  1229. colors[14] = (colors[2] + 2*colors[6]) / 3;
  1230. colors[15] = 255;
  1231. }
  1232. else
  1233. {
  1234. colors[ 8] = (colors[0] + colors[4]) / 2;
  1235. colors[ 9] = (colors[1] + colors[5]) / 2;
  1236. colors[10] = (colors[2] + colors[6]) / 2;
  1237. colors[11] = 255;
  1238. colors[12] = 0;
  1239. colors[13] = 0;
  1240. colors[14] = 0;
  1241. colors[15] = 0;
  1242. }
  1243. for (uint32_t ii = 0, next = 8*4; ii < 16*4; ii += 4, next += 2)
  1244. {
  1245. int idx = ( (_src[next>>3] >> (next & 7) ) & 3) * 4;
  1246. _dst[ii+0] = colors[idx+0];
  1247. _dst[ii+1] = colors[idx+1];
  1248. _dst[ii+2] = colors[idx+2];
  1249. _dst[ii+3] = colors[idx+3];
  1250. }
  1251. }
  1252. static void decodeBlockDxt23A(uint8_t _dst[16*4], const uint8_t _src[8])
  1253. {
  1254. if (!BX_ENABLED(BIMG_DECODE_BC2) )
  1255. {
  1256. return;
  1257. }
  1258. for (uint32_t ii = 0, next = 0; ii < 16*4; ii += 4, next += 4)
  1259. {
  1260. uint32_t c0 = (_src[next>>3] >> (next&7) ) & 0xf;
  1261. _dst[ii] = bitRangeConvert(c0, 4, 8);
  1262. }
  1263. }
  1264. static void decodeBlockDxt45A(uint8_t _dst[16*4], const uint8_t _src[8])
  1265. {
  1266. if (!BX_ENABLED(BIMG_DECODE_BC3 || BIMG_DECODE_BC4 || BIMG_DECODE_BC5) )
  1267. {
  1268. return;
  1269. }
  1270. uint8_t alpha[8];
  1271. alpha[0] = _src[0];
  1272. alpha[1] = _src[1];
  1273. if (alpha[0] > alpha[1])
  1274. {
  1275. alpha[2] = (6*alpha[0] + 1*alpha[1]) / 7;
  1276. alpha[3] = (5*alpha[0] + 2*alpha[1]) / 7;
  1277. alpha[4] = (4*alpha[0] + 3*alpha[1]) / 7;
  1278. alpha[5] = (3*alpha[0] + 4*alpha[1]) / 7;
  1279. alpha[6] = (2*alpha[0] + 5*alpha[1]) / 7;
  1280. alpha[7] = (1*alpha[0] + 6*alpha[1]) / 7;
  1281. }
  1282. else
  1283. {
  1284. alpha[2] = (4*alpha[0] + 1*alpha[1]) / 5;
  1285. alpha[3] = (3*alpha[0] + 2*alpha[1]) / 5;
  1286. alpha[4] = (2*alpha[0] + 3*alpha[1]) / 5;
  1287. alpha[5] = (1*alpha[0] + 4*alpha[1]) / 5;
  1288. alpha[6] = 0;
  1289. alpha[7] = 255;
  1290. }
  1291. uint32_t idx0 = _src[2];
  1292. uint32_t idx1 = _src[5];
  1293. idx0 |= uint32_t(_src[3])<<8;
  1294. idx1 |= uint32_t(_src[6])<<8;
  1295. idx0 |= uint32_t(_src[4])<<16;
  1296. idx1 |= uint32_t(_src[7])<<16;
  1297. for (uint32_t ii = 0; ii < 8*4; ii += 4)
  1298. {
  1299. _dst[ii] = alpha[idx0&7];
  1300. _dst[ii+32] = alpha[idx1&7];
  1301. idx0 >>= 3;
  1302. idx1 >>= 3;
  1303. }
  1304. }
  1305. // BC6H, BC7
  1306. //
  1307. // Reference(s):
  1308. // - https://web.archive.org/web/20181126035446/https://www.khronos.org/registry/OpenGL/extensions/ARB/ARB_texture_compression_bptc.txt
  1309. // - https://web.archive.org/web/20181126035538/https://docs.microsoft.com/en-us/windows/desktop/direct3d11/bc6h-format
  1310. //
  1311. static const uint16_t s_bptcP2[] =
  1312. { // 3210 0000000000 1111111111 2222222222 3333333333
  1313. 0xcccc, // 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1
  1314. 0x8888, // 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1
  1315. 0xeeee, // 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1
  1316. 0xecc8, // 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1
  1317. 0xc880, // 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1
  1318. 0xfeec, // 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1
  1319. 0xfec8, // 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1
  1320. 0xec80, // 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1
  1321. 0xc800, // 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1
  1322. 0xffec, // 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
  1323. 0xfe80, // 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1
  1324. 0xe800, // 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1
  1325. 0xffe8, // 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
  1326. 0xff00, // 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1
  1327. 0xfff0, // 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
  1328. 0xf000, // 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1
  1329. 0xf710, // 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1
  1330. 0x008e, // 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0
  1331. 0x7100, // 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0
  1332. 0x08ce, // 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0
  1333. 0x008c, // 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0
  1334. 0x7310, // 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0
  1335. 0x3100, // 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0
  1336. 0x8cce, // 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1
  1337. 0x088c, // 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0
  1338. 0x3110, // 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0
  1339. 0x6666, // 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0
  1340. 0x366c, // 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0
  1341. 0x17e8, // 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0
  1342. 0x0ff0, // 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0
  1343. 0x718e, // 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0
  1344. 0x399c, // 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0
  1345. 0xaaaa, // 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1
  1346. 0xf0f0, // 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1
  1347. 0x5a5a, // 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0
  1348. 0x33cc, // 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0
  1349. 0x3c3c, // 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0
  1350. 0x55aa, // 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0
  1351. 0x9696, // 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1
  1352. 0xa55a, // 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1
  1353. 0x73ce, // 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0
  1354. 0x13c8, // 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0
  1355. 0x324c, // 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0
  1356. 0x3bdc, // 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0
  1357. 0x6996, // 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0
  1358. 0xc33c, // 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1
  1359. 0x9966, // 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1
  1360. 0x0660, // 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0
  1361. 0x0272, // 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0
  1362. 0x04e4, // 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0
  1363. 0x4e40, // 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0
  1364. 0x2720, // 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0
  1365. 0xc936, // 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1
  1366. 0x936c, // 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1
  1367. 0x39c6, // 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0
  1368. 0x639c, // 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0
  1369. 0x9336, // 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1
  1370. 0x9cc6, // 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1
  1371. 0x817e, // 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1
  1372. 0xe718, // 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1
  1373. 0xccf0, // 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1
  1374. 0x0fcc, // 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0
  1375. 0x7744, // 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0
  1376. 0xee22, // 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1
  1377. };
  1378. static const uint32_t s_bptcP3[] =
  1379. { // 76543210 0000 1111 2222 3333 4444 5555 6666 7777
  1380. 0xaa685050, // 0, 0, 1, 1, 0, 0, 1, 1, 0, 2, 2, 1, 2, 2, 2, 2
  1381. 0x6a5a5040, // 0, 0, 0, 1, 0, 0, 1, 1, 2, 2, 1, 1, 2, 2, 2, 1
  1382. 0x5a5a4200, // 0, 0, 0, 0, 2, 0, 0, 1, 2, 2, 1, 1, 2, 2, 1, 1
  1383. 0x5450a0a8, // 0, 2, 2, 2, 0, 0, 2, 2, 0, 0, 1, 1, 0, 1, 1, 1
  1384. 0xa5a50000, // 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 2, 1, 1, 2, 2
  1385. 0xa0a05050, // 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 2, 2, 0, 0, 2, 2
  1386. 0x5555a0a0, // 0, 0, 2, 2, 0, 0, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1
  1387. 0x5a5a5050, // 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1
  1388. 0xaa550000, // 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2
  1389. 0xaa555500, // 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2
  1390. 0xaaaa5500, // 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2
  1391. 0x90909090, // 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2
  1392. 0x94949494, // 0, 1, 1, 2, 0, 1, 1, 2, 0, 1, 1, 2, 0, 1, 1, 2
  1393. 0xa4a4a4a4, // 0, 1, 2, 2, 0, 1, 2, 2, 0, 1, 2, 2, 0, 1, 2, 2
  1394. 0xa9a59450, // 0, 0, 1, 1, 0, 1, 1, 2, 1, 1, 2, 2, 1, 2, 2, 2
  1395. 0x2a0a4250, // 0, 0, 1, 1, 2, 0, 0, 1, 2, 2, 0, 0, 2, 2, 2, 0
  1396. 0xa5945040, // 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 2, 1, 1, 2, 2
  1397. 0x0a425054, // 0, 1, 1, 1, 0, 0, 1, 1, 2, 0, 0, 1, 2, 2, 0, 0
  1398. 0xa5a5a500, // 0, 0, 0, 0, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2
  1399. 0x55a0a0a0, // 0, 0, 2, 2, 0, 0, 2, 2, 0, 0, 2, 2, 1, 1, 1, 1
  1400. 0xa8a85454, // 0, 1, 1, 1, 0, 1, 1, 1, 0, 2, 2, 2, 0, 2, 2, 2
  1401. 0x6a6a4040, // 0, 0, 0, 1, 0, 0, 0, 1, 2, 2, 2, 1, 2, 2, 2, 1
  1402. 0xa4a45000, // 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 2, 2, 0, 1, 2, 2
  1403. 0x1a1a0500, // 0, 0, 0, 0, 1, 1, 0, 0, 2, 2, 1, 0, 2, 2, 1, 0
  1404. 0x0050a4a4, // 0, 1, 2, 2, 0, 1, 2, 2, 0, 0, 1, 1, 0, 0, 0, 0
  1405. 0xaaa59090, // 0, 0, 1, 2, 0, 0, 1, 2, 1, 1, 2, 2, 2, 2, 2, 2
  1406. 0x14696914, // 0, 1, 1, 0, 1, 2, 2, 1, 1, 2, 2, 1, 0, 1, 1, 0
  1407. 0x69691400, // 0, 0, 0, 0, 0, 1, 1, 0, 1, 2, 2, 1, 1, 2, 2, 1
  1408. 0xa08585a0, // 0, 0, 2, 2, 1, 1, 0, 2, 1, 1, 0, 2, 0, 0, 2, 2
  1409. 0xaa821414, // 0, 1, 1, 0, 0, 1, 1, 0, 2, 0, 0, 2, 2, 2, 2, 2
  1410. 0x50a4a450, // 0, 0, 1, 1, 0, 1, 2, 2, 0, 1, 2, 2, 0, 0, 1, 1
  1411. 0x6a5a0200, // 0, 0, 0, 0, 2, 0, 0, 0, 2, 2, 1, 1, 2, 2, 2, 1
  1412. 0xa9a58000, // 0, 0, 0, 0, 0, 0, 0, 2, 1, 1, 2, 2, 1, 2, 2, 2
  1413. 0x5090a0a8, // 0, 2, 2, 2, 0, 0, 2, 2, 0, 0, 1, 2, 0, 0, 1, 1
  1414. 0xa8a09050, // 0, 0, 1, 1, 0, 0, 1, 2, 0, 0, 2, 2, 0, 2, 2, 2
  1415. 0x24242424, // 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0
  1416. 0x00aa5500, // 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 0, 0, 0, 0
  1417. 0x24924924, // 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0
  1418. 0x24499224, // 0, 1, 2, 0, 2, 0, 1, 2, 1, 2, 0, 1, 0, 1, 2, 0
  1419. 0x50a50a50, // 0, 0, 1, 1, 2, 2, 0, 0, 1, 1, 2, 2, 0, 0, 1, 1
  1420. 0x500aa550, // 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 0, 0, 0, 0, 1, 1
  1421. 0xaaaa4444, // 0, 1, 0, 1, 0, 1, 0, 1, 2, 2, 2, 2, 2, 2, 2, 2
  1422. 0x66660000, // 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 2, 1, 2, 1, 2, 1
  1423. 0xa5a0a5a0, // 0, 0, 2, 2, 1, 1, 2, 2, 0, 0, 2, 2, 1, 1, 2, 2
  1424. 0x50a050a0, // 0, 0, 2, 2, 0, 0, 1, 1, 0, 0, 2, 2, 0, 0, 1, 1
  1425. 0x69286928, // 0, 2, 2, 0, 1, 2, 2, 1, 0, 2, 2, 0, 1, 2, 2, 1
  1426. 0x44aaaa44, // 0, 1, 0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 0, 1, 0, 1
  1427. 0x66666600, // 0, 0, 0, 0, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1
  1428. 0xaa444444, // 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 2, 2, 2, 2
  1429. 0x54a854a8, // 0, 2, 2, 2, 0, 1, 1, 1, 0, 2, 2, 2, 0, 1, 1, 1
  1430. 0x95809580, // 0, 0, 0, 2, 1, 1, 1, 2, 0, 0, 0, 2, 1, 1, 1, 2
  1431. 0x96969600, // 0, 0, 0, 0, 2, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2
  1432. 0xa85454a8, // 0, 2, 2, 2, 0, 1, 1, 1, 0, 1, 1, 1, 0, 2, 2, 2
  1433. 0x80959580, // 0, 0, 0, 2, 1, 1, 1, 2, 1, 1, 1, 2, 0, 0, 0, 2
  1434. 0xaa141414, // 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 2, 2, 2, 2
  1435. 0x96960000, // 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 1, 2, 2, 1, 1, 2
  1436. 0xaaaa1414, // 0, 1, 1, 0, 0, 1, 1, 0, 2, 2, 2, 2, 2, 2, 2, 2
  1437. 0xa05050a0, // 0, 0, 2, 2, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 2, 2
  1438. 0xa0a5a5a0, // 0, 0, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2, 0, 0, 2, 2
  1439. 0x96000000, // 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 1, 2
  1440. 0x40804080, // 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 1
  1441. 0xa9a8a9a8, // 0, 2, 2, 2, 1, 2, 2, 2, 0, 2, 2, 2, 1, 2, 2, 2
  1442. 0xaaaaaa44, // 0, 1, 0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
  1443. 0x2a4a5254, // 0, 1, 1, 1, 2, 0, 1, 1, 2, 2, 0, 1, 2, 2, 2, 0
  1444. };
  1445. static const uint8_t s_bptcA2[] =
  1446. {
  1447. 15, 15, 15, 15, 15, 15, 15, 15,
  1448. 15, 15, 15, 15, 15, 15, 15, 15,
  1449. 15, 2, 8, 2, 2, 8, 8, 15,
  1450. 2, 8, 2, 2, 8, 8, 2, 2,
  1451. 15, 15, 6, 8, 2, 8, 15, 15,
  1452. 2, 8, 2, 2, 2, 15, 15, 6,
  1453. 6, 2, 6, 8, 15, 15, 2, 2,
  1454. 15, 15, 15, 15, 15, 2, 2, 15,
  1455. };
  1456. static const uint8_t s_bptcA3[2][64] =
  1457. {
  1458. {
  1459. 3, 3, 15, 15, 8, 3, 15, 15,
  1460. 8, 8, 6, 6, 6, 5, 3, 3,
  1461. 3, 3, 8, 15, 3, 3, 6, 10,
  1462. 5, 8, 8, 6, 8, 5, 15, 15,
  1463. 8, 15, 3, 5, 6, 10, 8, 15,
  1464. 15, 3, 15, 5, 15, 15, 15, 15,
  1465. 3, 15, 5, 5, 5, 8, 5, 10,
  1466. 5, 10, 8, 13, 15, 12, 3, 3,
  1467. },
  1468. {
  1469. 15, 8, 8, 3, 15, 15, 3, 8,
  1470. 15, 15, 15, 15, 15, 15, 15, 8,
  1471. 15, 8, 15, 3, 15, 8, 15, 8,
  1472. 3, 15, 6, 10, 15, 15, 10, 8,
  1473. 15, 3, 15, 10, 10, 8, 9, 10,
  1474. 6, 15, 8, 15, 3, 6, 6, 8,
  1475. 15, 3, 15, 15, 15, 15, 15, 15,
  1476. 15, 15, 15, 15, 3, 15, 15, 8,
  1477. },
  1478. };
  1479. static const uint8_t s_bptcFactors[3][16] =
  1480. {
  1481. { 0, 21, 43, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  1482. { 0, 9, 18, 27, 37, 46, 55, 64, 0, 0, 0, 0, 0, 0, 0, 0 },
  1483. { 0, 4, 9, 13, 17, 21, 26, 30, 34, 38, 43, 47, 51, 55, 60, 64 },
  1484. };
  1485. struct BitReader
  1486. {
  1487. BitReader(const uint8_t* _data, uint16_t _bitPos = 0)
  1488. : m_data(_data)
  1489. , m_bitPos(_bitPos)
  1490. {
  1491. }
  1492. uint16_t read(uint8_t _numBits)
  1493. {
  1494. const uint16_t pos = m_bitPos / 8;
  1495. const uint16_t shift = m_bitPos & 7;
  1496. uint32_t data = 0;
  1497. bx::memCopy(&data, &m_data[pos], bx::min(4, 16-pos) );
  1498. m_bitPos += _numBits;
  1499. return uint16_t( (data >> shift) & ( (1 << _numBits)-1) );
  1500. }
  1501. uint16_t peek(uint16_t _offset, uint8_t _numBits)
  1502. {
  1503. const uint16_t bitPos = m_bitPos + _offset;
  1504. const uint16_t shift = bitPos & 7;
  1505. uint16_t pos = bitPos / 8;
  1506. uint32_t data = 0;
  1507. bx::memCopy(&data, &m_data[pos], bx::min(4, 16-pos) );
  1508. return uint8_t( (data >> shift) & ( (1 << _numBits)-1) );
  1509. }
  1510. const uint8_t* m_data;
  1511. uint16_t m_bitPos;
  1512. };
  1513. static uint16_t bc6hUnquantize(uint16_t _value, bool _signed, uint8_t _endpointBits)
  1514. {
  1515. const uint16_t maxValue = 1<<(_endpointBits-1);
  1516. if (_signed)
  1517. {
  1518. if (_endpointBits >= 16)
  1519. {
  1520. return _value;
  1521. }
  1522. const bool sign = !!(_value & 0x8000);
  1523. _value &= 0x7fff;
  1524. uint16_t unq;
  1525. if (0 == _value)
  1526. {
  1527. unq = 0;
  1528. }
  1529. else if (_value >= maxValue-1)
  1530. {
  1531. unq = 0x7fff;
  1532. }
  1533. else
  1534. {
  1535. unq = ( (_value<<15) + 0x4000) >> (_endpointBits-1);
  1536. }
  1537. return sign ? -unq : unq;
  1538. }
  1539. if (_endpointBits >= 15)
  1540. {
  1541. return _value;
  1542. }
  1543. if (0 == _value)
  1544. {
  1545. return 0;
  1546. }
  1547. if (_value == maxValue)
  1548. {
  1549. return UINT16_MAX;
  1550. }
  1551. return ( (_value<<15) + 0x4000) >> (_endpointBits-1);
  1552. }
  1553. static uint16_t bc6hUnquantizeFinal(uint16_t _value, bool _signed)
  1554. {
  1555. if (_signed)
  1556. {
  1557. const uint16_t sign = _value & 0x8000;
  1558. _value &= 0x7fff;
  1559. return ( (_value * 31) >> 5) | sign;
  1560. }
  1561. return (_value * 31) >> 6;
  1562. }
  1563. static uint16_t signExtend(uint16_t _value, uint8_t _numBits)
  1564. {
  1565. const uint16_t mask = 1 << (_numBits - 1);
  1566. const uint16_t result = (_value ^ mask) - mask;
  1567. return result;
  1568. }
  1569. struct Bc6hModeInfo
  1570. {
  1571. uint8_t transformed;
  1572. uint8_t partitionBits;
  1573. uint8_t endpointBits;
  1574. uint8_t deltaBits[3];
  1575. };
  1576. static const Bc6hModeInfo s_bc6hModeInfo[] =
  1577. { // +--------------------------- transformed
  1578. // | +------------------------ partition bits
  1579. // | | +--------------------- endpoint bits
  1580. // | | | +-------------- delta bits
  1581. { 1, 5, 10, { 5, 5, 5 } }, // 00 2-bits
  1582. { 1, 5, 7, { 6, 6, 6 } }, // 01
  1583. { 1, 5, 11, { 5, 4, 4 } }, // 00010 5-bits
  1584. { 0, 0, 10, { 10, 10, 10 } }, // 00011
  1585. { 0, 0, 0, { 0, 0, 0 } }, // -
  1586. { 0, 0, 0, { 0, 0, 0 } }, // -
  1587. { 1, 5, 11, { 4, 5, 4 } }, // 00110
  1588. { 1, 0, 11, { 9, 9, 9 } }, // 00010
  1589. { 0, 0, 0, { 0, 0, 0 } }, // -
  1590. { 0, 0, 0, { 0, 0, 0 } }, // -
  1591. { 1, 5, 11, { 4, 4, 5 } }, // 00010
  1592. { 1, 0, 12, { 8, 8, 8 } }, // 00010
  1593. { 0, 0, 0, { 0, 0, 0 } }, // -
  1594. { 0, 0, 0, { 0, 0, 0 } }, // -
  1595. { 1, 5, 9, { 5, 5, 5 } }, // 00010
  1596. { 1, 0, 16, { 4, 4, 4 } }, // 00010
  1597. { 0, 0, 0, { 0, 0, 0 } }, // -
  1598. { 0, 0, 0, { 0, 0, 0 } }, // -
  1599. { 1, 5, 8, { 6, 5, 5 } }, // 00010
  1600. { 0, 0, 0, { 0, 0, 0 } }, // -
  1601. { 0, 0, 0, { 0, 0, 0 } }, // -
  1602. { 0, 0, 0, { 0, 0, 0 } }, // -
  1603. { 1, 5, 8, { 5, 6, 5 } }, // 00010
  1604. { 0, 0, 0, { 0, 0, 0 } }, // -
  1605. { 0, 0, 0, { 0, 0, 0 } }, // -
  1606. { 0, 0, 0, { 0, 0, 0 } }, // -
  1607. { 1, 5, 8, { 5, 5, 6 } }, // 00010
  1608. { 0, 0, 0, { 0, 0, 0 } }, // -
  1609. { 0, 0, 0, { 0, 0, 0 } }, // -
  1610. { 0, 0, 0, { 0, 0, 0 } }, // -
  1611. { 0, 5, 6, { 6, 6, 6 } }, // 00010
  1612. { 0, 0, 0, { 0, 0, 0 } }, // -
  1613. };
  1614. static void decodeBlockBc6h(uint16_t _dst[16*3], const uint8_t _src[16], bool _signed)
  1615. {
  1616. if (!BX_ENABLED(BIMG_DECODE_BC6) )
  1617. {
  1618. return;
  1619. }
  1620. uint8_t src[16];
  1621. bx::memCopy(src, _src, 16);
  1622. BitReader bit(src);
  1623. uint8_t mode = uint8_t(bit.read(2));
  1624. uint16_t epR[4] = { /* rw, rx, ry, rz */ };
  1625. uint16_t epG[4] = { /* gw, gx, gy, gz */ };
  1626. uint16_t epB[4] = { /* bw, bx, by, bz */ };
  1627. if (mode & 2)
  1628. {
  1629. // 5-bit mode
  1630. mode |= bit.read(3) << 2;
  1631. if (0 == s_bc6hModeInfo[mode].endpointBits)
  1632. {
  1633. bx::memSet(_dst, 0, 16*3*2);
  1634. return;
  1635. }
  1636. switch (mode)
  1637. {
  1638. case 2:
  1639. epR[0] |= bit.read(10) << 0;
  1640. epG[0] |= bit.read(10) << 0;
  1641. epB[0] |= bit.read(10) << 0;
  1642. epR[1] |= bit.read( 5) << 0;
  1643. epR[0] |= bit.read( 1) << 10;
  1644. epG[2] |= bit.read( 4) << 0;
  1645. epG[1] |= bit.read( 4) << 0;
  1646. epG[0] |= bit.read( 1) << 10;
  1647. epB[3] |= bit.read( 1) << 0;
  1648. epG[3] |= bit.read( 4) << 0;
  1649. epB[1] |= bit.read( 4) << 0;
  1650. epB[0] |= bit.read( 1) << 10;
  1651. epB[3] |= bit.read( 1) << 1;
  1652. epB[2] |= bit.read( 4) << 0;
  1653. epR[2] |= bit.read( 5) << 0;
  1654. epB[3] |= bit.read( 1) << 2;
  1655. epR[3] |= bit.read( 5) << 0;
  1656. epB[3] |= bit.read( 1) << 3;
  1657. break;
  1658. case 3:
  1659. epR[0] |= bit.read(10) << 0;
  1660. epG[0] |= bit.read(10) << 0;
  1661. epB[0] |= bit.read(10) << 0;
  1662. epR[1] |= bit.read(10) << 0;
  1663. epG[1] |= bit.read(10) << 0;
  1664. epB[1] |= bit.read(10) << 0;
  1665. break;
  1666. case 6:
  1667. epR[0] |= bit.read(10) << 0;
  1668. epG[0] |= bit.read(10) << 0;
  1669. epB[0] |= bit.read(10) << 0;
  1670. epR[1] |= bit.read( 4) << 0;
  1671. epR[0] |= bit.read( 1) << 10;
  1672. epG[3] |= bit.read( 1) << 4;
  1673. epG[2] |= bit.read( 4) << 0;
  1674. epG[1] |= bit.read( 5) << 0;
  1675. epG[0] |= bit.read( 1) << 10;
  1676. epG[3] |= bit.read( 4) << 0;
  1677. epB[1] |= bit.read( 4) << 0;
  1678. epB[0] |= bit.read( 1) << 10;
  1679. epB[3] |= bit.read( 1) << 1;
  1680. epB[2] |= bit.read( 4) << 0;
  1681. epR[2] |= bit.read( 4) << 0;
  1682. epB[3] |= bit.read( 1) << 0;
  1683. epB[3] |= bit.read( 1) << 2;
  1684. epR[3] |= bit.read( 4) << 0;
  1685. epG[2] |= bit.read( 1) << 4;
  1686. epB[3] |= bit.read( 1) << 3;
  1687. break;
  1688. case 7:
  1689. epR[0] |= bit.read(10) << 0;
  1690. epG[0] |= bit.read(10) << 0;
  1691. epB[0] |= bit.read(10) << 0;
  1692. epR[1] |= bit.read( 9) << 0;
  1693. epR[0] |= bit.read( 1) << 10;
  1694. epG[1] |= bit.read( 9) << 0;
  1695. epG[0] |= bit.read( 1) << 10;
  1696. epB[1] |= bit.read( 9) << 0;
  1697. epB[0] |= bit.read( 1) << 10;
  1698. break;
  1699. case 10:
  1700. epR[0] |= bit.read(10) << 0;
  1701. epG[0] |= bit.read(10) << 0;
  1702. epB[0] |= bit.read(10) << 0;
  1703. epR[1] |= bit.read( 4) << 0;
  1704. epR[0] |= bit.read( 1) << 10;
  1705. epB[2] |= bit.read( 1) << 4;
  1706. epG[2] |= bit.read( 4) << 0;
  1707. epG[1] |= bit.read( 4) << 0;
  1708. epG[0] |= bit.read( 1) << 10;
  1709. epB[3] |= bit.read( 1) << 0;
  1710. epG[3] |= bit.read( 4) << 0;
  1711. epB[1] |= bit.read( 5) << 0;
  1712. epB[0] |= bit.read( 1) << 10;
  1713. epB[2] |= bit.read( 4) << 0;
  1714. epR[2] |= bit.read( 4) << 0;
  1715. epB[3] |= bit.read( 1) << 1;
  1716. epB[3] |= bit.read( 1) << 2;
  1717. epR[3] |= bit.read( 4) << 0;
  1718. epB[3] |= bit.read( 1) << 4;
  1719. epB[3] |= bit.read( 1) << 3;
  1720. break;
  1721. case 11:
  1722. epR[0] |= bit.read(10) << 0;
  1723. epG[0] |= bit.read(10) << 0;
  1724. epB[0] |= bit.read(10) << 0;
  1725. epR[1] |= bit.read( 8) << 0;
  1726. epR[0] |= bit.read( 1) << 11;
  1727. epR[0] |= bit.read( 1) << 10;
  1728. epG[1] |= bit.read( 8) << 0;
  1729. epG[0] |= bit.read( 1) << 11;
  1730. epG[0] |= bit.read( 1) << 10;
  1731. epB[1] |= bit.read( 8) << 0;
  1732. epB[0] |= bit.read( 1) << 11;
  1733. epB[0] |= bit.read( 1) << 10;
  1734. break;
  1735. case 14:
  1736. epR[0] |= bit.read( 9) << 0;
  1737. epB[2] |= bit.read( 1) << 4;
  1738. epG[0] |= bit.read( 9) << 0;
  1739. epG[2] |= bit.read( 1) << 4;
  1740. epB[0] |= bit.read( 9) << 0;
  1741. epB[3] |= bit.read( 1) << 4;
  1742. epR[1] |= bit.read( 5) << 0;
  1743. epG[3] |= bit.read( 1) << 4;
  1744. epG[2] |= bit.read( 4) << 0;
  1745. epG[1] |= bit.read( 5) << 0;
  1746. epB[3] |= bit.read( 1) << 0;
  1747. epG[3] |= bit.read( 4) << 0;
  1748. epB[1] |= bit.read( 5) << 0;
  1749. epB[3] |= bit.read( 1) << 1;
  1750. epB[2] |= bit.read( 4) << 0;
  1751. epR[2] |= bit.read( 5) << 0;
  1752. epB[3] |= bit.read( 1) << 2;
  1753. epR[3] |= bit.read( 5) << 0;
  1754. epB[3] |= bit.read( 1) << 3;
  1755. break;
  1756. case 15:
  1757. epR[0] |= bit.read(10) << 0;
  1758. epG[0] |= bit.read(10) << 0;
  1759. epB[0] |= bit.read(10) << 0;
  1760. epR[1] |= bit.read( 4) << 0;
  1761. epR[0] |= bit.read( 1) << 15;
  1762. epR[0] |= bit.read( 1) << 14;
  1763. epR[0] |= bit.read( 1) << 13;
  1764. epR[0] |= bit.read( 1) << 12;
  1765. epR[0] |= bit.read( 1) << 11;
  1766. epR[0] |= bit.read( 1) << 10;
  1767. epG[1] |= bit.read( 4) << 0;
  1768. epG[0] |= bit.read( 1) << 15;
  1769. epG[0] |= bit.read( 1) << 14;
  1770. epG[0] |= bit.read( 1) << 13;
  1771. epG[0] |= bit.read( 1) << 12;
  1772. epG[0] |= bit.read( 1) << 11;
  1773. epG[0] |= bit.read( 1) << 10;
  1774. epB[1] |= bit.read( 4) << 0;
  1775. epB[0] |= bit.read( 1) << 15;
  1776. epB[0] |= bit.read( 1) << 14;
  1777. epB[0] |= bit.read( 1) << 13;
  1778. epB[0] |= bit.read( 1) << 12;
  1779. epB[0] |= bit.read( 1) << 11;
  1780. epB[0] |= bit.read( 1) << 10;
  1781. break;
  1782. case 18:
  1783. epR[0] |= bit.read( 8) << 0;
  1784. epG[3] |= bit.read( 1) << 4;
  1785. epB[2] |= bit.read( 1) << 4;
  1786. epG[0] |= bit.read( 8) << 0;
  1787. epB[3] |= bit.read( 1) << 2;
  1788. epG[2] |= bit.read( 1) << 4;
  1789. epB[0] |= bit.read( 8) << 0;
  1790. epB[3] |= bit.read( 1) << 3;
  1791. epB[3] |= bit.read( 1) << 4;
  1792. epR[1] |= bit.read( 6) << 0;
  1793. epG[2] |= bit.read( 4) << 0;
  1794. epG[1] |= bit.read( 5) << 0;
  1795. epB[3] |= bit.read( 1) << 0;
  1796. epG[3] |= bit.read( 4) << 0;
  1797. epB[1] |= bit.read( 5) << 0;
  1798. epB[3] |= bit.read( 1) << 1;
  1799. epB[2] |= bit.read( 4) << 0;
  1800. epR[2] |= bit.read( 6) << 0;
  1801. epR[3] |= bit.read( 6) << 0;
  1802. break;
  1803. case 22:
  1804. epR[0] |= bit.read( 8) << 0;
  1805. epB[3] |= bit.read( 1) << 0;
  1806. epB[2] |= bit.read( 1) << 4;
  1807. epG[0] |= bit.read( 8) << 0;
  1808. epG[2] |= bit.read( 1) << 5;
  1809. epG[2] |= bit.read( 1) << 4;
  1810. epB[0] |= bit.read( 8) << 0;
  1811. epG[3] |= bit.read( 1) << 5;
  1812. epB[3] |= bit.read( 1) << 4;
  1813. epR[1] |= bit.read( 5) << 0;
  1814. epG[3] |= bit.read( 1) << 4;
  1815. epG[2] |= bit.read( 4) << 0;
  1816. epG[1] |= bit.read( 6) << 0;
  1817. epG[3] |= bit.read( 4) << 0;
  1818. epB[1] |= bit.read( 5) << 0;
  1819. epB[3] |= bit.read( 1) << 1;
  1820. epB[2] |= bit.read( 4) << 0;
  1821. epR[2] |= bit.read( 5) << 0;
  1822. epB[3] |= bit.read( 1) << 2;
  1823. epR[3] |= bit.read( 5) << 0;
  1824. epB[3] |= bit.read( 1) << 3;
  1825. break;
  1826. case 26:
  1827. epR[0] |= bit.read( 8) << 0;
  1828. epB[3] |= bit.read( 1) << 1;
  1829. epB[2] |= bit.read( 1) << 4;
  1830. epG[0] |= bit.read( 8) << 0;
  1831. epB[2] |= bit.read( 1) << 5;
  1832. epG[2] |= bit.read( 1) << 4;
  1833. epB[0] |= bit.read( 8) << 0;
  1834. epB[3] |= bit.read( 1) << 5;
  1835. epB[3] |= bit.read( 1) << 4;
  1836. epR[1] |= bit.read( 5) << 0;
  1837. epG[3] |= bit.read( 1) << 4;
  1838. epG[2] |= bit.read( 4) << 0;
  1839. epG[1] |= bit.read( 5) << 0;
  1840. epB[3] |= bit.read( 1) << 0;
  1841. epG[3] |= bit.read( 4) << 0;
  1842. epB[1] |= bit.read( 6) << 0;
  1843. epB[2] |= bit.read( 4) << 0;
  1844. epR[2] |= bit.read( 5) << 0;
  1845. epB[3] |= bit.read( 1) << 2;
  1846. epR[3] |= bit.read( 5) << 0;
  1847. epB[3] |= bit.read( 1) << 3;
  1848. break;
  1849. case 30:
  1850. epR[0] |= bit.read( 6) << 0;
  1851. epG[3] |= bit.read( 1) << 4;
  1852. epB[3] |= bit.read( 1) << 0;
  1853. epB[3] |= bit.read( 1) << 1;
  1854. epB[2] |= bit.read( 1) << 4;
  1855. epG[0] |= bit.read( 6) << 0;
  1856. epG[2] |= bit.read( 1) << 5;
  1857. epB[2] |= bit.read( 1) << 5;
  1858. epB[3] |= bit.read( 1) << 2;
  1859. epG[2] |= bit.read( 1) << 4;
  1860. epB[0] |= bit.read( 6) << 0;
  1861. epG[3] |= bit.read( 1) << 5;
  1862. epB[3] |= bit.read( 1) << 3;
  1863. epB[3] |= bit.read( 1) << 5;
  1864. epB[3] |= bit.read( 1) << 4;
  1865. epR[1] |= bit.read( 6) << 0;
  1866. epG[2] |= bit.read( 4) << 0;
  1867. epG[1] |= bit.read( 6) << 0;
  1868. epG[3] |= bit.read( 4) << 0;
  1869. epB[1] |= bit.read( 6) << 0;
  1870. epB[2] |= bit.read( 4) << 0;
  1871. epR[2] |= bit.read( 6) << 0;
  1872. epR[3] |= bit.read( 6) << 0;
  1873. break;
  1874. default:
  1875. break;
  1876. }
  1877. }
  1878. else
  1879. {
  1880. switch (mode)
  1881. {
  1882. case 0:
  1883. epG[2] |= bit.read( 1) << 4;
  1884. epB[2] |= bit.read( 1) << 4;
  1885. epB[3] |= bit.read( 1) << 4;
  1886. epR[0] |= bit.read(10) << 0;
  1887. epG[0] |= bit.read(10) << 0;
  1888. epB[0] |= bit.read(10) << 0;
  1889. epR[1] |= bit.read( 5) << 0;
  1890. epG[3] |= bit.read( 1) << 4;
  1891. epG[2] |= bit.read( 4) << 0;
  1892. epG[1] |= bit.read( 5) << 0;
  1893. epB[3] |= bit.read( 1) << 0;
  1894. epG[3] |= bit.read( 4) << 0;
  1895. epB[1] |= bit.read( 5) << 0;
  1896. epB[3] |= bit.read( 1) << 1;
  1897. epB[2] |= bit.read( 4) << 0;
  1898. epR[2] |= bit.read( 5) << 0;
  1899. epB[3] |= bit.read( 1) << 2;
  1900. epR[3] |= bit.read( 5) << 0;
  1901. epB[3] |= bit.read( 1) << 3;
  1902. break;
  1903. case 1:
  1904. epG[2] |= bit.read( 1) << 5;
  1905. epG[3] |= bit.read( 1) << 4;
  1906. epG[3] |= bit.read( 1) << 5;
  1907. epR[0] |= bit.read( 7) << 0;
  1908. epB[3] |= bit.read( 1) << 0;
  1909. epB[3] |= bit.read( 1) << 1;
  1910. epB[2] |= bit.read( 1) << 4;
  1911. epG[0] |= bit.read( 7) << 0;
  1912. epB[2] |= bit.read( 1) << 5;
  1913. epB[3] |= bit.read( 1) << 2;
  1914. epG[2] |= bit.read( 1) << 4;
  1915. epB[0] |= bit.read( 7) << 0;
  1916. epB[3] |= bit.read( 1) << 3;
  1917. epB[3] |= bit.read( 1) << 5;
  1918. epB[3] |= bit.read( 1) << 4;
  1919. epR[1] |= bit.read( 6) << 0;
  1920. epG[2] |= bit.read( 4) << 0;
  1921. epG[1] |= bit.read( 6) << 0;
  1922. epG[3] |= bit.read( 4) << 0;
  1923. epB[1] |= bit.read( 6) << 0;
  1924. epB[2] |= bit.read( 4) << 0;
  1925. epR[2] |= bit.read( 6) << 0;
  1926. epR[3] |= bit.read( 6) << 0;
  1927. break;
  1928. default:
  1929. break;
  1930. }
  1931. }
  1932. const Bc6hModeInfo mi = s_bc6hModeInfo[mode];
  1933. if (_signed)
  1934. {
  1935. epR[0] = signExtend(epR[0], mi.endpointBits);
  1936. epG[0] = signExtend(epG[0], mi.endpointBits);
  1937. epB[0] = signExtend(epB[0], mi.endpointBits);
  1938. }
  1939. const uint8_t numSubsets = !!mi.partitionBits + 1;
  1940. for (uint8_t ii = 1, num = numSubsets*2; ii < num; ++ii)
  1941. {
  1942. if (_signed
  1943. || mi.transformed)
  1944. {
  1945. epR[ii] = signExtend(epR[ii], mi.deltaBits[0]);
  1946. epG[ii] = signExtend(epG[ii], mi.deltaBits[1]);
  1947. epB[ii] = signExtend(epB[ii], mi.deltaBits[2]);
  1948. }
  1949. if (mi.transformed)
  1950. {
  1951. const uint16_t mask = (1<<mi.endpointBits) - 1;
  1952. epR[ii] = (epR[ii] + epR[0]) & mask;
  1953. epG[ii] = (epG[ii] + epG[0]) & mask;
  1954. epB[ii] = (epB[ii] + epB[0]) & mask;
  1955. if (_signed)
  1956. {
  1957. epR[ii] = signExtend(epR[ii], mi.endpointBits);
  1958. epG[ii] = signExtend(epG[ii], mi.endpointBits);
  1959. epB[ii] = signExtend(epB[ii], mi.endpointBits);
  1960. }
  1961. }
  1962. }
  1963. for (uint8_t ii = 0, num = numSubsets*2; ii < num; ++ii)
  1964. {
  1965. epR[ii] = bc6hUnquantize(epR[ii], _signed, mi.endpointBits);
  1966. epG[ii] = bc6hUnquantize(epG[ii], _signed, mi.endpointBits);
  1967. epB[ii] = bc6hUnquantize(epB[ii], _signed, mi.endpointBits);
  1968. }
  1969. const uint8_t partitionSetIdx = uint8_t(mi.partitionBits ? bit.read(5) : 0);
  1970. const uint8_t indexBits = mi.partitionBits ? 3 : 4;
  1971. const uint8_t* factors = s_bptcFactors[indexBits-2];
  1972. for (uint8_t yy = 0; yy < 4; ++yy)
  1973. {
  1974. for (uint8_t xx = 0; xx < 4; ++xx)
  1975. {
  1976. const uint8_t idx = yy*4+xx;
  1977. uint8_t subsetIndex = 0;
  1978. uint8_t indexAnchor = 0;
  1979. if (0 != mi.partitionBits)
  1980. {
  1981. subsetIndex = (s_bptcP2[partitionSetIdx] >> idx) & 1;
  1982. indexAnchor = subsetIndex ? s_bptcA2[partitionSetIdx] : 0;
  1983. }
  1984. const uint8_t anchor = idx == indexAnchor;
  1985. const uint8_t num = indexBits - anchor;
  1986. const uint8_t index = (uint8_t)bit.read(num);
  1987. const uint8_t fc = factors[index];
  1988. const uint8_t fca = 64 - fc;
  1989. const uint8_t fcb = fc;
  1990. subsetIndex *= 2;
  1991. uint16_t rr = bc6hUnquantizeFinal( (epR[subsetIndex]*fca + epR[subsetIndex + 1]*fcb + 32) >> 6, _signed);
  1992. uint16_t gg = bc6hUnquantizeFinal( (epG[subsetIndex]*fca + epG[subsetIndex + 1]*fcb + 32) >> 6, _signed);
  1993. uint16_t bb = bc6hUnquantizeFinal( (epB[subsetIndex]*fca + epB[subsetIndex + 1]*fcb + 32) >> 6, _signed);
  1994. uint16_t* rgba = &_dst[idx*3];
  1995. rgba[0] = rr;
  1996. rgba[1] = gg;
  1997. rgba[2] = bb;
  1998. }
  1999. }
  2000. }
  2001. static void decodeBlockBc6h(float _dst[16*4], const uint8_t _src[16])
  2002. {
  2003. if (!BX_ENABLED(BIMG_DECODE_BC6) )
  2004. {
  2005. return;
  2006. }
  2007. uint16_t tmp[16*3];
  2008. decodeBlockBc6h(tmp, _src, true);
  2009. for (uint32_t ii = 0; ii < 16; ++ii)
  2010. {
  2011. _dst[ii*4+0] = bx::halfToFloat(tmp[ii*3+0]);
  2012. _dst[ii*4+1] = bx::halfToFloat(tmp[ii*3+1]);
  2013. _dst[ii*4+2] = bx::halfToFloat(tmp[ii*3+2]);
  2014. _dst[ii*4+3] = 1.0f;
  2015. }
  2016. }
  2017. struct Bc7ModeInfo
  2018. {
  2019. uint8_t numSubsets;
  2020. uint8_t partitionBits;
  2021. uint8_t rotationBits;
  2022. uint8_t indexSelectionBits;
  2023. uint8_t colorBits;
  2024. uint8_t alphaBits;
  2025. uint8_t endpointPBits;
  2026. uint8_t sharedPBits;
  2027. uint8_t indexBits[2];
  2028. };
  2029. static const Bc7ModeInfo s_bp7ModeInfo[] =
  2030. { // +---------------------------- num subsets
  2031. // | +------------------------- partition bits
  2032. // | | +---------------------- rotation bits
  2033. // | | | +------------------- index selection bits
  2034. // | | | | +---------------- color bits
  2035. // | | | | | +------------- alpha bits
  2036. // | | | | | | +---------- endpoint P-bits
  2037. // | | | | | | | +------- shared P-bits
  2038. // | | | | | | | | +-- 2x index bits
  2039. { 3, 4, 0, 0, 4, 0, 1, 0, { 3, 0 } }, // 0
  2040. { 2, 6, 0, 0, 6, 0, 0, 1, { 3, 0 } }, // 1
  2041. { 3, 6, 0, 0, 5, 0, 0, 0, { 2, 0 } }, // 2
  2042. { 2, 6, 0, 0, 7, 0, 1, 0, { 2, 0 } }, // 3
  2043. { 1, 0, 2, 1, 5, 6, 0, 0, { 2, 3 } }, // 4
  2044. { 1, 0, 2, 0, 7, 8, 0, 0, { 2, 2 } }, // 5
  2045. { 1, 0, 0, 0, 7, 7, 1, 0, { 4, 0 } }, // 6
  2046. { 2, 6, 0, 0, 5, 5, 1, 0, { 2, 0 } }, // 7
  2047. };
  2048. static void decodeBlockBc7(uint8_t _dst[16*4], const uint8_t _src[16])
  2049. {
  2050. if (!BX_ENABLED(BIMG_DECODE_BC7) )
  2051. {
  2052. return;
  2053. }
  2054. BitReader bit(_src);
  2055. uint8_t mode = 0;
  2056. for (; mode < 8 && 0 == bit.read(1); ++mode)
  2057. {
  2058. }
  2059. if (mode == 8)
  2060. {
  2061. bx::memSet(_dst, 0, 16*4);
  2062. return;
  2063. }
  2064. const Bc7ModeInfo& mi = s_bp7ModeInfo[mode];
  2065. const uint8_t modePBits = 0 != mi.endpointPBits
  2066. ? mi.endpointPBits
  2067. : mi.sharedPBits
  2068. ;
  2069. const uint8_t partitionSetIdx = uint8_t(bit.read(mi.partitionBits) );
  2070. const uint8_t rotationMode = uint8_t(bit.read(mi.rotationBits) );
  2071. const uint8_t indexSelectionMode = uint8_t(bit.read(mi.indexSelectionBits) );
  2072. uint8_t epR[6];
  2073. uint8_t epG[6];
  2074. uint8_t epB[6];
  2075. uint8_t epA[6];
  2076. for (uint8_t ii = 0; ii < mi.numSubsets; ++ii)
  2077. {
  2078. epR[ii*2+0] = uint8_t(bit.read(mi.colorBits) << modePBits);
  2079. epR[ii*2+1] = uint8_t(bit.read(mi.colorBits) << modePBits);
  2080. }
  2081. for (uint8_t ii = 0; ii < mi.numSubsets; ++ii)
  2082. {
  2083. epG[ii*2+0] = uint8_t(bit.read(mi.colorBits) << modePBits);
  2084. epG[ii*2+1] = uint8_t(bit.read(mi.colorBits) << modePBits);
  2085. }
  2086. for (uint8_t ii = 0; ii < mi.numSubsets; ++ii)
  2087. {
  2088. epB[ii*2+0] = uint8_t(bit.read(mi.colorBits) << modePBits);
  2089. epB[ii*2+1] = uint8_t(bit.read(mi.colorBits) << modePBits);
  2090. }
  2091. if (mi.alphaBits)
  2092. {
  2093. for (uint8_t ii = 0; ii < mi.numSubsets; ++ii)
  2094. {
  2095. epA[ii*2+0] = uint8_t(bit.read(mi.alphaBits) << modePBits);
  2096. epA[ii*2+1] = uint8_t(bit.read(mi.alphaBits) << modePBits);
  2097. }
  2098. }
  2099. else
  2100. {
  2101. bx::memSet(epA, 0xff, 6);
  2102. }
  2103. if (0 != modePBits)
  2104. {
  2105. for (uint8_t ii = 0; ii < mi.numSubsets; ++ii)
  2106. {
  2107. const uint8_t pda = uint8_t( bit.read(modePBits) );
  2108. const uint8_t pdb = uint8_t(0 == mi.sharedPBits ? bit.read(modePBits) : pda);
  2109. epR[ii*2+0] |= pda;
  2110. epR[ii*2+1] |= pdb;
  2111. epG[ii*2+0] |= pda;
  2112. epG[ii*2+1] |= pdb;
  2113. epB[ii*2+0] |= pda;
  2114. epB[ii*2+1] |= pdb;
  2115. epA[ii*2+0] |= pda;
  2116. epA[ii*2+1] |= pdb;
  2117. }
  2118. }
  2119. const uint8_t colorBits = mi.colorBits + modePBits;
  2120. for (uint8_t ii = 0; ii < mi.numSubsets; ++ii)
  2121. {
  2122. epR[ii*2+0] = bitRangeConvert(epR[ii*2+0], colorBits, 8);
  2123. epR[ii*2+1] = bitRangeConvert(epR[ii*2+1], colorBits, 8);
  2124. epG[ii*2+0] = bitRangeConvert(epG[ii*2+0], colorBits, 8);
  2125. epG[ii*2+1] = bitRangeConvert(epG[ii*2+1], colorBits, 8);
  2126. epB[ii*2+0] = bitRangeConvert(epB[ii*2+0], colorBits, 8);
  2127. epB[ii*2+1] = bitRangeConvert(epB[ii*2+1], colorBits, 8);
  2128. }
  2129. if (mi.alphaBits)
  2130. {
  2131. const uint8_t alphaBits = mi.alphaBits + modePBits;
  2132. for (uint8_t ii = 0; ii < mi.numSubsets; ++ii)
  2133. {
  2134. epA[ii*2+0] = bitRangeConvert(epA[ii*2+0], alphaBits, 8);
  2135. epA[ii*2+1] = bitRangeConvert(epA[ii*2+1], alphaBits, 8);
  2136. }
  2137. }
  2138. const bool hasIndexBits1 = 0 != mi.indexBits[1];
  2139. const uint8_t* factors[] =
  2140. {
  2141. s_bptcFactors[mi.indexBits[0]-2],
  2142. hasIndexBits1 ? s_bptcFactors[mi.indexBits[1]-2] : factors[0],
  2143. };
  2144. uint16_t offset[2] =
  2145. {
  2146. 0,
  2147. uint16_t(mi.numSubsets*(16*mi.indexBits[0]-1) ),
  2148. };
  2149. for (uint8_t yy = 0; yy < 4; ++yy)
  2150. {
  2151. for (uint8_t xx = 0; xx < 4; ++xx)
  2152. {
  2153. const uint8_t idx = yy*4+xx;
  2154. uint8_t subsetIndex = 0;
  2155. uint8_t indexAnchor = 0;
  2156. switch (mi.numSubsets)
  2157. {
  2158. case 2:
  2159. subsetIndex = (s_bptcP2[partitionSetIdx] >> idx) & 1;
  2160. indexAnchor = 0 != subsetIndex ? s_bptcA2[partitionSetIdx] : 0;
  2161. break;
  2162. case 3:
  2163. subsetIndex = (s_bptcP3[partitionSetIdx] >> (2*idx) ) & 3;
  2164. indexAnchor = 0 != subsetIndex ? s_bptcA3[subsetIndex-1][partitionSetIdx] : 0;
  2165. break;
  2166. default:
  2167. break;
  2168. }
  2169. const uint8_t anchor = idx == indexAnchor;
  2170. const uint8_t num[2] =
  2171. {
  2172. uint8_t( mi.indexBits[0] - anchor ),
  2173. uint8_t(hasIndexBits1 ? mi.indexBits[1] - anchor : 0),
  2174. };
  2175. const uint8_t index[2] =
  2176. {
  2177. (uint8_t)bit.peek(offset[0], num[0]),
  2178. hasIndexBits1 ? (uint8_t)bit.peek(offset[1], num[1]) : index[0],
  2179. };
  2180. offset[0] += num[0];
  2181. offset[1] += num[1];
  2182. const uint8_t fc = factors[ indexSelectionMode][index[ indexSelectionMode] ];
  2183. const uint8_t fa = factors[!indexSelectionMode][index[!indexSelectionMode] ];
  2184. const uint8_t fca = 64 - fc;
  2185. const uint8_t fcb = fc;
  2186. const uint8_t faa = 64 - fa;
  2187. const uint8_t fab = fa;
  2188. subsetIndex *= 2;
  2189. uint8_t rr = uint8_t(uint16_t(epR[subsetIndex]*fca + epR[subsetIndex + 1]*fcb + 32) >> 6);
  2190. uint8_t gg = uint8_t(uint16_t(epG[subsetIndex]*fca + epG[subsetIndex + 1]*fcb + 32) >> 6);
  2191. uint8_t bb = uint8_t(uint16_t(epB[subsetIndex]*fca + epB[subsetIndex + 1]*fcb + 32) >> 6);
  2192. uint8_t aa = uint8_t(uint16_t(epA[subsetIndex]*faa + epA[subsetIndex + 1]*fab + 32) >> 6);
  2193. switch (rotationMode)
  2194. {
  2195. case 1: bx::swap(aa, rr); break;
  2196. case 2: bx::swap(aa, gg); break;
  2197. case 3: bx::swap(aa, bb); break;
  2198. default: break;
  2199. };
  2200. uint8_t* bgra = &_dst[idx*4];
  2201. bgra[0] = bb;
  2202. bgra[1] = gg;
  2203. bgra[2] = rr;
  2204. bgra[3] = aa;
  2205. }
  2206. }
  2207. }
  2208. // ATC
  2209. //
  2210. static void decodeBlockATC(uint8_t _dst[16*4], const uint8_t _src[8])
  2211. {
  2212. if (!BX_ENABLED(BIMG_DECODE_ATC) )
  2213. {
  2214. return;
  2215. }
  2216. uint8_t colors[4*4];
  2217. uint32_t c0 = _src[0] | (_src[1] << 8);
  2218. uint32_t c1 = _src[2] | (_src[3] << 8);
  2219. if (0 == (c0 & 0x8000) )
  2220. {
  2221. colors[ 0] = bitRangeConvert( (c0>> 0)&0x1f, 5, 8);
  2222. colors[ 1] = bitRangeConvert( (c0>> 5)&0x1f, 5, 8);
  2223. colors[ 2] = bitRangeConvert( (c0>>10)&0x1f, 5, 8);
  2224. colors[12] = bitRangeConvert( (c1>> 0)&0x1f, 5, 8);
  2225. colors[13] = bitRangeConvert( (c1>> 5)&0x3f, 6, 8);
  2226. colors[14] = bitRangeConvert( (c1>>11)&0x1f, 5, 8);
  2227. colors[ 4] = (2 * colors[0] + colors[12]) / 3;
  2228. colors[ 5] = (2 * colors[1] + colors[13]) / 3;
  2229. colors[ 6] = (2 * colors[2] + colors[14]) / 3;
  2230. colors[ 8] = (colors[0] + 2 * colors[12]) / 3;
  2231. colors[ 9] = (colors[1] + 2 * colors[13]) / 3;
  2232. colors[10] = (colors[2] + 2 * colors[14]) / 3;
  2233. }
  2234. else
  2235. {
  2236. colors[ 0] = 0;
  2237. colors[ 1] = 0;
  2238. colors[ 2] = 0;
  2239. colors[ 8] = bitRangeConvert( (c0>> 0)&0x1f, 5, 8);
  2240. colors[ 9] = bitRangeConvert( (c0>> 5)&0x1f, 5, 8);
  2241. colors[10] = bitRangeConvert( (c0>>10)&0x1f, 5, 8);
  2242. colors[12] = bitRangeConvert( (c1>> 0)&0x1f, 5, 8);
  2243. colors[13] = bitRangeConvert( (c1>> 5)&0x3f, 6, 8);
  2244. colors[14] = bitRangeConvert( (c1>>11)&0x1f, 5, 8);
  2245. colors[ 4] = colors[ 8] - colors[12] / 4;
  2246. colors[ 5] = colors[ 9] - colors[13] / 4;
  2247. colors[ 6] = colors[10] - colors[14] / 4;
  2248. }
  2249. for (uint32_t ii = 0, next = 8*4; ii < 16*4; ii += 4, next += 2)
  2250. {
  2251. int32_t idx = ( (_src[next>>3] >> (next & 7) ) & 3) * 4;
  2252. _dst[ii+0] = colors[idx+0];
  2253. _dst[ii+1] = colors[idx+1];
  2254. _dst[ii+2] = colors[idx+2];
  2255. _dst[ii+3] = colors[idx+3];
  2256. }
  2257. }
  2258. static const int32_t s_etc1Mod[8][4] =
  2259. {
  2260. { 2, 8, -2, -8 },
  2261. { 5, 17, -5, -17 },
  2262. { 9, 29, -9, -29 },
  2263. { 13, 42, -13, -42 },
  2264. { 18, 60, -18, -60 },
  2265. { 24, 80, -24, -80 },
  2266. { 33, 106, -33, -106 },
  2267. { 47, 183, -47, -183 },
  2268. };
  2269. static const uint8_t s_etc2Mod[] = { 3, 6, 11, 16, 23, 32, 41, 64 };
  2270. static uint8_t uint8_sat(int32_t _a)
  2271. {
  2272. using namespace bx;
  2273. const uint32_t min = uint32_imin(_a, 255);
  2274. const uint32_t result = uint32_imax(min, 0);
  2275. return (uint8_t)result;
  2276. }
  2277. static uint8_t uint8_satadd(int32_t _a, int32_t _b)
  2278. {
  2279. const int32_t add = _a + _b;
  2280. return uint8_sat(add);
  2281. }
  2282. static void decodeBlockEtc2ModeT(uint8_t _dst[16*4], const uint8_t _src[8])
  2283. {
  2284. uint8_t rgb[16];
  2285. // 0 1 2 3 4 5 6 7
  2286. // 7654321076543210765432107654321076543210765432107654321076543210
  2287. // ...rr.rrggggbbbbrrrrggggbbbbDD.Dmmmmmmmmmmmmmmmmllllllllllllllll
  2288. // ^ ^ ^ ^ ^
  2289. // +-- c0 +-- c1 | +-- msb +-- lsb
  2290. // +-- dist
  2291. rgb[ 0] = ( (_src[0] >> 1) & 0xc)
  2292. | (_src[0] & 0x3)
  2293. ;
  2294. rgb[ 1] = _src[1] >> 4;
  2295. rgb[ 2] = _src[1] & 0xf;
  2296. rgb[ 8] = _src[2] >> 4;
  2297. rgb[ 9] = _src[2] & 0xf;
  2298. rgb[10] = _src[3] >> 4;
  2299. rgb[ 0] = bitRangeConvert(rgb[ 0], 4, 8);
  2300. rgb[ 1] = bitRangeConvert(rgb[ 1], 4, 8);
  2301. rgb[ 2] = bitRangeConvert(rgb[ 2], 4, 8);
  2302. rgb[ 8] = bitRangeConvert(rgb[ 8], 4, 8);
  2303. rgb[ 9] = bitRangeConvert(rgb[ 9], 4, 8);
  2304. rgb[10] = bitRangeConvert(rgb[10], 4, 8);
  2305. uint8_t dist = ((_src[3] >> 1) & 0x6) | (_src[3] & 0x1);
  2306. int32_t mod = s_etc2Mod[dist];
  2307. rgb[ 4] = uint8_satadd(rgb[ 8], mod);
  2308. rgb[ 5] = uint8_satadd(rgb[ 9], mod);
  2309. rgb[ 6] = uint8_satadd(rgb[10], mod);
  2310. rgb[12] = uint8_satadd(rgb[ 8], -mod);
  2311. rgb[13] = uint8_satadd(rgb[ 9], -mod);
  2312. rgb[14] = uint8_satadd(rgb[10], -mod);
  2313. uint32_t indexMsb = (_src[4]<<8) | _src[5];
  2314. uint32_t indexLsb = (_src[6]<<8) | _src[7];
  2315. for (uint32_t ii = 0; ii < 16; ++ii)
  2316. {
  2317. const uint32_t idx = (ii&0xc) | ( (ii & 0x3)<<4);
  2318. const uint32_t lsbi = indexLsb & 1;
  2319. const uint32_t msbi = (indexMsb & 1)<<1;
  2320. const uint32_t pal = (lsbi | msbi)<<2;
  2321. _dst[idx + 0] = rgb[pal+2];
  2322. _dst[idx + 1] = rgb[pal+1];
  2323. _dst[idx + 2] = rgb[pal+0];
  2324. _dst[idx + 3] = 255;
  2325. indexLsb >>= 1;
  2326. indexMsb >>= 1;
  2327. }
  2328. }
  2329. static void decodeBlockEtc2ModeH(uint8_t _dst[16*4], const uint8_t _src[8])
  2330. {
  2331. uint8_t rgb[16];
  2332. // 0 1 2 3 4 5 6 7
  2333. // 7654321076543210765432107654321076543210765432107654321076543210
  2334. // .rrrrggg...gb.bbbrrrrggggbbbbD.Dmmmmmmmmmmmmmmmmllllllllllllllll
  2335. // ^ ^ ^ ^ ^
  2336. // +-- c0 +-- c1 | +-- msb +-- lsb
  2337. // +-- dist
  2338. rgb[ 0] = (_src[0] >> 3) & 0xf;
  2339. rgb[ 1] = ( (_src[0] << 1) & 0xe)
  2340. | ( (_src[1] >> 4) & 0x1)
  2341. ;
  2342. rgb[ 2] = (_src[1] & 0x8)
  2343. | ( (_src[1] << 1) & 0x6)
  2344. | (_src[2] >> 7)
  2345. ;
  2346. rgb[ 8] = (_src[2] >> 3) & 0xf;
  2347. rgb[ 9] = ( (_src[2] << 1) & 0xe)
  2348. | (_src[3] >> 7)
  2349. ;
  2350. rgb[10] = (_src[3] >> 3) & 0xf;
  2351. rgb[ 0] = bitRangeConvert(rgb[ 0], 4, 8);
  2352. rgb[ 1] = bitRangeConvert(rgb[ 1], 4, 8);
  2353. rgb[ 2] = bitRangeConvert(rgb[ 2], 4, 8);
  2354. rgb[ 8] = bitRangeConvert(rgb[ 8], 4, 8);
  2355. rgb[ 9] = bitRangeConvert(rgb[ 9], 4, 8);
  2356. rgb[10] = bitRangeConvert(rgb[10], 4, 8);
  2357. uint32_t col0 = uint32_t(rgb[0]<<16) | uint32_t(rgb[1]<<8) | uint32_t(rgb[ 2]);
  2358. uint32_t col1 = uint32_t(rgb[8]<<16) | uint32_t(rgb[9]<<8) | uint32_t(rgb[10]);
  2359. uint8_t dist = (_src[3] & 0x4) | ((_src[3]<<1)&0x2) | (col0 >= col1);
  2360. int32_t mod = s_etc2Mod[dist];
  2361. rgb[ 4] = uint8_satadd(rgb[ 0], -mod);
  2362. rgb[ 5] = uint8_satadd(rgb[ 1], -mod);
  2363. rgb[ 6] = uint8_satadd(rgb[ 2], -mod);
  2364. rgb[ 0] = uint8_satadd(rgb[ 0], mod);
  2365. rgb[ 1] = uint8_satadd(rgb[ 1], mod);
  2366. rgb[ 2] = uint8_satadd(rgb[ 2], mod);
  2367. rgb[12] = uint8_satadd(rgb[ 8], -mod);
  2368. rgb[13] = uint8_satadd(rgb[ 9], -mod);
  2369. rgb[14] = uint8_satadd(rgb[10], -mod);
  2370. rgb[ 8] = uint8_satadd(rgb[ 8], mod);
  2371. rgb[ 9] = uint8_satadd(rgb[ 9], mod);
  2372. rgb[10] = uint8_satadd(rgb[10], mod);
  2373. uint32_t indexMsb = (_src[4]<<8) | _src[5];
  2374. uint32_t indexLsb = (_src[6]<<8) | _src[7];
  2375. for (uint32_t ii = 0; ii < 16; ++ii)
  2376. {
  2377. const uint32_t idx = (ii&0xc) | ( (ii & 0x3)<<4);
  2378. const uint32_t lsbi = indexLsb & 1;
  2379. const uint32_t msbi = (indexMsb & 1)<<1;
  2380. const uint32_t pal = (lsbi | msbi)<<2;
  2381. _dst[idx + 0] = rgb[pal+2];
  2382. _dst[idx + 1] = rgb[pal+1];
  2383. _dst[idx + 2] = rgb[pal+0];
  2384. _dst[idx + 3] = 255;
  2385. indexLsb >>= 1;
  2386. indexMsb >>= 1;
  2387. }
  2388. }
  2389. static void decodeBlockEtc2ModePlanar(uint8_t _dst[16*4], const uint8_t _src[8])
  2390. {
  2391. // 0 1 2 3 4 5 6 7
  2392. // 7654321076543210765432107654321076543210765432107654321076543210
  2393. // .rrrrrrg.ggggggb...bb.bbbrrrrr.rgggggggbbbbbbrrrrrrgggggggbbbbbb
  2394. // ^ ^ ^
  2395. // +-- c0 +-- cH +-- cV
  2396. uint8_t c0[3];
  2397. uint8_t cH[3];
  2398. uint8_t cV[3];
  2399. c0[0] = (_src[0] >> 1) & 0x3f;
  2400. c0[1] = ( (_src[0] & 1) << 6)
  2401. | ( (_src[1] >> 1) & 0x3f)
  2402. ;
  2403. c0[2] = ( (_src[1] & 1) << 5)
  2404. | ( (_src[2] & 0x18) )
  2405. | ( (_src[2] << 1) & 6)
  2406. | ( (_src[3] >> 7) )
  2407. ;
  2408. cH[0] = ( (_src[3] >> 1) & 0x3e)
  2409. | (_src[3] & 1)
  2410. ;
  2411. cH[1] = _src[4] >> 1;
  2412. cH[2] = ( (_src[4] & 1) << 5)
  2413. | (_src[5] >> 3)
  2414. ;
  2415. cV[0] = ( (_src[5] & 0x7) << 3)
  2416. | (_src[6] >> 5)
  2417. ;
  2418. cV[1] = ( (_src[6] & 0x1f) << 2)
  2419. | (_src[7] >> 5)
  2420. ;
  2421. cV[2] = _src[7] & 0x3f;
  2422. c0[0] = bitRangeConvert(c0[0], 6, 8);
  2423. c0[1] = bitRangeConvert(c0[1], 7, 8);
  2424. c0[2] = bitRangeConvert(c0[2], 6, 8);
  2425. cH[0] = bitRangeConvert(cH[0], 6, 8);
  2426. cH[1] = bitRangeConvert(cH[1], 7, 8);
  2427. cH[2] = bitRangeConvert(cH[2], 6, 8);
  2428. cV[0] = bitRangeConvert(cV[0], 6, 8);
  2429. cV[1] = bitRangeConvert(cV[1], 7, 8);
  2430. cV[2] = bitRangeConvert(cV[2], 6, 8);
  2431. int16_t dy[3];
  2432. dy[0] = cV[0] - c0[0];
  2433. dy[1] = cV[1] - c0[1];
  2434. dy[2] = cV[2] - c0[2];
  2435. int16_t sx[3];
  2436. sx[0] = int16_t(c0[0])<<2;
  2437. sx[1] = int16_t(c0[1])<<2;
  2438. sx[2] = int16_t(c0[2])<<2;
  2439. int16_t ex[3];
  2440. ex[0] = int16_t(cH[0])<<2;
  2441. ex[1] = int16_t(cH[1])<<2;
  2442. ex[2] = int16_t(cH[2])<<2;
  2443. for (int32_t vv = 0; vv < 4; ++vv)
  2444. {
  2445. int16_t dx[3];
  2446. dx[0] = (ex[0] - sx[0])>>2;
  2447. dx[1] = (ex[1] - sx[1])>>2;
  2448. dx[2] = (ex[2] - sx[2])>>2;
  2449. for (int32_t hh = 0; hh < 4; ++hh)
  2450. {
  2451. const uint32_t idx = (vv<<4) + (hh<<2);
  2452. _dst[idx + 0] = uint8_sat( (sx[2] + dx[2]*hh)>>2);
  2453. _dst[idx + 1] = uint8_sat( (sx[1] + dx[1]*hh)>>2);
  2454. _dst[idx + 2] = uint8_sat( (sx[0] + dx[0]*hh)>>2);
  2455. _dst[idx + 3] = 255;
  2456. }
  2457. sx[0] += dy[0];
  2458. sx[1] += dy[1];
  2459. sx[2] += dy[2];
  2460. ex[0] += dy[0];
  2461. ex[1] += dy[1];
  2462. ex[2] += dy[2];
  2463. }
  2464. }
  2465. static void decodeBlockEtc12(uint8_t _dst[16*4], const uint8_t _src[8])
  2466. {
  2467. if (!BX_ENABLED(BIMG_DECODE_ETC1 || BIMG_DECODE_ETC2) )
  2468. {
  2469. return;
  2470. }
  2471. bool flipBit = 0 != (_src[3] & 0x1);
  2472. bool diffBit = 0 != (_src[3] & 0x2);
  2473. uint8_t rgb[8];
  2474. if (diffBit)
  2475. {
  2476. rgb[0] = _src[0] >> 3;
  2477. rgb[1] = _src[1] >> 3;
  2478. rgb[2] = _src[2] >> 3;
  2479. int8_t diff[3];
  2480. diff[0] = int8_t( (_src[0] & 0x7)<<5)>>5;
  2481. diff[1] = int8_t( (_src[1] & 0x7)<<5)>>5;
  2482. diff[2] = int8_t( (_src[2] & 0x7)<<5)>>5;
  2483. int8_t rr = rgb[0] + diff[0];
  2484. int8_t gg = rgb[1] + diff[1];
  2485. int8_t bb = rgb[2] + diff[2];
  2486. // Etc2 3-modes
  2487. if (rr < 0 || rr > 31)
  2488. {
  2489. decodeBlockEtc2ModeT(_dst, _src);
  2490. return;
  2491. }
  2492. if (gg < 0 || gg > 31)
  2493. {
  2494. decodeBlockEtc2ModeH(_dst, _src);
  2495. return;
  2496. }
  2497. if (bb < 0 || bb > 31)
  2498. {
  2499. decodeBlockEtc2ModePlanar(_dst, _src);
  2500. return;
  2501. }
  2502. // Etc1
  2503. rgb[0] = bitRangeConvert(rgb[0], 5, 8);
  2504. rgb[1] = bitRangeConvert(rgb[1], 5, 8);
  2505. rgb[2] = bitRangeConvert(rgb[2], 5, 8);
  2506. rgb[4] = bitRangeConvert(rr, 5, 8);
  2507. rgb[5] = bitRangeConvert(gg, 5, 8);
  2508. rgb[6] = bitRangeConvert(bb, 5, 8);
  2509. }
  2510. else
  2511. {
  2512. rgb[0] = _src[0] >> 4;
  2513. rgb[1] = _src[1] >> 4;
  2514. rgb[2] = _src[2] >> 4;
  2515. rgb[4] = _src[0] & 0xf;
  2516. rgb[5] = _src[1] & 0xf;
  2517. rgb[6] = _src[2] & 0xf;
  2518. rgb[0] = bitRangeConvert(rgb[0], 4, 8);
  2519. rgb[1] = bitRangeConvert(rgb[1], 4, 8);
  2520. rgb[2] = bitRangeConvert(rgb[2], 4, 8);
  2521. rgb[4] = bitRangeConvert(rgb[4], 4, 8);
  2522. rgb[5] = bitRangeConvert(rgb[5], 4, 8);
  2523. rgb[6] = bitRangeConvert(rgb[6], 4, 8);
  2524. }
  2525. uint32_t table[2];
  2526. table[0] = (_src[3] >> 5) & 0x7;
  2527. table[1] = (_src[3] >> 2) & 0x7;
  2528. uint32_t indexMsb = (_src[4]<<8) | _src[5];
  2529. uint32_t indexLsb = (_src[6]<<8) | _src[7];
  2530. if (flipBit)
  2531. {
  2532. for (uint32_t ii = 0; ii < 16; ++ii)
  2533. {
  2534. const uint32_t block = (ii>>1)&1;
  2535. const uint32_t color = block<<2;
  2536. const uint32_t idx = (ii&0xc) | ( (ii & 0x3)<<4);
  2537. const uint32_t lsbi = indexLsb & 1;
  2538. const uint32_t msbi = (indexMsb & 1)<<1;
  2539. const int32_t mod = s_etc1Mod[table[block] ][lsbi | msbi];
  2540. _dst[idx + 0] = uint8_satadd(rgb[color+2], mod);
  2541. _dst[idx + 1] = uint8_satadd(rgb[color+1], mod);
  2542. _dst[idx + 2] = uint8_satadd(rgb[color+0], mod);
  2543. _dst[idx + 3] = 255;
  2544. indexLsb >>= 1;
  2545. indexMsb >>= 1;
  2546. }
  2547. }
  2548. else
  2549. {
  2550. for (uint32_t ii = 0; ii < 16; ++ii)
  2551. {
  2552. const uint32_t block = ii>>3;
  2553. const uint32_t color = block<<2;
  2554. const uint32_t idx = (ii&0xc) | ( (ii & 0x3)<<4);
  2555. const uint32_t lsbi = indexLsb & 1;
  2556. const uint32_t msbi = (indexMsb & 1)<<1;
  2557. const int32_t mod = s_etc1Mod[table[block] ][lsbi | msbi];
  2558. _dst[idx + 0] = uint8_satadd(rgb[color+2], mod);
  2559. _dst[idx + 1] = uint8_satadd(rgb[color+1], mod);
  2560. _dst[idx + 2] = uint8_satadd(rgb[color+0], mod);
  2561. _dst[idx + 3] = 255;
  2562. indexLsb >>= 1;
  2563. indexMsb >>= 1;
  2564. }
  2565. }
  2566. }
  2567. static const int8_t s_etc2aMod[16][8] =
  2568. {
  2569. { -3, -6, -9, -15, 2, 5, 8, 14 },
  2570. { -3, -7, -10, -13, 2, 6, 9, 12 },
  2571. { -2, -5, -8, -13, 1, 4, 7, 12 },
  2572. { -2, -4, -6, -13, 1, 3, 5, 12 },
  2573. { -3, -6, -8, -12, 2, 5, 7, 11 },
  2574. { -3, -7, -9, -11, 2, 6, 8, 10 },
  2575. { -4, -7, -8, -11, 3, 6, 7, 10 },
  2576. { -3, -5, -8, -11, 2, 4, 7, 10 },
  2577. { -2, -6, -8, -10, 1, 5, 7, 9 },
  2578. { -2, -5, -8, -10, 1, 4, 7, 9 },
  2579. { -2, -4, -8, -10, 1, 3, 7, 9 },
  2580. { -2, -5, -7, -10, 1, 4, 6, 9 },
  2581. { -3, -4, -7, -10, 2, 3, 6, 9 },
  2582. { -1, -2, -3, -10, 0, 1, 2, 9 },
  2583. { -4, -6, -8, -9, 3, 5, 7, 8 },
  2584. { -3, -5, -7, -9, 2, 4, 6, 8 }
  2585. };
  2586. void decodeBlockEtc2Alpha(uint8_t _dst[16 * 4], const uint8_t _src[8])
  2587. {
  2588. if (!BX_ENABLED(BIMG_DECODE_ETC2))
  2589. {
  2590. return;
  2591. }
  2592. const int32_t bc = _src[0];
  2593. const int8_t *modTable = s_etc2aMod[_src[1] & 0x0f];
  2594. const int32_t mult = (_src[1] & 0xf0) >> 4;
  2595. const uint64_t indices = ((uint64_t)_src[2] << 40)
  2596. | ((uint64_t)_src[3] << 32)
  2597. | ((uint64_t)_src[4] << 24)
  2598. | ((uint64_t)_src[5] << 16)
  2599. | ((uint64_t)_src[6] << 8)
  2600. | _src[7];
  2601. for (int ii = 0; ii < 16; ii++) {
  2602. const uint32_t idx = (ii & 0xc) | ((ii & 0x3) << 4);
  2603. const int32_t mod = modTable[(indices >> (45 - ii * 3)) & 0x7];
  2604. _dst[idx + 3] = uint8_satadd(bc, mod*mult);
  2605. }
  2606. }
  2607. static const uint8_t s_pvrtcFactors[16][4] =
  2608. {
  2609. { 4, 4, 4, 4 },
  2610. { 2, 6, 2, 6 },
  2611. { 8, 0, 8, 0 },
  2612. { 6, 2, 6, 2 },
  2613. { 2, 2, 6, 6 },
  2614. { 1, 3, 3, 9 },
  2615. { 4, 0, 12, 0 },
  2616. { 3, 1, 9, 3 },
  2617. { 8, 8, 0, 0 },
  2618. { 4, 12, 0, 0 },
  2619. { 16, 0, 0, 0 },
  2620. { 12, 4, 0, 0 },
  2621. { 6, 6, 2, 2 },
  2622. { 3, 9, 1, 3 },
  2623. { 12, 0, 4, 0 },
  2624. { 9, 3, 3, 1 },
  2625. };
  2626. static const uint8_t s_pvrtcWeights[8][4] =
  2627. {
  2628. { 8, 0, 8, 0 },
  2629. { 5, 3, 5, 3 },
  2630. { 3, 5, 3, 5 },
  2631. { 0, 8, 0, 8 },
  2632. { 8, 0, 8, 0 },
  2633. { 4, 4, 4, 4 },
  2634. { 4, 4, 4, 4 },
  2635. { 0, 8, 0, 8 },
  2636. };
  2637. uint32_t morton2d(uint32_t _x, uint32_t _y)
  2638. {
  2639. using namespace bx;
  2640. const uint32_t tmpx = uint32_part1by1(_x);
  2641. const uint32_t xbits = uint32_sll(tmpx, 1);
  2642. const uint32_t ybits = uint32_part1by1(_y);
  2643. const uint32_t result = uint32_or(xbits, ybits);
  2644. return result;
  2645. }
  2646. uint32_t getColor(const uint8_t _src[8])
  2647. {
  2648. return 0
  2649. | _src[7]<<24
  2650. | _src[6]<<16
  2651. | _src[5]<<8
  2652. | _src[4]
  2653. ;
  2654. }
  2655. static void decodeBlockPtc14RgbAddA(uint32_t _block, uint32_t* _r, uint32_t* _g, uint32_t* _b, uint8_t _factor)
  2656. {
  2657. if (0 != (_block & (1<<15) ) )
  2658. {
  2659. *_r += bitRangeConvert( (_block >> 10) & 0x1f, 5, 8) * _factor;
  2660. *_g += bitRangeConvert( (_block >> 5) & 0x1f, 5, 8) * _factor;
  2661. *_b += bitRangeConvert( (_block >> 1) & 0x0f, 4, 8) * _factor;
  2662. }
  2663. else
  2664. {
  2665. *_r += bitRangeConvert( (_block >> 8) & 0xf, 4, 8) * _factor;
  2666. *_g += bitRangeConvert( (_block >> 4) & 0xf, 4, 8) * _factor;
  2667. *_b += bitRangeConvert( (_block >> 1) & 0x7, 3, 8) * _factor;
  2668. }
  2669. }
  2670. static void decodeBlockPtc14RgbAddB(uint32_t _block, uint32_t* _r, uint32_t* _g, uint32_t* _b, uint8_t _factor)
  2671. {
  2672. if (0 != (_block & (1<<31) ) )
  2673. {
  2674. *_r += bitRangeConvert( (_block >> 26) & 0x1f, 5, 8) * _factor;
  2675. *_g += bitRangeConvert( (_block >> 21) & 0x1f, 5, 8) * _factor;
  2676. *_b += bitRangeConvert( (_block >> 16) & 0x1f, 5, 8) * _factor;
  2677. }
  2678. else
  2679. {
  2680. *_r += bitRangeConvert( (_block >> 24) & 0xf, 4, 8) * _factor;
  2681. *_g += bitRangeConvert( (_block >> 20) & 0xf, 4, 8) * _factor;
  2682. *_b += bitRangeConvert( (_block >> 16) & 0xf, 4, 8) * _factor;
  2683. }
  2684. }
  2685. static void decodeBlockPtc14(uint8_t _dst[16*4], const uint8_t* _src, uint32_t _x, uint32_t _y, uint32_t _width, uint32_t _height)
  2686. {
  2687. // 0 1 2 3 4 5 6 7
  2688. // 7654321076543210765432107654321076543210765432107654321076543210
  2689. // mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmyrrrrrgggggbbbbbxrrrrrgggggbbbbp
  2690. // ^ ^^ ^^ ^
  2691. // +-- modulation data |+- B color |+- A color |
  2692. // +-- B opaque +-- A opaque |
  2693. // alpha punchthrough --+
  2694. const uint8_t* bc = &_src[morton2d(_x, _y) * 8];
  2695. uint32_t mod = 0
  2696. | bc[3]<<24
  2697. | bc[2]<<16
  2698. | bc[1]<<8
  2699. | bc[0]
  2700. ;
  2701. const bool punchthrough = !!(bc[7] & 1);
  2702. const uint8_t* weightTable = s_pvrtcWeights[4 * punchthrough];
  2703. const uint8_t* factorTable = s_pvrtcFactors[0];
  2704. for (int yy = 0; yy < 4; ++yy)
  2705. {
  2706. const uint32_t yOffset = (yy < 2) ? -1 : 0;
  2707. const uint32_t y0 = (_y + yOffset) % _height;
  2708. const uint32_t y1 = (y0 + 1) % _height;
  2709. for (int xx = 0; xx < 4; ++xx)
  2710. {
  2711. const uint32_t xOffset = (xx < 2) ? -1 : 0;
  2712. const uint32_t x0 = (_x + xOffset) % _width;
  2713. const uint32_t x1 = (x0 + 1) % _width;
  2714. const uint32_t bc0 = getColor(&_src[morton2d(x0, y0) * 8]);
  2715. const uint32_t bc1 = getColor(&_src[morton2d(x1, y0) * 8]);
  2716. const uint32_t bc2 = getColor(&_src[morton2d(x0, y1) * 8]);
  2717. const uint32_t bc3 = getColor(&_src[morton2d(x1, y1) * 8]);
  2718. const uint8_t f0 = factorTable[0];
  2719. const uint8_t f1 = factorTable[1];
  2720. const uint8_t f2 = factorTable[2];
  2721. const uint8_t f3 = factorTable[3];
  2722. uint32_t ar = 0, ag = 0, ab = 0;
  2723. decodeBlockPtc14RgbAddA(bc0, &ar, &ag, &ab, f0);
  2724. decodeBlockPtc14RgbAddA(bc1, &ar, &ag, &ab, f1);
  2725. decodeBlockPtc14RgbAddA(bc2, &ar, &ag, &ab, f2);
  2726. decodeBlockPtc14RgbAddA(bc3, &ar, &ag, &ab, f3);
  2727. uint32_t br = 0, bg = 0, bb = 0;
  2728. decodeBlockPtc14RgbAddB(bc0, &br, &bg, &bb, f0);
  2729. decodeBlockPtc14RgbAddB(bc1, &br, &bg, &bb, f1);
  2730. decodeBlockPtc14RgbAddB(bc2, &br, &bg, &bb, f2);
  2731. decodeBlockPtc14RgbAddB(bc3, &br, &bg, &bb, f3);
  2732. const uint8_t* weight = &weightTable[(mod & 3)*4];
  2733. const uint8_t wa = weight[0];
  2734. const uint8_t wb = weight[1];
  2735. _dst[(yy*4 + xx)*4+0] = uint8_t( (ab * wa + bb * wb) >> 7);
  2736. _dst[(yy*4 + xx)*4+1] = uint8_t( (ag * wa + bg * wb) >> 7);
  2737. _dst[(yy*4 + xx)*4+2] = uint8_t( (ar * wa + br * wb) >> 7);
  2738. _dst[(yy*4 + xx)*4+3] = 255;
  2739. mod >>= 2;
  2740. factorTable += 4;
  2741. }
  2742. }
  2743. }
  2744. static void decodeBlockPtc14ARgbaAddA(uint32_t _block, uint32_t* _r, uint32_t* _g, uint32_t* _b, uint32_t* _a, uint8_t _factor)
  2745. {
  2746. if (0 != (_block & (1<<15) ) )
  2747. {
  2748. *_r += bitRangeConvert( (_block >> 10) & 0x1f, 5, 8) * _factor;
  2749. *_g += bitRangeConvert( (_block >> 5) & 0x1f, 5, 8) * _factor;
  2750. *_b += bitRangeConvert( (_block >> 1) & 0x0f, 4, 8) * _factor;
  2751. *_a += 255 * _factor;
  2752. }
  2753. else
  2754. {
  2755. *_r += bitRangeConvert( (_block >> 8) & 0xf, 4, 8) * _factor;
  2756. *_g += bitRangeConvert( (_block >> 4) & 0xf, 4, 8) * _factor;
  2757. *_b += bitRangeConvert( (_block >> 1) & 0x7, 3, 8) * _factor;
  2758. *_a += bitRangeConvert( (_block >> 12) & 0x7, 3, 8) * _factor;
  2759. }
  2760. }
  2761. static void decodeBlockPtc14ARgbaAddB(uint32_t _block, uint32_t* _r, uint32_t* _g, uint32_t* _b, uint32_t* _a, uint8_t _factor)
  2762. {
  2763. if (0 != (_block & (1<<31) ) )
  2764. {
  2765. *_r += bitRangeConvert( (_block >> 26) & 0x1f, 5, 8) * _factor;
  2766. *_g += bitRangeConvert( (_block >> 21) & 0x1f, 5, 8) * _factor;
  2767. *_b += bitRangeConvert( (_block >> 16) & 0x1f, 5, 8) * _factor;
  2768. *_a += 255 * _factor;
  2769. }
  2770. else
  2771. {
  2772. *_r += bitRangeConvert( (_block >> 24) & 0xf, 4, 8) * _factor;
  2773. *_g += bitRangeConvert( (_block >> 20) & 0xf, 4, 8) * _factor;
  2774. *_b += bitRangeConvert( (_block >> 16) & 0xf, 4, 8) * _factor;
  2775. *_a += bitRangeConvert( (_block >> 28) & 0x7, 3, 8) * _factor;
  2776. }
  2777. }
  2778. static void decodeBlockPtc14A(uint8_t _dst[16*4], const uint8_t* _src, uint32_t _x, uint32_t _y, uint32_t _width, uint32_t _height)
  2779. {
  2780. // 0 1 2 3 4 5 6 7
  2781. // 7654321076543210765432107654321076543210765432107654321076543210
  2782. // mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmyrrrrrgggggbbbbbxrrrrrgggggbbbbp
  2783. // ^ ^^ ^^ ^
  2784. // +-- modulation data |+- B color |+- A color |
  2785. // +-- B opaque +-- A opaque |
  2786. // alpha punchthrough --+
  2787. const uint8_t* bc = &_src[morton2d(_x, _y) * 8];
  2788. uint32_t mod = 0
  2789. | bc[3]<<24
  2790. | bc[2]<<16
  2791. | bc[1]<<8
  2792. | bc[0]
  2793. ;
  2794. const bool punchthrough = !!(bc[7] & 1);
  2795. const uint8_t* weightTable = s_pvrtcWeights[4 * punchthrough];
  2796. const uint8_t* factorTable = s_pvrtcFactors[0];
  2797. for (int yy = 0; yy < 4; ++yy)
  2798. {
  2799. const uint32_t yOffset = (yy < 2) ? -1 : 0;
  2800. const uint32_t y0 = (_y + yOffset) % _height;
  2801. const uint32_t y1 = (y0 + 1) % _height;
  2802. for (int xx = 0; xx < 4; ++xx)
  2803. {
  2804. const uint32_t xOffset = (xx < 2) ? -1 : 0;
  2805. const uint32_t x0 = (_x + xOffset) % _width;
  2806. const uint32_t x1 = (x0 + 1) % _width;
  2807. const uint32_t bc0 = getColor(&_src[morton2d(x0, y0) * 8]);
  2808. const uint32_t bc1 = getColor(&_src[morton2d(x1, y0) * 8]);
  2809. const uint32_t bc2 = getColor(&_src[morton2d(x0, y1) * 8]);
  2810. const uint32_t bc3 = getColor(&_src[morton2d(x1, y1) * 8]);
  2811. const uint8_t f0 = factorTable[0];
  2812. const uint8_t f1 = factorTable[1];
  2813. const uint8_t f2 = factorTable[2];
  2814. const uint8_t f3 = factorTable[3];
  2815. uint32_t ar = 0, ag = 0, ab = 0, aa = 0;
  2816. decodeBlockPtc14ARgbaAddA(bc0, &ar, &ag, &ab, &aa, f0);
  2817. decodeBlockPtc14ARgbaAddA(bc1, &ar, &ag, &ab, &aa, f1);
  2818. decodeBlockPtc14ARgbaAddA(bc2, &ar, &ag, &ab, &aa, f2);
  2819. decodeBlockPtc14ARgbaAddA(bc3, &ar, &ag, &ab, &aa, f3);
  2820. uint32_t br = 0, bg = 0, bb = 0, ba = 0;
  2821. decodeBlockPtc14ARgbaAddB(bc0, &br, &bg, &bb, &ba, f0);
  2822. decodeBlockPtc14ARgbaAddB(bc1, &br, &bg, &bb, &ba, f1);
  2823. decodeBlockPtc14ARgbaAddB(bc2, &br, &bg, &bb, &ba, f2);
  2824. decodeBlockPtc14ARgbaAddB(bc3, &br, &bg, &bb, &ba, f3);
  2825. const uint8_t* weight = &weightTable[(mod & 3)*4];
  2826. const uint8_t wa = weight[0];
  2827. const uint8_t wb = weight[1];
  2828. const uint8_t wc = weight[2];
  2829. const uint8_t wd = weight[3];
  2830. _dst[(yy*4 + xx)*4+0] = uint8_t( (ab * wa + bb * wb) >> 7);
  2831. _dst[(yy*4 + xx)*4+1] = uint8_t( (ag * wa + bg * wb) >> 7);
  2832. _dst[(yy*4 + xx)*4+2] = uint8_t( (ar * wa + br * wb) >> 7);
  2833. _dst[(yy*4 + xx)*4+3] = uint8_t( (aa * wc + ba * wd) >> 7);
  2834. mod >>= 2;
  2835. factorTable += 4;
  2836. }
  2837. }
  2838. }
  2839. 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)
  2840. {
  2841. const ImageBlockInfo& blockInfo = getBlockInfo(_format);
  2842. const uint16_t blockWidth = blockInfo.blockWidth;
  2843. const uint16_t blockHeight = blockInfo.blockHeight;
  2844. const uint16_t minBlockX = blockInfo.minBlockX;
  2845. const uint16_t minBlockY = blockInfo.minBlockY;
  2846. _width = bx::max<uint16_t>(blockWidth * minBlockX, ( (_width + blockWidth - 1) / blockWidth)*blockWidth);
  2847. _height = bx::max<uint16_t>(blockHeight * minBlockY, ( (_height + blockHeight - 1) / blockHeight)*blockHeight);
  2848. _depth = bx::max<uint16_t>(1, _depth);
  2849. _numLayers = bx::max<uint16_t>(1, _numLayers);
  2850. const uint8_t numMips = _hasMips ? imageGetNumMips(_format, _width, _height, _depth) : 1;
  2851. uint32_t size = imageGetSize(NULL, _width, _height, _depth, _cubeMap, _hasMips, _numLayers, _format);
  2852. ImageContainer* imageContainer = (ImageContainer*)BX_ALIGNED_ALLOC(_allocator, size + bx::alignUp(sizeof(ImageContainer), 16), 16);
  2853. imageContainer->m_allocator = _allocator;
  2854. imageContainer->m_data = bx::alignPtr(imageContainer + 1, 0, 16);
  2855. imageContainer->m_format = _format;
  2856. imageContainer->m_orientation = Orientation::R0;
  2857. imageContainer->m_size = size;
  2858. imageContainer->m_offset = 0;
  2859. imageContainer->m_width = _width;
  2860. imageContainer->m_height = _height;
  2861. imageContainer->m_depth = _depth;
  2862. imageContainer->m_numLayers = _numLayers;
  2863. imageContainer->m_numMips = numMips;
  2864. imageContainer->m_hasAlpha = false;
  2865. imageContainer->m_cubeMap = _cubeMap;
  2866. imageContainer->m_ktx = false;
  2867. imageContainer->m_pvr3 = false;
  2868. imageContainer->m_ktxLE = false;
  2869. imageContainer->m_srgb = false;
  2870. if (NULL != _data)
  2871. {
  2872. bx::memCopy(imageContainer->m_data, _data, imageContainer->m_size);
  2873. }
  2874. return imageContainer;
  2875. }
  2876. void imageFree(ImageContainer* _imageContainer)
  2877. {
  2878. BX_ALIGNED_FREE(_imageContainer->m_allocator, _imageContainer, 16);
  2879. }
  2880. // DDS
  2881. #define DDS_MAGIC BX_MAKEFOURCC('D', 'D', 'S', ' ')
  2882. #define DDS_HEADER_SIZE 124
  2883. #define DDS_DXT1 BX_MAKEFOURCC('D', 'X', 'T', '1')
  2884. #define DDS_DXT2 BX_MAKEFOURCC('D', 'X', 'T', '2')
  2885. #define DDS_DXT3 BX_MAKEFOURCC('D', 'X', 'T', '3')
  2886. #define DDS_DXT4 BX_MAKEFOURCC('D', 'X', 'T', '4')
  2887. #define DDS_DXT5 BX_MAKEFOURCC('D', 'X', 'T', '5')
  2888. #define DDS_ATI1 BX_MAKEFOURCC('A', 'T', 'I', '1')
  2889. #define DDS_BC4U BX_MAKEFOURCC('B', 'C', '4', 'U')
  2890. #define DDS_ATI2 BX_MAKEFOURCC('A', 'T', 'I', '2')
  2891. #define DDS_BC5U BX_MAKEFOURCC('B', 'C', '5', 'U')
  2892. #define DDS_DX10 BX_MAKEFOURCC('D', 'X', '1', '0')
  2893. #define DDS_ETC1 BX_MAKEFOURCC('E', 'T', 'C', '1')
  2894. #define DDS_ETC2 BX_MAKEFOURCC('E', 'T', 'C', '2')
  2895. #define DDS_ET2A BX_MAKEFOURCC('E', 'T', '2', 'A')
  2896. #define DDS_PTC2 BX_MAKEFOURCC('P', 'T', 'C', '2')
  2897. #define DDS_PTC4 BX_MAKEFOURCC('P', 'T', 'C', '4')
  2898. #define DDS_ATC BX_MAKEFOURCC('A', 'T', 'C', ' ')
  2899. #define DDS_ATCE BX_MAKEFOURCC('A', 'T', 'C', 'E')
  2900. #define DDS_ATCI BX_MAKEFOURCC('A', 'T', 'C', 'I')
  2901. #define DDS_ASTC4x4 BX_MAKEFOURCC('A', 'S', '4', '4')
  2902. #define DDS_ASTC5x5 BX_MAKEFOURCC('A', 'S', '5', '5')
  2903. #define DDS_ASTC6x6 BX_MAKEFOURCC('A', 'S', '6', '6')
  2904. #define DDS_ASTC8x5 BX_MAKEFOURCC('A', 'S', '8', '5')
  2905. #define DDS_ASTC8x6 BX_MAKEFOURCC('A', 'S', '8', '6')
  2906. #define DDS_ASTC10x5 BX_MAKEFOURCC('A', 'S', ':', '5')
  2907. #define DDS_R8G8B8 20
  2908. #define DDS_A8R8G8B8 21
  2909. #define DDS_R5G6B5 23
  2910. #define DDS_A1R5G5B5 25
  2911. #define DDS_A4R4G4B4 26
  2912. #define DDS_A2B10G10R10 31
  2913. #define DDS_G16R16 34
  2914. #define DDS_A2R10G10B10 35
  2915. #define DDS_A16B16G16R16 36
  2916. #define DDS_A8L8 51
  2917. #define DDS_R16F 111
  2918. #define DDS_G16R16F 112
  2919. #define DDS_A16B16G16R16F 113
  2920. #define DDS_R32F 114
  2921. #define DDS_G32R32F 115
  2922. #define DDS_A32B32G32R32F 116
  2923. #define DDS_FORMAT_R32G32B32A32_FLOAT 2
  2924. #define DDS_FORMAT_R32G32B32A32_UINT 3
  2925. #define DDS_FORMAT_R16G16B16A16_FLOAT 10
  2926. #define DDS_FORMAT_R16G16B16A16_UNORM 11
  2927. #define DDS_FORMAT_R16G16B16A16_UINT 12
  2928. #define DDS_FORMAT_R32G32_FLOAT 16
  2929. #define DDS_FORMAT_R32G32_UINT 17
  2930. #define DDS_FORMAT_R10G10B10A2_UNORM 24
  2931. #define DDS_FORMAT_R11G11B10_FLOAT 26
  2932. #define DDS_FORMAT_R8G8B8A8_UNORM 28
  2933. #define DDS_FORMAT_R8G8B8A8_UNORM_SRGB 29
  2934. #define DDS_FORMAT_R16G16_FLOAT 34
  2935. #define DDS_FORMAT_R16G16_UNORM 35
  2936. #define DDS_FORMAT_R32_FLOAT 41
  2937. #define DDS_FORMAT_R32_UINT 42
  2938. #define DDS_FORMAT_R8G8_UNORM 49
  2939. #define DDS_FORMAT_R16_FLOAT 54
  2940. #define DDS_FORMAT_R16_UNORM 56
  2941. #define DDS_FORMAT_R8_UNORM 61
  2942. #define DDS_FORMAT_R1_UNORM 66
  2943. #define DDS_FORMAT_BC1_UNORM 71
  2944. #define DDS_FORMAT_BC1_UNORM_SRGB 72
  2945. #define DDS_FORMAT_BC2_UNORM 74
  2946. #define DDS_FORMAT_BC2_UNORM_SRGB 75
  2947. #define DDS_FORMAT_BC3_UNORM 77
  2948. #define DDS_FORMAT_BC3_UNORM_SRGB 78
  2949. #define DDS_FORMAT_BC4_UNORM 80
  2950. #define DDS_FORMAT_BC5_UNORM 83
  2951. #define DDS_FORMAT_B5G6R5_UNORM 85
  2952. #define DDS_FORMAT_B5G5R5A1_UNORM 86
  2953. #define DDS_FORMAT_B8G8R8A8_UNORM 87
  2954. #define DDS_FORMAT_B8G8R8A8_UNORM_SRGB 91
  2955. #define DDS_FORMAT_BC6H_SF16 96
  2956. #define DDS_FORMAT_BC7_UNORM 98
  2957. #define DDS_FORMAT_BC7_UNORM_SRGB 99
  2958. #define DDS_FORMAT_B4G4R4A4_UNORM 115
  2959. #define DDS_DX10_DIMENSION_TEXTURE2D 3
  2960. #define DDS_DX10_DIMENSION_TEXTURE3D 4
  2961. #define DDS_DX10_MISC_TEXTURECUBE 4
  2962. #define DDSD_CAPS 0x00000001
  2963. #define DDSD_HEIGHT 0x00000002
  2964. #define DDSD_WIDTH 0x00000004
  2965. #define DDSD_PITCH 0x00000008
  2966. #define DDSD_PIXELFORMAT 0x00001000
  2967. #define DDSD_MIPMAPCOUNT 0x00020000
  2968. #define DDSD_LINEARSIZE 0x00080000
  2969. #define DDSD_DEPTH 0x00800000
  2970. #define DDPF_ALPHAPIXELS 0x00000001
  2971. #define DDPF_ALPHA 0x00000002
  2972. #define DDPF_FOURCC 0x00000004
  2973. #define DDPF_INDEXED 0x00000020
  2974. #define DDPF_RGB 0x00000040
  2975. #define DDPF_YUV 0x00000200
  2976. #define DDPF_LUMINANCE 0x00020000
  2977. #define DDPF_BUMPDUDV 0x00080000
  2978. #define DDSCAPS_COMPLEX 0x00000008
  2979. #define DDSCAPS_TEXTURE 0x00001000
  2980. #define DDSCAPS_MIPMAP 0x00400000
  2981. #define DDSCAPS2_VOLUME 0x00200000
  2982. #define DDSCAPS2_CUBEMAP 0x00000200
  2983. #define DDSCAPS2_CUBEMAP_POSITIVEX 0x00000400
  2984. #define DDSCAPS2_CUBEMAP_NEGATIVEX 0x00000800
  2985. #define DDSCAPS2_CUBEMAP_POSITIVEY 0x00001000
  2986. #define DDSCAPS2_CUBEMAP_NEGATIVEY 0x00002000
  2987. #define DDSCAPS2_CUBEMAP_POSITIVEZ 0x00004000
  2988. #define DDSCAPS2_CUBEMAP_NEGATIVEZ 0x00008000
  2989. #define DSCAPS2_CUBEMAP_ALLSIDES (0 \
  2990. | DDSCAPS2_CUBEMAP_POSITIVEX \
  2991. | DDSCAPS2_CUBEMAP_NEGATIVEX \
  2992. | DDSCAPS2_CUBEMAP_POSITIVEY \
  2993. | DDSCAPS2_CUBEMAP_NEGATIVEY \
  2994. | DDSCAPS2_CUBEMAP_POSITIVEZ \
  2995. | DDSCAPS2_CUBEMAP_NEGATIVEZ \
  2996. )
  2997. struct TranslateDdsFormat
  2998. {
  2999. uint32_t m_format;
  3000. TextureFormat::Enum m_textureFormat;
  3001. bool m_srgb;
  3002. };
  3003. static const TranslateDdsFormat s_translateDdsFourccFormat[] =
  3004. {
  3005. { DDS_DXT1, TextureFormat::BC1, false },
  3006. { DDS_DXT2, TextureFormat::BC2, false },
  3007. { DDS_DXT3, TextureFormat::BC2, false },
  3008. { DDS_DXT4, TextureFormat::BC3, false },
  3009. { DDS_DXT5, TextureFormat::BC3, false },
  3010. { DDS_ATI1, TextureFormat::BC4, false },
  3011. { DDS_BC4U, TextureFormat::BC4, false },
  3012. { DDS_ATI2, TextureFormat::BC5, false },
  3013. { DDS_BC5U, TextureFormat::BC5, false },
  3014. { DDS_ETC1, TextureFormat::ETC1, false },
  3015. { DDS_ETC2, TextureFormat::ETC2, false },
  3016. { DDS_ET2A, TextureFormat::ETC2A, false },
  3017. { DDS_PTC2, TextureFormat::PTC12A, false },
  3018. { DDS_PTC4, TextureFormat::PTC14A, false },
  3019. { DDS_ATC , TextureFormat::ATC, false },
  3020. { DDS_ATCE, TextureFormat::ATCE, false },
  3021. { DDS_ATCI, TextureFormat::ATCI, false },
  3022. { DDS_ASTC4x4, TextureFormat::ASTC4x4, false },
  3023. { DDS_ASTC5x5, TextureFormat::ASTC5x5, false },
  3024. { DDS_ASTC6x6, TextureFormat::ASTC6x6, false },
  3025. { DDS_ASTC8x5, TextureFormat::ASTC8x5, false },
  3026. { DDS_ASTC8x6, TextureFormat::ASTC8x6, false },
  3027. { DDS_ASTC10x5, TextureFormat::ASTC10x5, false },
  3028. { DDS_A16B16G16R16, TextureFormat::RGBA16, false },
  3029. { DDS_A16B16G16R16F, TextureFormat::RGBA16F, false },
  3030. { DDPF_RGB|DDPF_ALPHAPIXELS, TextureFormat::BGRA8, false },
  3031. { DDPF_INDEXED, TextureFormat::R8, false },
  3032. { DDPF_LUMINANCE, TextureFormat::R8, false },
  3033. { DDPF_ALPHA, TextureFormat::R8, false },
  3034. { DDS_R16F, TextureFormat::R16F, false },
  3035. { DDS_R32F, TextureFormat::R32F, false },
  3036. { DDS_A8L8, TextureFormat::RG8, false },
  3037. { DDS_G16R16, TextureFormat::RG16, false },
  3038. { DDS_G16R16F, TextureFormat::RG16F, false },
  3039. { DDS_G32R32F, TextureFormat::RG32F, false },
  3040. { DDS_R8G8B8, TextureFormat::RGB8, false },
  3041. { DDS_A8R8G8B8, TextureFormat::BGRA8, false },
  3042. { DDS_A16B16G16R16, TextureFormat::RGBA16, false },
  3043. { DDS_A16B16G16R16F, TextureFormat::RGBA16F, false },
  3044. { DDS_A32B32G32R32F, TextureFormat::RGBA32F, false },
  3045. { DDS_R5G6B5, TextureFormat::R5G6B5, false },
  3046. { DDS_A4R4G4B4, TextureFormat::RGBA4, false },
  3047. { DDS_A1R5G5B5, TextureFormat::RGB5A1, false },
  3048. { DDS_A2B10G10R10, TextureFormat::RGB10A2, false },
  3049. };
  3050. static const TranslateDdsFormat s_translateDxgiFormat[] =
  3051. {
  3052. { DDS_FORMAT_BC1_UNORM, TextureFormat::BC1, false },
  3053. { DDS_FORMAT_BC1_UNORM_SRGB, TextureFormat::BC1, true },
  3054. { DDS_FORMAT_BC2_UNORM, TextureFormat::BC2, false },
  3055. { DDS_FORMAT_BC2_UNORM_SRGB, TextureFormat::BC2, true },
  3056. { DDS_FORMAT_BC3_UNORM, TextureFormat::BC3, false },
  3057. { DDS_FORMAT_BC3_UNORM_SRGB, TextureFormat::BC3, true },
  3058. { DDS_FORMAT_BC4_UNORM, TextureFormat::BC4, false },
  3059. { DDS_FORMAT_BC5_UNORM, TextureFormat::BC5, false },
  3060. { DDS_FORMAT_BC6H_SF16, TextureFormat::BC6H, false },
  3061. { DDS_FORMAT_BC7_UNORM, TextureFormat::BC7, false },
  3062. { DDS_FORMAT_BC7_UNORM_SRGB, TextureFormat::BC7, true },
  3063. { DDS_FORMAT_R1_UNORM, TextureFormat::R1, false },
  3064. { DDS_FORMAT_R8_UNORM, TextureFormat::R8, false },
  3065. { DDS_FORMAT_R16_UNORM, TextureFormat::R16, false },
  3066. { DDS_FORMAT_R16_FLOAT, TextureFormat::R16F, false },
  3067. { DDS_FORMAT_R32_UINT, TextureFormat::R32U, false },
  3068. { DDS_FORMAT_R32_FLOAT, TextureFormat::R32F, false },
  3069. { DDS_FORMAT_R8G8_UNORM, TextureFormat::RG8, false },
  3070. { DDS_FORMAT_R16G16_UNORM, TextureFormat::RG16, false },
  3071. { DDS_FORMAT_R16G16_FLOAT, TextureFormat::RG16F, false },
  3072. { DDS_FORMAT_R32G32_UINT, TextureFormat::RG32U, false },
  3073. { DDS_FORMAT_R32G32_FLOAT, TextureFormat::RG32F, false },
  3074. { DDS_FORMAT_B8G8R8A8_UNORM, TextureFormat::BGRA8, false },
  3075. { DDS_FORMAT_B8G8R8A8_UNORM_SRGB, TextureFormat::BGRA8, true },
  3076. { DDS_FORMAT_R8G8B8A8_UNORM, TextureFormat::RGBA8, false },
  3077. { DDS_FORMAT_R8G8B8A8_UNORM_SRGB, TextureFormat::RGBA8, true },
  3078. { DDS_FORMAT_R16G16B16A16_UNORM, TextureFormat::RGBA16, false },
  3079. { DDS_FORMAT_R16G16B16A16_FLOAT, TextureFormat::RGBA16F, false },
  3080. { DDS_FORMAT_R32G32B32A32_UINT, TextureFormat::RGBA32U, false },
  3081. { DDS_FORMAT_R32G32B32A32_FLOAT, TextureFormat::RGBA32F, false },
  3082. { DDS_FORMAT_B5G6R5_UNORM, TextureFormat::R5G6B5, false },
  3083. { DDS_FORMAT_B4G4R4A4_UNORM, TextureFormat::RGBA4, false },
  3084. { DDS_FORMAT_B5G5R5A1_UNORM, TextureFormat::RGB5A1, false },
  3085. { DDS_FORMAT_R10G10B10A2_UNORM, TextureFormat::RGB10A2, false },
  3086. { DDS_FORMAT_R11G11B10_FLOAT, TextureFormat::RG11B10F, false },
  3087. };
  3088. struct TranslateDdsPixelFormat
  3089. {
  3090. uint32_t m_bitCount;
  3091. uint32_t m_flags;
  3092. uint32_t m_bitmask[4];
  3093. TextureFormat::Enum m_textureFormat;
  3094. };
  3095. static const TranslateDdsPixelFormat s_translateDdsPixelFormat[] =
  3096. {
  3097. { 8, DDPF_LUMINANCE, { 0x000000ff, 0x00000000, 0x00000000, 0x00000000 }, TextureFormat::R8 },
  3098. { 16, DDPF_BUMPDUDV, { 0x000000ff, 0x0000ff00, 0x00000000, 0x00000000 }, TextureFormat::RG8S },
  3099. { 16, DDPF_RGB, { 0x0000ffff, 0x00000000, 0x00000000, 0x00000000 }, TextureFormat::R16U },
  3100. { 16, DDPF_RGB|DDPF_ALPHAPIXELS, { 0x00000f00, 0x000000f0, 0x0000000f, 0x0000f000 }, TextureFormat::RGBA4 },
  3101. { 16, DDPF_RGB, { 0x0000f800, 0x000007e0, 0x0000001f, 0x00000000 }, TextureFormat::R5G6B5 },
  3102. { 16, DDPF_RGB, { 0x00007c00, 0x000003e0, 0x0000001f, 0x00008000 }, TextureFormat::RGB5A1 },
  3103. { 24, DDPF_RGB, { 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000 }, TextureFormat::RGB8 },
  3104. { 24, DDPF_RGB, { 0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000 }, TextureFormat::RGB8 },
  3105. { 32, DDPF_RGB, { 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000 }, TextureFormat::BGRA8 },
  3106. { 32, DDPF_RGB|DDPF_ALPHAPIXELS, { 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000 }, TextureFormat::RGBA8 },
  3107. { 32, DDPF_BUMPDUDV, { 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000 }, TextureFormat::RGBA8S },
  3108. { 32, DDPF_RGB, { 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000 }, TextureFormat::BGRA8 },
  3109. { 32, DDPF_RGB|DDPF_ALPHAPIXELS, { 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000 }, TextureFormat::BGRA8 }, // D3DFMT_A8R8G8B8
  3110. { 32, DDPF_RGB|DDPF_ALPHAPIXELS, { 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000 }, TextureFormat::BGRA8 }, // D3DFMT_X8R8G8B8
  3111. { 32, DDPF_RGB|DDPF_ALPHAPIXELS, { 0x000003ff, 0x000ffc00, 0x3ff00000, 0xc0000000 }, TextureFormat::RGB10A2 },
  3112. { 32, DDPF_RGB, { 0x0000ffff, 0xffff0000, 0x00000000, 0x00000000 }, TextureFormat::RG16 },
  3113. { 32, DDPF_BUMPDUDV, { 0x0000ffff, 0xffff0000, 0x00000000, 0x00000000 }, TextureFormat::RG16S },
  3114. { 32, DDPF_RGB, { 0xffffffff, 0x00000000, 0x00000000, 0x00000000 }, TextureFormat::R32U },
  3115. };
  3116. bool imageParseDds(ImageContainer& _imageContainer, bx::ReaderSeekerI* _reader, bx::Error* _err)
  3117. {
  3118. BX_ERROR_SCOPE(_err);
  3119. int32_t total = 0;
  3120. uint32_t headerSize;
  3121. total += bx::read(_reader, headerSize, _err);
  3122. if (!_err->isOk()
  3123. || headerSize < DDS_HEADER_SIZE)
  3124. {
  3125. BX_ERROR_SET(_err, BIMG_ERROR, "DDS: Invalid header size.");
  3126. return false;
  3127. }
  3128. uint32_t flags;
  3129. total += bx::read(_reader, flags, _err);
  3130. if (!_err->isOk() )
  3131. {
  3132. return false;
  3133. }
  3134. if ( (flags & (DDSD_CAPS|DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT) ) != (DDSD_CAPS|DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT) )
  3135. {
  3136. BX_ERROR_SET(_err, BIMG_ERROR, "DDS: Invalid flags.");
  3137. return false;
  3138. }
  3139. uint32_t height;
  3140. total += bx::read(_reader, height, _err);
  3141. uint32_t width;
  3142. total += bx::read(_reader, width, _err);
  3143. uint32_t pitch;
  3144. total += bx::read(_reader, pitch, _err);
  3145. uint32_t depth;
  3146. total += bx::read(_reader, depth, _err);
  3147. uint32_t mips;
  3148. total += bx::read(_reader, mips, _err);
  3149. bx::skip(_reader, 44); // reserved
  3150. total += 44;
  3151. uint32_t pixelFormatSize;
  3152. total += bx::read(_reader, pixelFormatSize, _err);
  3153. uint32_t pixelFlags;
  3154. total += bx::read(_reader, pixelFlags, _err);
  3155. uint32_t fourcc;
  3156. total += bx::read(_reader, fourcc, _err);
  3157. uint32_t bitCount;
  3158. total += bx::read(_reader, bitCount, _err);
  3159. uint32_t bitmask[4];
  3160. total += bx::read(_reader, bitmask, sizeof(bitmask), _err);
  3161. uint32_t caps[4];
  3162. total += bx::read(_reader, caps, _err);
  3163. bx::skip(_reader, 4);
  3164. total += 4; // reserved
  3165. if (!_err->isOk() )
  3166. {
  3167. return false;
  3168. }
  3169. uint32_t dxgiFormat = 0;
  3170. uint32_t arraySize = 1;
  3171. if (DDPF_FOURCC == (pixelFlags & DDPF_FOURCC)
  3172. && DDS_DX10 == fourcc)
  3173. {
  3174. total += bx::read(_reader, dxgiFormat, _err);
  3175. uint32_t dims;
  3176. total += bx::read(_reader, dims, _err);
  3177. uint32_t miscFlags;
  3178. total += bx::read(_reader, miscFlags, _err);
  3179. total += bx::read(_reader, arraySize, _err);
  3180. uint32_t miscFlags2;
  3181. total += bx::read(_reader, miscFlags2, _err);
  3182. }
  3183. if (!_err->isOk() )
  3184. {
  3185. return false;
  3186. }
  3187. if ( (caps[0] & DDSCAPS_TEXTURE) == 0)
  3188. {
  3189. BX_ERROR_SET(_err, BIMG_ERROR, "DDS: Unsupported caps.");
  3190. return false;
  3191. }
  3192. bool cubeMap = 0 != (caps[1] & DDSCAPS2_CUBEMAP);
  3193. if (cubeMap)
  3194. {
  3195. if ( (caps[1] & DSCAPS2_CUBEMAP_ALLSIDES) != DSCAPS2_CUBEMAP_ALLSIDES)
  3196. {
  3197. // partial cube map is not supported.
  3198. BX_ERROR_SET(_err, BIMG_ERROR, "DDS: Incomplete cubemap.");
  3199. return false;
  3200. }
  3201. }
  3202. TextureFormat::Enum format = TextureFormat::Unknown;
  3203. bool hasAlpha = pixelFlags & DDPF_ALPHAPIXELS;
  3204. bool srgb = false;
  3205. if (dxgiFormat == 0)
  3206. {
  3207. if (DDPF_FOURCC == (pixelFlags & DDPF_FOURCC) )
  3208. {
  3209. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateDdsFourccFormat); ++ii)
  3210. {
  3211. if (s_translateDdsFourccFormat[ii].m_format == fourcc)
  3212. {
  3213. format = s_translateDdsFourccFormat[ii].m_textureFormat;
  3214. break;
  3215. }
  3216. }
  3217. }
  3218. else
  3219. {
  3220. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateDdsPixelFormat); ++ii)
  3221. {
  3222. const TranslateDdsPixelFormat& pf = s_translateDdsPixelFormat[ii];
  3223. if (pf.m_bitCount == bitCount
  3224. && pf.m_flags == pixelFlags
  3225. && pf.m_bitmask[0] == bitmask[0]
  3226. && pf.m_bitmask[1] == bitmask[1]
  3227. && pf.m_bitmask[2] == bitmask[2]
  3228. && pf.m_bitmask[3] == bitmask[3])
  3229. {
  3230. format = pf.m_textureFormat;
  3231. break;
  3232. }
  3233. }
  3234. }
  3235. }
  3236. else
  3237. {
  3238. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateDxgiFormat); ++ii)
  3239. {
  3240. if (s_translateDxgiFormat[ii].m_format == dxgiFormat)
  3241. {
  3242. format = s_translateDxgiFormat[ii].m_textureFormat;
  3243. srgb = s_translateDxgiFormat[ii].m_srgb;
  3244. break;
  3245. }
  3246. }
  3247. }
  3248. if (TextureFormat::Unknown == format)
  3249. {
  3250. BX_ERROR_SET(_err, BIMG_ERROR, "DDS: Unknown texture format.");
  3251. return false;
  3252. }
  3253. _imageContainer.m_allocator = NULL;
  3254. _imageContainer.m_data = NULL;
  3255. _imageContainer.m_size = 0;
  3256. _imageContainer.m_offset = (uint32_t)bx::seek(_reader);
  3257. _imageContainer.m_width = width;
  3258. _imageContainer.m_height = height;
  3259. _imageContainer.m_depth = depth;
  3260. _imageContainer.m_format = format;
  3261. _imageContainer.m_orientation = Orientation::R0;
  3262. _imageContainer.m_numLayers = uint16_t(arraySize);
  3263. _imageContainer.m_numMips = uint8_t( (caps[0] & DDSCAPS_MIPMAP) ? mips : 1);
  3264. _imageContainer.m_hasAlpha = hasAlpha;
  3265. _imageContainer.m_cubeMap = cubeMap;
  3266. _imageContainer.m_ktx = false;
  3267. _imageContainer.m_ktxLE = false;
  3268. _imageContainer.m_pvr3 = false;
  3269. _imageContainer.m_srgb = srgb;
  3270. return true;
  3271. }
  3272. ImageContainer* imageParseDds(bx::AllocatorI* _allocator, const void* _src, uint32_t _size, bx::Error* _err)
  3273. {
  3274. return imageParseT<DDS_MAGIC, imageParseDds>(_allocator, _src, _size, _err);
  3275. }
  3276. // KTX
  3277. #define KTX_MAGIC BX_MAKEFOURCC(0xAB, 'K', 'T', 'X')
  3278. #define KTX_HEADER_SIZE 64
  3279. #define KTX_ETC1_RGB8_OES 0x8D64
  3280. #define KTX_COMPRESSED_R11_EAC 0x9270
  3281. #define KTX_COMPRESSED_SIGNED_R11_EAC 0x9271
  3282. #define KTX_COMPRESSED_RG11_EAC 0x9272
  3283. #define KTX_COMPRESSED_SIGNED_RG11_EAC 0x9273
  3284. #define KTX_COMPRESSED_RGB8_ETC2 0x9274
  3285. #define KTX_COMPRESSED_SRGB8_ETC2 0x9275
  3286. #define KTX_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2 0x9276
  3287. #define KTX_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2 0x9277
  3288. #define KTX_COMPRESSED_RGBA8_ETC2_EAC 0x9278
  3289. #define KTX_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC 0x9279
  3290. #define KTX_COMPRESSED_RGB_PVRTC_4BPPV1_IMG 0x8C00
  3291. #define KTX_COMPRESSED_RGB_PVRTC_2BPPV1_IMG 0x8C01
  3292. #define KTX_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG 0x8C02
  3293. #define KTX_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG 0x8C03
  3294. #define KTX_COMPRESSED_RGBA_PVRTC_2BPPV2_IMG 0x9137
  3295. #define KTX_COMPRESSED_RGBA_PVRTC_4BPPV2_IMG 0x9138
  3296. #define KTX_COMPRESSED_RGB_S3TC_DXT1_EXT 0x83F0
  3297. #define KTX_COMPRESSED_RGBA_S3TC_DXT1_EXT 0x83F1
  3298. #define KTX_COMPRESSED_RGBA_S3TC_DXT3_EXT 0x83F2
  3299. #define KTX_COMPRESSED_RGBA_S3TC_DXT5_EXT 0x83F3
  3300. #define KTX_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT 0x8C4D
  3301. #define KTX_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT 0x8C4E
  3302. #define KTX_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT 0x8C4F
  3303. #define KTX_COMPRESSED_LUMINANCE_LATC1_EXT 0x8C70
  3304. #define KTX_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT 0x8C72
  3305. #define KTX_COMPRESSED_RGBA_BPTC_UNORM_ARB 0x8E8C
  3306. #define KTX_COMPRESSED_SRGB_ALPHA_BPTC_UNORM_ARB 0x8E8D
  3307. #define KTX_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB 0x8E8E
  3308. #define KTX_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT_ARB 0x8E8F
  3309. #define KTX_COMPRESSED_SRGB_PVRTC_2BPPV1_EXT 0x8A54
  3310. #define KTX_COMPRESSED_SRGB_PVRTC_4BPPV1_EXT 0x8A55
  3311. #define KTX_COMPRESSED_SRGB_ALPHA_PVRTC_2BPPV1_EXT 0x8A56
  3312. #define KTX_COMPRESSED_SRGB_ALPHA_PVRTC_4BPPV1_EXT 0x8A57
  3313. #define KTX_ATC_RGB_AMD 0x8C92
  3314. #define KTX_ATC_RGBA_EXPLICIT_ALPHA_AMD 0x8C93
  3315. #define KTX_ATC_RGBA_INTERPOLATED_ALPHA_AMD 0x87EE
  3316. #define KTX_COMPRESSED_RGBA_ASTC_4x4_KHR 0x93B0
  3317. #define KTX_COMPRESSED_RGBA_ASTC_5x5_KHR 0x93B2
  3318. #define KTX_COMPRESSED_RGBA_ASTC_6x6_KHR 0x93B4
  3319. #define KTX_COMPRESSED_RGBA_ASTC_8x5_KHR 0x93B5
  3320. #define KTX_COMPRESSED_RGBA_ASTC_8x6_KHR 0x93B6
  3321. #define KTX_COMPRESSED_RGBA_ASTC_10x5_KHR 0x93B8
  3322. #define KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR 0x93D0
  3323. #define KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR 0x93D2
  3324. #define KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR 0x93D4
  3325. #define KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR 0x93D5
  3326. #define KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR 0x93D6
  3327. #define KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR 0x93D8
  3328. #define KTX_A8 0x803C
  3329. #define KTX_R8 0x8229
  3330. #define KTX_R16 0x822A
  3331. #define KTX_RG8 0x822B
  3332. #define KTX_RG16 0x822C
  3333. #define KTX_R16F 0x822D
  3334. #define KTX_R32F 0x822E
  3335. #define KTX_RG16F 0x822F
  3336. #define KTX_RG32F 0x8230
  3337. #define KTX_RGBA8 0x8058
  3338. #define KTX_RGBA16 0x805B
  3339. #define KTX_RGBA16F 0x881A
  3340. #define KTX_R32UI 0x8236
  3341. #define KTX_RG32UI 0x823C
  3342. #define KTX_RGBA32UI 0x8D70
  3343. #define KTX_RGBA32F 0x8814
  3344. #define KTX_RGB565 0x8D62
  3345. #define KTX_RGBA4 0x8056
  3346. #define KTX_RGB5_A1 0x8057
  3347. #define KTX_RGB10_A2 0x8059
  3348. #define KTX_R8I 0x8231
  3349. #define KTX_R8UI 0x8232
  3350. #define KTX_R16I 0x8233
  3351. #define KTX_R16UI 0x8234
  3352. #define KTX_R32I 0x8235
  3353. #define KTX_R32UI 0x8236
  3354. #define KTX_RG8I 0x8237
  3355. #define KTX_RG8UI 0x8238
  3356. #define KTX_RG16I 0x8239
  3357. #define KTX_RG16UI 0x823A
  3358. #define KTX_RG32I 0x823B
  3359. #define KTX_RG32UI 0x823C
  3360. #define KTX_R8_SNORM 0x8F94
  3361. #define KTX_RG8_SNORM 0x8F95
  3362. #define KTX_RGB8_SNORM 0x8F96
  3363. #define KTX_RGBA8_SNORM 0x8F97
  3364. #define KTX_R16_SNORM 0x8F98
  3365. #define KTX_RG16_SNORM 0x8F99
  3366. #define KTX_RGB16_SNORM 0x8F9A
  3367. #define KTX_RGBA16_SNORM 0x8F9B
  3368. #define KTX_SRGB8 0x8C41
  3369. #define KTX_SRGB8_ALPHA8 0x8C43
  3370. #define KTX_RGBA32UI 0x8D70
  3371. #define KTX_RGB32UI 0x8D71
  3372. #define KTX_RGBA16UI 0x8D76
  3373. #define KTX_RGB16UI 0x8D77
  3374. #define KTX_RGBA8UI 0x8D7C
  3375. #define KTX_RGB8UI 0x8D7D
  3376. #define KTX_RGBA32I 0x8D82
  3377. #define KTX_RGB32I 0x8D83
  3378. #define KTX_RGBA16I 0x8D88
  3379. #define KTX_RGB16I 0x8D89
  3380. #define KTX_RGBA8I 0x8D8E
  3381. #define KTX_RGB8 0x8051
  3382. #define KTX_RGB8I 0x8D8F
  3383. #define KTX_RGB9_E5 0x8C3D
  3384. #define KTX_R11F_G11F_B10F 0x8C3A
  3385. #define KTX_ZERO 0
  3386. #define KTX_RED 0x1903
  3387. #define KTX_ALPHA 0x1906
  3388. #define KTX_RGB 0x1907
  3389. #define KTX_RGBA 0x1908
  3390. #define KTX_BGRA 0x80E1
  3391. #define KTX_RG 0x8227
  3392. #define KTX_BYTE 0x1400
  3393. #define KTX_UNSIGNED_BYTE 0x1401
  3394. #define KTX_SHORT 0x1402
  3395. #define KTX_UNSIGNED_SHORT 0x1403
  3396. #define KTX_INT 0x1404
  3397. #define KTX_UNSIGNED_INT 0x1405
  3398. #define KTX_FLOAT 0x1406
  3399. #define KTX_HALF_FLOAT 0x140B
  3400. #define KTX_UNSIGNED_INT_5_9_9_9_REV 0x8C3E
  3401. #define KTX_UNSIGNED_SHORT_5_6_5 0x8363
  3402. #define KTX_UNSIGNED_SHORT_4_4_4_4 0x8033
  3403. #define KTX_UNSIGNED_SHORT_5_5_5_1 0x8034
  3404. #define KTX_UNSIGNED_INT_2_10_10_10_REV 0x8368
  3405. #define KTX_UNSIGNED_INT_10F_11F_11F_REV 0x8C3B
  3406. struct KtxFormatInfo
  3407. {
  3408. uint32_t m_internalFmt;
  3409. uint32_t m_internalFmtSrgb;
  3410. uint32_t m_fmt;
  3411. uint32_t m_type;
  3412. };
  3413. static const KtxFormatInfo s_translateKtxFormat[] =
  3414. {
  3415. { KTX_COMPRESSED_RGBA_S3TC_DXT1_EXT, KTX_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT, KTX_COMPRESSED_RGBA_S3TC_DXT1_EXT, KTX_ZERO, }, // BC1
  3416. { KTX_COMPRESSED_RGBA_S3TC_DXT3_EXT, KTX_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT, KTX_COMPRESSED_RGBA_S3TC_DXT3_EXT, KTX_ZERO, }, // BC2
  3417. { KTX_COMPRESSED_RGBA_S3TC_DXT5_EXT, KTX_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT, KTX_COMPRESSED_RGBA_S3TC_DXT5_EXT, KTX_ZERO, }, // BC3
  3418. { KTX_COMPRESSED_LUMINANCE_LATC1_EXT, KTX_ZERO, KTX_COMPRESSED_LUMINANCE_LATC1_EXT, KTX_ZERO, }, // BC4
  3419. { KTX_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT, KTX_ZERO, KTX_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT, KTX_ZERO, }, // BC5
  3420. { KTX_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB, KTX_ZERO, KTX_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB, KTX_ZERO, }, // BC6H
  3421. { KTX_COMPRESSED_RGBA_BPTC_UNORM_ARB, KTX_ZERO, KTX_COMPRESSED_RGBA_BPTC_UNORM_ARB, KTX_ZERO, }, // BC7
  3422. { KTX_ETC1_RGB8_OES, KTX_ZERO, KTX_ETC1_RGB8_OES, KTX_ZERO, }, // ETC1
  3423. { KTX_COMPRESSED_RGB8_ETC2, KTX_ZERO, KTX_COMPRESSED_RGB8_ETC2, KTX_ZERO, }, // ETC2
  3424. { KTX_COMPRESSED_RGBA8_ETC2_EAC, KTX_COMPRESSED_SRGB8_ETC2, KTX_COMPRESSED_RGBA8_ETC2_EAC, KTX_ZERO, }, // ETC2A
  3425. { KTX_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2, KTX_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2, KTX_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2, KTX_ZERO, }, // ETC2A1
  3426. { KTX_COMPRESSED_RGB_PVRTC_2BPPV1_IMG, KTX_COMPRESSED_SRGB_PVRTC_2BPPV1_EXT, KTX_COMPRESSED_RGB_PVRTC_2BPPV1_IMG, KTX_ZERO, }, // PTC12
  3427. { KTX_COMPRESSED_RGB_PVRTC_4BPPV1_IMG, KTX_COMPRESSED_SRGB_PVRTC_4BPPV1_EXT, KTX_COMPRESSED_RGB_PVRTC_4BPPV1_IMG, KTX_ZERO, }, // PTC14
  3428. { KTX_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG, KTX_COMPRESSED_SRGB_ALPHA_PVRTC_2BPPV1_EXT, KTX_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG, KTX_ZERO, }, // PTC12A
  3429. { KTX_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG, KTX_COMPRESSED_SRGB_ALPHA_PVRTC_4BPPV1_EXT, KTX_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG, KTX_ZERO, }, // PTC14A
  3430. { KTX_COMPRESSED_RGBA_PVRTC_2BPPV2_IMG, KTX_ZERO, KTX_COMPRESSED_RGBA_PVRTC_2BPPV2_IMG, KTX_ZERO, }, // PTC22
  3431. { KTX_COMPRESSED_RGBA_PVRTC_4BPPV2_IMG, KTX_ZERO, KTX_COMPRESSED_RGBA_PVRTC_4BPPV2_IMG, KTX_ZERO, }, // PTC24
  3432. { KTX_ATC_RGB_AMD, KTX_ZERO, KTX_ATC_RGB_AMD, KTX_ZERO, }, // ATC
  3433. { KTX_ATC_RGBA_EXPLICIT_ALPHA_AMD, KTX_ZERO, KTX_ATC_RGBA_EXPLICIT_ALPHA_AMD, KTX_ZERO, }, // ATCE
  3434. { KTX_ATC_RGBA_INTERPOLATED_ALPHA_AMD, KTX_ZERO, KTX_ATC_RGBA_INTERPOLATED_ALPHA_AMD, KTX_ZERO, }, // ATCI
  3435. { KTX_COMPRESSED_RGBA_ASTC_4x4_KHR, KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR, KTX_COMPRESSED_RGBA_ASTC_4x4_KHR, KTX_ZERO, }, // ASTC4x4
  3436. { KTX_COMPRESSED_RGBA_ASTC_5x5_KHR, KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR, KTX_COMPRESSED_RGBA_ASTC_5x5_KHR, KTX_ZERO, }, // ASTC5x5
  3437. { KTX_COMPRESSED_RGBA_ASTC_6x6_KHR, KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR, KTX_COMPRESSED_RGBA_ASTC_6x6_KHR, KTX_ZERO, }, // ASTC6x6
  3438. { KTX_COMPRESSED_RGBA_ASTC_8x5_KHR, KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR, KTX_COMPRESSED_RGBA_ASTC_8x5_KHR, KTX_ZERO, }, // ASTC8x5
  3439. { KTX_COMPRESSED_RGBA_ASTC_8x6_KHR, KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR, KTX_COMPRESSED_RGBA_ASTC_8x6_KHR, KTX_ZERO, }, // ASTC8x6
  3440. { KTX_COMPRESSED_RGBA_ASTC_10x5_KHR, KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR, KTX_COMPRESSED_RGBA_ASTC_10x5_KHR, KTX_ZERO, }, // ASTC10x5
  3441. { KTX_ZERO, KTX_ZERO, KTX_ZERO, KTX_ZERO, }, // Unknown
  3442. { KTX_ZERO, KTX_ZERO, KTX_ZERO, KTX_ZERO, }, // R1
  3443. { KTX_ALPHA, KTX_ZERO, KTX_ALPHA, KTX_UNSIGNED_BYTE, }, // A8
  3444. { KTX_R8, KTX_ZERO, KTX_RED, KTX_UNSIGNED_BYTE, }, // R8
  3445. { KTX_R8I, KTX_ZERO, KTX_RED, KTX_BYTE, }, // R8S
  3446. { KTX_R8UI, KTX_ZERO, KTX_RED, KTX_UNSIGNED_BYTE, }, // R8S
  3447. { KTX_R8_SNORM, KTX_ZERO, KTX_RED, KTX_BYTE, }, // R8S
  3448. { KTX_R16, KTX_ZERO, KTX_RED, KTX_UNSIGNED_SHORT, }, // R16
  3449. { KTX_R16I, KTX_ZERO, KTX_RED, KTX_SHORT, }, // R16I
  3450. { KTX_R16UI, KTX_ZERO, KTX_RED, KTX_UNSIGNED_SHORT, }, // R16U
  3451. { KTX_R16F, KTX_ZERO, KTX_RED, KTX_HALF_FLOAT, }, // R16F
  3452. { KTX_R16_SNORM, KTX_ZERO, KTX_RED, KTX_SHORT, }, // R16S
  3453. { KTX_R32I, KTX_ZERO, KTX_RED, KTX_INT, }, // R32I
  3454. { KTX_R32UI, KTX_ZERO, KTX_RED, KTX_UNSIGNED_INT, }, // R32U
  3455. { KTX_R32F, KTX_ZERO, KTX_RED, KTX_FLOAT, }, // R32F
  3456. { KTX_RG8, KTX_ZERO, KTX_RG, KTX_UNSIGNED_BYTE, }, // RG8
  3457. { KTX_RG8I, KTX_ZERO, KTX_RG, KTX_BYTE, }, // RG8I
  3458. { KTX_RG8UI, KTX_ZERO, KTX_RG, KTX_UNSIGNED_BYTE, }, // RG8U
  3459. { KTX_RG8_SNORM, KTX_ZERO, KTX_RG, KTX_BYTE, }, // RG8S
  3460. { KTX_RG16, KTX_ZERO, KTX_RG, KTX_UNSIGNED_SHORT, }, // RG16
  3461. { KTX_RG16I, KTX_ZERO, KTX_RG, KTX_SHORT, }, // RG16
  3462. { KTX_RG16UI, KTX_ZERO, KTX_RG, KTX_UNSIGNED_SHORT, }, // RG16
  3463. { KTX_RG16F, KTX_ZERO, KTX_RG, KTX_FLOAT, }, // RG16F
  3464. { KTX_RG16_SNORM, KTX_ZERO, KTX_RG, KTX_SHORT, }, // RG16S
  3465. { KTX_RG32I, KTX_ZERO, KTX_RG, KTX_INT, }, // RG32I
  3466. { KTX_RG32UI, KTX_ZERO, KTX_RG, KTX_UNSIGNED_INT, }, // RG32U
  3467. { KTX_RG32F, KTX_ZERO, KTX_RG, KTX_FLOAT, }, // RG32F
  3468. { KTX_RGB8, KTX_SRGB8, KTX_RGB, KTX_UNSIGNED_BYTE, }, // RGB8
  3469. { KTX_RGB8I, KTX_ZERO, KTX_RGB, KTX_BYTE, }, // RGB8I
  3470. { KTX_RGB8UI, KTX_ZERO, KTX_RGB, KTX_UNSIGNED_BYTE, }, // RGB8U
  3471. { KTX_RGB8_SNORM, KTX_ZERO, KTX_RGB, KTX_BYTE, }, // RGB8S
  3472. { KTX_RGB9_E5, KTX_ZERO, KTX_RGB, KTX_UNSIGNED_INT_5_9_9_9_REV, }, // RGB9E5F
  3473. { KTX_BGRA, KTX_SRGB8_ALPHA8, KTX_BGRA, KTX_UNSIGNED_BYTE, }, // BGRA8
  3474. { KTX_RGBA8, KTX_SRGB8_ALPHA8, KTX_RGBA, KTX_UNSIGNED_BYTE, }, // RGBA8
  3475. { KTX_RGBA8I, KTX_ZERO, KTX_RGBA, KTX_BYTE, }, // RGBA8I
  3476. { KTX_RGBA8UI, KTX_ZERO, KTX_RGBA, KTX_UNSIGNED_BYTE, }, // RGBA8U
  3477. { KTX_RGBA8_SNORM, KTX_ZERO, KTX_RGBA, KTX_BYTE, }, // RGBA8S
  3478. { KTX_RGBA16, KTX_ZERO, KTX_RGBA, KTX_UNSIGNED_SHORT, }, // RGBA16
  3479. { KTX_RGBA16I, KTX_ZERO, KTX_RGBA, KTX_SHORT, }, // RGBA16I
  3480. { KTX_RGBA16UI, KTX_ZERO, KTX_RGBA, KTX_UNSIGNED_SHORT, }, // RGBA16U
  3481. { KTX_RGBA16F, KTX_ZERO, KTX_RGBA, KTX_HALF_FLOAT, }, // RGBA16F
  3482. { KTX_RGBA16_SNORM, KTX_ZERO, KTX_RGBA, KTX_SHORT, }, // RGBA16S
  3483. { KTX_RGBA32I, KTX_ZERO, KTX_RGBA, KTX_INT, }, // RGBA32I
  3484. { KTX_RGBA32UI, KTX_ZERO, KTX_RGBA, KTX_UNSIGNED_INT, }, // RGBA32U
  3485. { KTX_RGBA32F, KTX_ZERO, KTX_RGBA, KTX_FLOAT, }, // RGBA32F
  3486. { KTX_RGB565, KTX_ZERO, KTX_RGB, KTX_UNSIGNED_SHORT_5_6_5, }, // R5G6B5
  3487. { KTX_RGBA4, KTX_ZERO, KTX_RGBA, KTX_UNSIGNED_SHORT_4_4_4_4, }, // RGBA4
  3488. { KTX_RGB5_A1, KTX_ZERO, KTX_RGBA, KTX_UNSIGNED_SHORT_5_5_5_1, }, // RGB5A1
  3489. { KTX_RGB10_A2, KTX_ZERO, KTX_RGBA, KTX_UNSIGNED_INT_2_10_10_10_REV, }, // RGB10A2
  3490. { KTX_R11F_G11F_B10F, KTX_ZERO, KTX_RGB, KTX_UNSIGNED_INT_10F_11F_11F_REV, }, // RG11B10F
  3491. };
  3492. BX_STATIC_ASSERT(TextureFormat::UnknownDepth == BX_COUNTOF(s_translateKtxFormat) );
  3493. struct KtxFormatInfo2
  3494. {
  3495. uint32_t m_internalFmt;
  3496. TextureFormat::Enum m_format;
  3497. };
  3498. static const KtxFormatInfo2 s_translateKtxFormat2[] =
  3499. {
  3500. { KTX_A8, TextureFormat::A8 },
  3501. { KTX_RED, TextureFormat::R8 },
  3502. { KTX_RGB, TextureFormat::RGB8 },
  3503. { KTX_RGBA, TextureFormat::RGBA8 },
  3504. { KTX_COMPRESSED_RGB_S3TC_DXT1_EXT, TextureFormat::BC1 },
  3505. };
  3506. bool imageParseKtx(ImageContainer& _imageContainer, bx::ReaderSeekerI* _reader, bx::Error* _err)
  3507. {
  3508. BX_ERROR_SCOPE(_err);
  3509. uint8_t identifier[8];
  3510. bx::read(_reader, identifier, _err);
  3511. if (identifier[1] != '1'
  3512. && identifier[2] != '1')
  3513. {
  3514. BX_ERROR_SET(_err, BIMG_ERROR, "KTX: Unrecognized version.");
  3515. return false;
  3516. }
  3517. uint32_t endianness;
  3518. bx::read(_reader, endianness, _err);
  3519. bool fromLittleEndian = 0x04030201 == endianness;
  3520. uint32_t glType;
  3521. bx::readHE(_reader, glType, fromLittleEndian, _err);
  3522. uint32_t glTypeSize;
  3523. bx::readHE(_reader, glTypeSize, fromLittleEndian, _err);
  3524. uint32_t glFormat;
  3525. bx::readHE(_reader, glFormat, fromLittleEndian, _err);
  3526. uint32_t glInternalFormat;
  3527. bx::readHE(_reader, glInternalFormat, fromLittleEndian, _err);
  3528. uint32_t glBaseInternalFormat;
  3529. bx::readHE(_reader, glBaseInternalFormat, fromLittleEndian, _err);
  3530. uint32_t width;
  3531. bx::readHE(_reader, width, fromLittleEndian, _err);
  3532. uint32_t height;
  3533. bx::readHE(_reader, height, fromLittleEndian, _err);
  3534. uint32_t depth;
  3535. bx::readHE(_reader, depth, fromLittleEndian, _err);
  3536. uint32_t numberOfArrayElements;
  3537. bx::readHE(_reader, numberOfArrayElements, fromLittleEndian, _err);
  3538. uint32_t numFaces;
  3539. bx::readHE(_reader, numFaces, fromLittleEndian, _err);
  3540. uint32_t numMips;
  3541. bx::readHE(_reader, numMips, fromLittleEndian, _err);
  3542. uint32_t metaDataSize;
  3543. bx::readHE(_reader, metaDataSize, fromLittleEndian, _err);
  3544. if (!_err->isOk() )
  3545. {
  3546. return false;
  3547. }
  3548. // skip meta garbage...
  3549. int64_t offset = bx::skip(_reader, metaDataSize);
  3550. TextureFormat::Enum format = TextureFormat::Unknown;
  3551. bool hasAlpha = false;
  3552. bool srgb = false;
  3553. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateKtxFormat); ++ii)
  3554. {
  3555. if (s_translateKtxFormat[ii].m_internalFmt == glInternalFormat)
  3556. {
  3557. format = TextureFormat::Enum(ii);
  3558. break;
  3559. }
  3560. if (s_translateKtxFormat[ii].m_internalFmtSrgb == glInternalFormat
  3561. && s_translateKtxFormat[ii].m_fmt == glBaseInternalFormat)
  3562. {
  3563. format = TextureFormat::Enum(ii);
  3564. srgb = true;
  3565. break;
  3566. }
  3567. }
  3568. if (TextureFormat::Unknown == format)
  3569. {
  3570. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateKtxFormat2); ++ii)
  3571. {
  3572. if (s_translateKtxFormat2[ii].m_internalFmt == glInternalFormat)
  3573. {
  3574. format = s_translateKtxFormat2[ii].m_format;
  3575. break;
  3576. }
  3577. }
  3578. }
  3579. _imageContainer.m_allocator = NULL;
  3580. _imageContainer.m_data = NULL;
  3581. _imageContainer.m_size = 0;
  3582. _imageContainer.m_offset = (uint32_t)offset;
  3583. _imageContainer.m_width = width;
  3584. _imageContainer.m_height = height;
  3585. _imageContainer.m_depth = depth;
  3586. _imageContainer.m_format = format;
  3587. _imageContainer.m_orientation = Orientation::R0;
  3588. _imageContainer.m_numLayers = uint16_t(bx::max<uint32_t>(numberOfArrayElements, 1) );
  3589. _imageContainer.m_numMips = uint8_t(bx::max<uint32_t>(numMips, 1) );
  3590. _imageContainer.m_hasAlpha = hasAlpha;
  3591. _imageContainer.m_cubeMap = numFaces > 1;
  3592. _imageContainer.m_ktx = true;
  3593. _imageContainer.m_ktxLE = fromLittleEndian;
  3594. _imageContainer.m_pvr3 = false;
  3595. _imageContainer.m_srgb = srgb;
  3596. if (TextureFormat::Unknown == format)
  3597. {
  3598. BX_ERROR_SET(_err, BIMG_ERROR, "KTX: Unrecognized image format.");
  3599. return false;
  3600. }
  3601. return true;
  3602. }
  3603. ImageContainer* imageParseKtx(bx::AllocatorI* _allocator, const void* _src, uint32_t _size, bx::Error* _err)
  3604. {
  3605. return imageParseT<KTX_MAGIC, imageParseKtx>(_allocator, _src, _size, _err);
  3606. }
  3607. // PVR3
  3608. #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) )
  3609. #define PVR3_MAGIC BX_MAKEFOURCC('P', 'V', 'R', 3)
  3610. #define PVR3_HEADER_SIZE 52
  3611. #define PVR3_PVRTC1_2BPP_RGB 0
  3612. #define PVR3_PVRTC1_2BPP_RGBA 1
  3613. #define PVR3_PVRTC1_4BPP_RGB 2
  3614. #define PVR3_PVRTC1_4BPP_RGBA 3
  3615. #define PVR3_PVRTC2_2BPP_RGBA 4
  3616. #define PVR3_PVRTC2_4BPP_RGBA 5
  3617. #define PVR3_ETC1 6
  3618. #define PVR3_DXT1 7
  3619. #define PVR3_DXT2 8
  3620. #define PVR3_DXT3 9
  3621. #define PVR3_DXT4 10
  3622. #define PVR3_DXT5 11
  3623. #define PVR3_BC4 12
  3624. #define PVR3_BC5 13
  3625. #define PVR3_R8 PVR3_MAKE8CC('r', 0, 0, 0, 8, 0, 0, 0)
  3626. #define PVR3_R16 PVR3_MAKE8CC('r', 0, 0, 0, 16, 0, 0, 0)
  3627. #define PVR3_R32 PVR3_MAKE8CC('r', 0, 0, 0, 32, 0, 0, 0)
  3628. #define PVR3_RG8 PVR3_MAKE8CC('r', 'g', 0, 0, 8, 8, 0, 0)
  3629. #define PVR3_RG16 PVR3_MAKE8CC('r', 'g', 0, 0, 16, 16, 0, 0)
  3630. #define PVR3_RG32 PVR3_MAKE8CC('r', 'g', 0, 0, 32, 32, 0, 0)
  3631. #define PVR3_BGRA8 PVR3_MAKE8CC('b', 'g', 'r', 'a', 8, 8, 8, 8)
  3632. #define PVR3_RGBA16 PVR3_MAKE8CC('r', 'g', 'b', 'a', 16, 16, 16, 16)
  3633. #define PVR3_RGBA32 PVR3_MAKE8CC('r', 'g', 'b', 'a', 32, 32, 32, 32)
  3634. #define PVR3_RGB565 PVR3_MAKE8CC('r', 'g', 'b', 0, 5, 6, 5, 0)
  3635. #define PVR3_RGBA4 PVR3_MAKE8CC('r', 'g', 'b', 'a', 4, 4, 4, 4)
  3636. #define PVR3_RGBA51 PVR3_MAKE8CC('r', 'g', 'b', 'a', 5, 5, 5, 1)
  3637. #define PVR3_RGB10A2 PVR3_MAKE8CC('r', 'g', 'b', 'a', 10, 10, 10, 2)
  3638. #define PVR3_CHANNEL_TYPE_ANY UINT32_MAX
  3639. #define PVR3_CHANNEL_TYPE_FLOAT UINT32_C(12)
  3640. struct TranslatePvr3Format
  3641. {
  3642. uint64_t m_format;
  3643. uint32_t m_channelTypeMask;
  3644. TextureFormat::Enum m_textureFormat;
  3645. };
  3646. static const TranslatePvr3Format s_translatePvr3Format[] =
  3647. {
  3648. { PVR3_PVRTC1_2BPP_RGB, PVR3_CHANNEL_TYPE_ANY, TextureFormat::PTC12 },
  3649. { PVR3_PVRTC1_2BPP_RGBA, PVR3_CHANNEL_TYPE_ANY, TextureFormat::PTC12A },
  3650. { PVR3_PVRTC1_4BPP_RGB, PVR3_CHANNEL_TYPE_ANY, TextureFormat::PTC14 },
  3651. { PVR3_PVRTC1_4BPP_RGBA, PVR3_CHANNEL_TYPE_ANY, TextureFormat::PTC14A },
  3652. { PVR3_PVRTC2_2BPP_RGBA, PVR3_CHANNEL_TYPE_ANY, TextureFormat::PTC22 },
  3653. { PVR3_PVRTC2_4BPP_RGBA, PVR3_CHANNEL_TYPE_ANY, TextureFormat::PTC24 },
  3654. { PVR3_ETC1, PVR3_CHANNEL_TYPE_ANY, TextureFormat::ETC1 },
  3655. { PVR3_DXT1, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC1 },
  3656. { PVR3_DXT2, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC2 },
  3657. { PVR3_DXT3, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC2 },
  3658. { PVR3_DXT4, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC3 },
  3659. { PVR3_DXT5, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC3 },
  3660. { PVR3_BC4, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC4 },
  3661. { PVR3_BC5, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC5 },
  3662. { PVR3_R8, PVR3_CHANNEL_TYPE_ANY, TextureFormat::R8 },
  3663. { PVR3_R16, PVR3_CHANNEL_TYPE_ANY, TextureFormat::R16U },
  3664. { PVR3_R16, PVR3_CHANNEL_TYPE_FLOAT, TextureFormat::R16F },
  3665. { PVR3_R32, PVR3_CHANNEL_TYPE_ANY, TextureFormat::R32U },
  3666. { PVR3_R32, PVR3_CHANNEL_TYPE_FLOAT, TextureFormat::R32F },
  3667. { PVR3_RG8, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RG8 },
  3668. { PVR3_RG16, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RG16 },
  3669. { PVR3_RG16, PVR3_CHANNEL_TYPE_FLOAT, TextureFormat::RG16F },
  3670. { PVR3_RG32, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RG16 },
  3671. { PVR3_RG32, PVR3_CHANNEL_TYPE_FLOAT, TextureFormat::RG32F },
  3672. { PVR3_BGRA8, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BGRA8 },
  3673. { PVR3_RGBA16, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RGBA16 },
  3674. { PVR3_RGBA16, PVR3_CHANNEL_TYPE_FLOAT, TextureFormat::RGBA16F },
  3675. { PVR3_RGBA32, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RGBA32U },
  3676. { PVR3_RGBA32, PVR3_CHANNEL_TYPE_FLOAT, TextureFormat::RGBA32F },
  3677. { PVR3_RGB565, PVR3_CHANNEL_TYPE_ANY, TextureFormat::R5G6B5 },
  3678. { PVR3_RGBA4, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RGBA4 },
  3679. { PVR3_RGBA51, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RGB5A1 },
  3680. { PVR3_RGB10A2, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RGB10A2 },
  3681. };
  3682. bool imageParsePvr3(ImageContainer& _imageContainer, bx::ReaderSeekerI* _reader, bx::Error* _err)
  3683. {
  3684. BX_ERROR_SCOPE(_err);
  3685. uint32_t flags;
  3686. bx::read(_reader, flags, _err);
  3687. uint64_t pixelFormat;
  3688. bx::read(_reader, pixelFormat, _err);
  3689. uint32_t colorSpace;
  3690. bx::read(_reader, colorSpace, _err); // 0 - linearRGB, 1 - sRGB
  3691. uint32_t channelType;
  3692. bx::read(_reader, channelType, _err);
  3693. uint32_t height;
  3694. bx::read(_reader, height, _err);
  3695. uint32_t width;
  3696. bx::read(_reader, width, _err);
  3697. uint32_t depth;
  3698. bx::read(_reader, depth, _err);
  3699. uint32_t numSurfaces;
  3700. bx::read(_reader, numSurfaces, _err);
  3701. uint32_t numFaces;
  3702. bx::read(_reader, numFaces, _err);
  3703. uint32_t numMips;
  3704. bx::read(_reader, numMips, _err);
  3705. uint32_t metaDataSize;
  3706. bx::read(_reader, metaDataSize, _err);
  3707. if (!_err->isOk() )
  3708. {
  3709. return false;
  3710. }
  3711. // skip meta garbage...
  3712. int64_t offset = bx::skip(_reader, metaDataSize);
  3713. TextureFormat::Enum format = TextureFormat::Unknown;
  3714. bool hasAlpha = false;
  3715. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translatePvr3Format); ++ii)
  3716. {
  3717. if (s_translatePvr3Format[ii].m_format == pixelFormat
  3718. && channelType == (s_translatePvr3Format[ii].m_channelTypeMask & channelType) )
  3719. {
  3720. format = s_translatePvr3Format[ii].m_textureFormat;
  3721. break;
  3722. }
  3723. }
  3724. _imageContainer.m_allocator = NULL;
  3725. _imageContainer.m_data = NULL;
  3726. _imageContainer.m_size = 0;
  3727. _imageContainer.m_offset = (uint32_t)offset;
  3728. _imageContainer.m_width = width;
  3729. _imageContainer.m_height = height;
  3730. _imageContainer.m_depth = depth;
  3731. _imageContainer.m_format = format;
  3732. _imageContainer.m_orientation = Orientation::R0;
  3733. _imageContainer.m_numLayers = 1;
  3734. _imageContainer.m_numMips = uint8_t(bx::max<uint32_t>(numMips, 1) );
  3735. _imageContainer.m_hasAlpha = hasAlpha;
  3736. _imageContainer.m_cubeMap = numFaces > 1;
  3737. _imageContainer.m_ktx = false;
  3738. _imageContainer.m_ktxLE = false;
  3739. _imageContainer.m_pvr3 = true;
  3740. _imageContainer.m_srgb = colorSpace > 0;
  3741. return TextureFormat::Unknown != format;
  3742. }
  3743. ImageContainer* imageParsePvr3(bx::AllocatorI* _allocator, const void* _src, uint32_t _size, bx::Error* _err)
  3744. {
  3745. return imageParseT<PVR3_MAGIC, imageParsePvr3>(_allocator, _src, _size, _err);
  3746. }
  3747. bool imageParse(ImageContainer& _imageContainer, bx::ReaderSeekerI* _reader, bx::Error* _err)
  3748. {
  3749. BX_ERROR_SCOPE(_err);
  3750. uint32_t magic;
  3751. bx::read(_reader, magic, _err);
  3752. if (DDS_MAGIC == magic)
  3753. {
  3754. return imageParseDds(_imageContainer, _reader, _err);
  3755. }
  3756. else if (KTX_MAGIC == magic)
  3757. {
  3758. return imageParseKtx(_imageContainer, _reader, _err);
  3759. }
  3760. else if (PVR3_MAGIC == magic)
  3761. {
  3762. return imageParsePvr3(_imageContainer, _reader, _err);
  3763. }
  3764. else if (BIMG_CHUNK_MAGIC_GNF == magic)
  3765. {
  3766. return imageParseGnf(_imageContainer, _reader, _err);
  3767. }
  3768. else if (BIMG_CHUNK_MAGIC_TEX == magic)
  3769. {
  3770. TextureCreate tc;
  3771. bx::read(_reader, tc, _err);
  3772. _imageContainer.m_format = tc.m_format;
  3773. _imageContainer.m_orientation = Orientation::R0;
  3774. _imageContainer.m_offset = UINT32_MAX;
  3775. _imageContainer.m_allocator = NULL;
  3776. if (NULL == tc.m_mem)
  3777. {
  3778. _imageContainer.m_data = NULL;
  3779. _imageContainer.m_size = 0;
  3780. }
  3781. else
  3782. {
  3783. _imageContainer.m_data = tc.m_mem->data;
  3784. _imageContainer.m_size = tc.m_mem->size;
  3785. }
  3786. _imageContainer.m_width = tc.m_width;
  3787. _imageContainer.m_height = tc.m_height;
  3788. _imageContainer.m_depth = tc.m_depth;
  3789. _imageContainer.m_numLayers = tc.m_numLayers;
  3790. _imageContainer.m_numMips = tc.m_numMips;
  3791. _imageContainer.m_hasAlpha = false;
  3792. _imageContainer.m_cubeMap = tc.m_cubeMap;
  3793. _imageContainer.m_ktx = false;
  3794. _imageContainer.m_ktxLE = false;
  3795. _imageContainer.m_pvr3 = false;
  3796. _imageContainer.m_srgb = false;
  3797. return _err->isOk();
  3798. }
  3799. BX_TRACE("Unrecognized image format (magic: 0x%08x)!", magic);
  3800. BX_ERROR_SET(_err, BIMG_ERROR, "Unrecognized image format.");
  3801. return false;
  3802. }
  3803. bool imageParse(ImageContainer& _imageContainer, const void* _data, uint32_t _size, bx::Error* _err)
  3804. {
  3805. BX_ERROR_SCOPE(_err);
  3806. bx::MemoryReader reader(_data, _size);
  3807. return imageParse(_imageContainer, &reader, _err);
  3808. }
  3809. void imageDecodeToR8(bx::AllocatorI* _allocator, void* _dst, const void* _src, uint32_t _width, uint32_t _height, uint32_t _depth, uint32_t _dstPitch, TextureFormat::Enum _srcFormat)
  3810. {
  3811. const uint8_t* src = (const uint8_t*)_src;
  3812. uint8_t* dst = (uint8_t*)_dst;
  3813. const uint32_t srcBpp = s_imageBlockInfo[_srcFormat].bitsPerPixel;
  3814. const uint32_t srcPitch = _width*srcBpp/8;
  3815. for (uint32_t zz = 0; zz < _depth; ++zz, src += _height*srcPitch, dst += _height*_dstPitch)
  3816. {
  3817. if (isCompressed(_srcFormat))
  3818. {
  3819. uint32_t size = imageGetSize(NULL, uint16_t(_width), uint16_t(_height), 0, false, false, 1, TextureFormat::RGBA8);
  3820. void* temp = BX_ALLOC(_allocator, size);
  3821. imageDecodeToRgba8(_allocator, temp, _src, _width, _height, _width*4, _srcFormat);
  3822. imageConvert(_allocator, dst, TextureFormat::R8, temp, TextureFormat::RGBA8, _width, _height, 1, _width*4, _dstPitch);
  3823. BX_FREE(_allocator, temp);
  3824. }
  3825. else
  3826. {
  3827. imageConvert(_allocator, dst, TextureFormat::R8, src, _srcFormat, _width, _height, 1, srcPitch, _dstPitch);
  3828. }
  3829. }
  3830. }
  3831. void imageDecodeToBgra8(bx::AllocatorI* _allocator, void* _dst, const void* _src, uint32_t _width, uint32_t _height, uint32_t _dstPitch, TextureFormat::Enum _srcFormat)
  3832. {
  3833. const uint8_t* src = (const uint8_t*)_src;
  3834. uint8_t* dst = (uint8_t*)_dst;
  3835. uint32_t width = _width/4;
  3836. uint32_t height = _height/4;
  3837. uint8_t temp[16*4];
  3838. switch (_srcFormat)
  3839. {
  3840. case TextureFormat::BC1:
  3841. if (BX_ENABLED(BIMG_DECODE_BC1) )
  3842. {
  3843. for (uint32_t yy = 0; yy < height; ++yy)
  3844. {
  3845. for (uint32_t xx = 0; xx < width; ++xx)
  3846. {
  3847. decodeBlockDxt1(temp, src);
  3848. src += 8;
  3849. uint8_t* block = &dst[yy*_dstPitch*4 + xx*16];
  3850. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  3851. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  3852. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  3853. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  3854. }
  3855. }
  3856. }
  3857. else
  3858. {
  3859. BX_WARN(false, "BC1 decoder is disabled (BIMG_DECODE_BC1).");
  3860. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00) );
  3861. }
  3862. break;
  3863. case TextureFormat::BC2:
  3864. if (BX_ENABLED(BIMG_DECODE_BC2) )
  3865. {
  3866. for (uint32_t yy = 0; yy < height; ++yy)
  3867. {
  3868. for (uint32_t xx = 0; xx < width; ++xx)
  3869. {
  3870. decodeBlockDxt23A(temp+3, src);
  3871. src += 8;
  3872. decodeBlockDxt(temp, src);
  3873. src += 8;
  3874. uint8_t* block = &dst[yy*_dstPitch*4 + xx*16];
  3875. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  3876. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  3877. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  3878. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  3879. }
  3880. }
  3881. }
  3882. else
  3883. {
  3884. BX_WARN(false, "BC2 decoder is disabled (BIMG_DECODE_BC2).");
  3885. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00) );
  3886. }
  3887. break;
  3888. case TextureFormat::BC3:
  3889. if (BX_ENABLED(BIMG_DECODE_BC3) )
  3890. {
  3891. for (uint32_t yy = 0; yy < height; ++yy)
  3892. {
  3893. for (uint32_t xx = 0; xx < width; ++xx)
  3894. {
  3895. decodeBlockDxt45A(temp+3, src);
  3896. src += 8;
  3897. decodeBlockDxt(temp, src);
  3898. src += 8;
  3899. uint8_t* block = &dst[yy*_dstPitch*4 + xx*16];
  3900. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  3901. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  3902. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  3903. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  3904. }
  3905. }
  3906. }
  3907. else
  3908. {
  3909. BX_WARN(false, "BC3 decoder is disabled (BIMG_DECODE_BC3).");
  3910. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00) );
  3911. }
  3912. break;
  3913. case TextureFormat::BC4:
  3914. if (BX_ENABLED(BIMG_DECODE_BC4) )
  3915. {
  3916. for (uint32_t yy = 0; yy < height; ++yy)
  3917. {
  3918. for (uint32_t xx = 0; xx < width; ++xx)
  3919. {
  3920. decodeBlockDxt45A(temp, src);
  3921. src += 8;
  3922. uint8_t* block = &dst[yy*_dstPitch*4 + xx*16];
  3923. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  3924. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  3925. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  3926. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  3927. }
  3928. }
  3929. }
  3930. else
  3931. {
  3932. BX_WARN(false, "BC4 decoder is disabled (BIMG_DECODE_BC4).");
  3933. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00) );
  3934. }
  3935. break;
  3936. case TextureFormat::BC5:
  3937. if (BX_ENABLED(BIMG_DECODE_BC5) )
  3938. {
  3939. for (uint32_t yy = 0; yy < height; ++yy)
  3940. {
  3941. for (uint32_t xx = 0; xx < width; ++xx)
  3942. {
  3943. decodeBlockDxt45A(temp+2, src);
  3944. src += 8;
  3945. decodeBlockDxt45A(temp+1, src);
  3946. src += 8;
  3947. for (uint32_t ii = 0; ii < 16; ++ii)
  3948. {
  3949. float nx = temp[ii*4+2]*2.0f/255.0f - 1.0f;
  3950. float ny = temp[ii*4+1]*2.0f/255.0f - 1.0f;
  3951. float nz = bx::sqrt(1.0f - nx*nx - ny*ny);
  3952. temp[ii*4+0] = uint8_t( (nz + 1.0f)*255.0f/2.0f);
  3953. temp[ii*4+3] = 0;
  3954. }
  3955. uint8_t* block = &dst[yy*_dstPitch*4 + xx*16];
  3956. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  3957. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  3958. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  3959. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  3960. }
  3961. }
  3962. }
  3963. else
  3964. {
  3965. BX_WARN(false, "BC5 decoder is disabled (BIMG_DECODE_BC5).");
  3966. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00) );
  3967. }
  3968. break;
  3969. case TextureFormat::BC6H:
  3970. if (BX_ENABLED(BIMG_DECODE_BC6) )
  3971. {
  3972. ImageContainer* rgba32f = imageAlloc(_allocator
  3973. , TextureFormat::RGBA32F
  3974. , uint16_t(_width)
  3975. , uint16_t(_height)
  3976. , uint16_t(1)
  3977. , 1
  3978. , false
  3979. , false
  3980. );
  3981. imageDecodeToRgba32f(_allocator, rgba32f->m_data, _src, _width, _height, 1, _width*16, _srcFormat);
  3982. imageConvert(_allocator, _dst, TextureFormat::BGRA8, rgba32f->m_data, TextureFormat::RGBA32F, _width, _height, 1, _width*16, _dstPitch);
  3983. imageFree(rgba32f);
  3984. }
  3985. else
  3986. {
  3987. BX_WARN(false, "BC6 decoder is disabled (BIMG_DECODE_BC6).");
  3988. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00) );
  3989. }
  3990. break;
  3991. case TextureFormat::BC7:
  3992. if (BX_ENABLED(BIMG_DECODE_BC7) )
  3993. {
  3994. for (uint32_t yy = 0; yy < height; ++yy)
  3995. {
  3996. for (uint32_t xx = 0; xx < width; ++xx)
  3997. {
  3998. decodeBlockBc7(temp, src);
  3999. src += 16;
  4000. uint8_t* block = &dst[yy*_dstPitch*4 + xx*16];
  4001. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  4002. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  4003. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  4004. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  4005. }
  4006. }
  4007. }
  4008. else
  4009. {
  4010. BX_WARN(false, "BC7 decoder is disabled (BIMG_DECODE_BC7).");
  4011. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00) );
  4012. }
  4013. break;
  4014. case TextureFormat::ETC1:
  4015. case TextureFormat::ETC2:
  4016. if (BX_ENABLED(BIMG_DECODE_ETC1 || BIMG_DECODE_ETC2) )
  4017. {
  4018. for (uint32_t yy = 0; yy < height; ++yy)
  4019. {
  4020. for (uint32_t xx = 0; xx < width; ++xx)
  4021. {
  4022. decodeBlockEtc12(temp, src);
  4023. src += 8;
  4024. uint8_t* block = &dst[yy*_dstPitch*4 + xx*16];
  4025. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  4026. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  4027. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  4028. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  4029. }
  4030. }
  4031. }
  4032. else
  4033. {
  4034. BX_WARN(false, "ETC1/ETC2 decoder is disabled (BIMG_DECODE_ETC1/ETC2).");
  4035. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00) );
  4036. }
  4037. break;
  4038. case TextureFormat::ETC2A:
  4039. if (BX_ENABLED(BIMG_DECODE_ETC2))
  4040. {
  4041. for (uint32_t yy = 0; yy < height; ++yy)
  4042. {
  4043. for (uint32_t xx = 0; xx < width; ++xx)
  4044. {
  4045. decodeBlockEtc12(temp, src + 8);
  4046. decodeBlockEtc2Alpha(temp, src);
  4047. src += 16;
  4048. uint8_t* block = &dst[yy*_dstPitch * 4 + xx * 16];
  4049. bx::memCopy(&block[0 * _dstPitch], &temp[0], 16);
  4050. bx::memCopy(&block[1 * _dstPitch], &temp[16], 16);
  4051. bx::memCopy(&block[2 * _dstPitch], &temp[32], 16);
  4052. bx::memCopy(&block[3 * _dstPitch], &temp[48], 16);
  4053. }
  4054. }
  4055. }
  4056. else
  4057. {
  4058. BX_WARN(false, "ETC2 decoder is disabled (BIMG_DECODE_ETC2).");
  4059. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00));
  4060. }
  4061. break;
  4062. case TextureFormat::ETC2A1:
  4063. BX_WARN(false, "ETC2A1 decoder is not implemented.");
  4064. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xffff0000) );
  4065. break;
  4066. case TextureFormat::PTC12:
  4067. BX_WARN(false, "PTC12 decoder is not implemented.");
  4068. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xffff00ff) );
  4069. break;
  4070. case TextureFormat::PTC12A:
  4071. BX_WARN(false, "PTC12A decoder is not implemented.");
  4072. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xffffff00) );
  4073. break;
  4074. case TextureFormat::PTC14:
  4075. for (uint32_t yy = 0; yy < height; ++yy)
  4076. {
  4077. for (uint32_t xx = 0; xx < width; ++xx)
  4078. {
  4079. decodeBlockPtc14(temp, src, xx, yy, width, height);
  4080. uint8_t* block = &dst[yy*_dstPitch*4 + xx*16];
  4081. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  4082. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  4083. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  4084. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  4085. }
  4086. }
  4087. break;
  4088. case TextureFormat::PTC14A:
  4089. for (uint32_t yy = 0; yy < height; ++yy)
  4090. {
  4091. for (uint32_t xx = 0; xx < width; ++xx)
  4092. {
  4093. decodeBlockPtc14A(temp, src, xx, yy, width, height);
  4094. uint8_t* block = &dst[yy*_dstPitch*4 + xx*16];
  4095. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  4096. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  4097. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  4098. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  4099. }
  4100. }
  4101. break;
  4102. case TextureFormat::PTC22:
  4103. BX_WARN(false, "PTC22 decoder is not implemented.");
  4104. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff00ff00), UINT32_C(0xff0000ff) );
  4105. break;
  4106. case TextureFormat::PTC24:
  4107. BX_WARN(false, "PTC24 decoder is not implemented.");
  4108. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xffffffff) );
  4109. break;
  4110. case TextureFormat::ATC:
  4111. for (uint32_t yy = 0; yy < height; ++yy)
  4112. {
  4113. for (uint32_t xx = 0; xx < width; ++xx)
  4114. {
  4115. decodeBlockATC(temp, src);
  4116. src += 8;
  4117. uint8_t* block = &dst[(yy*_dstPitch+xx*4)*4];
  4118. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  4119. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  4120. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  4121. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  4122. }
  4123. }
  4124. break;
  4125. case TextureFormat::ATCE:
  4126. for (uint32_t yy = 0; yy < height; ++yy)
  4127. {
  4128. for (uint32_t xx = 0; xx < width; ++xx)
  4129. {
  4130. decodeBlockDxt23A(temp+3, src);
  4131. src += 8;
  4132. decodeBlockATC(temp, src);
  4133. src += 8;
  4134. uint8_t* block = &dst[(yy*_dstPitch+xx*4)*4];
  4135. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  4136. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  4137. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  4138. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  4139. }
  4140. }
  4141. break;
  4142. case TextureFormat::ATCI:
  4143. for (uint32_t yy = 0; yy < height; ++yy)
  4144. {
  4145. for (uint32_t xx = 0; xx < width; ++xx)
  4146. {
  4147. decodeBlockDxt45A(temp+3, src);
  4148. src += 8;
  4149. decodeBlockATC(temp, src);
  4150. src += 8;
  4151. uint8_t* block = &dst[(yy*_dstPitch+xx*4)*4];
  4152. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  4153. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  4154. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  4155. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  4156. }
  4157. }
  4158. break;
  4159. case TextureFormat::ASTC4x4:
  4160. case TextureFormat::ASTC5x5:
  4161. case TextureFormat::ASTC6x6:
  4162. case TextureFormat::ASTC8x5:
  4163. case TextureFormat::ASTC8x6:
  4164. case TextureFormat::ASTC10x5:
  4165. imageDecodeToRgba8(_allocator, _dst, _src, _width, _height, _dstPitch, _srcFormat);
  4166. imageSwizzleBgra8(_dst, _dstPitch, _width, _height, _dst, _dstPitch);
  4167. break;
  4168. case TextureFormat::RGBA8:
  4169. {
  4170. const uint32_t srcPitch = _width * 4;
  4171. imageSwizzleBgra8(_dst, _dstPitch, _width, _height, _src, srcPitch);
  4172. }
  4173. break;
  4174. case TextureFormat::BGRA8:
  4175. {
  4176. const uint32_t srcPitch = _width * 4;
  4177. const uint32_t size = bx::uint32_min(srcPitch, _dstPitch);
  4178. bx::memCopy(_dst, _dstPitch, _src, srcPitch, size, _height);
  4179. }
  4180. break;
  4181. default:
  4182. {
  4183. const uint32_t srcBpp = s_imageBlockInfo[_srcFormat].bitsPerPixel;
  4184. const uint32_t srcPitch = _width * srcBpp / 8;
  4185. if (!imageConvert(_allocator, _dst, TextureFormat::BGRA8, _src, _srcFormat, _width, _height, 1, srcPitch, _dstPitch) )
  4186. {
  4187. // Failed to convert, just make ugly red-yellow checkerboard texture.
  4188. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xffff0000), UINT32_C(0xffffff00) );
  4189. }
  4190. }
  4191. break;
  4192. }
  4193. }
  4194. void imageDecodeToRgba8(bx::AllocatorI* _allocator, void* _dst, const void* _src, uint32_t _width, uint32_t _height, uint32_t _dstPitch, TextureFormat::Enum _srcFormat)
  4195. {
  4196. switch (_srcFormat)
  4197. {
  4198. case TextureFormat::RGBA8:
  4199. {
  4200. const uint32_t srcPitch = _width * 4;
  4201. const uint32_t size = bx::uint32_min(srcPitch, _dstPitch);
  4202. bx::memCopy(_dst, _dstPitch, _src, srcPitch, size, _height);
  4203. }
  4204. break;
  4205. case TextureFormat::BGRA8:
  4206. {
  4207. const uint32_t srcPitch = _width * 4;
  4208. imageSwizzleBgra8(_dst, _dstPitch, _width, _height, _src, srcPitch);
  4209. }
  4210. break;
  4211. case TextureFormat::ASTC4x4:
  4212. case TextureFormat::ASTC5x5:
  4213. case TextureFormat::ASTC6x6:
  4214. case TextureFormat::ASTC8x5:
  4215. case TextureFormat::ASTC8x6:
  4216. case TextureFormat::ASTC10x5:
  4217. if (BX_ENABLED(BIMG_DECODE_ASTC) )
  4218. {
  4219. if (!astc_codec::ASTCDecompressToRGBA(
  4220. (const uint8_t*)_src
  4221. , imageGetSize(NULL, uint16_t(_width), uint16_t(_height), 0, false, false, 1, _srcFormat)
  4222. , _width
  4223. , _height
  4224. , TextureFormat::ASTC4x4 == _srcFormat ? astc_codec::FootprintType::k4x4
  4225. : TextureFormat::ASTC5x5 == _srcFormat ? astc_codec::FootprintType::k5x5
  4226. : TextureFormat::ASTC6x6 == _srcFormat ? astc_codec::FootprintType::k6x6
  4227. : TextureFormat::ASTC8x5 == _srcFormat ? astc_codec::FootprintType::k8x5
  4228. : TextureFormat::ASTC8x6 == _srcFormat ? astc_codec::FootprintType::k8x6
  4229. : astc_codec::FootprintType::k10x5
  4230. , (uint8_t*)_dst
  4231. , _width*_height*4
  4232. , _dstPitch
  4233. ) )
  4234. {
  4235. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xffffff00) );
  4236. }
  4237. }
  4238. else
  4239. {
  4240. BX_WARN(false, "ASTC decoder is disabled (BIMG_DECODE_ASTC).");
  4241. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00) );
  4242. }
  4243. break;
  4244. default:
  4245. {
  4246. const uint32_t srcPitch = _width * 4;
  4247. imageDecodeToBgra8(_allocator, _dst, _src, _width, _height, _dstPitch, _srcFormat);
  4248. imageSwizzleBgra8(_dst, _dstPitch, _width, _height, _dst, srcPitch);
  4249. }
  4250. break;
  4251. }
  4252. }
  4253. void imageRgba8ToRgba32fRef(void* _dst, uint32_t _width, uint32_t _height, uint32_t _srcPitch, const void* _src)
  4254. {
  4255. const uint32_t dstWidth = _width;
  4256. const uint32_t dstHeight = _height;
  4257. if (0 == dstWidth
  4258. || 0 == dstHeight)
  4259. {
  4260. return;
  4261. }
  4262. float* dst = (float*)_dst;
  4263. const uint8_t* src = (const uint8_t*)_src;
  4264. for (uint32_t yy = 0, ystep = _srcPitch; yy < dstHeight; ++yy, src += ystep)
  4265. {
  4266. const uint8_t* rgba = src;
  4267. for (uint32_t xx = 0; xx < dstWidth; ++xx, rgba += 4, dst += 4)
  4268. {
  4269. dst[0] = bx::toLinear(rgba[0]);
  4270. dst[1] = bx::toLinear(rgba[1]);
  4271. dst[2] = bx::toLinear(rgba[2]);
  4272. dst[3] = rgba[3];
  4273. }
  4274. }
  4275. }
  4276. void imageRgba8ToRgba32f(void* _dst, uint32_t _width, uint32_t _height, uint32_t _srcPitch, const void* _src)
  4277. {
  4278. const uint32_t dstWidth = _width;
  4279. const uint32_t dstHeight = _height;
  4280. if (0 == dstWidth
  4281. || 0 == dstHeight)
  4282. {
  4283. return;
  4284. }
  4285. float* dst = (float*)_dst;
  4286. const uint8_t* src = (const uint8_t*)_src;
  4287. using namespace bx;
  4288. const simd128_t unpack = simd_ld(1.0f/256.0f, 1.0f/256.0f/256.0f, 1.0f/65536.0f/256.0f, 1.0f/16777216.0f/256.0f);
  4289. const simd128_t umask = simd_ild(0xff, 0xff00, 0xff0000, 0xff000000);
  4290. const simd128_t wflip = simd_ild(0, 0, 0, 0x80000000);
  4291. const simd128_t wadd = simd_ld(0.0f, 0.0f, 0.0f, 32768.0f*65536.0f);
  4292. for (uint32_t yy = 0, ystep = _srcPitch; yy < dstHeight; ++yy, src += ystep)
  4293. {
  4294. const uint8_t* rgba = src;
  4295. for (uint32_t xx = 0; xx < dstWidth; ++xx, rgba += 4, dst += 4)
  4296. {
  4297. const simd128_t abgr0 = simd_splat(rgba);
  4298. const simd128_t abgr0m = simd_and(abgr0, umask);
  4299. const simd128_t abgr0x = simd_xor(abgr0m, wflip);
  4300. const simd128_t abgr0f = simd_itof(abgr0x);
  4301. const simd128_t abgr0c = simd_add(abgr0f, wadd);
  4302. const simd128_t abgr0n = simd_mul(abgr0c, unpack);
  4303. simd_st(dst, abgr0n);
  4304. }
  4305. }
  4306. }
  4307. void imageDecodeToRgba32f(bx::AllocatorI* _allocator, void* _dst, const void* _src, uint32_t _width, uint32_t _height, uint32_t _depth, uint32_t _dstPitch, TextureFormat::Enum _srcFormat)
  4308. {
  4309. const uint8_t* src = (const uint8_t*)_src;
  4310. uint8_t* dst = (uint8_t*)_dst;
  4311. const uint32_t srcBpp = s_imageBlockInfo[_srcFormat].bitsPerPixel;
  4312. const uint32_t srcPitch = _width*srcBpp/8;
  4313. for (uint32_t zz = 0; zz < _depth; ++zz, src += _height*srcPitch, dst += _height*_dstPitch)
  4314. {
  4315. switch (_srcFormat)
  4316. {
  4317. case TextureFormat::BC5:
  4318. {
  4319. uint32_t width = _width/4;
  4320. uint32_t height = _height/4;
  4321. const uint8_t* srcData = src;
  4322. for (uint32_t yy = 0; yy < height; ++yy)
  4323. {
  4324. for (uint32_t xx = 0; xx < width; ++xx)
  4325. {
  4326. uint8_t temp[16*4];
  4327. decodeBlockDxt45A(temp+2, srcData);
  4328. srcData += 8;
  4329. decodeBlockDxt45A(temp+1, srcData);
  4330. srcData += 8;
  4331. for (uint32_t ii = 0; ii < 16; ++ii)
  4332. {
  4333. float nx = temp[ii*4+2]*2.0f/255.0f - 1.0f;
  4334. float ny = temp[ii*4+1]*2.0f/255.0f - 1.0f;
  4335. float nz = bx::sqrt(1.0f - nx*nx - ny*ny);
  4336. const uint32_t offset = (yy*4 + ii/4)*_width*16 + (xx*4 + ii%4)*16;
  4337. float* block = (float*)&dst[offset];
  4338. block[0] = nx;
  4339. block[1] = ny;
  4340. block[2] = nz;
  4341. block[3] = 0.0f;
  4342. }
  4343. }
  4344. }
  4345. }
  4346. break;
  4347. case TextureFormat::BC6H:
  4348. {
  4349. uint32_t width = _width/4;
  4350. uint32_t height = _height/4;
  4351. const uint8_t* srcData = src;
  4352. for (uint32_t yy = 0; yy < height; ++yy)
  4353. {
  4354. for (uint32_t xx = 0; xx < width; ++xx)
  4355. {
  4356. float tmp[16*4];
  4357. decodeBlockBc6h(tmp, srcData);
  4358. srcData += 16;
  4359. uint8_t* block = (uint8_t*)&dst[yy*_dstPitch*4 + xx*64];
  4360. bx::memCopy(&block[0*_dstPitch], &tmp[ 0], 64);
  4361. bx::memCopy(&block[1*_dstPitch], &tmp[16], 64);
  4362. bx::memCopy(&block[2*_dstPitch], &tmp[32], 64);
  4363. bx::memCopy(&block[3*_dstPitch], &tmp[48], 64);
  4364. }
  4365. }
  4366. }
  4367. break;
  4368. case TextureFormat::RGBA32F:
  4369. bx::memCopy(dst, src, _dstPitch*_height);
  4370. break;
  4371. default:
  4372. if (isCompressed(_srcFormat) )
  4373. {
  4374. uint32_t size = imageGetSize(NULL, uint16_t(_width), uint16_t(_height), 0, false, false, 1, TextureFormat::RGBA8);
  4375. void* temp = BX_ALLOC(_allocator, size);
  4376. imageDecodeToRgba8(_allocator, temp, src, _width, _height, _width*4, _srcFormat);
  4377. imageRgba8ToRgba32f(dst, _width, _height, _width*4, temp);
  4378. BX_FREE(_allocator, temp);
  4379. }
  4380. else
  4381. {
  4382. imageConvert(_allocator, dst, TextureFormat::RGBA32F, src, _srcFormat, _width, _height, 1, srcPitch, _dstPitch);
  4383. }
  4384. break;
  4385. }
  4386. }
  4387. }
  4388. bool imageGetRawData(const ImageContainer& _imageContainer, uint16_t _side, uint8_t _lod, const void* _data, uint32_t _size, ImageMip& _mip)
  4389. {
  4390. uint32_t offset = _imageContainer.m_offset;
  4391. TextureFormat::Enum format = TextureFormat::Enum(_imageContainer.m_format);
  4392. bool hasAlpha = _imageContainer.m_hasAlpha;
  4393. const ImageBlockInfo& blockInfo = s_imageBlockInfo[format];
  4394. const uint8_t bpp = blockInfo.bitsPerPixel;
  4395. const uint32_t blockSize = blockInfo.blockSize;
  4396. const uint32_t blockWidth = blockInfo.blockWidth;
  4397. const uint32_t blockHeight = blockInfo.blockHeight;
  4398. const uint32_t minBlockX = blockInfo.minBlockX;
  4399. const uint32_t minBlockY = blockInfo.minBlockY;
  4400. if (UINT32_MAX == _imageContainer.m_offset)
  4401. {
  4402. if (NULL == _imageContainer.m_data)
  4403. {
  4404. return false;
  4405. }
  4406. offset = 0;
  4407. _data = _imageContainer.m_data;
  4408. _size = _imageContainer.m_size;
  4409. }
  4410. const uint8_t* data = (const uint8_t*)_data;
  4411. const uint16_t numSides = _imageContainer.m_numLayers * (_imageContainer.m_cubeMap ? 6 : 1);
  4412. if (_imageContainer.m_ktx || _imageContainer.m_pvr3)
  4413. {
  4414. uint32_t width = _imageContainer.m_width;
  4415. uint32_t height = _imageContainer.m_height;
  4416. uint32_t depth = _imageContainer.m_depth;
  4417. for (uint8_t lod = 0, num = _imageContainer.m_numMips; lod < num; ++lod)
  4418. {
  4419. width = bx::max<uint32_t>(blockWidth * minBlockX, ( (width + blockWidth - 1) / blockWidth )*blockWidth);
  4420. height = bx::max<uint32_t>(blockHeight * minBlockY, ( (height + blockHeight - 1) / blockHeight)*blockHeight);
  4421. depth = bx::max<uint32_t>(1, depth);
  4422. const uint32_t mipSize = width/blockWidth * height/blockHeight * depth * blockSize;
  4423. if (_imageContainer.m_ktx)
  4424. {
  4425. const uint32_t size = mipSize * numSides;
  4426. uint32_t imageSize = bx::toHostEndian(*(const uint32_t*)&data[offset], _imageContainer.m_ktxLE);
  4427. BX_ASSERT(size == imageSize, "KTX: Image size mismatch %d (expected %d).", size, imageSize);
  4428. BX_UNUSED(size, imageSize);
  4429. offset += sizeof(uint32_t);
  4430. }
  4431. for (uint16_t side = 0; side < numSides; ++side)
  4432. {
  4433. BX_ASSERT(offset <= _size, "Reading past size of data buffer! (offset %d, size %d)", offset, _size);
  4434. if (side == _side
  4435. && lod == _lod)
  4436. {
  4437. _mip.m_width = width;
  4438. _mip.m_height = height;
  4439. _mip.m_depth = depth;
  4440. _mip.m_blockSize = blockSize;
  4441. _mip.m_size = mipSize;
  4442. _mip.m_data = &data[offset];
  4443. _mip.m_bpp = bpp;
  4444. _mip.m_format = format;
  4445. _mip.m_hasAlpha = hasAlpha;
  4446. return true;
  4447. }
  4448. offset += mipSize;
  4449. BX_UNUSED(_size);
  4450. }
  4451. width >>= 1;
  4452. height >>= 1;
  4453. depth >>= 1;
  4454. }
  4455. }
  4456. else
  4457. {
  4458. for (uint16_t side = 0; side < numSides; ++side)
  4459. {
  4460. uint32_t width = _imageContainer.m_width;
  4461. uint32_t height = _imageContainer.m_height;
  4462. uint32_t depth = _imageContainer.m_depth;
  4463. for (uint8_t lod = 0, num = _imageContainer.m_numMips; lod < num; ++lod)
  4464. {
  4465. BX_ASSERT(offset <= _size, "Reading past size of data buffer! (offset %d, size %d)", offset, _size);
  4466. width = bx::max<uint32_t>(blockWidth * minBlockX, ( (width + blockWidth - 1) / blockWidth )*blockWidth);
  4467. height = bx::max<uint32_t>(blockHeight * minBlockY, ( (height + blockHeight - 1) / blockHeight)*blockHeight);
  4468. depth = bx::max<uint32_t>(1, depth);
  4469. uint32_t mipSize = width/blockWidth * height/blockHeight * depth * blockSize;
  4470. if (side == _side
  4471. && lod == _lod)
  4472. {
  4473. _mip.m_width = width;
  4474. _mip.m_height = height;
  4475. _mip.m_depth = depth;
  4476. _mip.m_blockSize = blockSize;
  4477. _mip.m_size = mipSize;
  4478. _mip.m_data = &data[offset];
  4479. _mip.m_bpp = bpp;
  4480. _mip.m_format = format;
  4481. _mip.m_hasAlpha = hasAlpha;
  4482. return true;
  4483. }
  4484. offset += mipSize;
  4485. BX_UNUSED(_size);
  4486. width >>= 1;
  4487. height >>= 1;
  4488. depth >>= 1;
  4489. }
  4490. }
  4491. }
  4492. return false;
  4493. }
  4494. 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)
  4495. {
  4496. BX_ERROR_SCOPE(_err);
  4497. uint8_t type = _grayscale ? 3 : 2;
  4498. uint8_t bpp = _grayscale ? 8 : 32;
  4499. uint8_t header[18] = {};
  4500. header[ 2] = type;
  4501. header[12] = _width &0xff;
  4502. header[13] = (_width >>8)&0xff;
  4503. header[14] = _height &0xff;
  4504. header[15] = (_height>>8)&0xff;
  4505. header[16] = bpp;
  4506. header[17] = 32;
  4507. int32_t total = 0;
  4508. total += bx::write(_writer, header, sizeof(header), _err);
  4509. uint32_t dstPitch = _width*bpp/8;
  4510. if (_yflip)
  4511. {
  4512. const uint8_t* data = (const uint8_t*)_src + _srcPitch*_height - _srcPitch;
  4513. for (uint32_t yy = 0; yy < _height && _err->isOk(); ++yy)
  4514. {
  4515. total += bx::write(_writer, data, dstPitch, _err);
  4516. data -= _srcPitch;
  4517. }
  4518. }
  4519. else if (_srcPitch == dstPitch)
  4520. {
  4521. total += bx::write(_writer, _src, _height*_srcPitch, _err);
  4522. }
  4523. else
  4524. {
  4525. const uint8_t* data = (const uint8_t*)_src;
  4526. for (uint32_t yy = 0; yy < _height && _err->isOk(); ++yy)
  4527. {
  4528. total += bx::write(_writer, data, dstPitch, _err);
  4529. data += _srcPitch;
  4530. }
  4531. }
  4532. return total;
  4533. }
  4534. template<typename Ty>
  4535. class HashWriter : public bx::WriterI
  4536. {
  4537. public:
  4538. HashWriter(bx::WriterI* _writer)
  4539. : m_writer(_writer)
  4540. {
  4541. begin();
  4542. }
  4543. void begin()
  4544. {
  4545. m_hash.begin();
  4546. }
  4547. uint32_t end()
  4548. {
  4549. return m_hash.end();
  4550. }
  4551. virtual int32_t write(const void* _data, int32_t _size, bx::Error* _err) override
  4552. {
  4553. m_hash.add(_data, _size);
  4554. return m_writer->write(_data, _size, _err);
  4555. }
  4556. private:
  4557. Ty m_hash;
  4558. bx::WriterI* m_writer;
  4559. };
  4560. int32_t imageWritePng(bx::WriterI* _writer, uint32_t _width, uint32_t _height, uint32_t _srcPitch, const void* _src, TextureFormat::Enum _format, bool _yflip, bx::Error* _err)
  4561. {
  4562. BX_ERROR_SCOPE(_err);
  4563. switch (_format)
  4564. {
  4565. case TextureFormat::R8:
  4566. case TextureFormat::RGBA8:
  4567. case TextureFormat::BGRA8:
  4568. break;
  4569. default:
  4570. BX_ERROR_SET(_err, BIMG_ERROR, "PNG: Unsupported texture format.");
  4571. return 0;
  4572. }
  4573. const bool grayscale = TextureFormat::R8 == _format;
  4574. const bool bgra = TextureFormat::BGRA8 == _format;
  4575. int32_t total = 0;
  4576. total += bx::write(_writer, "\x89PNG\r\n\x1a\n", _err);
  4577. total += bx::write(_writer, bx::toBigEndian<uint32_t>(13), _err);
  4578. HashWriter<bx::HashCrc32> writerC(_writer);
  4579. total += bx::write(&writerC, "IHDR", _err);
  4580. total += bx::write(&writerC, bx::toBigEndian(_width), _err);
  4581. total += bx::write(&writerC, bx::toBigEndian(_height), _err);
  4582. total += bx::write(&writerC, "\x08\x06", _err);
  4583. total += bx::writeRep(&writerC, 0, 3, _err);
  4584. total += bx::write(_writer, bx::toBigEndian(writerC.end() ), _err);
  4585. const uint32_t bpp = grayscale ? 8 : 32;
  4586. const uint32_t stride = _width*bpp/8;
  4587. const uint16_t zlen = bx::toLittleEndian<uint16_t>(uint16_t(stride + 1) );
  4588. const uint16_t zlenC = bx::toLittleEndian<uint16_t>(~zlen);
  4589. total += bx::write(_writer, bx::toBigEndian<uint32_t>(_height*(stride+6)+6), _err);
  4590. writerC.begin();
  4591. total += bx::write(&writerC, "IDAT", _err);
  4592. total += bx::write(&writerC, "\x78\x9c", _err);
  4593. const uint8_t* data = (const uint8_t*)_src;
  4594. int32_t step = int32_t(_srcPitch);
  4595. if (_yflip)
  4596. {
  4597. data += _srcPitch*_height - _srcPitch;
  4598. step = -step;
  4599. }
  4600. HashWriter<bx::HashAdler32> writerA(&writerC);
  4601. for (uint32_t ii = 0; ii < _height && _err->isOk(); ++ii)
  4602. {
  4603. total += bx::write(&writerC, uint8_t(ii == _height-1 ? 1 : 0), _err);
  4604. total += bx::write(&writerC, zlen, _err);
  4605. total += bx::write(&writerC, zlenC, _err);
  4606. total += bx::write(&writerA, uint8_t(0), _err);
  4607. if (bgra)
  4608. {
  4609. for (uint32_t xx = 0; xx < _width; ++xx)
  4610. {
  4611. const uint8_t* texel = &data[xx*4];
  4612. const uint8_t bb = texel[0];
  4613. const uint8_t gg = texel[1];
  4614. const uint8_t rr = texel[2];
  4615. const uint8_t aa = texel[3];
  4616. total += bx::write(&writerA, rr, _err);
  4617. total += bx::write(&writerA, gg, _err);
  4618. total += bx::write(&writerA, bb, _err);
  4619. total += bx::write(&writerA, aa, _err);
  4620. }
  4621. }
  4622. else
  4623. {
  4624. total += bx::write(&writerA, data, stride, _err);
  4625. }
  4626. data += step;
  4627. }
  4628. total += bx::write(&writerC, bx::toBigEndian(writerA.end() ), _err);
  4629. total += bx::write(_writer, bx::toBigEndian(writerC.end() ), _err);
  4630. total += bx::write(&writerC, uint32_t(0), _err);
  4631. writerC.begin();
  4632. total += bx::write(&writerC, "IEND", _err);
  4633. total += bx::write(_writer, bx::toBigEndian(writerC.end() ), _err);
  4634. return total;
  4635. }
  4636. int32_t imageWriteExr(bx::WriterI* _writer, uint32_t _width, uint32_t _height, uint32_t _srcPitch, const void* _src, TextureFormat::Enum _format, bool _yflip, bx::Error* _err)
  4637. {
  4638. BX_ERROR_SCOPE(_err);
  4639. const uint32_t bpp = getBitsPerPixel(_format);
  4640. uint32_t bytesPerChannel = 0;
  4641. switch (_format)
  4642. {
  4643. case TextureFormat::RGBA16F:
  4644. bytesPerChannel = 2;
  4645. break;
  4646. default:
  4647. BX_ERROR_SET(_err, BIMG_ERROR, "EXR: Unsupported texture format.");
  4648. return 0;
  4649. }
  4650. int32_t total = 0;
  4651. total += bx::write(_writer, "v/1\x01", _err);
  4652. total += bx::writeLE(_writer, uint32_t(2), _err);
  4653. total += bx::write(_writer, "channels", _err);
  4654. total += bx::write(_writer, '\0', _err);
  4655. total += bx::write(_writer, "chlist", _err);
  4656. total += bx::write(_writer, '\0', _err);
  4657. total += bx::writeLE(_writer, uint32_t(18*4+1), _err);
  4658. const uint8_t cdata[] = { 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0 };
  4659. // Order is always ABGR order because Photoshop and GIMP ignore these fields and
  4660. // assume it's in ABGR order.
  4661. total += bx::write(_writer, 'A', _err);
  4662. total += bx::write(_writer, cdata, BX_COUNTOF(cdata), _err);
  4663. total += bx::write(_writer, 'B', _err);
  4664. total += bx::write(_writer, cdata, BX_COUNTOF(cdata), _err);
  4665. total += bx::write(_writer, 'G', _err);
  4666. total += bx::write(_writer, cdata, BX_COUNTOF(cdata), _err);
  4667. total += bx::write(_writer, 'R', _err);
  4668. total += bx::write(_writer, cdata, BX_COUNTOF(cdata), _err);
  4669. total += bx::write(_writer, '\0', _err);
  4670. total += bx::write(_writer, "compression", _err);
  4671. total += bx::write(_writer, '\0', _err);
  4672. total += bx::write(_writer, "compression", _err);
  4673. total += bx::write(_writer, '\0', _err);
  4674. total += bx::writeLE(_writer, uint32_t(1), _err);
  4675. total += bx::write(_writer, '\0', _err); // no compression
  4676. total += bx::write(_writer, "dataWindow", _err);
  4677. total += bx::write(_writer, '\0', _err);
  4678. total += bx::write(_writer, "box2i", _err);
  4679. total += bx::write(_writer, '\0', _err);
  4680. total += bx::writeLE(_writer, uint32_t(16), _err);
  4681. total += bx::writeRep(_writer, '\0', 8, _err);
  4682. total += bx::writeLE(_writer, _width-1, _err);
  4683. total += bx::writeLE(_writer, _height-1, _err);
  4684. total += bx::write(_writer, "displayWindow", _err);
  4685. total += bx::write(_writer, '\0', _err);
  4686. total += bx::write(_writer, "box2i", _err);
  4687. total += bx::write(_writer, '\0', _err);
  4688. total += bx::writeLE(_writer, uint32_t(16), _err);
  4689. total += bx::writeRep(_writer, '\0', 8, _err);
  4690. total += bx::writeLE(_writer, _width-1, _err);
  4691. total += bx::writeLE(_writer, _height-1, _err);
  4692. total += bx::write(_writer, "lineOrder", _err);
  4693. total += bx::write(_writer, '\0', _err);
  4694. total += bx::write(_writer, "lineOrder", _err);
  4695. total += bx::write(_writer, '\0', _err);
  4696. total += bx::writeLE(_writer, uint32_t(1), _err);
  4697. total += bx::write(_writer, _yflip, _err);
  4698. total += bx::write(_writer, "pixelAspectRatio", _err);
  4699. total += bx::write(_writer, '\0', _err);
  4700. total += bx::write(_writer, "float", _err);
  4701. total += bx::write(_writer, '\0', _err);
  4702. total += bx::writeLE(_writer, uint32_t(4), _err);
  4703. total += bx::writeLE(_writer, 1.0f, _err);
  4704. total += bx::write(_writer, "screenWindowCenter", _err);
  4705. total += bx::write(_writer, '\0', _err);
  4706. total += bx::write(_writer, "v2f", _err);
  4707. total += bx::write(_writer, '\0', _err);
  4708. total += bx::writeLE(_writer, uint32_t(8), _err);
  4709. total += bx::writeRep(_writer, '\0', 8, _err);
  4710. total += bx::write(_writer, "screenWindowWidth", _err);
  4711. total += bx::write(_writer, '\0', _err);
  4712. total += bx::write(_writer, "float", _err);
  4713. total += bx::write(_writer, '\0', _err);
  4714. total += bx::writeLE(_writer, uint32_t(4), _err);
  4715. total += bx::writeLE(_writer, 1.0f, _err);
  4716. total += bx::write(_writer, '\0', _err);
  4717. const uint32_t exrStride = _width*bpp/8;
  4718. uint64_t offset = 0;
  4719. for (uint32_t yy = 0; yy < _height && _err->isOk(); ++yy)
  4720. {
  4721. total += bx::writeLE(_writer, (offset), _err);
  4722. offset += exrStride + 8 /* offset */;
  4723. }
  4724. const uint8_t* data = (const uint8_t*)_src;
  4725. for (uint32_t yy = 0; yy < _height && _err->isOk(); ++yy)
  4726. {
  4727. total += bx::writeLE(_writer, yy, _err);
  4728. total += bx::writeLE(_writer, exrStride, _err);
  4729. for (uint32_t xx = 0; xx < _width && _err->isOk(); ++xx)
  4730. {
  4731. total += bx::write(_writer, &data[xx*bpp/8+3*bytesPerChannel], bytesPerChannel, _err);
  4732. }
  4733. for (uint32_t xx = 0; xx < _width && _err->isOk(); ++xx)
  4734. {
  4735. total += bx::write(_writer, &data[xx*bpp/8+2*bytesPerChannel], bytesPerChannel, _err);
  4736. }
  4737. for (uint32_t xx = 0; xx < _width && _err->isOk(); ++xx)
  4738. {
  4739. total += bx::write(_writer, &data[xx*bpp/8+1*bytesPerChannel], bytesPerChannel, _err);
  4740. }
  4741. for (uint32_t xx = 0; xx < _width && _err->isOk(); ++xx)
  4742. {
  4743. total += bx::write(_writer, &data[xx*bpp/8+0*bytesPerChannel], bytesPerChannel, _err);
  4744. }
  4745. data += _srcPitch;
  4746. }
  4747. return total;
  4748. }
  4749. int32_t imageWriteHdr(bx::WriterI* _writer, uint32_t _width, uint32_t _height, uint32_t _srcPitch, const void* _src, TextureFormat::Enum _format, bool _yflip, bx::Error* _err)
  4750. {
  4751. BX_ERROR_SCOPE(_err);
  4752. int32_t total = 0;
  4753. total += bx::write(_writer, "#?RADIANCE\n" , _err);
  4754. total += bx::write(_writer, "FORMAT=32-bit_rle_rgbe\n" , _err);
  4755. total += bx::write(_writer, '\n' , _err);
  4756. total += bx::write(_writer, _err, "%cY %d +X %d\n", _yflip ? '+' : '-', _height, _width);
  4757. UnpackFn unpack = getUnpack(_format);
  4758. const uint32_t bpp = getBitsPerPixel(_format);
  4759. const uint8_t* data = (const uint8_t*)_src;
  4760. for (uint32_t yy = 0; yy < _height && _err->isOk(); ++yy)
  4761. {
  4762. for (uint32_t xx = 0; xx < _width && _err->isOk(); ++xx)
  4763. {
  4764. float rgba[4];
  4765. unpack(rgba, &data[xx*bpp/8]);
  4766. const float maxVal = bx::max(rgba[0], rgba[1], rgba[2]);
  4767. const float exp = bx::ceil(bx::log2(maxVal) );
  4768. const float toRgb8 = 255.0f * 1.0f/bx::ldexp(1.0f, int(exp) );
  4769. uint8_t rgbe[4];
  4770. rgbe[0] = uint8_t(rgba[0] * toRgb8);
  4771. rgbe[1] = uint8_t(rgba[1] * toRgb8);
  4772. rgbe[2] = uint8_t(rgba[2] * toRgb8);
  4773. rgbe[3] = uint8_t(exp+128.0f);
  4774. total += bx::write(_writer, rgbe, 4, _err);
  4775. }
  4776. data += _srcPitch;
  4777. }
  4778. return total;
  4779. }
  4780. 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)
  4781. {
  4782. BX_ERROR_SCOPE(_err);
  4783. uint32_t ddspf = UINT32_MAX;
  4784. uint32_t dxgiFormat = UINT32_MAX;
  4785. uint32_t fourccFormat = UINT32_MAX;
  4786. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateDdsPixelFormat); ++ii)
  4787. {
  4788. if (s_translateDdsPixelFormat[ii].m_textureFormat == _format)
  4789. {
  4790. ddspf = ii;
  4791. break;
  4792. }
  4793. }
  4794. if (UINT32_MAX == ddspf)
  4795. {
  4796. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateDxgiFormat); ++ii)
  4797. {
  4798. if (s_translateDxgiFormat[ii].m_textureFormat == _format)
  4799. {
  4800. dxgiFormat = s_translateDxgiFormat[ii].m_format;
  4801. break;
  4802. }
  4803. }
  4804. }
  4805. if (UINT32_MAX == ddspf && UINT32_MAX == dxgiFormat)
  4806. {
  4807. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateDdsFourccFormat); ++ii)
  4808. {
  4809. if (s_translateDdsFourccFormat[ii].m_textureFormat == _format)
  4810. {
  4811. fourccFormat = s_translateDdsFourccFormat[ii].m_format;
  4812. break;
  4813. }
  4814. }
  4815. }
  4816. if (UINT32_MAX == ddspf
  4817. && UINT32_MAX == dxgiFormat
  4818. && UINT32_MAX == fourccFormat)
  4819. {
  4820. BX_ERROR_SET(_err, BIMG_ERROR, "DDS: output format not supported.");
  4821. return 0;
  4822. }
  4823. const uint32_t bpp = getBitsPerPixel(_format);
  4824. uint32_t total = 0;
  4825. total += bx::write(_writer, uint32_t(DDS_MAGIC), _err);
  4826. uint32_t headerStart = total;
  4827. total += bx::write(_writer, uint32_t(DDS_HEADER_SIZE), _err);
  4828. total += bx::write(_writer, uint32_t(0
  4829. | DDSD_HEIGHT
  4830. | DDSD_WIDTH
  4831. | DDSD_PIXELFORMAT
  4832. | DDSD_CAPS
  4833. | (1 < _depth ? DDSD_DEPTH : 0)
  4834. | (1 < _numMips ? DDSD_MIPMAPCOUNT : 0)
  4835. | (isCompressed(_format) ? DDSD_LINEARSIZE : DDSD_PITCH)
  4836. )
  4837. , _err
  4838. );
  4839. const uint32_t pitchOrLinearSize = isCompressed(_format)
  4840. ? _width*_height*bpp/8
  4841. : _width*bpp/8
  4842. ;
  4843. total += bx::write(_writer, _height, _err);
  4844. total += bx::write(_writer, _width, _err);
  4845. total += bx::write(_writer, pitchOrLinearSize, _err);
  4846. total += bx::write(_writer, _depth, _err);
  4847. total += bx::write(_writer, uint32_t(_numMips), _err);
  4848. total += bx::writeRep(_writer, 0, 44, _err); // reserved1
  4849. if (UINT32_MAX != ddspf)
  4850. {
  4851. const TranslateDdsPixelFormat& pf = s_translateDdsPixelFormat[ddspf];
  4852. total += bx::write(_writer, uint32_t(8*sizeof(uint32_t) ), _err); // pixelFormatSize
  4853. total += bx::write(_writer, pf.m_flags, _err);
  4854. total += bx::write(_writer, uint32_t(0), _err);
  4855. total += bx::write(_writer, pf.m_bitCount, _err);
  4856. total += bx::write(_writer, pf.m_bitmask, _err);
  4857. }
  4858. else
  4859. {
  4860. total += bx::write(_writer, uint32_t(8*sizeof(uint32_t) ), _err); // pixelFormatSize
  4861. total += bx::write(_writer, uint32_t(DDPF_FOURCC), _err);
  4862. if (UINT32_MAX != fourccFormat)
  4863. {
  4864. total += bx::write(_writer, fourccFormat, _err);
  4865. }
  4866. else
  4867. {
  4868. total += bx::write(_writer, uint32_t(DDS_DX10), _err);
  4869. }
  4870. total += bx::write(_writer, uint32_t(0), _err); // bitCount
  4871. total += bx::writeRep(_writer, 0, 4*sizeof(uint32_t), _err); // bitmask
  4872. }
  4873. uint32_t caps[4] =
  4874. {
  4875. uint32_t(DDSCAPS_TEXTURE | (1 < _numMips ? DDSCAPS_COMPLEX|DDSCAPS_MIPMAP : 0) ),
  4876. uint32_t(_cubeMap ? DDSCAPS2_CUBEMAP|DSCAPS2_CUBEMAP_ALLSIDES : 0),
  4877. 0,
  4878. 0,
  4879. };
  4880. total += bx::write(_writer, caps, sizeof(caps), _err);
  4881. total += bx::writeRep(_writer, 0, 4, _err); // reserved2
  4882. BX_WARN(total-headerStart == DDS_HEADER_SIZE
  4883. , "DDS: Failed to write header size %d (expected: %d)."
  4884. , total-headerStart
  4885. , DDS_HEADER_SIZE
  4886. );
  4887. if (UINT32_MAX != dxgiFormat)
  4888. {
  4889. total += bx::write(_writer, dxgiFormat, _err);
  4890. total += bx::write(_writer, uint32_t(1 < _depth ? DDS_DX10_DIMENSION_TEXTURE3D : DDS_DX10_DIMENSION_TEXTURE2D), _err); // dims
  4891. total += bx::write(_writer, uint32_t(_cubeMap ? DDS_DX10_MISC_TEXTURECUBE : 0), _err); // miscFlags
  4892. total += bx::write(_writer, uint32_t(1), _err); // arraySize
  4893. total += bx::write(_writer, uint32_t(0), _err); // miscFlags2
  4894. BX_WARN(total-headerStart == DDS_HEADER_SIZE+20
  4895. , "DDS: Failed to write header size %d (expected: %d)."
  4896. , total-headerStart
  4897. , DDS_HEADER_SIZE+20
  4898. );
  4899. BX_UNUSED(headerStart);
  4900. }
  4901. return total;
  4902. }
  4903. int32_t imageWriteDds(bx::WriterI* _writer, ImageContainer& _imageContainer, const void* _data, uint32_t _size, bx::Error* _err)
  4904. {
  4905. BX_ERROR_SCOPE(_err);
  4906. int32_t total = 0;
  4907. total += imageWriteDdsHeader(_writer
  4908. , TextureFormat::Enum(_imageContainer.m_format)
  4909. , _imageContainer.m_cubeMap
  4910. , _imageContainer.m_width
  4911. , _imageContainer.m_height
  4912. , _imageContainer.m_depth
  4913. , _imageContainer.m_numMips
  4914. , _err
  4915. );
  4916. if (!_err->isOk() )
  4917. {
  4918. return total;
  4919. }
  4920. for (uint8_t side = 0, numSides = _imageContainer.m_cubeMap ? 6 : 1; side < numSides && _err->isOk(); ++side)
  4921. {
  4922. for (uint8_t lod = 0, num = _imageContainer.m_numMips; lod < num && _err->isOk(); ++lod)
  4923. {
  4924. ImageMip mip;
  4925. if (imageGetRawData(_imageContainer, side, lod, _data, _size, mip) )
  4926. {
  4927. total += bx::write(_writer, mip.m_data, mip.m_size, _err);
  4928. }
  4929. }
  4930. }
  4931. return total;
  4932. }
  4933. 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, bool _srgb, bx::Error* _err)
  4934. {
  4935. BX_ERROR_SCOPE(_err);
  4936. const KtxFormatInfo& tfi = s_translateKtxFormat[_format];
  4937. uint32_t internalFmt = tfi.m_internalFmt;
  4938. if (_srgb && tfi.m_internalFmtSrgb != KTX_ZERO) {
  4939. internalFmt = tfi.m_internalFmtSrgb;
  4940. }
  4941. int32_t total = 0;
  4942. total += bx::write(_writer, "\xabKTX 11\xbb\r\n\x1a\n", 12, _err);
  4943. total += bx::write(_writer, uint32_t(0x04030201), _err);
  4944. total += bx::write(_writer, uint32_t(0), _err); // glType
  4945. total += bx::write(_writer, uint32_t(1), _err); // glTypeSize
  4946. total += bx::write(_writer, uint32_t(0), _err); // glFormat
  4947. total += bx::write(_writer, internalFmt, _err); // glInternalFormat
  4948. total += bx::write(_writer, tfi.m_fmt, _err); // glBaseInternalFormat
  4949. total += bx::write(_writer, _width, _err);
  4950. total += bx::write(_writer, _height, _err);
  4951. total += bx::write(_writer, _depth, _err);
  4952. total += bx::write(_writer, _numLayers, _err); // numberOfArrayElements
  4953. total += bx::write(_writer, _cubeMap ? uint32_t(6) : uint32_t(0), _err);
  4954. total += bx::write(_writer, uint32_t(_numMips), _err);
  4955. total += bx::write(_writer, uint32_t(0), _err); // Meta-data size.
  4956. BX_WARN(total == 64, "KTX: Failed to write header size %d (expected: %d).", total, 64);
  4957. return total;
  4958. }
  4959. 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, bool _srgb, const void* _src, bx::Error* _err)
  4960. {
  4961. BX_ERROR_SCOPE(_err);
  4962. int32_t total = 0;
  4963. total += imageWriteKtxHeader(_writer, _format, _cubeMap, _width, _height, _depth, _numMips, _numLayers, _srgb, _err);
  4964. if (!_err->isOk() )
  4965. {
  4966. return total;
  4967. }
  4968. const ImageBlockInfo& blockInfo = s_imageBlockInfo[_format];
  4969. const uint32_t blockWidth = blockInfo.blockWidth;
  4970. const uint32_t blockHeight = blockInfo.blockHeight;
  4971. const uint32_t minBlockX = blockInfo.minBlockX;
  4972. const uint32_t minBlockY = blockInfo.minBlockY;
  4973. const uint8_t blockSize = blockInfo.blockSize;
  4974. const uint8_t* src = (const uint8_t*)_src;
  4975. const uint32_t numLayers = bx::max<uint32_t>(_numLayers, 1);
  4976. const uint32_t numSides = _cubeMap ? 6 : 1;
  4977. uint32_t width = _width;
  4978. uint32_t height = _height;
  4979. uint32_t depth = _depth;
  4980. for (uint8_t lod = 0; lod < _numMips && _err->isOk(); ++lod)
  4981. {
  4982. width = bx::max<uint32_t>(blockWidth * minBlockX, ( (width + blockWidth - 1) / blockWidth )*blockWidth);
  4983. height = bx::max<uint32_t>(blockHeight * minBlockY, ( (height + blockHeight - 1) / blockHeight)*blockHeight);
  4984. depth = bx::max<uint32_t>(1, depth);
  4985. const uint32_t mipSize = width/blockWidth * height/blockHeight * depth * blockSize;
  4986. const uint32_t size = mipSize * numLayers * numSides;
  4987. total += bx::write(_writer, size, _err);
  4988. for (uint32_t layer = 0; layer < numLayers && _err->isOk(); ++layer)
  4989. {
  4990. for (uint8_t side = 0; side < numSides && _err->isOk(); ++side)
  4991. {
  4992. total += bx::write(_writer, src, mipSize, _err);
  4993. src += mipSize;
  4994. }
  4995. }
  4996. width >>= 1;
  4997. height >>= 1;
  4998. depth >>= 1;
  4999. }
  5000. return total;
  5001. }
  5002. int32_t imageWriteKtx(bx::WriterI* _writer, ImageContainer& _imageContainer, const void* _data, uint32_t _size, bx::Error* _err)
  5003. {
  5004. BX_ERROR_SCOPE(_err);
  5005. int32_t total = 0;
  5006. total += imageWriteKtxHeader(_writer
  5007. , TextureFormat::Enum(_imageContainer.m_format)
  5008. , _imageContainer.m_cubeMap
  5009. , _imageContainer.m_width
  5010. , _imageContainer.m_height
  5011. , _imageContainer.m_depth
  5012. , _imageContainer.m_numMips
  5013. , _imageContainer.m_numLayers
  5014. , _imageContainer.m_srgb
  5015. , _err
  5016. );
  5017. if (!_err->isOk() )
  5018. {
  5019. return total;
  5020. }
  5021. const uint32_t numMips = _imageContainer.m_numMips;
  5022. const uint32_t numLayers = bx::max<uint32_t>(_imageContainer.m_numLayers, 1);
  5023. const uint32_t numSides = _imageContainer.m_cubeMap ? 6 : 1;
  5024. for (uint8_t lod = 0; lod < numMips && _err->isOk(); ++lod)
  5025. {
  5026. ImageMip mip;
  5027. imageGetRawData(_imageContainer, 0, lod, _data, _size, mip);
  5028. const uint32_t size = mip.m_size*numSides*numLayers;
  5029. total += bx::write(_writer, size, _err);
  5030. for (uint32_t layer = 0; layer < numLayers && _err->isOk(); ++layer)
  5031. {
  5032. for (uint8_t side = 0; side < numSides && _err->isOk(); ++side)
  5033. {
  5034. if (imageGetRawData(_imageContainer, uint16_t(layer*numSides + side), lod, _data, _size, mip) )
  5035. {
  5036. total += bx::write(_writer, mip.m_data, mip.m_size, _err);
  5037. }
  5038. }
  5039. }
  5040. }
  5041. return total;
  5042. }
  5043. } // namespace bimg