image.cpp 196 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616
  1. /*
  2. * Copyright 2011-2019 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. 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. 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. bimg::ImageMip mip;
  519. bimg::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. bimg::ImageMip mip;
  557. bimg::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_CHECK(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);
  1124. ImageContainer imageContainer;
  1125. if (magicT != magic
  1126. || !parseFnT(imageContainer, &reader, _err) )
  1127. {
  1128. return NULL;
  1129. }
  1130. ImageContainer* output = imageAlloc(_allocator
  1131. , imageContainer.m_format
  1132. , uint16_t(imageContainer.m_width)
  1133. , uint16_t(imageContainer.m_height)
  1134. , uint16_t(imageContainer.m_depth)
  1135. , imageContainer.m_numLayers
  1136. , imageContainer.m_cubeMap
  1137. , 1 < imageContainer.m_numMips
  1138. );
  1139. const uint16_t numSides = imageContainer.m_numLayers * (imageContainer.m_cubeMap ? 6 : 1);
  1140. for (uint16_t side = 0; side < numSides; ++side)
  1141. {
  1142. for (uint8_t lod = 0, num = imageContainer.m_numMips; lod < num; ++lod)
  1143. {
  1144. ImageMip dstMip;
  1145. if (imageGetRawData(*output, side, lod, output->m_data, output->m_size, dstMip) )
  1146. {
  1147. ImageMip mip;
  1148. if (imageGetRawData(imageContainer, side, lod, _src, _size, mip) )
  1149. {
  1150. uint8_t* dstData = const_cast<uint8_t*>(dstMip.m_data);
  1151. bx::memCopy(dstData, mip.m_data, mip.m_size);
  1152. }
  1153. }
  1154. }
  1155. }
  1156. return output;
  1157. }
  1158. uint8_t bitRangeConvert(uint32_t _in, uint32_t _from, uint32_t _to)
  1159. {
  1160. using namespace bx;
  1161. uint32_t tmp0 = uint32_sll(1, _to);
  1162. uint32_t tmp1 = uint32_sll(1, _from);
  1163. uint32_t tmp2 = uint32_dec(tmp0);
  1164. uint32_t tmp3 = uint32_dec(tmp1);
  1165. uint32_t tmp4 = uint32_mul(_in, tmp2);
  1166. uint32_t tmp5 = uint32_add(tmp3, tmp4);
  1167. uint32_t tmp6 = uint32_srl(tmp5, _from);
  1168. uint32_t tmp7 = uint32_add(tmp5, tmp6);
  1169. uint32_t result = uint32_srl(tmp7, _from);
  1170. return uint8_t(result);
  1171. }
  1172. void decodeBlockDxt(uint8_t _dst[16*4], const uint8_t _src[8])
  1173. {
  1174. if (!BX_ENABLED(BIMG_DECODE_BC2 || BIMG_DECODE_BC3) )
  1175. {
  1176. return;
  1177. }
  1178. uint8_t colors[4*3];
  1179. uint32_t c0 = _src[0] | (_src[1] << 8);
  1180. colors[0] = bitRangeConvert( (c0>> 0)&0x1f, 5, 8);
  1181. colors[1] = bitRangeConvert( (c0>> 5)&0x3f, 6, 8);
  1182. colors[2] = bitRangeConvert( (c0>>11)&0x1f, 5, 8);
  1183. uint32_t c1 = _src[2] | (_src[3] << 8);
  1184. colors[3] = bitRangeConvert( (c1>> 0)&0x1f, 5, 8);
  1185. colors[4] = bitRangeConvert( (c1>> 5)&0x3f, 6, 8);
  1186. colors[5] = bitRangeConvert( (c1>>11)&0x1f, 5, 8);
  1187. colors[6] = (2*colors[0] + colors[3]) / 3;
  1188. colors[7] = (2*colors[1] + colors[4]) / 3;
  1189. colors[8] = (2*colors[2] + colors[5]) / 3;
  1190. colors[ 9] = (colors[0] + 2*colors[3]) / 3;
  1191. colors[10] = (colors[1] + 2*colors[4]) / 3;
  1192. colors[11] = (colors[2] + 2*colors[5]) / 3;
  1193. for (uint32_t ii = 0, next = 8*4; ii < 16*4; ii += 4, next += 2)
  1194. {
  1195. int idx = ( (_src[next>>3] >> (next & 7) ) & 3) * 3;
  1196. _dst[ii+0] = colors[idx+0];
  1197. _dst[ii+1] = colors[idx+1];
  1198. _dst[ii+2] = colors[idx+2];
  1199. }
  1200. }
  1201. void decodeBlockDxt1(uint8_t _dst[16*4], const uint8_t _src[8])
  1202. {
  1203. if (!BX_ENABLED(BIMG_DECODE_BC1 || BIMG_DECODE_BC2 || BIMG_DECODE_BC3) )
  1204. {
  1205. return;
  1206. }
  1207. uint8_t colors[4*4];
  1208. uint32_t c0 = _src[0] | (_src[1] << 8);
  1209. colors[0] = bitRangeConvert( (c0>> 0)&0x1f, 5, 8);
  1210. colors[1] = bitRangeConvert( (c0>> 5)&0x3f, 6, 8);
  1211. colors[2] = bitRangeConvert( (c0>>11)&0x1f, 5, 8);
  1212. colors[3] = 255;
  1213. uint32_t c1 = _src[2] | (_src[3] << 8);
  1214. colors[4] = bitRangeConvert( (c1>> 0)&0x1f, 5, 8);
  1215. colors[5] = bitRangeConvert( (c1>> 5)&0x3f, 6, 8);
  1216. colors[6] = bitRangeConvert( (c1>>11)&0x1f, 5, 8);
  1217. colors[7] = 255;
  1218. if (c0 > c1)
  1219. {
  1220. colors[ 8] = (2*colors[0] + colors[4]) / 3;
  1221. colors[ 9] = (2*colors[1] + colors[5]) / 3;
  1222. colors[10] = (2*colors[2] + colors[6]) / 3;
  1223. colors[11] = 255;
  1224. colors[12] = (colors[0] + 2*colors[4]) / 3;
  1225. colors[13] = (colors[1] + 2*colors[5]) / 3;
  1226. colors[14] = (colors[2] + 2*colors[6]) / 3;
  1227. colors[15] = 255;
  1228. }
  1229. else
  1230. {
  1231. colors[ 8] = (colors[0] + colors[4]) / 2;
  1232. colors[ 9] = (colors[1] + colors[5]) / 2;
  1233. colors[10] = (colors[2] + colors[6]) / 2;
  1234. colors[11] = 255;
  1235. colors[12] = 0;
  1236. colors[13] = 0;
  1237. colors[14] = 0;
  1238. colors[15] = 0;
  1239. }
  1240. for (uint32_t ii = 0, next = 8*4; ii < 16*4; ii += 4, next += 2)
  1241. {
  1242. int idx = ( (_src[next>>3] >> (next & 7) ) & 3) * 4;
  1243. _dst[ii+0] = colors[idx+0];
  1244. _dst[ii+1] = colors[idx+1];
  1245. _dst[ii+2] = colors[idx+2];
  1246. _dst[ii+3] = colors[idx+3];
  1247. }
  1248. }
  1249. void decodeBlockDxt23A(uint8_t _dst[16*4], const uint8_t _src[8])
  1250. {
  1251. if (!BX_ENABLED(BIMG_DECODE_BC2) )
  1252. {
  1253. return;
  1254. }
  1255. for (uint32_t ii = 0, next = 0; ii < 16*4; ii += 4, next += 4)
  1256. {
  1257. uint32_t c0 = (_src[next>>3] >> (next&7) ) & 0xf;
  1258. _dst[ii] = bitRangeConvert(c0, 4, 8);
  1259. }
  1260. }
  1261. void decodeBlockDxt45A(uint8_t _dst[16*4], const uint8_t _src[8])
  1262. {
  1263. if (!BX_ENABLED(BIMG_DECODE_BC3 || BIMG_DECODE_BC4 || BIMG_DECODE_BC5) )
  1264. {
  1265. return;
  1266. }
  1267. uint8_t alpha[8];
  1268. alpha[0] = _src[0];
  1269. alpha[1] = _src[1];
  1270. if (alpha[0] > alpha[1])
  1271. {
  1272. alpha[2] = (6*alpha[0] + 1*alpha[1]) / 7;
  1273. alpha[3] = (5*alpha[0] + 2*alpha[1]) / 7;
  1274. alpha[4] = (4*alpha[0] + 3*alpha[1]) / 7;
  1275. alpha[5] = (3*alpha[0] + 4*alpha[1]) / 7;
  1276. alpha[6] = (2*alpha[0] + 5*alpha[1]) / 7;
  1277. alpha[7] = (1*alpha[0] + 6*alpha[1]) / 7;
  1278. }
  1279. else
  1280. {
  1281. alpha[2] = (4*alpha[0] + 1*alpha[1]) / 5;
  1282. alpha[3] = (3*alpha[0] + 2*alpha[1]) / 5;
  1283. alpha[4] = (2*alpha[0] + 3*alpha[1]) / 5;
  1284. alpha[5] = (1*alpha[0] + 4*alpha[1]) / 5;
  1285. alpha[6] = 0;
  1286. alpha[7] = 255;
  1287. }
  1288. uint32_t idx0 = _src[2];
  1289. uint32_t idx1 = _src[5];
  1290. idx0 |= uint32_t(_src[3])<<8;
  1291. idx1 |= uint32_t(_src[6])<<8;
  1292. idx0 |= uint32_t(_src[4])<<16;
  1293. idx1 |= uint32_t(_src[7])<<16;
  1294. for (uint32_t ii = 0; ii < 8*4; ii += 4)
  1295. {
  1296. _dst[ii] = alpha[idx0&7];
  1297. _dst[ii+32] = alpha[idx1&7];
  1298. idx0 >>= 3;
  1299. idx1 >>= 3;
  1300. }
  1301. }
  1302. // BC6H, BC7
  1303. //
  1304. // Reference(s):
  1305. // - https://web.archive.org/web/20181126035446/https://www.khronos.org/registry/OpenGL/extensions/ARB/ARB_texture_compression_bptc.txt
  1306. // - https://web.archive.org/web/20181126035538/https://docs.microsoft.com/en-us/windows/desktop/direct3d11/bc6h-format
  1307. //
  1308. static const uint16_t s_bptcP2[] =
  1309. { // 3210 0000000000 1111111111 2222222222 3333333333
  1310. 0xcccc, // 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1
  1311. 0x8888, // 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1
  1312. 0xeeee, // 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1
  1313. 0xecc8, // 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1
  1314. 0xc880, // 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 1
  1315. 0xfeec, // 0, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1
  1316. 0xfec8, // 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1
  1317. 0xec80, // 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 1
  1318. 0xc800, // 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1
  1319. 0xffec, // 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
  1320. 0xfe80, // 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1
  1321. 0xe800, // 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1
  1322. 0xffe8, // 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
  1323. 0xff00, // 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1
  1324. 0xfff0, // 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
  1325. 0xf000, // 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1
  1326. 0xf710, // 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1
  1327. 0x008e, // 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0
  1328. 0x7100, // 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0
  1329. 0x08ce, // 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0
  1330. 0x008c, // 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0
  1331. 0x7310, // 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0
  1332. 0x3100, // 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0
  1333. 0x8cce, // 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1
  1334. 0x088c, // 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0
  1335. 0x3110, // 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0
  1336. 0x6666, // 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0
  1337. 0x366c, // 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0
  1338. 0x17e8, // 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0
  1339. 0x0ff0, // 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0
  1340. 0x718e, // 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0
  1341. 0x399c, // 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0
  1342. 0xaaaa, // 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1
  1343. 0xf0f0, // 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1
  1344. 0x5a5a, // 0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0
  1345. 0x33cc, // 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0
  1346. 0x3c3c, // 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0
  1347. 0x55aa, // 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0
  1348. 0x9696, // 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 1
  1349. 0xa55a, // 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1
  1350. 0x73ce, // 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0
  1351. 0x13c8, // 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0
  1352. 0x324c, // 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0
  1353. 0x3bdc, // 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0
  1354. 0x6996, // 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0
  1355. 0xc33c, // 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1
  1356. 0x9966, // 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1
  1357. 0x0660, // 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0
  1358. 0x0272, // 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0
  1359. 0x04e4, // 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0
  1360. 0x4e40, // 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0
  1361. 0x2720, // 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0
  1362. 0xc936, // 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1
  1363. 0x936c, // 0, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 1
  1364. 0x39c6, // 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0
  1365. 0x639c, // 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0
  1366. 0x9336, // 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1
  1367. 0x9cc6, // 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1
  1368. 0x817e, // 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1
  1369. 0xe718, // 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1
  1370. 0xccf0, // 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1
  1371. 0x0fcc, // 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0
  1372. 0x7744, // 0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0
  1373. 0xee22, // 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1
  1374. };
  1375. static const uint32_t s_bptcP3[] =
  1376. { // 76543210 0000 1111 2222 3333 4444 5555 6666 7777
  1377. 0xaa685050, // 0, 0, 1, 1, 0, 0, 1, 1, 0, 2, 2, 1, 2, 2, 2, 2
  1378. 0x6a5a5040, // 0, 0, 0, 1, 0, 0, 1, 1, 2, 2, 1, 1, 2, 2, 2, 1
  1379. 0x5a5a4200, // 0, 0, 0, 0, 2, 0, 0, 1, 2, 2, 1, 1, 2, 2, 1, 1
  1380. 0x5450a0a8, // 0, 2, 2, 2, 0, 0, 2, 2, 0, 0, 1, 1, 0, 1, 1, 1
  1381. 0xa5a50000, // 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 2, 1, 1, 2, 2
  1382. 0xa0a05050, // 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 2, 2, 0, 0, 2, 2
  1383. 0x5555a0a0, // 0, 0, 2, 2, 0, 0, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1
  1384. 0x5a5a5050, // 0, 0, 1, 1, 0, 0, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1
  1385. 0xaa550000, // 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2
  1386. 0xaa555500, // 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2
  1387. 0xaaaa5500, // 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2
  1388. 0x90909090, // 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2
  1389. 0x94949494, // 0, 1, 1, 2, 0, 1, 1, 2, 0, 1, 1, 2, 0, 1, 1, 2
  1390. 0xa4a4a4a4, // 0, 1, 2, 2, 0, 1, 2, 2, 0, 1, 2, 2, 0, 1, 2, 2
  1391. 0xa9a59450, // 0, 0, 1, 1, 0, 1, 1, 2, 1, 1, 2, 2, 1, 2, 2, 2
  1392. 0x2a0a4250, // 0, 0, 1, 1, 2, 0, 0, 1, 2, 2, 0, 0, 2, 2, 2, 0
  1393. 0xa5945040, // 0, 0, 0, 1, 0, 0, 1, 1, 0, 1, 1, 2, 1, 1, 2, 2
  1394. 0x0a425054, // 0, 1, 1, 1, 0, 0, 1, 1, 2, 0, 0, 1, 2, 2, 0, 0
  1395. 0xa5a5a500, // 0, 0, 0, 0, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2
  1396. 0x55a0a0a0, // 0, 0, 2, 2, 0, 0, 2, 2, 0, 0, 2, 2, 1, 1, 1, 1
  1397. 0xa8a85454, // 0, 1, 1, 1, 0, 1, 1, 1, 0, 2, 2, 2, 0, 2, 2, 2
  1398. 0x6a6a4040, // 0, 0, 0, 1, 0, 0, 0, 1, 2, 2, 2, 1, 2, 2, 2, 1
  1399. 0xa4a45000, // 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 2, 2, 0, 1, 2, 2
  1400. 0x1a1a0500, // 0, 0, 0, 0, 1, 1, 0, 0, 2, 2, 1, 0, 2, 2, 1, 0
  1401. 0x0050a4a4, // 0, 1, 2, 2, 0, 1, 2, 2, 0, 0, 1, 1, 0, 0, 0, 0
  1402. 0xaaa59090, // 0, 0, 1, 2, 0, 0, 1, 2, 1, 1, 2, 2, 2, 2, 2, 2
  1403. 0x14696914, // 0, 1, 1, 0, 1, 2, 2, 1, 1, 2, 2, 1, 0, 1, 1, 0
  1404. 0x69691400, // 0, 0, 0, 0, 0, 1, 1, 0, 1, 2, 2, 1, 1, 2, 2, 1
  1405. 0xa08585a0, // 0, 0, 2, 2, 1, 1, 0, 2, 1, 1, 0, 2, 0, 0, 2, 2
  1406. 0xaa821414, // 0, 1, 1, 0, 0, 1, 1, 0, 2, 0, 0, 2, 2, 2, 2, 2
  1407. 0x50a4a450, // 0, 0, 1, 1, 0, 1, 2, 2, 0, 1, 2, 2, 0, 0, 1, 1
  1408. 0x6a5a0200, // 0, 0, 0, 0, 2, 0, 0, 0, 2, 2, 1, 1, 2, 2, 2, 1
  1409. 0xa9a58000, // 0, 0, 0, 0, 0, 0, 0, 2, 1, 1, 2, 2, 1, 2, 2, 2
  1410. 0x5090a0a8, // 0, 2, 2, 2, 0, 0, 2, 2, 0, 0, 1, 2, 0, 0, 1, 1
  1411. 0xa8a09050, // 0, 0, 1, 1, 0, 0, 1, 2, 0, 0, 2, 2, 0, 2, 2, 2
  1412. 0x24242424, // 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0, 0, 1, 2, 0
  1413. 0x00aa5500, // 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 0, 0, 0, 0
  1414. 0x24924924, // 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0
  1415. 0x24499224, // 0, 1, 2, 0, 2, 0, 1, 2, 1, 2, 0, 1, 0, 1, 2, 0
  1416. 0x50a50a50, // 0, 0, 1, 1, 2, 2, 0, 0, 1, 1, 2, 2, 0, 0, 1, 1
  1417. 0x500aa550, // 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 0, 0, 0, 0, 1, 1
  1418. 0xaaaa4444, // 0, 1, 0, 1, 0, 1, 0, 1, 2, 2, 2, 2, 2, 2, 2, 2
  1419. 0x66660000, // 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 2, 1, 2, 1, 2, 1
  1420. 0xa5a0a5a0, // 0, 0, 2, 2, 1, 1, 2, 2, 0, 0, 2, 2, 1, 1, 2, 2
  1421. 0x50a050a0, // 0, 0, 2, 2, 0, 0, 1, 1, 0, 0, 2, 2, 0, 0, 1, 1
  1422. 0x69286928, // 0, 2, 2, 0, 1, 2, 2, 1, 0, 2, 2, 0, 1, 2, 2, 1
  1423. 0x44aaaa44, // 0, 1, 0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 0, 1, 0, 1
  1424. 0x66666600, // 0, 0, 0, 0, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1
  1425. 0xaa444444, // 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 2, 2, 2, 2
  1426. 0x54a854a8, // 0, 2, 2, 2, 0, 1, 1, 1, 0, 2, 2, 2, 0, 1, 1, 1
  1427. 0x95809580, // 0, 0, 0, 2, 1, 1, 1, 2, 0, 0, 0, 2, 1, 1, 1, 2
  1428. 0x96969600, // 0, 0, 0, 0, 2, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 2
  1429. 0xa85454a8, // 0, 2, 2, 2, 0, 1, 1, 1, 0, 1, 1, 1, 0, 2, 2, 2
  1430. 0x80959580, // 0, 0, 0, 2, 1, 1, 1, 2, 1, 1, 1, 2, 0, 0, 0, 2
  1431. 0xaa141414, // 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 2, 2, 2, 2
  1432. 0x96960000, // 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 1, 2, 2, 1, 1, 2
  1433. 0xaaaa1414, // 0, 1, 1, 0, 0, 1, 1, 0, 2, 2, 2, 2, 2, 2, 2, 2
  1434. 0xa05050a0, // 0, 0, 2, 2, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 2, 2
  1435. 0xa0a5a5a0, // 0, 0, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2, 0, 0, 2, 2
  1436. 0x96000000, // 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 1, 2
  1437. 0x40804080, // 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 1
  1438. 0xa9a8a9a8, // 0, 2, 2, 2, 1, 2, 2, 2, 0, 2, 2, 2, 1, 2, 2, 2
  1439. 0xaaaaaa44, // 0, 1, 0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2
  1440. 0x2a4a5254, // 0, 1, 1, 1, 2, 0, 1, 1, 2, 2, 0, 1, 2, 2, 2, 0
  1441. };
  1442. static const uint8_t s_bptcA2[] =
  1443. {
  1444. 15, 15, 15, 15, 15, 15, 15, 15,
  1445. 15, 15, 15, 15, 15, 15, 15, 15,
  1446. 15, 2, 8, 2, 2, 8, 8, 15,
  1447. 2, 8, 2, 2, 8, 8, 2, 2,
  1448. 15, 15, 6, 8, 2, 8, 15, 15,
  1449. 2, 8, 2, 2, 2, 15, 15, 6,
  1450. 6, 2, 6, 8, 15, 15, 2, 2,
  1451. 15, 15, 15, 15, 15, 2, 2, 15,
  1452. };
  1453. static const uint8_t s_bptcA3[2][64] =
  1454. {
  1455. {
  1456. 3, 3, 15, 15, 8, 3, 15, 15,
  1457. 8, 8, 6, 6, 6, 5, 3, 3,
  1458. 3, 3, 8, 15, 3, 3, 6, 10,
  1459. 5, 8, 8, 6, 8, 5, 15, 15,
  1460. 8, 15, 3, 5, 6, 10, 8, 15,
  1461. 15, 3, 15, 5, 15, 15, 15, 15,
  1462. 3, 15, 5, 5, 5, 8, 5, 10,
  1463. 5, 10, 8, 13, 15, 12, 3, 3,
  1464. },
  1465. {
  1466. 15, 8, 8, 3, 15, 15, 3, 8,
  1467. 15, 15, 15, 15, 15, 15, 15, 8,
  1468. 15, 8, 15, 3, 15, 8, 15, 8,
  1469. 3, 15, 6, 10, 15, 15, 10, 8,
  1470. 15, 3, 15, 10, 10, 8, 9, 10,
  1471. 6, 15, 8, 15, 3, 6, 6, 8,
  1472. 15, 3, 15, 15, 15, 15, 15, 15,
  1473. 15, 15, 15, 15, 3, 15, 15, 8,
  1474. },
  1475. };
  1476. static const uint8_t s_bptcFactors[3][16] =
  1477. {
  1478. { 0, 21, 43, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
  1479. { 0, 9, 18, 27, 37, 46, 55, 64, 0, 0, 0, 0, 0, 0, 0, 0 },
  1480. { 0, 4, 9, 13, 17, 21, 26, 30, 34, 38, 43, 47, 51, 55, 60, 64 },
  1481. };
  1482. struct BitReader
  1483. {
  1484. BitReader(const uint8_t* _data, uint16_t _bitPos = 0)
  1485. : m_data(_data)
  1486. , m_bitPos(_bitPos)
  1487. {
  1488. }
  1489. uint16_t read(uint8_t _numBits)
  1490. {
  1491. const uint16_t pos = m_bitPos / 8;
  1492. const uint16_t shift = m_bitPos & 7;
  1493. uint32_t data = 0;
  1494. bx::memCopy(&data, &m_data[pos], bx::min(4, 16-pos) );
  1495. m_bitPos += _numBits;
  1496. return uint16_t( (data >> shift) & ( (1 << _numBits)-1) );
  1497. }
  1498. uint16_t peek(uint16_t _offset, uint8_t _numBits)
  1499. {
  1500. const uint16_t bitPos = m_bitPos + _offset;
  1501. const uint16_t shift = bitPos & 7;
  1502. uint16_t pos = bitPos / 8;
  1503. uint32_t data = 0;
  1504. bx::memCopy(&data, &m_data[pos], bx::min(4, 16-pos) );
  1505. return uint8_t( (data >> shift) & ( (1 << _numBits)-1) );
  1506. }
  1507. const uint8_t* m_data;
  1508. uint16_t m_bitPos;
  1509. };
  1510. uint16_t bc6hUnquantize(uint16_t _value, bool _signed, uint8_t _endpointBits)
  1511. {
  1512. const uint16_t maxValue = 1<<(_endpointBits-1);
  1513. if (_signed)
  1514. {
  1515. if (_endpointBits >= 16)
  1516. {
  1517. return _value;
  1518. }
  1519. const bool sign = !!(_value & 0x8000);
  1520. _value &= 0x7fff;
  1521. uint16_t unq;
  1522. if (0 == _value)
  1523. {
  1524. unq = 0;
  1525. }
  1526. else if (_value >= maxValue-1)
  1527. {
  1528. unq = 0x7fff;
  1529. }
  1530. else
  1531. {
  1532. unq = ( (_value<<15) + 0x4000) >> (_endpointBits-1);
  1533. }
  1534. return sign ? -unq : unq;
  1535. }
  1536. if (_endpointBits >= 15)
  1537. {
  1538. return _value;
  1539. }
  1540. if (0 == _value)
  1541. {
  1542. return 0;
  1543. }
  1544. if (_value == maxValue)
  1545. {
  1546. return UINT16_MAX;
  1547. }
  1548. return ( (_value<<15) + 0x4000) >> (_endpointBits-1);
  1549. }
  1550. uint16_t bc6hUnquantizeFinal(uint16_t _value, bool _signed)
  1551. {
  1552. if (_signed)
  1553. {
  1554. const uint16_t sign = _value & 0x8000;
  1555. _value &= 0x7fff;
  1556. return ( (_value * 31) >> 5) | sign;
  1557. }
  1558. return (_value * 31) >> 6;
  1559. }
  1560. uint16_t signExtend(uint16_t _value, uint8_t _numBits)
  1561. {
  1562. const uint16_t mask = 1 << (_numBits - 1);
  1563. const uint16_t result = (_value ^ mask) - mask;
  1564. return result;
  1565. }
  1566. struct Bc6hModeInfo
  1567. {
  1568. uint8_t transformed;
  1569. uint8_t partitionBits;
  1570. uint8_t endpointBits;
  1571. uint8_t deltaBits[3];
  1572. };
  1573. static const Bc6hModeInfo s_bc6hModeInfo[] =
  1574. { // +--------------------------- transformed
  1575. // | +------------------------ partition bits
  1576. // | | +--------------------- endpoint bits
  1577. // | | | +-------------- delta bits
  1578. { 1, 5, 10, { 5, 5, 5 } }, // 00 2-bits
  1579. { 1, 5, 7, { 6, 6, 6 } }, // 01
  1580. { 1, 5, 11, { 5, 4, 4 } }, // 00010 5-bits
  1581. { 0, 0, 10, { 10, 10, 10 } }, // 00011
  1582. { 0, 0, 0, { 0, 0, 0 } }, // -
  1583. { 0, 0, 0, { 0, 0, 0 } }, // -
  1584. { 1, 5, 11, { 4, 5, 4 } }, // 00110
  1585. { 1, 0, 11, { 9, 9, 9 } }, // 00010
  1586. { 0, 0, 0, { 0, 0, 0 } }, // -
  1587. { 0, 0, 0, { 0, 0, 0 } }, // -
  1588. { 1, 5, 11, { 4, 4, 5 } }, // 00010
  1589. { 1, 0, 12, { 8, 8, 8 } }, // 00010
  1590. { 0, 0, 0, { 0, 0, 0 } }, // -
  1591. { 0, 0, 0, { 0, 0, 0 } }, // -
  1592. { 1, 5, 9, { 5, 5, 5 } }, // 00010
  1593. { 1, 0, 16, { 4, 4, 4 } }, // 00010
  1594. { 0, 0, 0, { 0, 0, 0 } }, // -
  1595. { 0, 0, 0, { 0, 0, 0 } }, // -
  1596. { 1, 5, 8, { 6, 5, 5 } }, // 00010
  1597. { 0, 0, 0, { 0, 0, 0 } }, // -
  1598. { 0, 0, 0, { 0, 0, 0 } }, // -
  1599. { 0, 0, 0, { 0, 0, 0 } }, // -
  1600. { 1, 5, 8, { 5, 6, 5 } }, // 00010
  1601. { 0, 0, 0, { 0, 0, 0 } }, // -
  1602. { 0, 0, 0, { 0, 0, 0 } }, // -
  1603. { 0, 0, 0, { 0, 0, 0 } }, // -
  1604. { 1, 5, 8, { 5, 5, 6 } }, // 00010
  1605. { 0, 0, 0, { 0, 0, 0 } }, // -
  1606. { 0, 0, 0, { 0, 0, 0 } }, // -
  1607. { 0, 0, 0, { 0, 0, 0 } }, // -
  1608. { 0, 5, 6, { 6, 6, 6 } }, // 00010
  1609. { 0, 0, 0, { 0, 0, 0 } }, // -
  1610. };
  1611. void decodeBlockBc6h(uint16_t _dst[16*3], const uint8_t _src[16], bool _signed)
  1612. {
  1613. BitReader bit(_src);
  1614. uint8_t mode = uint8_t(bit.read(2) );
  1615. if (mode & 2)
  1616. {
  1617. // 5-bit mode
  1618. mode |= bit.read(3) << 2;
  1619. }
  1620. const Bc6hModeInfo& mi = s_bc6hModeInfo[mode];
  1621. if (0 == mi.endpointBits)
  1622. {
  1623. bx::memSet(_dst, 0, 16*3*2);
  1624. return;
  1625. }
  1626. uint16_t epR[4] = { /* rw, rx, ry, rz */ };
  1627. uint16_t epG[4] = { /* gw, gx, gy, gz */ };
  1628. uint16_t epB[4] = { /* bw, bx, by, bz */ };
  1629. switch (mode)
  1630. {
  1631. case 0:
  1632. epG[2] |= bit.read( 1) << 4;
  1633. epB[2] |= bit.read( 1) << 4;
  1634. epB[3] |= bit.read( 1) << 4;
  1635. epR[0] |= bit.read(10) << 0;
  1636. epG[0] |= bit.read(10) << 0;
  1637. epB[0] |= bit.read(10) << 0;
  1638. epR[1] |= bit.read( 5) << 0;
  1639. epG[3] |= bit.read( 1) << 4;
  1640. epG[2] |= bit.read( 4) << 0;
  1641. epG[1] |= bit.read( 5) << 0;
  1642. epB[3] |= bit.read( 1) << 0;
  1643. epG[3] |= bit.read( 4) << 0;
  1644. epB[1] |= bit.read( 5) << 0;
  1645. epB[3] |= bit.read( 1) << 1;
  1646. epB[2] |= bit.read( 4) << 0;
  1647. epR[2] |= bit.read( 5) << 0;
  1648. epB[3] |= bit.read( 1) << 2;
  1649. epR[3] |= bit.read( 5) << 0;
  1650. epB[3] |= bit.read( 1) << 3;
  1651. break;
  1652. case 1:
  1653. epG[2] |= bit.read( 1) << 5;
  1654. epG[3] |= bit.read( 1) << 4;
  1655. epG[3] |= bit.read( 1) << 5;
  1656. epR[0] |= bit.read( 7) << 0;
  1657. epB[3] |= bit.read( 1) << 0;
  1658. epB[3] |= bit.read( 1) << 1;
  1659. epB[2] |= bit.read( 1) << 4;
  1660. epG[0] |= bit.read( 7) << 0;
  1661. epB[2] |= bit.read( 1) << 5;
  1662. epB[3] |= bit.read( 1) << 2;
  1663. epG[2] |= bit.read( 1) << 4;
  1664. epB[0] |= bit.read( 7) << 0;
  1665. epB[3] |= bit.read( 1) << 3;
  1666. epB[3] |= bit.read( 1) << 5;
  1667. epB[3] |= bit.read( 1) << 4;
  1668. epR[1] |= bit.read( 6) << 0;
  1669. epG[2] |= bit.read( 4) << 0;
  1670. epG[1] |= bit.read( 6) << 0;
  1671. epG[3] |= bit.read( 4) << 0;
  1672. epB[1] |= bit.read( 6) << 0;
  1673. epB[2] |= bit.read( 4) << 0;
  1674. epR[2] |= bit.read( 6) << 0;
  1675. epR[3] |= bit.read( 6) << 0;
  1676. break;
  1677. case 2:
  1678. epR[0] |= bit.read(10) << 0;
  1679. epG[0] |= bit.read(10) << 0;
  1680. epB[0] |= bit.read(10) << 0;
  1681. epR[1] |= bit.read( 5) << 0;
  1682. epR[0] |= bit.read( 1) << 10;
  1683. epG[2] |= bit.read( 4) << 0;
  1684. epG[1] |= bit.read( 4) << 0;
  1685. epG[0] |= bit.read( 1) << 10;
  1686. epB[3] |= bit.read( 1) << 0;
  1687. epG[3] |= bit.read( 4) << 0;
  1688. epB[1] |= bit.read( 4) << 0;
  1689. epB[0] |= bit.read( 1) << 10;
  1690. epB[3] |= bit.read( 1) << 1;
  1691. epB[2] |= bit.read( 4) << 0;
  1692. epR[2] |= bit.read( 5) << 0;
  1693. epB[3] |= bit.read( 1) << 2;
  1694. epR[3] |= bit.read( 5) << 0;
  1695. epB[3] |= bit.read( 1) << 3;
  1696. break;
  1697. case 3:
  1698. epR[0] |= bit.read(10) << 0;
  1699. epG[0] |= bit.read(10) << 0;
  1700. epB[0] |= bit.read(10) << 0;
  1701. epR[1] |= bit.read(10) << 0;
  1702. epG[1] |= bit.read(10) << 0;
  1703. epB[1] |= bit.read(10) << 0;
  1704. break;
  1705. case 6:
  1706. epR[0] |= bit.read(10) << 0;
  1707. epG[0] |= bit.read(10) << 0;
  1708. epB[0] |= bit.read(10) << 0;
  1709. epR[1] |= bit.read( 4) << 0;
  1710. epR[0] |= bit.read( 1) << 10;
  1711. epG[3] |= bit.read( 1) << 4;
  1712. epG[2] |= bit.read( 4) << 0;
  1713. epG[1] |= bit.read( 5) << 0;
  1714. epG[0] |= bit.read( 1) << 10;
  1715. epG[3] |= bit.read( 4) << 0;
  1716. epB[1] |= bit.read( 4) << 0;
  1717. epB[0] |= bit.read( 1) << 10;
  1718. epB[3] |= bit.read( 1) << 1;
  1719. epB[2] |= bit.read( 4) << 0;
  1720. epR[2] |= bit.read( 4) << 0;
  1721. epB[3] |= bit.read( 1) << 0;
  1722. epB[3] |= bit.read( 1) << 2;
  1723. epR[3] |= bit.read( 4) << 0;
  1724. epG[2] |= bit.read( 1) << 4;
  1725. epB[3] |= bit.read( 1) << 3;
  1726. break;
  1727. case 7:
  1728. epR[0] |= bit.read(10) << 0;
  1729. epG[0] |= bit.read(10) << 0;
  1730. epB[0] |= bit.read(10) << 0;
  1731. epR[1] |= bit.read( 9) << 0;
  1732. epR[0] |= bit.read( 1) << 10;
  1733. epG[1] |= bit.read( 9) << 0;
  1734. epG[0] |= bit.read( 1) << 10;
  1735. epB[1] |= bit.read( 9) << 0;
  1736. epB[0] |= bit.read( 1) << 10;
  1737. break;
  1738. case 10:
  1739. epR[0] |= bit.read(10) << 0;
  1740. epG[0] |= bit.read(10) << 0;
  1741. epB[0] |= bit.read(10) << 0;
  1742. epR[1] |= bit.read( 4) << 0;
  1743. epR[0] |= bit.read( 1) << 10;
  1744. epB[2] |= bit.read( 1) << 4;
  1745. epG[2] |= bit.read( 4) << 0;
  1746. epG[1] |= bit.read( 4) << 0;
  1747. epG[0] |= bit.read( 1) << 10;
  1748. epB[3] |= bit.read( 1) << 0;
  1749. epG[3] |= bit.read( 4) << 0;
  1750. epB[1] |= bit.read( 5) << 0;
  1751. epB[0] |= bit.read( 1) << 10;
  1752. epB[2] |= bit.read( 4) << 0;
  1753. epR[2] |= bit.read( 4) << 0;
  1754. epB[3] |= bit.read( 1) << 1;
  1755. epB[3] |= bit.read( 1) << 2;
  1756. epR[3] |= bit.read( 4) << 0;
  1757. epB[3] |= bit.read( 1) << 4;
  1758. epB[3] |= bit.read( 1) << 3;
  1759. break;
  1760. case 11:
  1761. epR[0] |= bit.read(10) << 0;
  1762. epG[0] |= bit.read(10) << 0;
  1763. epB[0] |= bit.read(10) << 0;
  1764. epR[1] |= bit.read( 8) << 0;
  1765. epR[0] |= bit.read( 1) << 11;
  1766. epR[0] |= bit.read( 1) << 10;
  1767. epG[1] |= bit.read( 8) << 0;
  1768. epG[0] |= bit.read( 1) << 11;
  1769. epG[0] |= bit.read( 1) << 10;
  1770. epB[1] |= bit.read( 8) << 0;
  1771. epB[0] |= bit.read( 1) << 11;
  1772. epB[0] |= bit.read( 1) << 10;
  1773. break;
  1774. case 14:
  1775. epR[0] |= bit.read( 9) << 0;
  1776. epB[2] |= bit.read( 1) << 4;
  1777. epG[0] |= bit.read( 9) << 0;
  1778. epG[2] |= bit.read( 1) << 4;
  1779. epB[0] |= bit.read( 9) << 0;
  1780. epB[3] |= bit.read( 1) << 4;
  1781. epR[1] |= bit.read( 5) << 0;
  1782. epG[3] |= bit.read( 1) << 4;
  1783. epG[2] |= bit.read( 4) << 0;
  1784. epG[1] |= bit.read( 5) << 0;
  1785. epB[3] |= bit.read( 1) << 0;
  1786. epG[3] |= bit.read( 4) << 0;
  1787. epB[1] |= bit.read( 5) << 0;
  1788. epB[3] |= bit.read( 1) << 1;
  1789. epB[2] |= bit.read( 4) << 0;
  1790. epR[2] |= bit.read( 5) << 0;
  1791. epB[3] |= bit.read( 1) << 2;
  1792. epR[3] |= bit.read( 5) << 0;
  1793. epB[3] |= bit.read( 1) << 3;
  1794. break;
  1795. case 15:
  1796. epR[0] |= bit.read(10) << 0;
  1797. epG[0] |= bit.read(10) << 0;
  1798. epB[0] |= bit.read(10) << 0;
  1799. epR[1] |= bit.read( 4) << 0;
  1800. epR[0] |= bit.read( 1) << 15;
  1801. epR[0] |= bit.read( 1) << 14;
  1802. epR[0] |= bit.read( 1) << 13;
  1803. epR[0] |= bit.read( 1) << 12;
  1804. epR[0] |= bit.read( 1) << 11;
  1805. epR[0] |= bit.read( 1) << 10;
  1806. epG[1] |= bit.read( 4) << 0;
  1807. epG[0] |= bit.read( 1) << 15;
  1808. epG[0] |= bit.read( 1) << 14;
  1809. epG[0] |= bit.read( 1) << 13;
  1810. epG[0] |= bit.read( 1) << 12;
  1811. epG[0] |= bit.read( 1) << 11;
  1812. epG[0] |= bit.read( 1) << 10;
  1813. epB[1] |= bit.read( 4) << 0;
  1814. epB[0] |= bit.read( 1) << 15;
  1815. epB[0] |= bit.read( 1) << 14;
  1816. epB[0] |= bit.read( 1) << 13;
  1817. epB[0] |= bit.read( 1) << 12;
  1818. epB[0] |= bit.read( 1) << 11;
  1819. epB[0] |= bit.read( 1) << 10;
  1820. break;
  1821. case 18:
  1822. epR[0] |= bit.read( 8) << 0;
  1823. epG[3] |= bit.read( 1) << 4;
  1824. epB[2] |= bit.read( 1) << 4;
  1825. epG[0] |= bit.read( 8) << 0;
  1826. epB[3] |= bit.read( 1) << 2;
  1827. epG[2] |= bit.read( 1) << 4;
  1828. epB[0] |= bit.read( 8) << 0;
  1829. epB[3] |= bit.read( 1) << 3;
  1830. epB[3] |= bit.read( 1) << 4;
  1831. epR[1] |= bit.read( 6) << 0;
  1832. epG[2] |= bit.read( 4) << 0;
  1833. epG[1] |= bit.read( 5) << 0;
  1834. epB[3] |= bit.read( 1) << 0;
  1835. epG[3] |= bit.read( 4) << 0;
  1836. epB[1] |= bit.read( 5) << 0;
  1837. epB[3] |= bit.read( 1) << 1;
  1838. epB[2] |= bit.read( 4) << 0;
  1839. epR[2] |= bit.read( 6) << 0;
  1840. epR[3] |= bit.read( 6) << 0;
  1841. break;
  1842. case 22:
  1843. epR[0] |= bit.read( 8) << 0;
  1844. epB[3] |= bit.read( 1) << 0;
  1845. epB[2] |= bit.read( 1) << 4;
  1846. epG[0] |= bit.read( 8) << 0;
  1847. epG[2] |= bit.read( 1) << 5;
  1848. epG[2] |= bit.read( 1) << 4;
  1849. epB[0] |= bit.read( 8) << 0;
  1850. epG[3] |= bit.read( 1) << 5;
  1851. epB[3] |= bit.read( 1) << 4;
  1852. epR[1] |= bit.read( 5) << 0;
  1853. epG[3] |= bit.read( 1) << 4;
  1854. epG[2] |= bit.read( 4) << 0;
  1855. epG[1] |= bit.read( 6) << 0;
  1856. epG[3] |= bit.read( 4) << 0;
  1857. epB[1] |= bit.read( 5) << 0;
  1858. epB[3] |= bit.read( 1) << 1;
  1859. epB[2] |= bit.read( 4) << 0;
  1860. epR[2] |= bit.read( 5) << 0;
  1861. epB[3] |= bit.read( 1) << 2;
  1862. epR[3] |= bit.read( 5) << 0;
  1863. epB[3] |= bit.read( 1) << 3;
  1864. break;
  1865. case 26:
  1866. epR[0] |= bit.read( 8) << 0;
  1867. epB[3] |= bit.read( 1) << 1;
  1868. epB[2] |= bit.read( 1) << 4;
  1869. epG[0] |= bit.read( 8) << 0;
  1870. epB[2] |= bit.read( 1) << 5;
  1871. epG[2] |= bit.read( 1) << 4;
  1872. epB[0] |= bit.read( 8) << 0;
  1873. epB[3] |= bit.read( 1) << 5;
  1874. epB[3] |= bit.read( 1) << 4;
  1875. epR[1] |= bit.read( 5) << 0;
  1876. epG[3] |= bit.read( 1) << 4;
  1877. epG[2] |= bit.read( 4) << 0;
  1878. epG[1] |= bit.read( 5) << 0;
  1879. epB[3] |= bit.read( 1) << 0;
  1880. epG[3] |= bit.read( 4) << 0;
  1881. epB[1] |= bit.read( 6) << 0;
  1882. epB[2] |= bit.read( 4) << 0;
  1883. epR[2] |= bit.read( 5) << 0;
  1884. epB[3] |= bit.read( 1) << 2;
  1885. epR[3] |= bit.read( 5) << 0;
  1886. epB[3] |= bit.read( 1) << 3;
  1887. break;
  1888. case 30:
  1889. epR[0] |= bit.read( 6) << 0;
  1890. epG[3] |= bit.read( 1) << 4;
  1891. epB[3] |= bit.read( 1) << 0;
  1892. epB[3] |= bit.read( 1) << 1;
  1893. epB[2] |= bit.read( 1) << 4;
  1894. epG[0] |= bit.read( 6) << 0;
  1895. epG[2] |= bit.read( 1) << 5;
  1896. epB[2] |= bit.read( 1) << 5;
  1897. epB[3] |= bit.read( 1) << 2;
  1898. epG[2] |= bit.read( 1) << 4;
  1899. epB[0] |= bit.read( 6) << 0;
  1900. epG[3] |= bit.read( 1) << 5;
  1901. epB[3] |= bit.read( 1) << 3;
  1902. epB[3] |= bit.read( 1) << 5;
  1903. epB[3] |= bit.read( 1) << 4;
  1904. epR[1] |= bit.read( 6) << 0;
  1905. epG[2] |= bit.read( 4) << 0;
  1906. epG[1] |= bit.read( 6) << 0;
  1907. epG[3] |= bit.read( 4) << 0;
  1908. epB[1] |= bit.read( 6) << 0;
  1909. epB[2] |= bit.read( 4) << 0;
  1910. epR[2] |= bit.read( 6) << 0;
  1911. epR[3] |= bit.read( 6) << 0;
  1912. break;
  1913. default:
  1914. break;
  1915. }
  1916. if (_signed)
  1917. {
  1918. epR[0] = signExtend(epR[0], mi.endpointBits);
  1919. epG[0] = signExtend(epG[0], mi.endpointBits);
  1920. epB[0] = signExtend(epB[0], mi.endpointBits);
  1921. }
  1922. const uint8_t numSubsets = !!mi.partitionBits + 1;
  1923. for (uint8_t ii = 1, num = numSubsets*2; ii < num; ++ii)
  1924. {
  1925. if (_signed
  1926. || mi.transformed)
  1927. {
  1928. epR[ii] = signExtend(epR[ii], mi.deltaBits[0]);
  1929. epG[ii] = signExtend(epG[ii], mi.deltaBits[1]);
  1930. epB[ii] = signExtend(epB[ii], mi.deltaBits[2]);
  1931. }
  1932. if (mi.transformed)
  1933. {
  1934. const uint16_t mask = (1<<mi.endpointBits) - 1;
  1935. epR[ii] = (epR[ii] + epR[0]) & mask;
  1936. epG[ii] = (epG[ii] + epG[0]) & mask;
  1937. epB[ii] = (epB[ii] + epB[0]) & mask;
  1938. if (_signed)
  1939. {
  1940. epR[ii] = signExtend(epR[ii], mi.endpointBits);
  1941. epG[ii] = signExtend(epG[ii], mi.endpointBits);
  1942. epB[ii] = signExtend(epB[ii], mi.endpointBits);
  1943. }
  1944. }
  1945. }
  1946. for (uint8_t ii = 0, num = numSubsets*2; ii < num; ++ii)
  1947. {
  1948. epR[ii] = bc6hUnquantize(epR[ii], _signed, mi.endpointBits);
  1949. epG[ii] = bc6hUnquantize(epG[ii], _signed, mi.endpointBits);
  1950. epB[ii] = bc6hUnquantize(epB[ii], _signed, mi.endpointBits);
  1951. }
  1952. const uint8_t partitionSetIdx = uint8_t(mi.partitionBits ? bit.read(5) : 0);
  1953. const uint8_t indexBits = mi.partitionBits ? 3 : 4;
  1954. const uint8_t* factors = s_bptcFactors[indexBits-2];
  1955. for (uint8_t yy = 0; yy < 4; ++yy)
  1956. {
  1957. for (uint8_t xx = 0; xx < 4; ++xx)
  1958. {
  1959. const uint8_t idx = yy*4+xx;
  1960. uint8_t subsetIndex = 0;
  1961. uint8_t indexAnchor = 0;
  1962. if (0 != mi.partitionBits)
  1963. {
  1964. subsetIndex = (s_bptcP2[partitionSetIdx] >> idx) & 1;
  1965. indexAnchor = subsetIndex ? s_bptcA2[partitionSetIdx] : 0;
  1966. }
  1967. const uint8_t anchor = idx == indexAnchor;
  1968. const uint8_t num = indexBits - anchor;
  1969. const uint8_t index = (uint8_t)bit.read(num);
  1970. const uint8_t fc = factors[index];
  1971. const uint8_t fca = 64 - fc;
  1972. const uint8_t fcb = fc;
  1973. subsetIndex *= 2;
  1974. uint16_t rr = bc6hUnquantizeFinal( (epR[subsetIndex]*fca + epR[subsetIndex + 1]*fcb + 32) >> 6, _signed);
  1975. uint16_t gg = bc6hUnquantizeFinal( (epG[subsetIndex]*fca + epG[subsetIndex + 1]*fcb + 32) >> 6, _signed);
  1976. uint16_t bb = bc6hUnquantizeFinal( (epB[subsetIndex]*fca + epB[subsetIndex + 1]*fcb + 32) >> 6, _signed);
  1977. uint16_t* rgba = &_dst[idx*3];
  1978. rgba[0] = rr;
  1979. rgba[1] = gg;
  1980. rgba[2] = bb;
  1981. }
  1982. }
  1983. }
  1984. void decodeBlockBc6h(float _dst[16*4], const uint8_t _src[16])
  1985. {
  1986. uint16_t tmp[16*3];
  1987. decodeBlockBc6h(tmp, _src, true);
  1988. for (uint32_t ii = 0; ii < 16; ++ii)
  1989. {
  1990. _dst[ii*4+0] = bx::halfToFloat(tmp[ii*3+0]);
  1991. _dst[ii*4+1] = bx::halfToFloat(tmp[ii*3+1]);
  1992. _dst[ii*4+2] = bx::halfToFloat(tmp[ii*3+2]);
  1993. _dst[ii*4+3] = 1.0f;
  1994. }
  1995. }
  1996. struct Bc7ModeInfo
  1997. {
  1998. uint8_t numSubsets;
  1999. uint8_t partitionBits;
  2000. uint8_t rotationBits;
  2001. uint8_t indexSelectionBits;
  2002. uint8_t colorBits;
  2003. uint8_t alphaBits;
  2004. uint8_t endpointPBits;
  2005. uint8_t sharedPBits;
  2006. uint8_t indexBits[2];
  2007. };
  2008. static const Bc7ModeInfo s_bp7ModeInfo[] =
  2009. { // +---------------------------- num subsets
  2010. // | +------------------------- partition bits
  2011. // | | +---------------------- rotation bits
  2012. // | | | +------------------- index selection bits
  2013. // | | | | +---------------- color bits
  2014. // | | | | | +------------- alpha bits
  2015. // | | | | | | +---------- endpoint P-bits
  2016. // | | | | | | | +------- shared P-bits
  2017. // | | | | | | | | +-- 2x index bits
  2018. { 3, 4, 0, 0, 4, 0, 1, 0, { 3, 0 } }, // 0
  2019. { 2, 6, 0, 0, 6, 0, 0, 1, { 3, 0 } }, // 1
  2020. { 3, 6, 0, 0, 5, 0, 0, 0, { 2, 0 } }, // 2
  2021. { 2, 6, 0, 0, 7, 0, 1, 0, { 2, 0 } }, // 3
  2022. { 1, 0, 2, 1, 5, 6, 0, 0, { 2, 3 } }, // 4
  2023. { 1, 0, 2, 0, 7, 8, 0, 0, { 2, 2 } }, // 5
  2024. { 1, 0, 0, 0, 7, 7, 1, 0, { 4, 0 } }, // 6
  2025. { 2, 6, 0, 0, 5, 5, 1, 0, { 2, 0 } }, // 7
  2026. };
  2027. void decodeBlockBc7(uint8_t _dst[16*4], const uint8_t _src[16])
  2028. {
  2029. BitReader bit(_src);
  2030. uint8_t mode = 0;
  2031. for (; mode < 8 && 0 == bit.read(1); ++mode)
  2032. {
  2033. }
  2034. if (mode == 8)
  2035. {
  2036. bx::memSet(_dst, 0, 16*4);
  2037. return;
  2038. }
  2039. const Bc7ModeInfo& mi = s_bp7ModeInfo[mode];
  2040. const uint8_t modePBits = 0 != mi.endpointPBits
  2041. ? mi.endpointPBits
  2042. : mi.sharedPBits
  2043. ;
  2044. const uint8_t partitionSetIdx = uint8_t(bit.read(mi.partitionBits) );
  2045. const uint8_t rotationMode = uint8_t(bit.read(mi.rotationBits) );
  2046. const uint8_t indexSelectionMode = uint8_t(bit.read(mi.indexSelectionBits) );
  2047. uint8_t epR[6];
  2048. uint8_t epG[6];
  2049. uint8_t epB[6];
  2050. uint8_t epA[6];
  2051. for (uint8_t ii = 0; ii < mi.numSubsets; ++ii)
  2052. {
  2053. epR[ii*2+0] = uint8_t(bit.read(mi.colorBits) << modePBits);
  2054. epR[ii*2+1] = uint8_t(bit.read(mi.colorBits) << modePBits);
  2055. }
  2056. for (uint8_t ii = 0; ii < mi.numSubsets; ++ii)
  2057. {
  2058. epG[ii*2+0] = uint8_t(bit.read(mi.colorBits) << modePBits);
  2059. epG[ii*2+1] = uint8_t(bit.read(mi.colorBits) << modePBits);
  2060. }
  2061. for (uint8_t ii = 0; ii < mi.numSubsets; ++ii)
  2062. {
  2063. epB[ii*2+0] = uint8_t(bit.read(mi.colorBits) << modePBits);
  2064. epB[ii*2+1] = uint8_t(bit.read(mi.colorBits) << modePBits);
  2065. }
  2066. if (mi.alphaBits)
  2067. {
  2068. for (uint8_t ii = 0; ii < mi.numSubsets; ++ii)
  2069. {
  2070. epA[ii*2+0] = uint8_t(bit.read(mi.alphaBits) << modePBits);
  2071. epA[ii*2+1] = uint8_t(bit.read(mi.alphaBits) << modePBits);
  2072. }
  2073. }
  2074. else
  2075. {
  2076. bx::memSet(epA, 0xff, 6);
  2077. }
  2078. if (0 != modePBits)
  2079. {
  2080. for (uint8_t ii = 0; ii < mi.numSubsets; ++ii)
  2081. {
  2082. const uint8_t pda = uint8_t( bit.read(modePBits) );
  2083. const uint8_t pdb = uint8_t(0 == mi.sharedPBits ? bit.read(modePBits) : pda);
  2084. epR[ii*2+0] |= pda;
  2085. epR[ii*2+1] |= pdb;
  2086. epG[ii*2+0] |= pda;
  2087. epG[ii*2+1] |= pdb;
  2088. epB[ii*2+0] |= pda;
  2089. epB[ii*2+1] |= pdb;
  2090. epA[ii*2+0] |= pda;
  2091. epA[ii*2+1] |= pdb;
  2092. }
  2093. }
  2094. const uint8_t colorBits = mi.colorBits + modePBits;
  2095. for (uint8_t ii = 0; ii < mi.numSubsets; ++ii)
  2096. {
  2097. epR[ii*2+0] = bitRangeConvert(epR[ii*2+0], colorBits, 8);
  2098. epR[ii*2+1] = bitRangeConvert(epR[ii*2+1], colorBits, 8);
  2099. epG[ii*2+0] = bitRangeConvert(epG[ii*2+0], colorBits, 8);
  2100. epG[ii*2+1] = bitRangeConvert(epG[ii*2+1], colorBits, 8);
  2101. epB[ii*2+0] = bitRangeConvert(epB[ii*2+0], colorBits, 8);
  2102. epB[ii*2+1] = bitRangeConvert(epB[ii*2+1], colorBits, 8);
  2103. }
  2104. if (mi.alphaBits)
  2105. {
  2106. const uint8_t alphaBits = mi.alphaBits + modePBits;
  2107. for (uint8_t ii = 0; ii < mi.numSubsets; ++ii)
  2108. {
  2109. epA[ii*2+0] = bitRangeConvert(epA[ii*2+0], alphaBits, 8);
  2110. epA[ii*2+1] = bitRangeConvert(epA[ii*2+1], alphaBits, 8);
  2111. }
  2112. }
  2113. const bool hasIndexBits1 = 0 != mi.indexBits[1];
  2114. const uint8_t* factors[] =
  2115. {
  2116. s_bptcFactors[mi.indexBits[0]-2],
  2117. hasIndexBits1 ? s_bptcFactors[mi.indexBits[1]-2] : factors[0],
  2118. };
  2119. uint16_t offset[2] =
  2120. {
  2121. 0,
  2122. uint16_t(mi.numSubsets*(16*mi.indexBits[0]-1) ),
  2123. };
  2124. for (uint8_t yy = 0; yy < 4; ++yy)
  2125. {
  2126. for (uint8_t xx = 0; xx < 4; ++xx)
  2127. {
  2128. const uint8_t idx = yy*4+xx;
  2129. uint8_t subsetIndex = 0;
  2130. uint8_t indexAnchor = 0;
  2131. switch (mi.numSubsets)
  2132. {
  2133. case 2:
  2134. subsetIndex = (s_bptcP2[partitionSetIdx] >> idx) & 1;
  2135. indexAnchor = 0 != subsetIndex ? s_bptcA2[partitionSetIdx] : 0;
  2136. break;
  2137. case 3:
  2138. subsetIndex = (s_bptcP3[partitionSetIdx] >> (2*idx) ) & 3;
  2139. indexAnchor = 0 != subsetIndex ? s_bptcA3[subsetIndex-1][partitionSetIdx] : 0;
  2140. break;
  2141. default:
  2142. break;
  2143. }
  2144. const uint8_t anchor = idx == indexAnchor;
  2145. const uint8_t num[2] =
  2146. {
  2147. uint8_t( mi.indexBits[0] - anchor ),
  2148. uint8_t(hasIndexBits1 ? mi.indexBits[1] - anchor : 0),
  2149. };
  2150. const uint8_t index[2] =
  2151. {
  2152. (uint8_t)bit.peek(offset[0], num[0]),
  2153. hasIndexBits1 ? (uint8_t)bit.peek(offset[1], num[1]) : index[0],
  2154. };
  2155. offset[0] += num[0];
  2156. offset[1] += num[1];
  2157. const uint8_t fc = factors[ indexSelectionMode][index[ indexSelectionMode] ];
  2158. const uint8_t fa = factors[!indexSelectionMode][index[!indexSelectionMode] ];
  2159. const uint8_t fca = 64 - fc;
  2160. const uint8_t fcb = fc;
  2161. const uint8_t faa = 64 - fa;
  2162. const uint8_t fab = fa;
  2163. subsetIndex *= 2;
  2164. uint8_t rr = uint8_t(uint16_t(epR[subsetIndex]*fca + epR[subsetIndex + 1]*fcb + 32) >> 6);
  2165. uint8_t gg = uint8_t(uint16_t(epG[subsetIndex]*fca + epG[subsetIndex + 1]*fcb + 32) >> 6);
  2166. uint8_t bb = uint8_t(uint16_t(epB[subsetIndex]*fca + epB[subsetIndex + 1]*fcb + 32) >> 6);
  2167. uint8_t aa = uint8_t(uint16_t(epA[subsetIndex]*faa + epA[subsetIndex + 1]*fab + 32) >> 6);
  2168. switch (rotationMode)
  2169. {
  2170. case 1: bx::swap(aa, rr); break;
  2171. case 2: bx::swap(aa, gg); break;
  2172. case 3: bx::swap(aa, bb); break;
  2173. default: break;
  2174. };
  2175. uint8_t* bgra = &_dst[idx*4];
  2176. bgra[0] = bb;
  2177. bgra[1] = gg;
  2178. bgra[2] = rr;
  2179. bgra[3] = aa;
  2180. }
  2181. }
  2182. }
  2183. // ATC
  2184. //
  2185. void decodeBlockATC(uint8_t _dst[16*4], const uint8_t _src[8])
  2186. {
  2187. uint8_t colors[4*4];
  2188. uint32_t c0 = _src[0] | (_src[1] << 8);
  2189. uint32_t c1 = _src[2] | (_src[3] << 8);
  2190. if (0 == (c0 & 0x8000) )
  2191. {
  2192. colors[ 0] = bitRangeConvert( (c0>> 0)&0x1f, 5, 8);
  2193. colors[ 1] = bitRangeConvert( (c0>> 5)&0x1f, 5, 8);
  2194. colors[ 2] = bitRangeConvert( (c0>>10)&0x1f, 5, 8);
  2195. colors[12] = bitRangeConvert( (c1>> 0)&0x1f, 5, 8);
  2196. colors[13] = bitRangeConvert( (c1>> 5)&0x3f, 6, 8);
  2197. colors[14] = bitRangeConvert( (c1>>11)&0x1f, 5, 8);
  2198. colors[ 4] = (2 * colors[0] + colors[12]) / 3;
  2199. colors[ 5] = (2 * colors[1] + colors[13]) / 3;
  2200. colors[ 6] = (2 * colors[2] + colors[14]) / 3;
  2201. colors[ 8] = (colors[0] + 2 * colors[12]) / 3;
  2202. colors[ 9] = (colors[1] + 2 * colors[13]) / 3;
  2203. colors[10] = (colors[2] + 2 * colors[14]) / 3;
  2204. }
  2205. else
  2206. {
  2207. colors[ 0] = 0;
  2208. colors[ 1] = 0;
  2209. colors[ 2] = 0;
  2210. colors[ 8] = bitRangeConvert( (c0>> 0)&0x1f, 5, 8);
  2211. colors[ 9] = bitRangeConvert( (c0>> 5)&0x1f, 5, 8);
  2212. colors[10] = bitRangeConvert( (c0>>10)&0x1f, 5, 8);
  2213. colors[12] = bitRangeConvert( (c1>> 0)&0x1f, 5, 8);
  2214. colors[13] = bitRangeConvert( (c1>> 5)&0x3f, 6, 8);
  2215. colors[14] = bitRangeConvert( (c1>>11)&0x1f, 5, 8);
  2216. colors[ 4] = colors[ 8] - colors[12] / 4;
  2217. colors[ 5] = colors[ 9] - colors[13] / 4;
  2218. colors[ 6] = colors[10] - colors[14] / 4;
  2219. }
  2220. for (uint32_t ii = 0, next = 8*4; ii < 16*4; ii += 4, next += 2)
  2221. {
  2222. int32_t idx = ( (_src[next>>3] >> (next & 7) ) & 3) * 4;
  2223. _dst[ii+0] = colors[idx+0];
  2224. _dst[ii+1] = colors[idx+1];
  2225. _dst[ii+2] = colors[idx+2];
  2226. _dst[ii+3] = colors[idx+3];
  2227. }
  2228. }
  2229. static const int32_t s_etc1Mod[8][4] =
  2230. {
  2231. { 2, 8, -2, -8 },
  2232. { 5, 17, -5, -17 },
  2233. { 9, 29, -9, -29 },
  2234. { 13, 42, -13, -42 },
  2235. { 18, 60, -18, -60 },
  2236. { 24, 80, -24, -80 },
  2237. { 33, 106, -33, -106 },
  2238. { 47, 183, -47, -183 },
  2239. };
  2240. static const uint8_t s_etc2Mod[] = { 3, 6, 11, 16, 23, 32, 41, 64 };
  2241. uint8_t uint8_sat(int32_t _a)
  2242. {
  2243. using namespace bx;
  2244. const uint32_t min = uint32_imin(_a, 255);
  2245. const uint32_t result = uint32_imax(min, 0);
  2246. return (uint8_t)result;
  2247. }
  2248. uint8_t uint8_satadd(int32_t _a, int32_t _b)
  2249. {
  2250. const int32_t add = _a + _b;
  2251. return uint8_sat(add);
  2252. }
  2253. void decodeBlockEtc2ModeT(uint8_t _dst[16*4], const uint8_t _src[8])
  2254. {
  2255. uint8_t rgb[16];
  2256. // 0 1 2 3 4 5 6 7
  2257. // 7654321076543210765432107654321076543210765432107654321076543210
  2258. // ...rr.rrggggbbbbrrrrggggbbbbDDD.mmmmmmmmmmmmmmmmllllllllllllllll
  2259. // ^ ^ ^ ^ ^
  2260. // +-- c0 +-- c1 | +-- msb +-- lsb
  2261. // +-- dist
  2262. rgb[ 0] = ( (_src[0] >> 1) & 0xc)
  2263. | (_src[0] & 0x3)
  2264. ;
  2265. rgb[ 1] = _src[1] >> 4;
  2266. rgb[ 2] = _src[1] & 0xf;
  2267. rgb[ 8] = _src[2] >> 4;
  2268. rgb[ 9] = _src[2] & 0xf;
  2269. rgb[10] = _src[3] >> 4;
  2270. rgb[ 0] = bitRangeConvert(rgb[ 0], 4, 8);
  2271. rgb[ 1] = bitRangeConvert(rgb[ 1], 4, 8);
  2272. rgb[ 2] = bitRangeConvert(rgb[ 2], 4, 8);
  2273. rgb[ 8] = bitRangeConvert(rgb[ 8], 4, 8);
  2274. rgb[ 9] = bitRangeConvert(rgb[ 9], 4, 8);
  2275. rgb[10] = bitRangeConvert(rgb[10], 4, 8);
  2276. uint8_t dist = (_src[3] >> 1) & 0x7;
  2277. int32_t mod = s_etc2Mod[dist];
  2278. rgb[ 4] = uint8_satadd(rgb[ 8], mod);
  2279. rgb[ 5] = uint8_satadd(rgb[ 9], mod);
  2280. rgb[ 6] = uint8_satadd(rgb[10], mod);
  2281. rgb[12] = uint8_satadd(rgb[ 8], -mod);
  2282. rgb[13] = uint8_satadd(rgb[ 9], -mod);
  2283. rgb[14] = uint8_satadd(rgb[10], -mod);
  2284. uint32_t indexMsb = (_src[4]<<8) | _src[5];
  2285. uint32_t indexLsb = (_src[6]<<8) | _src[7];
  2286. for (uint32_t ii = 0; ii < 16; ++ii)
  2287. {
  2288. const uint32_t idx = (ii&0xc) | ( (ii & 0x3)<<4);
  2289. const uint32_t lsbi = indexLsb & 1;
  2290. const uint32_t msbi = (indexMsb & 1)<<1;
  2291. const uint32_t pal = (lsbi | msbi)<<2;
  2292. _dst[idx + 0] = rgb[pal+2];
  2293. _dst[idx + 1] = rgb[pal+1];
  2294. _dst[idx + 2] = rgb[pal+0];
  2295. _dst[idx + 3] = 255;
  2296. indexLsb >>= 1;
  2297. indexMsb >>= 1;
  2298. }
  2299. }
  2300. void decodeBlockEtc2ModeH(uint8_t _dst[16*4], const uint8_t _src[8])
  2301. {
  2302. uint8_t rgb[16];
  2303. // 0 1 2 3 4 5 6 7
  2304. // 7654321076543210765432107654321076543210765432107654321076543210
  2305. // .rrrrggg...gb.bbbrrrrggggbbbbDD.mmmmmmmmmmmmmmmmllllllllllllllll
  2306. // ^ ^ ^ ^ ^
  2307. // +-- c0 +-- c1 | +-- msb +-- lsb
  2308. // +-- dist
  2309. rgb[ 0] = (_src[0] >> 3) & 0xf;
  2310. rgb[ 1] = ( (_src[0] << 1) & 0xe)
  2311. | ( (_src[1] >> 4) & 0x1)
  2312. ;
  2313. rgb[ 2] = (_src[1] & 0x8)
  2314. | ( (_src[1] << 1) & 0x6)
  2315. | (_src[2] >> 7)
  2316. ;
  2317. rgb[ 8] = (_src[2] >> 3) & 0xf;
  2318. rgb[ 9] = ( (_src[2] << 1) & 0xe)
  2319. | (_src[3] >> 7)
  2320. ;
  2321. rgb[10] = (_src[2] >> 3) & 0xf;
  2322. rgb[ 0] = bitRangeConvert(rgb[ 0], 4, 8);
  2323. rgb[ 1] = bitRangeConvert(rgb[ 1], 4, 8);
  2324. rgb[ 2] = bitRangeConvert(rgb[ 2], 4, 8);
  2325. rgb[ 8] = bitRangeConvert(rgb[ 8], 4, 8);
  2326. rgb[ 9] = bitRangeConvert(rgb[ 9], 4, 8);
  2327. rgb[10] = bitRangeConvert(rgb[10], 4, 8);
  2328. uint32_t col0 = uint32_t(rgb[0]<<16) | uint32_t(rgb[1]<<8) | uint32_t(rgb[ 2]);
  2329. uint32_t col1 = uint32_t(rgb[8]<<16) | uint32_t(rgb[9]<<8) | uint32_t(rgb[10]);
  2330. uint8_t dist = (_src[3] & 0x6) | (col0 >= col1);
  2331. int32_t mod = s_etc2Mod[dist];
  2332. rgb[ 4] = uint8_satadd(rgb[ 0], -mod);
  2333. rgb[ 5] = uint8_satadd(rgb[ 1], -mod);
  2334. rgb[ 6] = uint8_satadd(rgb[ 2], -mod);
  2335. rgb[ 0] = uint8_satadd(rgb[ 0], mod);
  2336. rgb[ 1] = uint8_satadd(rgb[ 1], mod);
  2337. rgb[ 2] = uint8_satadd(rgb[ 2], mod);
  2338. rgb[12] = uint8_satadd(rgb[ 8], -mod);
  2339. rgb[13] = uint8_satadd(rgb[ 9], -mod);
  2340. rgb[14] = uint8_satadd(rgb[10], -mod);
  2341. rgb[ 8] = uint8_satadd(rgb[ 8], mod);
  2342. rgb[ 9] = uint8_satadd(rgb[ 9], mod);
  2343. rgb[10] = uint8_satadd(rgb[10], mod);
  2344. uint32_t indexMsb = (_src[4]<<8) | _src[5];
  2345. uint32_t indexLsb = (_src[6]<<8) | _src[7];
  2346. for (uint32_t ii = 0; ii < 16; ++ii)
  2347. {
  2348. const uint32_t idx = (ii&0xc) | ( (ii & 0x3)<<4);
  2349. const uint32_t lsbi = indexLsb & 1;
  2350. const uint32_t msbi = (indexMsb & 1)<<1;
  2351. const uint32_t pal = (lsbi | msbi)<<2;
  2352. _dst[idx + 0] = rgb[pal+2];
  2353. _dst[idx + 1] = rgb[pal+1];
  2354. _dst[idx + 2] = rgb[pal+0];
  2355. _dst[idx + 3] = 255;
  2356. indexLsb >>= 1;
  2357. indexMsb >>= 1;
  2358. }
  2359. }
  2360. void decodeBlockEtc2ModePlanar(uint8_t _dst[16*4], const uint8_t _src[8])
  2361. {
  2362. // 0 1 2 3 4 5 6 7
  2363. // 7654321076543210765432107654321076543210765432107654321076543210
  2364. // .rrrrrrg.ggggggb...bb.bbbrrrrr.rgggggggbbbbbbrrrrrrgggggggbbbbbb
  2365. // ^ ^ ^
  2366. // +-- c0 +-- cH +-- cV
  2367. uint8_t c0[3];
  2368. uint8_t cH[3];
  2369. uint8_t cV[3];
  2370. c0[0] = (_src[0] >> 1) & 0x3f;
  2371. c0[1] = ( (_src[0] & 1) << 6)
  2372. | ( (_src[1] >> 1) & 0x3f)
  2373. ;
  2374. c0[2] = ( (_src[1] & 1) << 5)
  2375. | ( (_src[2] & 0x18) )
  2376. | ( (_src[2] << 1) & 6)
  2377. | ( (_src[3] >> 7) )
  2378. ;
  2379. cH[0] = ( (_src[3] >> 1) & 0x3e)
  2380. | (_src[3] & 1)
  2381. ;
  2382. cH[1] = _src[4] >> 1;
  2383. cH[2] = ( (_src[4] & 1) << 5)
  2384. | (_src[5] >> 3)
  2385. ;
  2386. cV[0] = ( (_src[5] & 0x7) << 3)
  2387. | (_src[6] >> 5)
  2388. ;
  2389. cV[1] = ( (_src[6] & 0x1f) << 2)
  2390. | (_src[7] >> 5)
  2391. ;
  2392. cV[2] = _src[7] & 0x3f;
  2393. c0[0] = bitRangeConvert(c0[0], 6, 8);
  2394. c0[1] = bitRangeConvert(c0[1], 7, 8);
  2395. c0[2] = bitRangeConvert(c0[2], 6, 8);
  2396. cH[0] = bitRangeConvert(cH[0], 6, 8);
  2397. cH[1] = bitRangeConvert(cH[1], 7, 8);
  2398. cH[2] = bitRangeConvert(cH[2], 6, 8);
  2399. cV[0] = bitRangeConvert(cV[0], 6, 8);
  2400. cV[1] = bitRangeConvert(cV[1], 7, 8);
  2401. cV[2] = bitRangeConvert(cV[2], 6, 8);
  2402. int16_t dy[3];
  2403. dy[0] = cV[0] - c0[0];
  2404. dy[1] = cV[1] - c0[1];
  2405. dy[2] = cV[2] - c0[2];
  2406. int16_t sx[3];
  2407. sx[0] = int16_t(c0[0])<<2;
  2408. sx[1] = int16_t(c0[1])<<2;
  2409. sx[2] = int16_t(c0[2])<<2;
  2410. int16_t ex[3];
  2411. ex[0] = int16_t(cH[0])<<2;
  2412. ex[1] = int16_t(cH[1])<<2;
  2413. ex[2] = int16_t(cH[2])<<2;
  2414. for (int32_t vv = 0; vv < 4; ++vv)
  2415. {
  2416. int16_t dx[3];
  2417. dx[0] = (ex[0] - sx[0])>>2;
  2418. dx[1] = (ex[1] - sx[1])>>2;
  2419. dx[2] = (ex[2] - sx[2])>>2;
  2420. for (int32_t hh = 0; hh < 4; ++hh)
  2421. {
  2422. const uint32_t idx = (vv<<4) + (hh<<2);
  2423. _dst[idx + 0] = uint8_sat( (sx[2] + dx[2]*hh)>>2);
  2424. _dst[idx + 1] = uint8_sat( (sx[1] + dx[1]*hh)>>2);
  2425. _dst[idx + 2] = uint8_sat( (sx[0] + dx[0]*hh)>>2);
  2426. _dst[idx + 3] = 255;
  2427. }
  2428. sx[0] += dy[0];
  2429. sx[1] += dy[1];
  2430. sx[2] += dy[2];
  2431. ex[0] += dy[0];
  2432. ex[1] += dy[1];
  2433. ex[2] += dy[2];
  2434. }
  2435. }
  2436. void decodeBlockEtc12(uint8_t _dst[16*4], const uint8_t _src[8])
  2437. {
  2438. bool flipBit = 0 != (_src[3] & 0x1);
  2439. bool diffBit = 0 != (_src[3] & 0x2);
  2440. uint8_t rgb[8];
  2441. if (diffBit)
  2442. {
  2443. rgb[0] = _src[0] >> 3;
  2444. rgb[1] = _src[1] >> 3;
  2445. rgb[2] = _src[2] >> 3;
  2446. int8_t diff[3];
  2447. diff[0] = int8_t( (_src[0] & 0x7)<<5)>>5;
  2448. diff[1] = int8_t( (_src[1] & 0x7)<<5)>>5;
  2449. diff[2] = int8_t( (_src[2] & 0x7)<<5)>>5;
  2450. int8_t rr = rgb[0] + diff[0];
  2451. int8_t gg = rgb[1] + diff[1];
  2452. int8_t bb = rgb[2] + diff[2];
  2453. // Etc2 3-modes
  2454. if (rr < 0 || rr > 31)
  2455. {
  2456. decodeBlockEtc2ModeT(_dst, _src);
  2457. return;
  2458. }
  2459. if (gg < 0 || gg > 31)
  2460. {
  2461. decodeBlockEtc2ModeH(_dst, _src);
  2462. return;
  2463. }
  2464. if (bb < 0 || bb > 31)
  2465. {
  2466. decodeBlockEtc2ModePlanar(_dst, _src);
  2467. return;
  2468. }
  2469. // Etc1
  2470. rgb[0] = bitRangeConvert(rgb[0], 5, 8);
  2471. rgb[1] = bitRangeConvert(rgb[1], 5, 8);
  2472. rgb[2] = bitRangeConvert(rgb[2], 5, 8);
  2473. rgb[4] = bitRangeConvert(rr, 5, 8);
  2474. rgb[5] = bitRangeConvert(gg, 5, 8);
  2475. rgb[6] = bitRangeConvert(bb, 5, 8);
  2476. }
  2477. else
  2478. {
  2479. rgb[0] = _src[0] >> 4;
  2480. rgb[1] = _src[1] >> 4;
  2481. rgb[2] = _src[2] >> 4;
  2482. rgb[4] = _src[0] & 0xf;
  2483. rgb[5] = _src[1] & 0xf;
  2484. rgb[6] = _src[2] & 0xf;
  2485. rgb[0] = bitRangeConvert(rgb[0], 4, 8);
  2486. rgb[1] = bitRangeConvert(rgb[1], 4, 8);
  2487. rgb[2] = bitRangeConvert(rgb[2], 4, 8);
  2488. rgb[4] = bitRangeConvert(rgb[4], 4, 8);
  2489. rgb[5] = bitRangeConvert(rgb[5], 4, 8);
  2490. rgb[6] = bitRangeConvert(rgb[6], 4, 8);
  2491. }
  2492. uint32_t table[2];
  2493. table[0] = (_src[3] >> 5) & 0x7;
  2494. table[1] = (_src[3] >> 2) & 0x7;
  2495. uint32_t indexMsb = (_src[4]<<8) | _src[5];
  2496. uint32_t indexLsb = (_src[6]<<8) | _src[7];
  2497. if (flipBit)
  2498. {
  2499. for (uint32_t ii = 0; ii < 16; ++ii)
  2500. {
  2501. const uint32_t block = (ii>>1)&1;
  2502. const uint32_t color = block<<2;
  2503. const uint32_t idx = (ii&0xc) | ( (ii & 0x3)<<4);
  2504. const uint32_t lsbi = indexLsb & 1;
  2505. const uint32_t msbi = (indexMsb & 1)<<1;
  2506. const int32_t mod = s_etc1Mod[table[block] ][lsbi | msbi];
  2507. _dst[idx + 0] = uint8_satadd(rgb[color+2], mod);
  2508. _dst[idx + 1] = uint8_satadd(rgb[color+1], mod);
  2509. _dst[idx + 2] = uint8_satadd(rgb[color+0], mod);
  2510. _dst[idx + 3] = 255;
  2511. indexLsb >>= 1;
  2512. indexMsb >>= 1;
  2513. }
  2514. }
  2515. else
  2516. {
  2517. for (uint32_t ii = 0; ii < 16; ++ii)
  2518. {
  2519. const uint32_t block = ii>>3;
  2520. const uint32_t color = block<<2;
  2521. const uint32_t idx = (ii&0xc) | ( (ii & 0x3)<<4);
  2522. const uint32_t lsbi = indexLsb & 1;
  2523. const uint32_t msbi = (indexMsb & 1)<<1;
  2524. const int32_t mod = s_etc1Mod[table[block] ][lsbi | msbi];
  2525. _dst[idx + 0] = uint8_satadd(rgb[color+2], mod);
  2526. _dst[idx + 1] = uint8_satadd(rgb[color+1], mod);
  2527. _dst[idx + 2] = uint8_satadd(rgb[color+0], mod);
  2528. _dst[idx + 3] = 255;
  2529. indexLsb >>= 1;
  2530. indexMsb >>= 1;
  2531. }
  2532. }
  2533. }
  2534. static const uint8_t s_pvrtcFactors[16][4] =
  2535. {
  2536. { 4, 4, 4, 4 },
  2537. { 2, 6, 2, 6 },
  2538. { 8, 0, 8, 0 },
  2539. { 6, 2, 6, 2 },
  2540. { 2, 2, 6, 6 },
  2541. { 1, 3, 3, 9 },
  2542. { 4, 0, 12, 0 },
  2543. { 3, 1, 9, 3 },
  2544. { 8, 8, 0, 0 },
  2545. { 4, 12, 0, 0 },
  2546. { 16, 0, 0, 0 },
  2547. { 12, 4, 0, 0 },
  2548. { 6, 6, 2, 2 },
  2549. { 3, 9, 1, 3 },
  2550. { 12, 0, 4, 0 },
  2551. { 9, 3, 3, 1 },
  2552. };
  2553. static const uint8_t s_pvrtcWeights[8][4] =
  2554. {
  2555. { 8, 0, 8, 0 },
  2556. { 5, 3, 5, 3 },
  2557. { 3, 5, 3, 5 },
  2558. { 0, 8, 0, 8 },
  2559. { 8, 0, 8, 0 },
  2560. { 4, 4, 4, 4 },
  2561. { 4, 4, 4, 4 },
  2562. { 0, 8, 0, 8 },
  2563. };
  2564. uint32_t morton2d(uint32_t _x, uint32_t _y)
  2565. {
  2566. using namespace bx;
  2567. const uint32_t tmpx = uint32_part1by1(_x);
  2568. const uint32_t xbits = uint32_sll(tmpx, 1);
  2569. const uint32_t ybits = uint32_part1by1(_y);
  2570. const uint32_t result = uint32_or(xbits, ybits);
  2571. return result;
  2572. }
  2573. uint32_t getColor(const uint8_t _src[8])
  2574. {
  2575. return 0
  2576. | _src[7]<<24
  2577. | _src[6]<<16
  2578. | _src[5]<<8
  2579. | _src[4]
  2580. ;
  2581. }
  2582. void decodeBlockPtc14RgbAddA(uint32_t _block, uint32_t* _r, uint32_t* _g, uint32_t* _b, uint8_t _factor)
  2583. {
  2584. if (0 != (_block & (1<<15) ) )
  2585. {
  2586. *_r += bitRangeConvert( (_block >> 10) & 0x1f, 5, 8) * _factor;
  2587. *_g += bitRangeConvert( (_block >> 5) & 0x1f, 5, 8) * _factor;
  2588. *_b += bitRangeConvert( (_block >> 1) & 0x0f, 4, 8) * _factor;
  2589. }
  2590. else
  2591. {
  2592. *_r += bitRangeConvert( (_block >> 8) & 0xf, 4, 8) * _factor;
  2593. *_g += bitRangeConvert( (_block >> 4) & 0xf, 4, 8) * _factor;
  2594. *_b += bitRangeConvert( (_block >> 1) & 0x7, 3, 8) * _factor;
  2595. }
  2596. }
  2597. void decodeBlockPtc14RgbAddB(uint32_t _block, uint32_t* _r, uint32_t* _g, uint32_t* _b, uint8_t _factor)
  2598. {
  2599. if (0 != (_block & (1<<31) ) )
  2600. {
  2601. *_r += bitRangeConvert( (_block >> 26) & 0x1f, 5, 8) * _factor;
  2602. *_g += bitRangeConvert( (_block >> 21) & 0x1f, 5, 8) * _factor;
  2603. *_b += bitRangeConvert( (_block >> 16) & 0x1f, 5, 8) * _factor;
  2604. }
  2605. else
  2606. {
  2607. *_r += bitRangeConvert( (_block >> 24) & 0xf, 4, 8) * _factor;
  2608. *_g += bitRangeConvert( (_block >> 20) & 0xf, 4, 8) * _factor;
  2609. *_b += bitRangeConvert( (_block >> 16) & 0xf, 4, 8) * _factor;
  2610. }
  2611. }
  2612. void decodeBlockPtc14(uint8_t _dst[16*4], const uint8_t* _src, uint32_t _x, uint32_t _y, uint32_t _width, uint32_t _height)
  2613. {
  2614. // 0 1 2 3 4 5 6 7
  2615. // 7654321076543210765432107654321076543210765432107654321076543210
  2616. // mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmyrrrrrgggggbbbbbxrrrrrgggggbbbbp
  2617. // ^ ^^ ^^ ^
  2618. // +-- modulation data |+- B color |+- A color |
  2619. // +-- B opaque +-- A opaque |
  2620. // alpha punchthrough --+
  2621. const uint8_t* bc = &_src[morton2d(_x, _y) * 8];
  2622. uint32_t mod = 0
  2623. | bc[3]<<24
  2624. | bc[2]<<16
  2625. | bc[1]<<8
  2626. | bc[0]
  2627. ;
  2628. const bool punchthrough = !!(bc[7] & 1);
  2629. const uint8_t* weightTable = s_pvrtcWeights[4 * punchthrough];
  2630. const uint8_t* factorTable = s_pvrtcFactors[0];
  2631. for (int yy = 0; yy < 4; ++yy)
  2632. {
  2633. const uint32_t yOffset = (yy < 2) ? -1 : 0;
  2634. const uint32_t y0 = (_y + yOffset) % _height;
  2635. const uint32_t y1 = (y0 + 1) % _height;
  2636. for (int xx = 0; xx < 4; ++xx)
  2637. {
  2638. const uint32_t xOffset = (xx < 2) ? -1 : 0;
  2639. const uint32_t x0 = (_x + xOffset) % _width;
  2640. const uint32_t x1 = (x0 + 1) % _width;
  2641. const uint32_t bc0 = getColor(&_src[morton2d(x0, y0) * 8]);
  2642. const uint32_t bc1 = getColor(&_src[morton2d(x1, y0) * 8]);
  2643. const uint32_t bc2 = getColor(&_src[morton2d(x0, y1) * 8]);
  2644. const uint32_t bc3 = getColor(&_src[morton2d(x1, y1) * 8]);
  2645. const uint8_t f0 = factorTable[0];
  2646. const uint8_t f1 = factorTable[1];
  2647. const uint8_t f2 = factorTable[2];
  2648. const uint8_t f3 = factorTable[3];
  2649. uint32_t ar = 0, ag = 0, ab = 0;
  2650. decodeBlockPtc14RgbAddA(bc0, &ar, &ag, &ab, f0);
  2651. decodeBlockPtc14RgbAddA(bc1, &ar, &ag, &ab, f1);
  2652. decodeBlockPtc14RgbAddA(bc2, &ar, &ag, &ab, f2);
  2653. decodeBlockPtc14RgbAddA(bc3, &ar, &ag, &ab, f3);
  2654. uint32_t br = 0, bg = 0, bb = 0;
  2655. decodeBlockPtc14RgbAddB(bc0, &br, &bg, &bb, f0);
  2656. decodeBlockPtc14RgbAddB(bc1, &br, &bg, &bb, f1);
  2657. decodeBlockPtc14RgbAddB(bc2, &br, &bg, &bb, f2);
  2658. decodeBlockPtc14RgbAddB(bc3, &br, &bg, &bb, f3);
  2659. const uint8_t* weight = &weightTable[(mod & 3)*4];
  2660. const uint8_t wa = weight[0];
  2661. const uint8_t wb = weight[1];
  2662. _dst[(yy*4 + xx)*4+0] = uint8_t( (ab * wa + bb * wb) >> 7);
  2663. _dst[(yy*4 + xx)*4+1] = uint8_t( (ag * wa + bg * wb) >> 7);
  2664. _dst[(yy*4 + xx)*4+2] = uint8_t( (ar * wa + br * wb) >> 7);
  2665. _dst[(yy*4 + xx)*4+3] = 255;
  2666. mod >>= 2;
  2667. factorTable += 4;
  2668. }
  2669. }
  2670. }
  2671. void decodeBlockPtc14ARgbaAddA(uint32_t _block, uint32_t* _r, uint32_t* _g, uint32_t* _b, uint32_t* _a, uint8_t _factor)
  2672. {
  2673. if (0 != (_block & (1<<15) ) )
  2674. {
  2675. *_r += bitRangeConvert( (_block >> 10) & 0x1f, 5, 8) * _factor;
  2676. *_g += bitRangeConvert( (_block >> 5) & 0x1f, 5, 8) * _factor;
  2677. *_b += bitRangeConvert( (_block >> 1) & 0x0f, 4, 8) * _factor;
  2678. *_a += 255 * _factor;
  2679. }
  2680. else
  2681. {
  2682. *_r += bitRangeConvert( (_block >> 8) & 0xf, 4, 8) * _factor;
  2683. *_g += bitRangeConvert( (_block >> 4) & 0xf, 4, 8) * _factor;
  2684. *_b += bitRangeConvert( (_block >> 1) & 0x7, 3, 8) * _factor;
  2685. *_a += bitRangeConvert( (_block >> 12) & 0x7, 3, 8) * _factor;
  2686. }
  2687. }
  2688. void decodeBlockPtc14ARgbaAddB(uint32_t _block, uint32_t* _r, uint32_t* _g, uint32_t* _b, uint32_t* _a, uint8_t _factor)
  2689. {
  2690. if (0 != (_block & (1<<31) ) )
  2691. {
  2692. *_r += bitRangeConvert( (_block >> 26) & 0x1f, 5, 8) * _factor;
  2693. *_g += bitRangeConvert( (_block >> 21) & 0x1f, 5, 8) * _factor;
  2694. *_b += bitRangeConvert( (_block >> 16) & 0x1f, 5, 8) * _factor;
  2695. *_a += 255 * _factor;
  2696. }
  2697. else
  2698. {
  2699. *_r += bitRangeConvert( (_block >> 24) & 0xf, 4, 8) * _factor;
  2700. *_g += bitRangeConvert( (_block >> 20) & 0xf, 4, 8) * _factor;
  2701. *_b += bitRangeConvert( (_block >> 16) & 0xf, 4, 8) * _factor;
  2702. *_a += bitRangeConvert( (_block >> 28) & 0x7, 3, 8) * _factor;
  2703. }
  2704. }
  2705. void decodeBlockPtc14A(uint8_t _dst[16*4], const uint8_t* _src, uint32_t _x, uint32_t _y, uint32_t _width, uint32_t _height)
  2706. {
  2707. // 0 1 2 3 4 5 6 7
  2708. // 7654321076543210765432107654321076543210765432107654321076543210
  2709. // mmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmyrrrrrgggggbbbbbxrrrrrgggggbbbbp
  2710. // ^ ^^ ^^ ^
  2711. // +-- modulation data |+- B color |+- A color |
  2712. // +-- B opaque +-- A opaque |
  2713. // alpha punchthrough --+
  2714. const uint8_t* bc = &_src[morton2d(_x, _y) * 8];
  2715. uint32_t mod = 0
  2716. | bc[3]<<24
  2717. | bc[2]<<16
  2718. | bc[1]<<8
  2719. | bc[0]
  2720. ;
  2721. const bool punchthrough = !!(bc[7] & 1);
  2722. const uint8_t* weightTable = s_pvrtcWeights[4 * punchthrough];
  2723. const uint8_t* factorTable = s_pvrtcFactors[0];
  2724. for (int yy = 0; yy < 4; ++yy)
  2725. {
  2726. const uint32_t yOffset = (yy < 2) ? -1 : 0;
  2727. const uint32_t y0 = (_y + yOffset) % _height;
  2728. const uint32_t y1 = (y0 + 1) % _height;
  2729. for (int xx = 0; xx < 4; ++xx)
  2730. {
  2731. const uint32_t xOffset = (xx < 2) ? -1 : 0;
  2732. const uint32_t x0 = (_x + xOffset) % _width;
  2733. const uint32_t x1 = (x0 + 1) % _width;
  2734. const uint32_t bc0 = getColor(&_src[morton2d(x0, y0) * 8]);
  2735. const uint32_t bc1 = getColor(&_src[morton2d(x1, y0) * 8]);
  2736. const uint32_t bc2 = getColor(&_src[morton2d(x0, y1) * 8]);
  2737. const uint32_t bc3 = getColor(&_src[morton2d(x1, y1) * 8]);
  2738. const uint8_t f0 = factorTable[0];
  2739. const uint8_t f1 = factorTable[1];
  2740. const uint8_t f2 = factorTable[2];
  2741. const uint8_t f3 = factorTable[3];
  2742. uint32_t ar = 0, ag = 0, ab = 0, aa = 0;
  2743. decodeBlockPtc14ARgbaAddA(bc0, &ar, &ag, &ab, &aa, f0);
  2744. decodeBlockPtc14ARgbaAddA(bc1, &ar, &ag, &ab, &aa, f1);
  2745. decodeBlockPtc14ARgbaAddA(bc2, &ar, &ag, &ab, &aa, f2);
  2746. decodeBlockPtc14ARgbaAddA(bc3, &ar, &ag, &ab, &aa, f3);
  2747. uint32_t br = 0, bg = 0, bb = 0, ba = 0;
  2748. decodeBlockPtc14ARgbaAddB(bc0, &br, &bg, &bb, &ba, f0);
  2749. decodeBlockPtc14ARgbaAddB(bc1, &br, &bg, &bb, &ba, f1);
  2750. decodeBlockPtc14ARgbaAddB(bc2, &br, &bg, &bb, &ba, f2);
  2751. decodeBlockPtc14ARgbaAddB(bc3, &br, &bg, &bb, &ba, f3);
  2752. const uint8_t* weight = &weightTable[(mod & 3)*4];
  2753. const uint8_t wa = weight[0];
  2754. const uint8_t wb = weight[1];
  2755. const uint8_t wc = weight[2];
  2756. const uint8_t wd = weight[3];
  2757. _dst[(yy*4 + xx)*4+0] = uint8_t( (ab * wa + bb * wb) >> 7);
  2758. _dst[(yy*4 + xx)*4+1] = uint8_t( (ag * wa + bg * wb) >> 7);
  2759. _dst[(yy*4 + xx)*4+2] = uint8_t( (ar * wa + br * wb) >> 7);
  2760. _dst[(yy*4 + xx)*4+3] = uint8_t( (aa * wc + ba * wd) >> 7);
  2761. mod >>= 2;
  2762. factorTable += 4;
  2763. }
  2764. }
  2765. }
  2766. 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)
  2767. {
  2768. const ImageBlockInfo& blockInfo = getBlockInfo(_format);
  2769. const uint16_t blockWidth = blockInfo.blockWidth;
  2770. const uint16_t blockHeight = blockInfo.blockHeight;
  2771. const uint16_t minBlockX = blockInfo.minBlockX;
  2772. const uint16_t minBlockY = blockInfo.minBlockY;
  2773. _width = bx::max<uint16_t>(blockWidth * minBlockX, ( (_width + blockWidth - 1) / blockWidth)*blockWidth);
  2774. _height = bx::max<uint16_t>(blockHeight * minBlockY, ( (_height + blockHeight - 1) / blockHeight)*blockHeight);
  2775. _depth = bx::max<uint16_t>(1, _depth);
  2776. _numLayers = bx::max<uint16_t>(1, _numLayers);
  2777. const uint8_t numMips = _hasMips ? imageGetNumMips(_format, _width, _height, _depth) : 1;
  2778. uint32_t size = imageGetSize(NULL, _width, _height, _depth, _cubeMap, _hasMips, _numLayers, _format);
  2779. ImageContainer* imageContainer = (ImageContainer*)BX_ALIGNED_ALLOC(_allocator, size + BX_ALIGN_16(sizeof(ImageContainer) ), 16);
  2780. imageContainer->m_allocator = _allocator;
  2781. imageContainer->m_data = bx::alignPtr(imageContainer + 1, 0, 16);
  2782. imageContainer->m_format = _format;
  2783. imageContainer->m_orientation = Orientation::R0;
  2784. imageContainer->m_size = size;
  2785. imageContainer->m_offset = 0;
  2786. imageContainer->m_width = _width;
  2787. imageContainer->m_height = _height;
  2788. imageContainer->m_depth = _depth;
  2789. imageContainer->m_numLayers = _numLayers;
  2790. imageContainer->m_numMips = numMips;
  2791. imageContainer->m_hasAlpha = false;
  2792. imageContainer->m_cubeMap = _cubeMap;
  2793. imageContainer->m_ktx = false;
  2794. imageContainer->m_ktxLE = false;
  2795. imageContainer->m_srgb = false;
  2796. if (NULL != _data)
  2797. {
  2798. bx::memCopy(imageContainer->m_data, _data, imageContainer->m_size);
  2799. }
  2800. return imageContainer;
  2801. }
  2802. void imageFree(ImageContainer* _imageContainer)
  2803. {
  2804. BX_ALIGNED_FREE(_imageContainer->m_allocator, _imageContainer, 16);
  2805. }
  2806. // DDS
  2807. #define DDS_MAGIC BX_MAKEFOURCC('D', 'D', 'S', ' ')
  2808. #define DDS_HEADER_SIZE 124
  2809. #define DDS_DXT1 BX_MAKEFOURCC('D', 'X', 'T', '1')
  2810. #define DDS_DXT2 BX_MAKEFOURCC('D', 'X', 'T', '2')
  2811. #define DDS_DXT3 BX_MAKEFOURCC('D', 'X', 'T', '3')
  2812. #define DDS_DXT4 BX_MAKEFOURCC('D', 'X', 'T', '4')
  2813. #define DDS_DXT5 BX_MAKEFOURCC('D', 'X', 'T', '5')
  2814. #define DDS_ATI1 BX_MAKEFOURCC('A', 'T', 'I', '1')
  2815. #define DDS_BC4U BX_MAKEFOURCC('B', 'C', '4', 'U')
  2816. #define DDS_ATI2 BX_MAKEFOURCC('A', 'T', 'I', '2')
  2817. #define DDS_BC5U BX_MAKEFOURCC('B', 'C', '5', 'U')
  2818. #define DDS_DX10 BX_MAKEFOURCC('D', 'X', '1', '0')
  2819. #define DDS_ETC1 BX_MAKEFOURCC('E', 'T', 'C', '1')
  2820. #define DDS_ETC2 BX_MAKEFOURCC('E', 'T', 'C', '2')
  2821. #define DDS_ET2A BX_MAKEFOURCC('E', 'T', '2', 'A')
  2822. #define DDS_PTC2 BX_MAKEFOURCC('P', 'T', 'C', '2')
  2823. #define DDS_PTC4 BX_MAKEFOURCC('P', 'T', 'C', '4')
  2824. #define DDS_ATC BX_MAKEFOURCC('A', 'T', 'C', ' ')
  2825. #define DDS_ATCE BX_MAKEFOURCC('A', 'T', 'C', 'E')
  2826. #define DDS_ATCI BX_MAKEFOURCC('A', 'T', 'C', 'I')
  2827. #define DDS_ASTC4x4 BX_MAKEFOURCC('A', 'S', '4', '4')
  2828. #define DDS_ASTC5x5 BX_MAKEFOURCC('A', 'S', '5', '5')
  2829. #define DDS_ASTC6x6 BX_MAKEFOURCC('A', 'S', '6', '6')
  2830. #define DDS_ASTC8x5 BX_MAKEFOURCC('A', 'S', '8', '5')
  2831. #define DDS_ASTC8x6 BX_MAKEFOURCC('A', 'S', '8', '6')
  2832. #define DDS_ASTC10x5 BX_MAKEFOURCC('A', 'S', ':', '5')
  2833. #define DDS_R8G8B8 20
  2834. #define DDS_A8R8G8B8 21
  2835. #define DDS_R5G6B5 23
  2836. #define DDS_A1R5G5B5 25
  2837. #define DDS_A4R4G4B4 26
  2838. #define DDS_A2B10G10R10 31
  2839. #define DDS_G16R16 34
  2840. #define DDS_A2R10G10B10 35
  2841. #define DDS_A16B16G16R16 36
  2842. #define DDS_A8L8 51
  2843. #define DDS_R16F 111
  2844. #define DDS_G16R16F 112
  2845. #define DDS_A16B16G16R16F 113
  2846. #define DDS_R32F 114
  2847. #define DDS_G32R32F 115
  2848. #define DDS_A32B32G32R32F 116
  2849. #define DDS_FORMAT_R32G32B32A32_FLOAT 2
  2850. #define DDS_FORMAT_R32G32B32A32_UINT 3
  2851. #define DDS_FORMAT_R16G16B16A16_FLOAT 10
  2852. #define DDS_FORMAT_R16G16B16A16_UNORM 11
  2853. #define DDS_FORMAT_R16G16B16A16_UINT 12
  2854. #define DDS_FORMAT_R32G32_FLOAT 16
  2855. #define DDS_FORMAT_R32G32_UINT 17
  2856. #define DDS_FORMAT_R10G10B10A2_UNORM 24
  2857. #define DDS_FORMAT_R11G11B10_FLOAT 26
  2858. #define DDS_FORMAT_R8G8B8A8_UNORM 28
  2859. #define DDS_FORMAT_R8G8B8A8_UNORM_SRGB 29
  2860. #define DDS_FORMAT_R16G16_FLOAT 34
  2861. #define DDS_FORMAT_R16G16_UNORM 35
  2862. #define DDS_FORMAT_R32_FLOAT 41
  2863. #define DDS_FORMAT_R32_UINT 42
  2864. #define DDS_FORMAT_R8G8_UNORM 49
  2865. #define DDS_FORMAT_R16_FLOAT 54
  2866. #define DDS_FORMAT_R16_UNORM 56
  2867. #define DDS_FORMAT_R8_UNORM 61
  2868. #define DDS_FORMAT_R1_UNORM 66
  2869. #define DDS_FORMAT_BC1_UNORM 71
  2870. #define DDS_FORMAT_BC1_UNORM_SRGB 72
  2871. #define DDS_FORMAT_BC2_UNORM 74
  2872. #define DDS_FORMAT_BC2_UNORM_SRGB 75
  2873. #define DDS_FORMAT_BC3_UNORM 77
  2874. #define DDS_FORMAT_BC3_UNORM_SRGB 78
  2875. #define DDS_FORMAT_BC4_UNORM 80
  2876. #define DDS_FORMAT_BC5_UNORM 83
  2877. #define DDS_FORMAT_B5G6R5_UNORM 85
  2878. #define DDS_FORMAT_B5G5R5A1_UNORM 86
  2879. #define DDS_FORMAT_B8G8R8A8_UNORM 87
  2880. #define DDS_FORMAT_B8G8R8A8_UNORM_SRGB 91
  2881. #define DDS_FORMAT_BC6H_SF16 96
  2882. #define DDS_FORMAT_BC7_UNORM 98
  2883. #define DDS_FORMAT_BC7_UNORM_SRGB 99
  2884. #define DDS_FORMAT_B4G4R4A4_UNORM 115
  2885. #define DDS_DX10_DIMENSION_TEXTURE2D 3
  2886. #define DDS_DX10_DIMENSION_TEXTURE3D 4
  2887. #define DDS_DX10_MISC_TEXTURECUBE 4
  2888. #define DDSD_CAPS 0x00000001
  2889. #define DDSD_HEIGHT 0x00000002
  2890. #define DDSD_WIDTH 0x00000004
  2891. #define DDSD_PITCH 0x00000008
  2892. #define DDSD_PIXELFORMAT 0x00001000
  2893. #define DDSD_MIPMAPCOUNT 0x00020000
  2894. #define DDSD_LINEARSIZE 0x00080000
  2895. #define DDSD_DEPTH 0x00800000
  2896. #define DDPF_ALPHAPIXELS 0x00000001
  2897. #define DDPF_ALPHA 0x00000002
  2898. #define DDPF_FOURCC 0x00000004
  2899. #define DDPF_INDEXED 0x00000020
  2900. #define DDPF_RGB 0x00000040
  2901. #define DDPF_YUV 0x00000200
  2902. #define DDPF_LUMINANCE 0x00020000
  2903. #define DDPF_BUMPDUDV 0x00080000
  2904. #define DDSCAPS_COMPLEX 0x00000008
  2905. #define DDSCAPS_TEXTURE 0x00001000
  2906. #define DDSCAPS_MIPMAP 0x00400000
  2907. #define DDSCAPS2_VOLUME 0x00200000
  2908. #define DDSCAPS2_CUBEMAP 0x00000200
  2909. #define DDSCAPS2_CUBEMAP_POSITIVEX 0x00000400
  2910. #define DDSCAPS2_CUBEMAP_NEGATIVEX 0x00000800
  2911. #define DDSCAPS2_CUBEMAP_POSITIVEY 0x00001000
  2912. #define DDSCAPS2_CUBEMAP_NEGATIVEY 0x00002000
  2913. #define DDSCAPS2_CUBEMAP_POSITIVEZ 0x00004000
  2914. #define DDSCAPS2_CUBEMAP_NEGATIVEZ 0x00008000
  2915. #define DSCAPS2_CUBEMAP_ALLSIDES (0 \
  2916. | DDSCAPS2_CUBEMAP_POSITIVEX \
  2917. | DDSCAPS2_CUBEMAP_NEGATIVEX \
  2918. | DDSCAPS2_CUBEMAP_POSITIVEY \
  2919. | DDSCAPS2_CUBEMAP_NEGATIVEY \
  2920. | DDSCAPS2_CUBEMAP_POSITIVEZ \
  2921. | DDSCAPS2_CUBEMAP_NEGATIVEZ \
  2922. )
  2923. struct TranslateDdsFormat
  2924. {
  2925. uint32_t m_format;
  2926. TextureFormat::Enum m_textureFormat;
  2927. bool m_srgb;
  2928. };
  2929. static const TranslateDdsFormat s_translateDdsFourccFormat[] =
  2930. {
  2931. { DDS_DXT1, TextureFormat::BC1, false },
  2932. { DDS_DXT2, TextureFormat::BC2, false },
  2933. { DDS_DXT3, TextureFormat::BC2, false },
  2934. { DDS_DXT4, TextureFormat::BC3, false },
  2935. { DDS_DXT5, TextureFormat::BC3, false },
  2936. { DDS_ATI1, TextureFormat::BC4, false },
  2937. { DDS_BC4U, TextureFormat::BC4, false },
  2938. { DDS_ATI2, TextureFormat::BC5, false },
  2939. { DDS_BC5U, TextureFormat::BC5, false },
  2940. { DDS_ETC1, TextureFormat::ETC1, false },
  2941. { DDS_ETC2, TextureFormat::ETC2, false },
  2942. { DDS_ET2A, TextureFormat::ETC2A, false },
  2943. { DDS_PTC2, TextureFormat::PTC12A, false },
  2944. { DDS_PTC4, TextureFormat::PTC14A, false },
  2945. { DDS_ATC , TextureFormat::ATC, false },
  2946. { DDS_ATCE, TextureFormat::ATCE, false },
  2947. { DDS_ATCI, TextureFormat::ATCI, false },
  2948. { DDS_ASTC4x4, TextureFormat::ASTC4x4, false },
  2949. { DDS_ASTC5x5, TextureFormat::ASTC5x5, false },
  2950. { DDS_ASTC6x6, TextureFormat::ASTC6x6, false },
  2951. { DDS_ASTC8x5, TextureFormat::ASTC8x5, false },
  2952. { DDS_ASTC8x6, TextureFormat::ASTC8x6, false },
  2953. { DDS_ASTC10x5, TextureFormat::ASTC10x5, false },
  2954. { DDS_A16B16G16R16, TextureFormat::RGBA16, false },
  2955. { DDS_A16B16G16R16F, TextureFormat::RGBA16F, false },
  2956. { DDPF_RGB|DDPF_ALPHAPIXELS, TextureFormat::BGRA8, false },
  2957. { DDPF_INDEXED, TextureFormat::R8, false },
  2958. { DDPF_LUMINANCE, TextureFormat::R8, false },
  2959. { DDPF_ALPHA, TextureFormat::R8, false },
  2960. { DDS_R16F, TextureFormat::R16F, false },
  2961. { DDS_R32F, TextureFormat::R32F, false },
  2962. { DDS_A8L8, TextureFormat::RG8, false },
  2963. { DDS_G16R16, TextureFormat::RG16, false },
  2964. { DDS_G16R16F, TextureFormat::RG16F, false },
  2965. { DDS_G32R32F, TextureFormat::RG32F, false },
  2966. { DDS_R8G8B8, TextureFormat::RGB8, false },
  2967. { DDS_A8R8G8B8, TextureFormat::BGRA8, false },
  2968. { DDS_A16B16G16R16, TextureFormat::RGBA16, false },
  2969. { DDS_A16B16G16R16F, TextureFormat::RGBA16F, false },
  2970. { DDS_A32B32G32R32F, TextureFormat::RGBA32F, false },
  2971. { DDS_R5G6B5, TextureFormat::R5G6B5, false },
  2972. { DDS_A4R4G4B4, TextureFormat::RGBA4, false },
  2973. { DDS_A1R5G5B5, TextureFormat::RGB5A1, false },
  2974. { DDS_A2B10G10R10, TextureFormat::RGB10A2, false },
  2975. };
  2976. static const TranslateDdsFormat s_translateDxgiFormat[] =
  2977. {
  2978. { DDS_FORMAT_BC1_UNORM, TextureFormat::BC1, false },
  2979. { DDS_FORMAT_BC1_UNORM_SRGB, TextureFormat::BC1, true },
  2980. { DDS_FORMAT_BC2_UNORM, TextureFormat::BC2, false },
  2981. { DDS_FORMAT_BC2_UNORM_SRGB, TextureFormat::BC2, true },
  2982. { DDS_FORMAT_BC3_UNORM, TextureFormat::BC3, false },
  2983. { DDS_FORMAT_BC3_UNORM_SRGB, TextureFormat::BC3, true },
  2984. { DDS_FORMAT_BC4_UNORM, TextureFormat::BC4, false },
  2985. { DDS_FORMAT_BC5_UNORM, TextureFormat::BC5, false },
  2986. { DDS_FORMAT_BC6H_SF16, TextureFormat::BC6H, false },
  2987. { DDS_FORMAT_BC7_UNORM, TextureFormat::BC7, false },
  2988. { DDS_FORMAT_BC7_UNORM_SRGB, TextureFormat::BC7, true },
  2989. { DDS_FORMAT_R1_UNORM, TextureFormat::R1, false },
  2990. { DDS_FORMAT_R8_UNORM, TextureFormat::R8, false },
  2991. { DDS_FORMAT_R16_UNORM, TextureFormat::R16, false },
  2992. { DDS_FORMAT_R16_FLOAT, TextureFormat::R16F, false },
  2993. { DDS_FORMAT_R32_UINT, TextureFormat::R32U, false },
  2994. { DDS_FORMAT_R32_FLOAT, TextureFormat::R32F, false },
  2995. { DDS_FORMAT_R8G8_UNORM, TextureFormat::RG8, false },
  2996. { DDS_FORMAT_R16G16_UNORM, TextureFormat::RG16, false },
  2997. { DDS_FORMAT_R16G16_FLOAT, TextureFormat::RG16F, false },
  2998. { DDS_FORMAT_R32G32_UINT, TextureFormat::RG32U, false },
  2999. { DDS_FORMAT_R32G32_FLOAT, TextureFormat::RG32F, false },
  3000. { DDS_FORMAT_B8G8R8A8_UNORM, TextureFormat::BGRA8, false },
  3001. { DDS_FORMAT_B8G8R8A8_UNORM_SRGB, TextureFormat::BGRA8, true },
  3002. { DDS_FORMAT_R8G8B8A8_UNORM, TextureFormat::RGBA8, false },
  3003. { DDS_FORMAT_R8G8B8A8_UNORM_SRGB, TextureFormat::RGBA8, true },
  3004. { DDS_FORMAT_R16G16B16A16_UNORM, TextureFormat::RGBA16, false },
  3005. { DDS_FORMAT_R16G16B16A16_FLOAT, TextureFormat::RGBA16F, false },
  3006. { DDS_FORMAT_R32G32B32A32_UINT, TextureFormat::RGBA32U, false },
  3007. { DDS_FORMAT_R32G32B32A32_FLOAT, TextureFormat::RGBA32F, false },
  3008. { DDS_FORMAT_B5G6R5_UNORM, TextureFormat::R5G6B5, false },
  3009. { DDS_FORMAT_B4G4R4A4_UNORM, TextureFormat::RGBA4, false },
  3010. { DDS_FORMAT_B5G5R5A1_UNORM, TextureFormat::RGB5A1, false },
  3011. { DDS_FORMAT_R10G10B10A2_UNORM, TextureFormat::RGB10A2, false },
  3012. { DDS_FORMAT_R11G11B10_FLOAT, TextureFormat::RG11B10F, false },
  3013. };
  3014. struct TranslateDdsPixelFormat
  3015. {
  3016. uint32_t m_bitCount;
  3017. uint32_t m_flags;
  3018. uint32_t m_bitmask[4];
  3019. TextureFormat::Enum m_textureFormat;
  3020. };
  3021. static const TranslateDdsPixelFormat s_translateDdsPixelFormat[] =
  3022. {
  3023. { 8, DDPF_LUMINANCE, { 0x000000ff, 0x00000000, 0x00000000, 0x00000000 }, TextureFormat::R8 },
  3024. { 16, DDPF_BUMPDUDV, { 0x000000ff, 0x0000ff00, 0x00000000, 0x00000000 }, TextureFormat::RG8S },
  3025. { 16, DDPF_RGB, { 0x0000ffff, 0x00000000, 0x00000000, 0x00000000 }, TextureFormat::R16U },
  3026. { 16, DDPF_RGB|DDPF_ALPHAPIXELS, { 0x00000f00, 0x000000f0, 0x0000000f, 0x0000f000 }, TextureFormat::RGBA4 },
  3027. { 16, DDPF_RGB, { 0x0000f800, 0x000007e0, 0x0000001f, 0x00000000 }, TextureFormat::R5G6B5 },
  3028. { 16, DDPF_RGB, { 0x00007c00, 0x000003e0, 0x0000001f, 0x00008000 }, TextureFormat::RGB5A1 },
  3029. { 24, DDPF_RGB, { 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000 }, TextureFormat::RGB8 },
  3030. { 24, DDPF_RGB, { 0x000000ff, 0x0000ff00, 0x00ff0000, 0x00000000 }, TextureFormat::RGB8 },
  3031. { 32, DDPF_RGB, { 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000 }, TextureFormat::BGRA8 },
  3032. { 32, DDPF_RGB|DDPF_ALPHAPIXELS, { 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000 }, TextureFormat::RGBA8 },
  3033. { 32, DDPF_BUMPDUDV, { 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000 }, TextureFormat::RGBA8S },
  3034. { 32, DDPF_RGB, { 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000 }, TextureFormat::BGRA8 },
  3035. { 32, DDPF_RGB|DDPF_ALPHAPIXELS, { 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000 }, TextureFormat::BGRA8 }, // D3DFMT_A8R8G8B8
  3036. { 32, DDPF_RGB|DDPF_ALPHAPIXELS, { 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00000000 }, TextureFormat::BGRA8 }, // D3DFMT_X8R8G8B8
  3037. { 32, DDPF_RGB|DDPF_ALPHAPIXELS, { 0x000003ff, 0x000ffc00, 0x3ff00000, 0xc0000000 }, TextureFormat::RGB10A2 },
  3038. { 32, DDPF_RGB, { 0x0000ffff, 0xffff0000, 0x00000000, 0x00000000 }, TextureFormat::RG16 },
  3039. { 32, DDPF_BUMPDUDV, { 0x0000ffff, 0xffff0000, 0x00000000, 0x00000000 }, TextureFormat::RG16S },
  3040. { 32, DDPF_RGB, { 0xffffffff, 0x00000000, 0x00000000, 0x00000000 }, TextureFormat::R32U },
  3041. };
  3042. bool imageParseDds(ImageContainer& _imageContainer, bx::ReaderSeekerI* _reader, bx::Error* _err)
  3043. {
  3044. BX_ERROR_SCOPE(_err);
  3045. int32_t total = 0;
  3046. uint32_t headerSize;
  3047. total += bx::read(_reader, headerSize, _err);
  3048. if (!_err->isOk()
  3049. || headerSize < DDS_HEADER_SIZE)
  3050. {
  3051. return false;
  3052. }
  3053. uint32_t flags;
  3054. total += bx::read(_reader, flags, _err);
  3055. if (!_err->isOk() )
  3056. {
  3057. return false;
  3058. }
  3059. if ( (flags & (DDSD_CAPS|DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT) ) != (DDSD_CAPS|DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT) )
  3060. {
  3061. BX_ERROR_SET(_err, BIMG_ERROR, "DDS: Invalid flags.");
  3062. return false;
  3063. }
  3064. uint32_t height;
  3065. total += bx::read(_reader, height, _err);
  3066. uint32_t width;
  3067. total += bx::read(_reader, width, _err);
  3068. uint32_t pitch;
  3069. total += bx::read(_reader, pitch, _err);
  3070. uint32_t depth;
  3071. total += bx::read(_reader, depth, _err);
  3072. uint32_t mips;
  3073. total += bx::read(_reader, mips, _err);
  3074. bx::skip(_reader, 44); // reserved
  3075. total += 44;
  3076. uint32_t pixelFormatSize;
  3077. total += bx::read(_reader, pixelFormatSize, _err);
  3078. uint32_t pixelFlags;
  3079. total += bx::read(_reader, pixelFlags, _err);
  3080. uint32_t fourcc;
  3081. total += bx::read(_reader, fourcc, _err);
  3082. uint32_t bitCount;
  3083. total += bx::read(_reader, bitCount, _err);
  3084. uint32_t bitmask[4];
  3085. total += bx::read(_reader, bitmask, sizeof(bitmask), _err);
  3086. uint32_t caps[4];
  3087. total += bx::read(_reader, caps, _err);
  3088. bx::skip(_reader, 4);
  3089. total += 4; // reserved
  3090. if (!_err->isOk() )
  3091. {
  3092. return false;
  3093. }
  3094. uint32_t dxgiFormat = 0;
  3095. uint32_t arraySize = 1;
  3096. if (DDPF_FOURCC == (pixelFlags & DDPF_FOURCC)
  3097. && DDS_DX10 == fourcc)
  3098. {
  3099. total += bx::read(_reader, dxgiFormat, _err);
  3100. uint32_t dims;
  3101. total += bx::read(_reader, dims, _err);
  3102. uint32_t miscFlags;
  3103. total += bx::read(_reader, miscFlags, _err);
  3104. total += bx::read(_reader, arraySize, _err);
  3105. uint32_t miscFlags2;
  3106. total += bx::read(_reader, miscFlags2, _err);
  3107. }
  3108. if (!_err->isOk() )
  3109. {
  3110. return false;
  3111. }
  3112. if ( (caps[0] & DDSCAPS_TEXTURE) == 0)
  3113. {
  3114. BX_ERROR_SET(_err, BIMG_ERROR, "DDS: Unsupported caps.");
  3115. return false;
  3116. }
  3117. bool cubeMap = 0 != (caps[1] & DDSCAPS2_CUBEMAP);
  3118. if (cubeMap)
  3119. {
  3120. if ( (caps[1] & DSCAPS2_CUBEMAP_ALLSIDES) != DSCAPS2_CUBEMAP_ALLSIDES)
  3121. {
  3122. // partial cube map is not supported.
  3123. BX_ERROR_SET(_err, BIMG_ERROR, "DDS: Incomplete cubemap.");
  3124. return false;
  3125. }
  3126. }
  3127. TextureFormat::Enum format = TextureFormat::Unknown;
  3128. bool hasAlpha = pixelFlags & DDPF_ALPHAPIXELS;
  3129. bool srgb = false;
  3130. if (dxgiFormat == 0)
  3131. {
  3132. if (DDPF_FOURCC == (pixelFlags & DDPF_FOURCC) )
  3133. {
  3134. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateDdsFourccFormat); ++ii)
  3135. {
  3136. if (s_translateDdsFourccFormat[ii].m_format == fourcc)
  3137. {
  3138. format = s_translateDdsFourccFormat[ii].m_textureFormat;
  3139. break;
  3140. }
  3141. }
  3142. }
  3143. else
  3144. {
  3145. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateDdsPixelFormat); ++ii)
  3146. {
  3147. const TranslateDdsPixelFormat& pf = s_translateDdsPixelFormat[ii];
  3148. if (pf.m_bitCount == bitCount
  3149. && pf.m_flags == pixelFlags
  3150. && pf.m_bitmask[0] == bitmask[0]
  3151. && pf.m_bitmask[1] == bitmask[1]
  3152. && pf.m_bitmask[2] == bitmask[2]
  3153. && pf.m_bitmask[3] == bitmask[3])
  3154. {
  3155. format = pf.m_textureFormat;
  3156. break;
  3157. }
  3158. }
  3159. }
  3160. }
  3161. else
  3162. {
  3163. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateDxgiFormat); ++ii)
  3164. {
  3165. if (s_translateDxgiFormat[ii].m_format == dxgiFormat)
  3166. {
  3167. format = s_translateDxgiFormat[ii].m_textureFormat;
  3168. srgb = s_translateDxgiFormat[ii].m_srgb;
  3169. break;
  3170. }
  3171. }
  3172. }
  3173. if (TextureFormat::Unknown == format)
  3174. {
  3175. BX_ERROR_SET(_err, BIMG_ERROR, "DDS: Unknown texture format.");
  3176. return false;
  3177. }
  3178. _imageContainer.m_allocator = NULL;
  3179. _imageContainer.m_data = NULL;
  3180. _imageContainer.m_size = 0;
  3181. _imageContainer.m_offset = (uint32_t)bx::seek(_reader);
  3182. _imageContainer.m_width = width;
  3183. _imageContainer.m_height = height;
  3184. _imageContainer.m_depth = depth;
  3185. _imageContainer.m_format = format;
  3186. _imageContainer.m_orientation = Orientation::R0;
  3187. _imageContainer.m_numLayers = uint16_t(arraySize);
  3188. _imageContainer.m_numMips = uint8_t( (caps[0] & DDSCAPS_MIPMAP) ? mips : 1);
  3189. _imageContainer.m_hasAlpha = hasAlpha;
  3190. _imageContainer.m_cubeMap = cubeMap;
  3191. _imageContainer.m_ktx = false;
  3192. _imageContainer.m_ktxLE = false;
  3193. _imageContainer.m_srgb = srgb;
  3194. return true;
  3195. }
  3196. ImageContainer* imageParseDds(bx::AllocatorI* _allocator, const void* _src, uint32_t _size, bx::Error* _err)
  3197. {
  3198. return imageParseT<DDS_MAGIC, imageParseDds>(_allocator, _src, _size, _err);
  3199. }
  3200. // KTX
  3201. #define KTX_MAGIC BX_MAKEFOURCC(0xAB, 'K', 'T', 'X')
  3202. #define KTX_HEADER_SIZE 64
  3203. #define KTX_ETC1_RGB8_OES 0x8D64
  3204. #define KTX_COMPRESSED_R11_EAC 0x9270
  3205. #define KTX_COMPRESSED_SIGNED_R11_EAC 0x9271
  3206. #define KTX_COMPRESSED_RG11_EAC 0x9272
  3207. #define KTX_COMPRESSED_SIGNED_RG11_EAC 0x9273
  3208. #define KTX_COMPRESSED_RGB8_ETC2 0x9274
  3209. #define KTX_COMPRESSED_SRGB8_ETC2 0x9275
  3210. #define KTX_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2 0x9276
  3211. #define KTX_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2 0x9277
  3212. #define KTX_COMPRESSED_RGBA8_ETC2_EAC 0x9278
  3213. #define KTX_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC 0x9279
  3214. #define KTX_COMPRESSED_RGB_PVRTC_4BPPV1_IMG 0x8C00
  3215. #define KTX_COMPRESSED_RGB_PVRTC_2BPPV1_IMG 0x8C01
  3216. #define KTX_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG 0x8C02
  3217. #define KTX_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG 0x8C03
  3218. #define KTX_COMPRESSED_RGBA_PVRTC_2BPPV2_IMG 0x9137
  3219. #define KTX_COMPRESSED_RGBA_PVRTC_4BPPV2_IMG 0x9138
  3220. #define KTX_COMPRESSED_RGB_S3TC_DXT1_EXT 0x83F0
  3221. #define KTX_COMPRESSED_RGBA_S3TC_DXT1_EXT 0x83F1
  3222. #define KTX_COMPRESSED_RGBA_S3TC_DXT3_EXT 0x83F2
  3223. #define KTX_COMPRESSED_RGBA_S3TC_DXT5_EXT 0x83F3
  3224. #define KTX_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT 0x8C4D
  3225. #define KTX_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT 0x8C4E
  3226. #define KTX_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT 0x8C4F
  3227. #define KTX_COMPRESSED_LUMINANCE_LATC1_EXT 0x8C70
  3228. #define KTX_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT 0x8C72
  3229. #define KTX_COMPRESSED_RGBA_BPTC_UNORM_ARB 0x8E8C
  3230. #define KTX_COMPRESSED_SRGB_ALPHA_BPTC_UNORM_ARB 0x8E8D
  3231. #define KTX_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB 0x8E8E
  3232. #define KTX_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT_ARB 0x8E8F
  3233. #define KTX_COMPRESSED_SRGB_PVRTC_2BPPV1_EXT 0x8A54
  3234. #define KTX_COMPRESSED_SRGB_PVRTC_4BPPV1_EXT 0x8A55
  3235. #define KTX_COMPRESSED_SRGB_ALPHA_PVRTC_2BPPV1_EXT 0x8A56
  3236. #define KTX_COMPRESSED_SRGB_ALPHA_PVRTC_4BPPV1_EXT 0x8A57
  3237. #define KTX_ATC_RGB_AMD 0x8C92
  3238. #define KTX_ATC_RGBA_EXPLICIT_ALPHA_AMD 0x8C93
  3239. #define KTX_ATC_RGBA_INTERPOLATED_ALPHA_AMD 0x87EE
  3240. #define KTX_COMPRESSED_RGBA_ASTC_4x4_KHR 0x93B0
  3241. #define KTX_COMPRESSED_RGBA_ASTC_5x5_KHR 0x93B2
  3242. #define KTX_COMPRESSED_RGBA_ASTC_6x6_KHR 0x93B4
  3243. #define KTX_COMPRESSED_RGBA_ASTC_8x5_KHR 0x93B5
  3244. #define KTX_COMPRESSED_RGBA_ASTC_8x6_KHR 0x93B6
  3245. #define KTX_COMPRESSED_RGBA_ASTC_10x5_KHR 0x93B8
  3246. #define KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR 0x93D0
  3247. #define KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR 0x93D2
  3248. #define KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR 0x93D4
  3249. #define KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR 0x93D5
  3250. #define KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR 0x93D6
  3251. #define KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR 0x93D8
  3252. #define KTX_A8 0x803C
  3253. #define KTX_R8 0x8229
  3254. #define KTX_R16 0x822A
  3255. #define KTX_RG8 0x822B
  3256. #define KTX_RG16 0x822C
  3257. #define KTX_R16F 0x822D
  3258. #define KTX_R32F 0x822E
  3259. #define KTX_RG16F 0x822F
  3260. #define KTX_RG32F 0x8230
  3261. #define KTX_RGBA8 0x8058
  3262. #define KTX_RGBA16 0x805B
  3263. #define KTX_RGBA16F 0x881A
  3264. #define KTX_R32UI 0x8236
  3265. #define KTX_RG32UI 0x823C
  3266. #define KTX_RGBA32UI 0x8D70
  3267. #define KTX_RGBA32F 0x8814
  3268. #define KTX_RGB565 0x8D62
  3269. #define KTX_RGBA4 0x8056
  3270. #define KTX_RGB5_A1 0x8057
  3271. #define KTX_RGB10_A2 0x8059
  3272. #define KTX_R8I 0x8231
  3273. #define KTX_R8UI 0x8232
  3274. #define KTX_R16I 0x8233
  3275. #define KTX_R16UI 0x8234
  3276. #define KTX_R32I 0x8235
  3277. #define KTX_R32UI 0x8236
  3278. #define KTX_RG8I 0x8237
  3279. #define KTX_RG8UI 0x8238
  3280. #define KTX_RG16I 0x8239
  3281. #define KTX_RG16UI 0x823A
  3282. #define KTX_RG32I 0x823B
  3283. #define KTX_RG32UI 0x823C
  3284. #define KTX_R8_SNORM 0x8F94
  3285. #define KTX_RG8_SNORM 0x8F95
  3286. #define KTX_RGB8_SNORM 0x8F96
  3287. #define KTX_RGBA8_SNORM 0x8F97
  3288. #define KTX_R16_SNORM 0x8F98
  3289. #define KTX_RG16_SNORM 0x8F99
  3290. #define KTX_RGB16_SNORM 0x8F9A
  3291. #define KTX_RGBA16_SNORM 0x8F9B
  3292. #define KTX_SRGB8 0x8C41
  3293. #define KTX_SRGB8_ALPHA8 0x8C43
  3294. #define KTX_RGBA32UI 0x8D70
  3295. #define KTX_RGB32UI 0x8D71
  3296. #define KTX_RGBA16UI 0x8D76
  3297. #define KTX_RGB16UI 0x8D77
  3298. #define KTX_RGBA8UI 0x8D7C
  3299. #define KTX_RGB8UI 0x8D7D
  3300. #define KTX_RGBA32I 0x8D82
  3301. #define KTX_RGB32I 0x8D83
  3302. #define KTX_RGBA16I 0x8D88
  3303. #define KTX_RGB16I 0x8D89
  3304. #define KTX_RGBA8I 0x8D8E
  3305. #define KTX_RGB8 0x8051
  3306. #define KTX_RGB8I 0x8D8F
  3307. #define KTX_RGB9_E5 0x8C3D
  3308. #define KTX_R11F_G11F_B10F 0x8C3A
  3309. #define KTX_ZERO 0
  3310. #define KTX_RED 0x1903
  3311. #define KTX_ALPHA 0x1906
  3312. #define KTX_RGB 0x1907
  3313. #define KTX_RGBA 0x1908
  3314. #define KTX_BGRA 0x80E1
  3315. #define KTX_RG 0x8227
  3316. #define KTX_BYTE 0x1400
  3317. #define KTX_UNSIGNED_BYTE 0x1401
  3318. #define KTX_SHORT 0x1402
  3319. #define KTX_UNSIGNED_SHORT 0x1403
  3320. #define KTX_INT 0x1404
  3321. #define KTX_UNSIGNED_INT 0x1405
  3322. #define KTX_FLOAT 0x1406
  3323. #define KTX_HALF_FLOAT 0x140B
  3324. #define KTX_UNSIGNED_INT_5_9_9_9_REV 0x8C3E
  3325. #define KTX_UNSIGNED_SHORT_5_6_5 0x8363
  3326. #define KTX_UNSIGNED_SHORT_4_4_4_4 0x8033
  3327. #define KTX_UNSIGNED_SHORT_5_5_5_1 0x8034
  3328. #define KTX_UNSIGNED_INT_2_10_10_10_REV 0x8368
  3329. #define KTX_UNSIGNED_INT_10F_11F_11F_REV 0x8C3B
  3330. struct KtxFormatInfo
  3331. {
  3332. uint32_t m_internalFmt;
  3333. uint32_t m_internalFmtSrgb;
  3334. uint32_t m_fmt;
  3335. uint32_t m_type;
  3336. };
  3337. static const KtxFormatInfo s_translateKtxFormat[] =
  3338. {
  3339. { KTX_COMPRESSED_RGBA_S3TC_DXT1_EXT, KTX_COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT, KTX_COMPRESSED_RGBA_S3TC_DXT1_EXT, KTX_ZERO, }, // BC1
  3340. { KTX_COMPRESSED_RGBA_S3TC_DXT3_EXT, KTX_COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT, KTX_COMPRESSED_RGBA_S3TC_DXT3_EXT, KTX_ZERO, }, // BC2
  3341. { KTX_COMPRESSED_RGBA_S3TC_DXT5_EXT, KTX_COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT, KTX_COMPRESSED_RGBA_S3TC_DXT5_EXT, KTX_ZERO, }, // BC3
  3342. { KTX_COMPRESSED_LUMINANCE_LATC1_EXT, KTX_ZERO, KTX_COMPRESSED_LUMINANCE_LATC1_EXT, KTX_ZERO, }, // BC4
  3343. { KTX_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT, KTX_ZERO, KTX_COMPRESSED_LUMINANCE_ALPHA_LATC2_EXT, KTX_ZERO, }, // BC5
  3344. { KTX_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB, KTX_ZERO, KTX_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB, KTX_ZERO, }, // BC6H
  3345. { KTX_COMPRESSED_RGBA_BPTC_UNORM_ARB, KTX_ZERO, KTX_COMPRESSED_RGBA_BPTC_UNORM_ARB, KTX_ZERO, }, // BC7
  3346. { KTX_ETC1_RGB8_OES, KTX_ZERO, KTX_ETC1_RGB8_OES, KTX_ZERO, }, // ETC1
  3347. { KTX_COMPRESSED_RGB8_ETC2, KTX_ZERO, KTX_COMPRESSED_RGB8_ETC2, KTX_ZERO, }, // ETC2
  3348. { KTX_COMPRESSED_RGBA8_ETC2_EAC, KTX_COMPRESSED_SRGB8_ETC2, KTX_COMPRESSED_RGBA8_ETC2_EAC, KTX_ZERO, }, // ETC2A
  3349. { KTX_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2, KTX_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2, KTX_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2, KTX_ZERO, }, // ETC2A1
  3350. { KTX_COMPRESSED_RGB_PVRTC_2BPPV1_IMG, KTX_COMPRESSED_SRGB_PVRTC_2BPPV1_EXT, KTX_COMPRESSED_RGB_PVRTC_2BPPV1_IMG, KTX_ZERO, }, // PTC12
  3351. { KTX_COMPRESSED_RGB_PVRTC_4BPPV1_IMG, KTX_COMPRESSED_SRGB_PVRTC_4BPPV1_EXT, KTX_COMPRESSED_RGB_PVRTC_4BPPV1_IMG, KTX_ZERO, }, // PTC14
  3352. { KTX_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG, KTX_COMPRESSED_SRGB_ALPHA_PVRTC_2BPPV1_EXT, KTX_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG, KTX_ZERO, }, // PTC12A
  3353. { KTX_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG, KTX_COMPRESSED_SRGB_ALPHA_PVRTC_4BPPV1_EXT, KTX_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG, KTX_ZERO, }, // PTC14A
  3354. { KTX_COMPRESSED_RGBA_PVRTC_2BPPV2_IMG, KTX_ZERO, KTX_COMPRESSED_RGBA_PVRTC_2BPPV2_IMG, KTX_ZERO, }, // PTC22
  3355. { KTX_COMPRESSED_RGBA_PVRTC_4BPPV2_IMG, KTX_ZERO, KTX_COMPRESSED_RGBA_PVRTC_4BPPV2_IMG, KTX_ZERO, }, // PTC24
  3356. { KTX_ATC_RGB_AMD, KTX_ZERO, KTX_ATC_RGB_AMD, KTX_ZERO, }, // ATC
  3357. { KTX_ATC_RGBA_EXPLICIT_ALPHA_AMD, KTX_ZERO, KTX_ATC_RGBA_EXPLICIT_ALPHA_AMD, KTX_ZERO, }, // ATCE
  3358. { KTX_ATC_RGBA_INTERPOLATED_ALPHA_AMD, KTX_ZERO, KTX_ATC_RGBA_INTERPOLATED_ALPHA_AMD, KTX_ZERO, }, // ATCI
  3359. { KTX_COMPRESSED_RGBA_ASTC_4x4_KHR, KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR, KTX_COMPRESSED_RGBA_ASTC_4x4_KHR, KTX_ZERO, }, // ASTC4x4
  3360. { KTX_COMPRESSED_RGBA_ASTC_5x5_KHR, KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR, KTX_COMPRESSED_RGBA_ASTC_5x5_KHR, KTX_ZERO, }, // ASTC5x5
  3361. { KTX_COMPRESSED_RGBA_ASTC_6x6_KHR, KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR, KTX_COMPRESSED_RGBA_ASTC_6x6_KHR, KTX_ZERO, }, // ASTC6x6
  3362. { KTX_COMPRESSED_RGBA_ASTC_8x5_KHR, KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR, KTX_COMPRESSED_RGBA_ASTC_8x5_KHR, KTX_ZERO, }, // ASTC8x5
  3363. { KTX_COMPRESSED_RGBA_ASTC_8x6_KHR, KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR, KTX_COMPRESSED_RGBA_ASTC_8x6_KHR, KTX_ZERO, }, // ASTC8x6
  3364. { KTX_COMPRESSED_RGBA_ASTC_10x5_KHR, KTX_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR, KTX_COMPRESSED_RGBA_ASTC_10x5_KHR, KTX_ZERO, }, // ASTC10x5
  3365. { KTX_ZERO, KTX_ZERO, KTX_ZERO, KTX_ZERO, }, // Unknown
  3366. { KTX_ZERO, KTX_ZERO, KTX_ZERO, KTX_ZERO, }, // R1
  3367. { KTX_ALPHA, KTX_ZERO, KTX_ALPHA, KTX_UNSIGNED_BYTE, }, // A8
  3368. { KTX_R8, KTX_ZERO, KTX_RED, KTX_UNSIGNED_BYTE, }, // R8
  3369. { KTX_R8I, KTX_ZERO, KTX_RED, KTX_BYTE, }, // R8S
  3370. { KTX_R8UI, KTX_ZERO, KTX_RED, KTX_UNSIGNED_BYTE, }, // R8S
  3371. { KTX_R8_SNORM, KTX_ZERO, KTX_RED, KTX_BYTE, }, // R8S
  3372. { KTX_R16, KTX_ZERO, KTX_RED, KTX_UNSIGNED_SHORT, }, // R16
  3373. { KTX_R16I, KTX_ZERO, KTX_RED, KTX_SHORT, }, // R16I
  3374. { KTX_R16UI, KTX_ZERO, KTX_RED, KTX_UNSIGNED_SHORT, }, // R16U
  3375. { KTX_R16F, KTX_ZERO, KTX_RED, KTX_HALF_FLOAT, }, // R16F
  3376. { KTX_R16_SNORM, KTX_ZERO, KTX_RED, KTX_SHORT, }, // R16S
  3377. { KTX_R32I, KTX_ZERO, KTX_RED, KTX_INT, }, // R32I
  3378. { KTX_R32UI, KTX_ZERO, KTX_RED, KTX_UNSIGNED_INT, }, // R32U
  3379. { KTX_R32F, KTX_ZERO, KTX_RED, KTX_FLOAT, }, // R32F
  3380. { KTX_RG8, KTX_ZERO, KTX_RG, KTX_UNSIGNED_BYTE, }, // RG8
  3381. { KTX_RG8I, KTX_ZERO, KTX_RG, KTX_BYTE, }, // RG8I
  3382. { KTX_RG8UI, KTX_ZERO, KTX_RG, KTX_UNSIGNED_BYTE, }, // RG8U
  3383. { KTX_RG8_SNORM, KTX_ZERO, KTX_RG, KTX_BYTE, }, // RG8S
  3384. { KTX_RG16, KTX_ZERO, KTX_RG, KTX_UNSIGNED_SHORT, }, // RG16
  3385. { KTX_RG16I, KTX_ZERO, KTX_RG, KTX_SHORT, }, // RG16
  3386. { KTX_RG16UI, KTX_ZERO, KTX_RG, KTX_UNSIGNED_SHORT, }, // RG16
  3387. { KTX_RG16F, KTX_ZERO, KTX_RG, KTX_FLOAT, }, // RG16F
  3388. { KTX_RG16_SNORM, KTX_ZERO, KTX_RG, KTX_SHORT, }, // RG16S
  3389. { KTX_RG32I, KTX_ZERO, KTX_RG, KTX_INT, }, // RG32I
  3390. { KTX_RG32UI, KTX_ZERO, KTX_RG, KTX_UNSIGNED_INT, }, // RG32U
  3391. { KTX_RG32F, KTX_ZERO, KTX_RG, KTX_FLOAT, }, // RG32F
  3392. { KTX_RGB8, KTX_SRGB8, KTX_RGB, KTX_UNSIGNED_BYTE, }, // RGB8
  3393. { KTX_RGB8I, KTX_ZERO, KTX_RGB, KTX_BYTE, }, // RGB8I
  3394. { KTX_RGB8UI, KTX_ZERO, KTX_RGB, KTX_UNSIGNED_BYTE, }, // RGB8U
  3395. { KTX_RGB8_SNORM, KTX_ZERO, KTX_RGB, KTX_BYTE, }, // RGB8S
  3396. { KTX_RGB9_E5, KTX_ZERO, KTX_RGB, KTX_UNSIGNED_INT_5_9_9_9_REV, }, // RGB9E5F
  3397. { KTX_BGRA, KTX_SRGB8_ALPHA8, KTX_BGRA, KTX_UNSIGNED_BYTE, }, // BGRA8
  3398. { KTX_RGBA8, KTX_SRGB8_ALPHA8, KTX_RGBA, KTX_UNSIGNED_BYTE, }, // RGBA8
  3399. { KTX_RGBA8I, KTX_ZERO, KTX_RGBA, KTX_BYTE, }, // RGBA8I
  3400. { KTX_RGBA8UI, KTX_ZERO, KTX_RGBA, KTX_UNSIGNED_BYTE, }, // RGBA8U
  3401. { KTX_RGBA8_SNORM, KTX_ZERO, KTX_RGBA, KTX_BYTE, }, // RGBA8S
  3402. { KTX_RGBA16, KTX_ZERO, KTX_RGBA, KTX_UNSIGNED_SHORT, }, // RGBA16
  3403. { KTX_RGBA16I, KTX_ZERO, KTX_RGBA, KTX_SHORT, }, // RGBA16I
  3404. { KTX_RGBA16UI, KTX_ZERO, KTX_RGBA, KTX_UNSIGNED_SHORT, }, // RGBA16U
  3405. { KTX_RGBA16F, KTX_ZERO, KTX_RGBA, KTX_HALF_FLOAT, }, // RGBA16F
  3406. { KTX_RGBA16_SNORM, KTX_ZERO, KTX_RGBA, KTX_SHORT, }, // RGBA16S
  3407. { KTX_RGBA32I, KTX_ZERO, KTX_RGBA, KTX_INT, }, // RGBA32I
  3408. { KTX_RGBA32UI, KTX_ZERO, KTX_RGBA, KTX_UNSIGNED_INT, }, // RGBA32U
  3409. { KTX_RGBA32F, KTX_ZERO, KTX_RGBA, KTX_FLOAT, }, // RGBA32F
  3410. { KTX_RGB565, KTX_ZERO, KTX_RGB, KTX_UNSIGNED_SHORT_5_6_5, }, // R5G6B5
  3411. { KTX_RGBA4, KTX_ZERO, KTX_RGBA, KTX_UNSIGNED_SHORT_4_4_4_4, }, // RGBA4
  3412. { KTX_RGB5_A1, KTX_ZERO, KTX_RGBA, KTX_UNSIGNED_SHORT_5_5_5_1, }, // RGB5A1
  3413. { KTX_RGB10_A2, KTX_ZERO, KTX_RGBA, KTX_UNSIGNED_INT_2_10_10_10_REV, }, // RGB10A2
  3414. { KTX_R11F_G11F_B10F, KTX_ZERO, KTX_RGB, KTX_UNSIGNED_INT_10F_11F_11F_REV, }, // RG11B10F
  3415. };
  3416. BX_STATIC_ASSERT(TextureFormat::UnknownDepth == BX_COUNTOF(s_translateKtxFormat) );
  3417. struct KtxFormatInfo2
  3418. {
  3419. uint32_t m_internalFmt;
  3420. TextureFormat::Enum m_format;
  3421. };
  3422. static const KtxFormatInfo2 s_translateKtxFormat2[] =
  3423. {
  3424. { KTX_A8, TextureFormat::A8 },
  3425. { KTX_RED, TextureFormat::R8 },
  3426. { KTX_RGB, TextureFormat::RGB8 },
  3427. { KTX_RGBA, TextureFormat::RGBA8 },
  3428. { KTX_COMPRESSED_RGB_S3TC_DXT1_EXT, TextureFormat::BC1 },
  3429. };
  3430. bool imageParseKtx(ImageContainer& _imageContainer, bx::ReaderSeekerI* _reader, bx::Error* _err)
  3431. {
  3432. BX_ERROR_SCOPE(_err);
  3433. uint8_t identifier[8];
  3434. bx::read(_reader, identifier);
  3435. if (identifier[1] != '1'
  3436. && identifier[2] != '1')
  3437. {
  3438. return false;
  3439. }
  3440. uint32_t endianness;
  3441. bx::read(_reader, endianness);
  3442. bool fromLittleEndian = 0x04030201 == endianness;
  3443. uint32_t glType;
  3444. bx::readHE(_reader, glType, fromLittleEndian);
  3445. uint32_t glTypeSize;
  3446. bx::readHE(_reader, glTypeSize, fromLittleEndian);
  3447. uint32_t glFormat;
  3448. bx::readHE(_reader, glFormat, fromLittleEndian);
  3449. uint32_t glInternalFormat;
  3450. bx::readHE(_reader, glInternalFormat, fromLittleEndian);
  3451. uint32_t glBaseInternalFormat;
  3452. bx::readHE(_reader, glBaseInternalFormat, fromLittleEndian);
  3453. uint32_t width;
  3454. bx::readHE(_reader, width, fromLittleEndian);
  3455. uint32_t height;
  3456. bx::readHE(_reader, height, fromLittleEndian);
  3457. uint32_t depth;
  3458. bx::readHE(_reader, depth, fromLittleEndian);
  3459. uint32_t numberOfArrayElements;
  3460. bx::readHE(_reader, numberOfArrayElements, fromLittleEndian);
  3461. uint32_t numFaces;
  3462. bx::readHE(_reader, numFaces, fromLittleEndian);
  3463. uint32_t numMips;
  3464. bx::readHE(_reader, numMips, fromLittleEndian);
  3465. uint32_t metaDataSize;
  3466. bx::readHE(_reader, metaDataSize, fromLittleEndian);
  3467. // skip meta garbage...
  3468. int64_t offset = bx::skip(_reader, metaDataSize);
  3469. TextureFormat::Enum format = TextureFormat::Unknown;
  3470. bool hasAlpha = false;
  3471. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateKtxFormat); ++ii)
  3472. {
  3473. if (s_translateKtxFormat[ii].m_internalFmt == glInternalFormat)
  3474. {
  3475. format = TextureFormat::Enum(ii);
  3476. break;
  3477. }
  3478. }
  3479. if (TextureFormat::Unknown == format)
  3480. {
  3481. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateKtxFormat2); ++ii)
  3482. {
  3483. if (s_translateKtxFormat2[ii].m_internalFmt == glInternalFormat)
  3484. {
  3485. format = s_translateKtxFormat2[ii].m_format;
  3486. break;
  3487. }
  3488. }
  3489. }
  3490. _imageContainer.m_allocator = NULL;
  3491. _imageContainer.m_data = NULL;
  3492. _imageContainer.m_size = 0;
  3493. _imageContainer.m_offset = (uint32_t)offset;
  3494. _imageContainer.m_width = width;
  3495. _imageContainer.m_height = height;
  3496. _imageContainer.m_depth = depth;
  3497. _imageContainer.m_format = format;
  3498. _imageContainer.m_orientation = Orientation::R0;
  3499. _imageContainer.m_numLayers = uint16_t(bx::max<uint32_t>(numberOfArrayElements, 1) );
  3500. _imageContainer.m_numMips = uint8_t(bx::max<uint32_t>(numMips, 1) );
  3501. _imageContainer.m_hasAlpha = hasAlpha;
  3502. _imageContainer.m_cubeMap = numFaces > 1;
  3503. _imageContainer.m_ktx = true;
  3504. _imageContainer.m_ktxLE = fromLittleEndian;
  3505. _imageContainer.m_srgb = false;
  3506. if (TextureFormat::Unknown == format)
  3507. {
  3508. BX_ERROR_SET(_err, BIMG_ERROR, "Unrecognized image format.");
  3509. return false;
  3510. }
  3511. return true;
  3512. }
  3513. ImageContainer* imageParseKtx(bx::AllocatorI* _allocator, const void* _src, uint32_t _size, bx::Error* _err)
  3514. {
  3515. return imageParseT<KTX_MAGIC, imageParseKtx>(_allocator, _src, _size, _err);
  3516. }
  3517. // PVR3
  3518. #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) )
  3519. #define PVR3_MAGIC BX_MAKEFOURCC('P', 'V', 'R', 3)
  3520. #define PVR3_HEADER_SIZE 52
  3521. #define PVR3_PVRTC1_2BPP_RGB 0
  3522. #define PVR3_PVRTC1_2BPP_RGBA 1
  3523. #define PVR3_PVRTC1_4BPP_RGB 2
  3524. #define PVR3_PVRTC1_4BPP_RGBA 3
  3525. #define PVR3_PVRTC2_2BPP_RGBA 4
  3526. #define PVR3_PVRTC2_4BPP_RGBA 5
  3527. #define PVR3_ETC1 6
  3528. #define PVR3_DXT1 7
  3529. #define PVR3_DXT2 8
  3530. #define PVR3_DXT3 9
  3531. #define PVR3_DXT4 10
  3532. #define PVR3_DXT5 11
  3533. #define PVR3_BC4 12
  3534. #define PVR3_BC5 13
  3535. #define PVR3_R8 PVR3_MAKE8CC('r', 0, 0, 0, 8, 0, 0, 0)
  3536. #define PVR3_R16 PVR3_MAKE8CC('r', 0, 0, 0, 16, 0, 0, 0)
  3537. #define PVR3_R32 PVR3_MAKE8CC('r', 0, 0, 0, 32, 0, 0, 0)
  3538. #define PVR3_RG8 PVR3_MAKE8CC('r', 'g', 0, 0, 8, 8, 0, 0)
  3539. #define PVR3_RG16 PVR3_MAKE8CC('r', 'g', 0, 0, 16, 16, 0, 0)
  3540. #define PVR3_RG32 PVR3_MAKE8CC('r', 'g', 0, 0, 32, 32, 0, 0)
  3541. #define PVR3_BGRA8 PVR3_MAKE8CC('b', 'g', 'r', 'a', 8, 8, 8, 8)
  3542. #define PVR3_RGBA16 PVR3_MAKE8CC('r', 'g', 'b', 'a', 16, 16, 16, 16)
  3543. #define PVR3_RGBA32 PVR3_MAKE8CC('r', 'g', 'b', 'a', 32, 32, 32, 32)
  3544. #define PVR3_RGB565 PVR3_MAKE8CC('r', 'g', 'b', 0, 5, 6, 5, 0)
  3545. #define PVR3_RGBA4 PVR3_MAKE8CC('r', 'g', 'b', 'a', 4, 4, 4, 4)
  3546. #define PVR3_RGBA51 PVR3_MAKE8CC('r', 'g', 'b', 'a', 5, 5, 5, 1)
  3547. #define PVR3_RGB10A2 PVR3_MAKE8CC('r', 'g', 'b', 'a', 10, 10, 10, 2)
  3548. #define PVR3_CHANNEL_TYPE_ANY UINT32_MAX
  3549. #define PVR3_CHANNEL_TYPE_FLOAT UINT32_C(12)
  3550. struct TranslatePvr3Format
  3551. {
  3552. uint64_t m_format;
  3553. uint32_t m_channelTypeMask;
  3554. TextureFormat::Enum m_textureFormat;
  3555. };
  3556. static const TranslatePvr3Format s_translatePvr3Format[] =
  3557. {
  3558. { PVR3_PVRTC1_2BPP_RGB, PVR3_CHANNEL_TYPE_ANY, TextureFormat::PTC12 },
  3559. { PVR3_PVRTC1_2BPP_RGBA, PVR3_CHANNEL_TYPE_ANY, TextureFormat::PTC12A },
  3560. { PVR3_PVRTC1_4BPP_RGB, PVR3_CHANNEL_TYPE_ANY, TextureFormat::PTC14 },
  3561. { PVR3_PVRTC1_4BPP_RGBA, PVR3_CHANNEL_TYPE_ANY, TextureFormat::PTC14A },
  3562. { PVR3_PVRTC2_2BPP_RGBA, PVR3_CHANNEL_TYPE_ANY, TextureFormat::PTC22 },
  3563. { PVR3_PVRTC2_4BPP_RGBA, PVR3_CHANNEL_TYPE_ANY, TextureFormat::PTC24 },
  3564. { PVR3_ETC1, PVR3_CHANNEL_TYPE_ANY, TextureFormat::ETC1 },
  3565. { PVR3_DXT1, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC1 },
  3566. { PVR3_DXT2, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC2 },
  3567. { PVR3_DXT3, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC2 },
  3568. { PVR3_DXT4, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC3 },
  3569. { PVR3_DXT5, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC3 },
  3570. { PVR3_BC4, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC4 },
  3571. { PVR3_BC5, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BC5 },
  3572. { PVR3_R8, PVR3_CHANNEL_TYPE_ANY, TextureFormat::R8 },
  3573. { PVR3_R16, PVR3_CHANNEL_TYPE_ANY, TextureFormat::R16U },
  3574. { PVR3_R16, PVR3_CHANNEL_TYPE_FLOAT, TextureFormat::R16F },
  3575. { PVR3_R32, PVR3_CHANNEL_TYPE_ANY, TextureFormat::R32U },
  3576. { PVR3_R32, PVR3_CHANNEL_TYPE_FLOAT, TextureFormat::R32F },
  3577. { PVR3_RG8, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RG8 },
  3578. { PVR3_RG16, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RG16 },
  3579. { PVR3_RG16, PVR3_CHANNEL_TYPE_FLOAT, TextureFormat::RG16F },
  3580. { PVR3_RG32, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RG16 },
  3581. { PVR3_RG32, PVR3_CHANNEL_TYPE_FLOAT, TextureFormat::RG32F },
  3582. { PVR3_BGRA8, PVR3_CHANNEL_TYPE_ANY, TextureFormat::BGRA8 },
  3583. { PVR3_RGBA16, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RGBA16 },
  3584. { PVR3_RGBA16, PVR3_CHANNEL_TYPE_FLOAT, TextureFormat::RGBA16F },
  3585. { PVR3_RGBA32, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RGBA32U },
  3586. { PVR3_RGBA32, PVR3_CHANNEL_TYPE_FLOAT, TextureFormat::RGBA32F },
  3587. { PVR3_RGB565, PVR3_CHANNEL_TYPE_ANY, TextureFormat::R5G6B5 },
  3588. { PVR3_RGBA4, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RGBA4 },
  3589. { PVR3_RGBA51, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RGB5A1 },
  3590. { PVR3_RGB10A2, PVR3_CHANNEL_TYPE_ANY, TextureFormat::RGB10A2 },
  3591. };
  3592. bool imageParsePvr3(ImageContainer& _imageContainer, bx::ReaderSeekerI* _reader, bx::Error* _err)
  3593. {
  3594. BX_ERROR_SCOPE(_err);
  3595. uint32_t flags;
  3596. bx::read(_reader, flags);
  3597. uint64_t pixelFormat;
  3598. bx::read(_reader, pixelFormat);
  3599. uint32_t colorSpace;
  3600. bx::read(_reader, colorSpace); // 0 - linearRGB, 1 - sRGB
  3601. uint32_t channelType;
  3602. bx::read(_reader, channelType);
  3603. uint32_t height;
  3604. bx::read(_reader, height);
  3605. uint32_t width;
  3606. bx::read(_reader, width);
  3607. uint32_t depth;
  3608. bx::read(_reader, depth);
  3609. uint32_t numSurfaces;
  3610. bx::read(_reader, numSurfaces);
  3611. uint32_t numFaces;
  3612. bx::read(_reader, numFaces);
  3613. uint32_t numMips;
  3614. bx::read(_reader, numMips);
  3615. uint32_t metaDataSize;
  3616. bx::read(_reader, metaDataSize);
  3617. // skip meta garbage...
  3618. int64_t offset = bx::skip(_reader, metaDataSize);
  3619. TextureFormat::Enum format = TextureFormat::Unknown;
  3620. bool hasAlpha = false;
  3621. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translatePvr3Format); ++ii)
  3622. {
  3623. if (s_translatePvr3Format[ii].m_format == pixelFormat
  3624. && channelType == (s_translatePvr3Format[ii].m_channelTypeMask & channelType) )
  3625. {
  3626. format = s_translatePvr3Format[ii].m_textureFormat;
  3627. break;
  3628. }
  3629. }
  3630. _imageContainer.m_allocator = NULL;
  3631. _imageContainer.m_data = NULL;
  3632. _imageContainer.m_size = 0;
  3633. _imageContainer.m_offset = (uint32_t)offset;
  3634. _imageContainer.m_width = width;
  3635. _imageContainer.m_height = height;
  3636. _imageContainer.m_depth = depth;
  3637. _imageContainer.m_format = format;
  3638. _imageContainer.m_orientation = Orientation::R0;
  3639. _imageContainer.m_numLayers = 1;
  3640. _imageContainer.m_numMips = uint8_t(bx::max<uint32_t>(numMips, 1) );
  3641. _imageContainer.m_hasAlpha = hasAlpha;
  3642. _imageContainer.m_cubeMap = numFaces > 1;
  3643. _imageContainer.m_ktx = false;
  3644. _imageContainer.m_ktxLE = false;
  3645. _imageContainer.m_srgb = colorSpace > 0;
  3646. return TextureFormat::Unknown != format;
  3647. }
  3648. ImageContainer* imageParsePvr3(bx::AllocatorI* _allocator, const void* _src, uint32_t _size, bx::Error* _err)
  3649. {
  3650. return imageParseT<PVR3_MAGIC, imageParsePvr3>(_allocator, _src, _size, _err);
  3651. }
  3652. bool imageParse(ImageContainer& _imageContainer, bx::ReaderSeekerI* _reader, bx::Error* _err)
  3653. {
  3654. BX_ERROR_SCOPE(_err);
  3655. uint32_t magic;
  3656. bx::read(_reader, magic, _err);
  3657. if (DDS_MAGIC == magic)
  3658. {
  3659. return imageParseDds(_imageContainer, _reader, _err);
  3660. }
  3661. else if (KTX_MAGIC == magic)
  3662. {
  3663. return imageParseKtx(_imageContainer, _reader, _err);
  3664. }
  3665. else if (PVR3_MAGIC == magic)
  3666. {
  3667. return imageParsePvr3(_imageContainer, _reader, _err);
  3668. }
  3669. else if (BIMG_CHUNK_MAGIC_GNF == magic)
  3670. {
  3671. return imageParseGnf(_imageContainer, _reader, _err);
  3672. }
  3673. else if (BIMG_CHUNK_MAGIC_TEX == magic)
  3674. {
  3675. TextureCreate tc;
  3676. bx::read(_reader, tc);
  3677. _imageContainer.m_format = tc.m_format;
  3678. _imageContainer.m_orientation = Orientation::R0;
  3679. _imageContainer.m_offset = UINT32_MAX;
  3680. _imageContainer.m_allocator = NULL;
  3681. if (NULL == tc.m_mem)
  3682. {
  3683. _imageContainer.m_data = NULL;
  3684. _imageContainer.m_size = 0;
  3685. }
  3686. else
  3687. {
  3688. _imageContainer.m_data = tc.m_mem->data;
  3689. _imageContainer.m_size = tc.m_mem->size;
  3690. }
  3691. _imageContainer.m_width = tc.m_width;
  3692. _imageContainer.m_height = tc.m_height;
  3693. _imageContainer.m_depth = tc.m_depth;
  3694. _imageContainer.m_numLayers = tc.m_numLayers;
  3695. _imageContainer.m_numMips = tc.m_numMips;
  3696. _imageContainer.m_hasAlpha = false;
  3697. _imageContainer.m_cubeMap = tc.m_cubeMap;
  3698. _imageContainer.m_ktx = false;
  3699. _imageContainer.m_ktxLE = false;
  3700. _imageContainer.m_srgb = false;
  3701. return _err->isOk();
  3702. }
  3703. BX_TRACE("Unrecognized image format (magic: 0x%08x)!", magic);
  3704. BX_ERROR_SET(_err, BIMG_ERROR, "Unrecognized image format.");
  3705. return false;
  3706. }
  3707. bool imageParse(ImageContainer& _imageContainer, const void* _data, uint32_t _size, bx::Error* _err)
  3708. {
  3709. BX_ERROR_SCOPE(_err);
  3710. bx::MemoryReader reader(_data, _size);
  3711. return imageParse(_imageContainer, &reader, _err);
  3712. }
  3713. 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)
  3714. {
  3715. const uint8_t* src = (const uint8_t*)_src;
  3716. uint8_t* dst = (uint8_t*)_dst;
  3717. const uint32_t srcBpp = s_imageBlockInfo[_srcFormat].bitsPerPixel;
  3718. const uint32_t srcPitch = _width*srcBpp/8;
  3719. for (uint32_t zz = 0; zz < _depth; ++zz, src += _height*srcPitch, dst += _height*_dstPitch)
  3720. {
  3721. if (isCompressed(_srcFormat))
  3722. {
  3723. uint32_t size = imageGetSize(NULL, uint16_t(_width), uint16_t(_height), 0, false, false, 1, TextureFormat::RGBA8);
  3724. void* temp = BX_ALLOC(_allocator, size);
  3725. imageDecodeToRgba8(_allocator, temp, _src, _width, _height, _width*4, _srcFormat);
  3726. imageConvert(_allocator, dst, TextureFormat::R8, temp, TextureFormat::RGBA8, _width, _height, 1, _width*4, _dstPitch);
  3727. BX_FREE(_allocator, temp);
  3728. }
  3729. else
  3730. {
  3731. imageConvert(_allocator, dst, TextureFormat::R8, src, _srcFormat, _width, _height, 1, srcPitch, _dstPitch);
  3732. }
  3733. }
  3734. }
  3735. void imageDecodeToBgra8(bx::AllocatorI* _allocator, void* _dst, const void* _src, uint32_t _width, uint32_t _height, uint32_t _dstPitch, TextureFormat::Enum _srcFormat)
  3736. {
  3737. const uint8_t* src = (const uint8_t*)_src;
  3738. uint8_t* dst = (uint8_t*)_dst;
  3739. uint32_t width = _width/4;
  3740. uint32_t height = _height/4;
  3741. uint8_t temp[16*4];
  3742. switch (_srcFormat)
  3743. {
  3744. case TextureFormat::BC1:
  3745. if (BX_ENABLED(BIMG_DECODE_BC1) )
  3746. {
  3747. for (uint32_t yy = 0; yy < height; ++yy)
  3748. {
  3749. for (uint32_t xx = 0; xx < width; ++xx)
  3750. {
  3751. decodeBlockDxt1(temp, src);
  3752. src += 8;
  3753. uint8_t* block = &dst[yy*_dstPitch*4 + xx*16];
  3754. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  3755. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  3756. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  3757. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  3758. }
  3759. }
  3760. }
  3761. else
  3762. {
  3763. BX_WARN(false, "BC1 decoder is disabled (BIMG_DECODE_BC1).");
  3764. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00) );
  3765. }
  3766. break;
  3767. case TextureFormat::BC2:
  3768. if (BX_ENABLED(BIMG_DECODE_BC2) )
  3769. {
  3770. for (uint32_t yy = 0; yy < height; ++yy)
  3771. {
  3772. for (uint32_t xx = 0; xx < width; ++xx)
  3773. {
  3774. decodeBlockDxt23A(temp+3, src);
  3775. src += 8;
  3776. decodeBlockDxt(temp, src);
  3777. src += 8;
  3778. uint8_t* block = &dst[yy*_dstPitch*4 + xx*16];
  3779. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  3780. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  3781. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  3782. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  3783. }
  3784. }
  3785. }
  3786. else
  3787. {
  3788. BX_WARN(false, "BC2 decoder is disabled (BIMG_DECODE_BC2).");
  3789. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00) );
  3790. }
  3791. break;
  3792. case TextureFormat::BC3:
  3793. if (BX_ENABLED(BIMG_DECODE_BC3) )
  3794. {
  3795. for (uint32_t yy = 0; yy < height; ++yy)
  3796. {
  3797. for (uint32_t xx = 0; xx < width; ++xx)
  3798. {
  3799. decodeBlockDxt45A(temp+3, src);
  3800. src += 8;
  3801. decodeBlockDxt(temp, src);
  3802. src += 8;
  3803. uint8_t* block = &dst[yy*_dstPitch*4 + xx*16];
  3804. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  3805. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  3806. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  3807. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  3808. }
  3809. }
  3810. }
  3811. else
  3812. {
  3813. BX_WARN(false, "BC3 decoder is disabled (BIMG_DECODE_BC3).");
  3814. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00) );
  3815. }
  3816. break;
  3817. case TextureFormat::BC4:
  3818. if (BX_ENABLED(BIMG_DECODE_BC4) )
  3819. {
  3820. for (uint32_t yy = 0; yy < height; ++yy)
  3821. {
  3822. for (uint32_t xx = 0; xx < width; ++xx)
  3823. {
  3824. decodeBlockDxt45A(temp, src);
  3825. src += 8;
  3826. uint8_t* block = &dst[yy*_dstPitch*4 + xx*16];
  3827. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  3828. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  3829. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  3830. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  3831. }
  3832. }
  3833. }
  3834. else
  3835. {
  3836. BX_WARN(false, "BC4 decoder is disabled (BIMG_DECODE_BC4).");
  3837. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00) );
  3838. }
  3839. break;
  3840. case TextureFormat::BC5:
  3841. if (BX_ENABLED(BIMG_DECODE_BC5) )
  3842. {
  3843. for (uint32_t yy = 0; yy < height; ++yy)
  3844. {
  3845. for (uint32_t xx = 0; xx < width; ++xx)
  3846. {
  3847. decodeBlockDxt45A(temp+2, src);
  3848. src += 8;
  3849. decodeBlockDxt45A(temp+1, src);
  3850. src += 8;
  3851. for (uint32_t ii = 0; ii < 16; ++ii)
  3852. {
  3853. float nx = temp[ii*4+2]*2.0f/255.0f - 1.0f;
  3854. float ny = temp[ii*4+1]*2.0f/255.0f - 1.0f;
  3855. float nz = bx::sqrt(1.0f - nx*nx - ny*ny);
  3856. temp[ii*4+0] = uint8_t( (nz + 1.0f)*255.0f/2.0f);
  3857. temp[ii*4+3] = 0;
  3858. }
  3859. uint8_t* block = &dst[yy*_dstPitch*4 + xx*16];
  3860. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  3861. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  3862. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  3863. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  3864. }
  3865. }
  3866. }
  3867. else
  3868. {
  3869. BX_WARN(false, "BC5 decoder is disabled (BIMG_DECODE_BC5).");
  3870. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00) );
  3871. }
  3872. break;
  3873. case TextureFormat::BC6H:
  3874. {
  3875. ImageContainer* rgba32f = imageAlloc(_allocator
  3876. , TextureFormat::RGBA32F
  3877. , uint16_t(_width)
  3878. , uint16_t(_height)
  3879. , uint16_t(1)
  3880. , 1
  3881. , false
  3882. , false
  3883. );
  3884. imageDecodeToRgba32f(_allocator, rgba32f->m_data, _src, _width, _height, 1, _width*16, _srcFormat);
  3885. imageConvert(_allocator, _dst, TextureFormat::BGRA8, rgba32f->m_data, TextureFormat::RGBA32F, _width, _height, 1, _width*16, _dstPitch);
  3886. imageFree(rgba32f);
  3887. }
  3888. break;
  3889. case TextureFormat::BC7:
  3890. for (uint32_t yy = 0; yy < height; ++yy)
  3891. {
  3892. for (uint32_t xx = 0; xx < width; ++xx)
  3893. {
  3894. decodeBlockBc7(temp, src);
  3895. src += 16;
  3896. uint8_t* block = &dst[yy*_dstPitch*4 + xx*16];
  3897. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  3898. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  3899. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  3900. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  3901. }
  3902. }
  3903. break;
  3904. case TextureFormat::ETC1:
  3905. case TextureFormat::ETC2:
  3906. for (uint32_t yy = 0; yy < height; ++yy)
  3907. {
  3908. for (uint32_t xx = 0; xx < width; ++xx)
  3909. {
  3910. decodeBlockEtc12(temp, src);
  3911. src += 8;
  3912. uint8_t* block = &dst[yy*_dstPitch*4 + xx*16];
  3913. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  3914. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  3915. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  3916. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  3917. }
  3918. }
  3919. break;
  3920. case TextureFormat::ETC2A:
  3921. BX_WARN(false, "ETC2A decoder is not implemented.");
  3922. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xff00ff00) );
  3923. break;
  3924. case TextureFormat::ETC2A1:
  3925. BX_WARN(false, "ETC2A1 decoder is not implemented.");
  3926. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xffff0000) );
  3927. break;
  3928. case TextureFormat::PTC12:
  3929. BX_WARN(false, "PTC12 decoder is not implemented.");
  3930. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xffff00ff) );
  3931. break;
  3932. case TextureFormat::PTC12A:
  3933. BX_WARN(false, "PTC12A decoder is not implemented.");
  3934. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xffffff00) );
  3935. break;
  3936. case TextureFormat::PTC14:
  3937. for (uint32_t yy = 0; yy < height; ++yy)
  3938. {
  3939. for (uint32_t xx = 0; xx < width; ++xx)
  3940. {
  3941. decodeBlockPtc14(temp, src, xx, yy, width, height);
  3942. uint8_t* block = &dst[yy*_dstPitch*4 + xx*16];
  3943. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  3944. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  3945. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  3946. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  3947. }
  3948. }
  3949. break;
  3950. case TextureFormat::PTC14A:
  3951. for (uint32_t yy = 0; yy < height; ++yy)
  3952. {
  3953. for (uint32_t xx = 0; xx < width; ++xx)
  3954. {
  3955. decodeBlockPtc14A(temp, src, xx, yy, width, height);
  3956. uint8_t* block = &dst[yy*_dstPitch*4 + xx*16];
  3957. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  3958. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  3959. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  3960. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  3961. }
  3962. }
  3963. break;
  3964. case TextureFormat::PTC22:
  3965. BX_WARN(false, "PTC22 decoder is not implemented.");
  3966. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff00ff00), UINT32_C(0xff0000ff) );
  3967. break;
  3968. case TextureFormat::PTC24:
  3969. BX_WARN(false, "PTC24 decoder is not implemented.");
  3970. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xffffffff) );
  3971. break;
  3972. case TextureFormat::ATC:
  3973. for (uint32_t yy = 0; yy < height; ++yy)
  3974. {
  3975. for (uint32_t xx = 0; xx < width; ++xx)
  3976. {
  3977. decodeBlockATC(temp, src);
  3978. src += 8;
  3979. uint8_t* block = &dst[(yy*_dstPitch+xx*4)*4];
  3980. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  3981. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  3982. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  3983. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  3984. }
  3985. }
  3986. break;
  3987. case TextureFormat::ATCE:
  3988. for (uint32_t yy = 0; yy < height; ++yy)
  3989. {
  3990. for (uint32_t xx = 0; xx < width; ++xx)
  3991. {
  3992. decodeBlockDxt23A(temp+3, src);
  3993. src += 8;
  3994. decodeBlockATC(temp, src);
  3995. src += 8;
  3996. uint8_t* block = &dst[(yy*_dstPitch+xx*4)*4];
  3997. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  3998. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  3999. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  4000. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  4001. }
  4002. }
  4003. break;
  4004. case TextureFormat::ATCI:
  4005. for (uint32_t yy = 0; yy < height; ++yy)
  4006. {
  4007. for (uint32_t xx = 0; xx < width; ++xx)
  4008. {
  4009. decodeBlockDxt45A(temp+3, src);
  4010. src += 8;
  4011. decodeBlockATC(temp, src);
  4012. src += 8;
  4013. uint8_t* block = &dst[(yy*_dstPitch+xx*4)*4];
  4014. bx::memCopy(&block[0*_dstPitch], &temp[ 0], 16);
  4015. bx::memCopy(&block[1*_dstPitch], &temp[16], 16);
  4016. bx::memCopy(&block[2*_dstPitch], &temp[32], 16);
  4017. bx::memCopy(&block[3*_dstPitch], &temp[48], 16);
  4018. }
  4019. }
  4020. break;
  4021. case TextureFormat::ASTC4x4:
  4022. case TextureFormat::ASTC5x5:
  4023. case TextureFormat::ASTC6x6:
  4024. case TextureFormat::ASTC8x5:
  4025. case TextureFormat::ASTC8x6:
  4026. case TextureFormat::ASTC10x5:
  4027. imageDecodeToRgba8(_allocator, _dst, _src, _width, _height, _dstPitch, _srcFormat);
  4028. imageSwizzleBgra8(_dst, _dstPitch, _width, _height, _dst, _dstPitch);
  4029. break;
  4030. case TextureFormat::RGBA8:
  4031. {
  4032. const uint32_t srcPitch = _width * 4;
  4033. imageSwizzleBgra8(_dst, _dstPitch, _width, _height, _src, srcPitch);
  4034. }
  4035. break;
  4036. case TextureFormat::BGRA8:
  4037. {
  4038. const uint32_t srcPitch = _width * 4;
  4039. const uint32_t size = bx::uint32_min(srcPitch, _dstPitch);
  4040. bx::memCopy(_dst, _dstPitch, _src, srcPitch, size, _height);
  4041. }
  4042. break;
  4043. default:
  4044. {
  4045. const uint32_t srcBpp = s_imageBlockInfo[_srcFormat].bitsPerPixel;
  4046. const uint32_t srcPitch = _width * srcBpp / 8;
  4047. if (!imageConvert(_allocator, _dst, TextureFormat::BGRA8, _src, _srcFormat, _width, _height, 1, srcPitch, _dstPitch) )
  4048. {
  4049. // Failed to convert, just make ugly red-yellow checkerboard texture.
  4050. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xffff0000), UINT32_C(0xffffff00) );
  4051. }
  4052. }
  4053. break;
  4054. }
  4055. }
  4056. void imageDecodeToRgba8(bx::AllocatorI* _allocator, void* _dst, const void* _src, uint32_t _width, uint32_t _height, uint32_t _dstPitch, TextureFormat::Enum _srcFormat)
  4057. {
  4058. switch (_srcFormat)
  4059. {
  4060. case TextureFormat::RGBA8:
  4061. {
  4062. const uint32_t srcPitch = _width * 4;
  4063. const uint32_t size = bx::uint32_min(srcPitch, _dstPitch);
  4064. bx::memCopy(_dst, _dstPitch, _src, srcPitch, size, _height);
  4065. }
  4066. break;
  4067. case TextureFormat::BGRA8:
  4068. {
  4069. const uint32_t srcPitch = _width * 4;
  4070. imageSwizzleBgra8(_dst, _dstPitch, _width, _height, _src, srcPitch);
  4071. }
  4072. break;
  4073. case TextureFormat::ASTC4x4:
  4074. case TextureFormat::ASTC5x5:
  4075. case TextureFormat::ASTC6x6:
  4076. case TextureFormat::ASTC8x5:
  4077. case TextureFormat::ASTC8x6:
  4078. case TextureFormat::ASTC10x5:
  4079. if (!astc_codec::ASTCDecompressToRGBA(
  4080. (const uint8_t*)_src
  4081. , imageGetSize(NULL, uint16_t(_width), uint16_t(_height), 0, false, false, 1, _srcFormat)
  4082. , _width
  4083. , _height
  4084. , TextureFormat::ASTC4x4 == _srcFormat ? astc_codec::FootprintType::k4x4
  4085. : TextureFormat::ASTC5x5 == _srcFormat ? astc_codec::FootprintType::k5x5
  4086. : TextureFormat::ASTC6x6 == _srcFormat ? astc_codec::FootprintType::k6x6
  4087. : TextureFormat::ASTC8x5 == _srcFormat ? astc_codec::FootprintType::k8x5
  4088. : TextureFormat::ASTC8x6 == _srcFormat ? astc_codec::FootprintType::k8x6
  4089. : astc_codec::FootprintType::k10x5
  4090. , (uint8_t*)_dst
  4091. , _width*_height*4
  4092. , _dstPitch
  4093. ) )
  4094. {
  4095. imageCheckerboard(_dst, _width, _height, 16, UINT32_C(0xff000000), UINT32_C(0xffffff00) );
  4096. }
  4097. break;
  4098. default:
  4099. {
  4100. const uint32_t srcPitch = _width * 4;
  4101. imageDecodeToBgra8(_allocator, _dst, _src, _width, _height, _dstPitch, _srcFormat);
  4102. imageSwizzleBgra8(_dst, _dstPitch, _width, _height, _dst, srcPitch);
  4103. }
  4104. break;
  4105. }
  4106. }
  4107. void imageRgba8ToRgba32fRef(void* _dst, uint32_t _width, uint32_t _height, uint32_t _srcPitch, const void* _src)
  4108. {
  4109. const uint32_t dstWidth = _width;
  4110. const uint32_t dstHeight = _height;
  4111. if (0 == dstWidth
  4112. || 0 == dstHeight)
  4113. {
  4114. return;
  4115. }
  4116. float* dst = (float*)_dst;
  4117. const uint8_t* src = (const uint8_t*)_src;
  4118. for (uint32_t yy = 0, ystep = _srcPitch; yy < dstHeight; ++yy, src += ystep)
  4119. {
  4120. const uint8_t* rgba = src;
  4121. for (uint32_t xx = 0; xx < dstWidth; ++xx, rgba += 4, dst += 4)
  4122. {
  4123. dst[0] = bx::toLinear(rgba[0]);
  4124. dst[1] = bx::toLinear(rgba[1]);
  4125. dst[2] = bx::toLinear(rgba[2]);
  4126. dst[3] = rgba[3];
  4127. }
  4128. }
  4129. }
  4130. void imageRgba8ToRgba32f(void* _dst, uint32_t _width, uint32_t _height, uint32_t _srcPitch, const void* _src)
  4131. {
  4132. const uint32_t dstWidth = _width;
  4133. const uint32_t dstHeight = _height;
  4134. if (0 == dstWidth
  4135. || 0 == dstHeight)
  4136. {
  4137. return;
  4138. }
  4139. float* dst = (float*)_dst;
  4140. const uint8_t* src = (const uint8_t*)_src;
  4141. using namespace bx;
  4142. 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);
  4143. const simd128_t umask = simd_ild(0xff, 0xff00, 0xff0000, 0xff000000);
  4144. const simd128_t wflip = simd_ild(0, 0, 0, 0x80000000);
  4145. const simd128_t wadd = simd_ld(0.0f, 0.0f, 0.0f, 32768.0f*65536.0f);
  4146. for (uint32_t yy = 0, ystep = _srcPitch; yy < dstHeight; ++yy, src += ystep)
  4147. {
  4148. const uint8_t* rgba = src;
  4149. for (uint32_t xx = 0; xx < dstWidth; ++xx, rgba += 4, dst += 4)
  4150. {
  4151. const simd128_t abgr0 = simd_splat(rgba);
  4152. const simd128_t abgr0m = simd_and(abgr0, umask);
  4153. const simd128_t abgr0x = simd_xor(abgr0m, wflip);
  4154. const simd128_t abgr0f = simd_itof(abgr0x);
  4155. const simd128_t abgr0c = simd_add(abgr0f, wadd);
  4156. const simd128_t abgr0n = simd_mul(abgr0c, unpack);
  4157. simd_st(dst, abgr0n);
  4158. }
  4159. }
  4160. }
  4161. 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)
  4162. {
  4163. const uint8_t* src = (const uint8_t*)_src;
  4164. uint8_t* dst = (uint8_t*)_dst;
  4165. const uint32_t srcBpp = s_imageBlockInfo[_srcFormat].bitsPerPixel;
  4166. const uint32_t srcPitch = _width*srcBpp/8;
  4167. for (uint32_t zz = 0; zz < _depth; ++zz, src += _height*srcPitch, dst += _height*_dstPitch)
  4168. {
  4169. switch (_srcFormat)
  4170. {
  4171. case TextureFormat::BC5:
  4172. {
  4173. uint32_t width = _width/4;
  4174. uint32_t height = _height/4;
  4175. const uint8_t* srcData = src;
  4176. for (uint32_t yy = 0; yy < height; ++yy)
  4177. {
  4178. for (uint32_t xx = 0; xx < width; ++xx)
  4179. {
  4180. uint8_t temp[16*4];
  4181. decodeBlockDxt45A(temp+2, srcData);
  4182. srcData += 8;
  4183. decodeBlockDxt45A(temp+1, srcData);
  4184. srcData += 8;
  4185. for (uint32_t ii = 0; ii < 16; ++ii)
  4186. {
  4187. float nx = temp[ii*4+2]*2.0f/255.0f - 1.0f;
  4188. float ny = temp[ii*4+1]*2.0f/255.0f - 1.0f;
  4189. float nz = bx::sqrt(1.0f - nx*nx - ny*ny);
  4190. const uint32_t offset = (yy*4 + ii/4)*_width*16 + (xx*4 + ii%4)*16;
  4191. float* block = (float*)&dst[offset];
  4192. block[0] = nx;
  4193. block[1] = ny;
  4194. block[2] = nz;
  4195. block[3] = 0.0f;
  4196. }
  4197. }
  4198. }
  4199. }
  4200. break;
  4201. case TextureFormat::BC6H:
  4202. {
  4203. uint32_t width = _width/4;
  4204. uint32_t height = _height/4;
  4205. const uint8_t* srcData = src;
  4206. for (uint32_t yy = 0; yy < height; ++yy)
  4207. {
  4208. for (uint32_t xx = 0; xx < width; ++xx)
  4209. {
  4210. float tmp[16*4];
  4211. decodeBlockBc6h(tmp, srcData);
  4212. srcData += 16;
  4213. uint8_t* block = (uint8_t*)&dst[yy*_dstPitch*4 + xx*64];
  4214. bx::memCopy(&block[0*_dstPitch], &tmp[ 0], 64);
  4215. bx::memCopy(&block[1*_dstPitch], &tmp[16], 64);
  4216. bx::memCopy(&block[2*_dstPitch], &tmp[32], 64);
  4217. bx::memCopy(&block[3*_dstPitch], &tmp[48], 64);
  4218. }
  4219. }
  4220. }
  4221. break;
  4222. case TextureFormat::RGBA32F:
  4223. bx::memCopy(dst, src, _dstPitch*_height);
  4224. break;
  4225. default:
  4226. if (isCompressed(_srcFormat) )
  4227. {
  4228. uint32_t size = imageGetSize(NULL, uint16_t(_width), uint16_t(_height), 0, false, false, 1, TextureFormat::RGBA8);
  4229. void* temp = BX_ALLOC(_allocator, size);
  4230. imageDecodeToRgba8(_allocator, temp, src, _width, _height, _width*4, _srcFormat);
  4231. imageRgba8ToRgba32f(dst, _width, _height, _width*4, temp);
  4232. BX_FREE(_allocator, temp);
  4233. }
  4234. else
  4235. {
  4236. imageConvert(_allocator, dst, TextureFormat::RGBA32F, src, _srcFormat, _width, _height, 1, srcPitch, _dstPitch);
  4237. }
  4238. break;
  4239. }
  4240. }
  4241. }
  4242. bool imageGetRawData(const ImageContainer& _imageContainer, uint16_t _side, uint8_t _lod, const void* _data, uint32_t _size, ImageMip& _mip)
  4243. {
  4244. uint32_t offset = _imageContainer.m_offset;
  4245. TextureFormat::Enum format = TextureFormat::Enum(_imageContainer.m_format);
  4246. bool hasAlpha = _imageContainer.m_hasAlpha;
  4247. const ImageBlockInfo& blockInfo = s_imageBlockInfo[format];
  4248. const uint8_t bpp = blockInfo.bitsPerPixel;
  4249. const uint32_t blockSize = blockInfo.blockSize;
  4250. const uint32_t blockWidth = blockInfo.blockWidth;
  4251. const uint32_t blockHeight = blockInfo.blockHeight;
  4252. const uint32_t minBlockX = blockInfo.minBlockX;
  4253. const uint32_t minBlockY = blockInfo.minBlockY;
  4254. if (UINT32_MAX == _imageContainer.m_offset)
  4255. {
  4256. if (NULL == _imageContainer.m_data)
  4257. {
  4258. return false;
  4259. }
  4260. offset = 0;
  4261. _data = _imageContainer.m_data;
  4262. _size = _imageContainer.m_size;
  4263. }
  4264. const uint8_t* data = (const uint8_t*)_data;
  4265. const uint16_t numSides = _imageContainer.m_numLayers * (_imageContainer.m_cubeMap ? 6 : 1);
  4266. if (_imageContainer.m_ktx)
  4267. {
  4268. uint32_t width = _imageContainer.m_width;
  4269. uint32_t height = _imageContainer.m_height;
  4270. uint32_t depth = _imageContainer.m_depth;
  4271. for (uint8_t lod = 0, num = _imageContainer.m_numMips; lod < num; ++lod)
  4272. {
  4273. width = bx::max<uint32_t>(blockWidth * minBlockX, ( (width + blockWidth - 1) / blockWidth )*blockWidth);
  4274. height = bx::max<uint32_t>(blockHeight * minBlockY, ( (height + blockHeight - 1) / blockHeight)*blockHeight);
  4275. depth = bx::max<uint32_t>(1, depth);
  4276. const uint32_t mipSize = width/blockWidth * height/blockHeight * depth * blockSize;
  4277. const uint32_t size = mipSize*numSides;
  4278. uint32_t imageSize = bx::toHostEndian(*(const uint32_t*)&data[offset], _imageContainer.m_ktxLE);
  4279. BX_CHECK(size == imageSize, "KTX: Image size mismatch %d (expected %d).", size, imageSize);
  4280. BX_UNUSED(size, imageSize);
  4281. offset += sizeof(uint32_t);
  4282. for (uint16_t side = 0; side < numSides; ++side)
  4283. {
  4284. BX_CHECK(offset <= _size, "Reading past size of data buffer! (offset %d, size %d)", offset, _size);
  4285. if (side == _side
  4286. && lod == _lod)
  4287. {
  4288. _mip.m_width = width;
  4289. _mip.m_height = height;
  4290. _mip.m_depth = depth;
  4291. _mip.m_blockSize = blockSize;
  4292. _mip.m_size = mipSize;
  4293. _mip.m_data = &data[offset];
  4294. _mip.m_bpp = bpp;
  4295. _mip.m_format = format;
  4296. _mip.m_hasAlpha = hasAlpha;
  4297. return true;
  4298. }
  4299. offset += mipSize;
  4300. BX_UNUSED(_size);
  4301. }
  4302. width >>= 1;
  4303. height >>= 1;
  4304. depth >>= 1;
  4305. }
  4306. }
  4307. else
  4308. {
  4309. for (uint16_t side = 0; side < numSides; ++side)
  4310. {
  4311. uint32_t width = _imageContainer.m_width;
  4312. uint32_t height = _imageContainer.m_height;
  4313. uint32_t depth = _imageContainer.m_depth;
  4314. for (uint8_t lod = 0, num = _imageContainer.m_numMips; lod < num; ++lod)
  4315. {
  4316. BX_CHECK(offset <= _size, "Reading past size of data buffer! (offset %d, size %d)", offset, _size);
  4317. width = bx::max<uint32_t>(blockWidth * minBlockX, ( (width + blockWidth - 1) / blockWidth )*blockWidth);
  4318. height = bx::max<uint32_t>(blockHeight * minBlockY, ( (height + blockHeight - 1) / blockHeight)*blockHeight);
  4319. depth = bx::max<uint32_t>(1, depth);
  4320. uint32_t mipSize = width/blockWidth * height/blockHeight * depth * blockSize;
  4321. if (side == _side
  4322. && lod == _lod)
  4323. {
  4324. _mip.m_width = width;
  4325. _mip.m_height = height;
  4326. _mip.m_depth = depth;
  4327. _mip.m_blockSize = blockSize;
  4328. _mip.m_size = mipSize;
  4329. _mip.m_data = &data[offset];
  4330. _mip.m_bpp = bpp;
  4331. _mip.m_format = format;
  4332. _mip.m_hasAlpha = hasAlpha;
  4333. return true;
  4334. }
  4335. offset += mipSize;
  4336. BX_UNUSED(_size);
  4337. width >>= 1;
  4338. height >>= 1;
  4339. depth >>= 1;
  4340. }
  4341. }
  4342. }
  4343. return false;
  4344. }
  4345. 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)
  4346. {
  4347. BX_ERROR_SCOPE(_err);
  4348. uint8_t type = _grayscale ? 3 : 2;
  4349. uint8_t bpp = _grayscale ? 8 : 32;
  4350. uint8_t header[18] = {};
  4351. header[ 2] = type;
  4352. header[12] = _width &0xff;
  4353. header[13] = (_width >>8)&0xff;
  4354. header[14] = _height &0xff;
  4355. header[15] = (_height>>8)&0xff;
  4356. header[16] = bpp;
  4357. header[17] = 32;
  4358. int32_t total = 0;
  4359. total += bx::write(_writer, header, sizeof(header), _err);
  4360. uint32_t dstPitch = _width*bpp/8;
  4361. if (_yflip)
  4362. {
  4363. const uint8_t* data = (const uint8_t*)_src + _srcPitch*_height - _srcPitch;
  4364. for (uint32_t yy = 0; yy < _height && _err->isOk(); ++yy)
  4365. {
  4366. total += bx::write(_writer, data, dstPitch, _err);
  4367. data -= _srcPitch;
  4368. }
  4369. }
  4370. else if (_srcPitch == dstPitch)
  4371. {
  4372. total += bx::write(_writer, _src, _height*_srcPitch, _err);
  4373. }
  4374. else
  4375. {
  4376. const uint8_t* data = (const uint8_t*)_src;
  4377. for (uint32_t yy = 0; yy < _height && _err->isOk(); ++yy)
  4378. {
  4379. total += bx::write(_writer, data, dstPitch, _err);
  4380. data += _srcPitch;
  4381. }
  4382. }
  4383. return total;
  4384. }
  4385. template<typename Ty>
  4386. class HashWriter : public bx::WriterI
  4387. {
  4388. public:
  4389. HashWriter(bx::WriterI* _writer)
  4390. : m_writer(_writer)
  4391. {
  4392. begin();
  4393. }
  4394. void begin()
  4395. {
  4396. m_hash.begin();
  4397. }
  4398. uint32_t end()
  4399. {
  4400. return m_hash.end();
  4401. }
  4402. virtual int32_t write(const void* _data, int32_t _size, bx::Error* _err) override
  4403. {
  4404. m_hash.add(_data, _size);
  4405. return m_writer->write(_data, _size, _err);
  4406. }
  4407. private:
  4408. Ty m_hash;
  4409. bx::WriterI* m_writer;
  4410. };
  4411. 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)
  4412. {
  4413. BX_ERROR_SCOPE(_err);
  4414. switch (_format)
  4415. {
  4416. case TextureFormat::R8:
  4417. case TextureFormat::RGBA8:
  4418. case TextureFormat::BGRA8:
  4419. break;
  4420. default:
  4421. BX_ERROR_SET(_err, BIMG_ERROR, "PNG: Unsupported texture format.");
  4422. return 0;
  4423. }
  4424. const bool grayscale = TextureFormat::R8 == _format;
  4425. const bool bgra = TextureFormat::BGRA8 == _format;
  4426. int32_t total = 0;
  4427. total += bx::write(_writer, "\x89PNG\r\n\x1a\n", _err);
  4428. total += bx::write(_writer, bx::toBigEndian<uint32_t>(13), _err);
  4429. HashWriter<bx::HashCrc32> writerC(_writer);
  4430. total += bx::write(&writerC, "IHDR", _err);
  4431. total += bx::write(&writerC, bx::toBigEndian(_width), _err);
  4432. total += bx::write(&writerC, bx::toBigEndian(_height), _err);
  4433. total += bx::write(&writerC, "\x08\x06", _err);
  4434. total += bx::writeRep(&writerC, 0, 3, _err);
  4435. total += bx::write(_writer, bx::toBigEndian(writerC.end() ), _err);
  4436. const uint32_t bpp = grayscale ? 8 : 32;
  4437. const uint32_t stride = _width*bpp/8;
  4438. const uint16_t zlen = bx::toLittleEndian<uint16_t>(uint16_t(stride + 1) );
  4439. const uint16_t zlenC = bx::toLittleEndian<uint16_t>(~zlen);
  4440. total += bx::write(_writer, bx::toBigEndian<uint32_t>(_height*(stride+6)+6), _err);
  4441. writerC.begin();
  4442. total += bx::write(&writerC, "IDAT", _err);
  4443. total += bx::write(&writerC, "\x78\x9c", _err);
  4444. const uint8_t* data = (const uint8_t*)_src;
  4445. int32_t step = int32_t(_srcPitch);
  4446. if (_yflip)
  4447. {
  4448. data += _srcPitch*_height - _srcPitch;
  4449. step = -step;
  4450. }
  4451. HashWriter<bx::HashAdler32> writerA(&writerC);
  4452. for (uint32_t ii = 0; ii < _height && _err->isOk(); ++ii)
  4453. {
  4454. total += bx::write(&writerC, uint8_t(ii == _height-1 ? 1 : 0), _err);
  4455. total += bx::write(&writerC, zlen, _err);
  4456. total += bx::write(&writerC, zlenC, _err);
  4457. total += bx::write(&writerA, uint8_t(0), _err);
  4458. if (bgra)
  4459. {
  4460. for (uint32_t xx = 0; xx < _width; ++xx)
  4461. {
  4462. const uint8_t* texel = &data[xx*4];
  4463. const uint8_t bb = texel[0];
  4464. const uint8_t gg = texel[1];
  4465. const uint8_t rr = texel[2];
  4466. const uint8_t aa = texel[3];
  4467. total += bx::write(&writerA, rr, _err);
  4468. total += bx::write(&writerA, gg, _err);
  4469. total += bx::write(&writerA, bb, _err);
  4470. total += bx::write(&writerA, aa, _err);
  4471. }
  4472. }
  4473. else
  4474. {
  4475. total += bx::write(&writerA, data, stride, _err);
  4476. }
  4477. data += step;
  4478. }
  4479. total += bx::write(&writerC, bx::toBigEndian(writerA.end() ), _err);
  4480. total += bx::write(_writer, bx::toBigEndian(writerC.end() ), _err);
  4481. total += bx::write(&writerC, uint32_t(0), _err);
  4482. writerC.begin();
  4483. total += bx::write(&writerC, "IEND", _err);
  4484. total += bx::write(_writer, bx::toBigEndian(writerC.end() ), _err);
  4485. return total;
  4486. }
  4487. 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)
  4488. {
  4489. BX_ERROR_SCOPE(_err);
  4490. const uint32_t bpp = getBitsPerPixel(_format);
  4491. uint32_t bytesPerChannel = 0;
  4492. switch (_format)
  4493. {
  4494. case TextureFormat::RGBA16F:
  4495. bytesPerChannel = 2;
  4496. break;
  4497. default:
  4498. BX_ERROR_SET(_err, BIMG_ERROR, "EXR: Unsupported texture format.");
  4499. return 0;
  4500. }
  4501. int32_t total = 0;
  4502. total += bx::write(_writer, "v/1\x01", _err);
  4503. total += bx::writeLE(_writer, uint32_t(2), _err);
  4504. total += bx::write(_writer, "channels", _err);
  4505. total += bx::write(_writer, '\0', _err);
  4506. total += bx::write(_writer, "chlist", _err);
  4507. total += bx::write(_writer, '\0', _err);
  4508. total += bx::writeLE(_writer, uint32_t(18*4+1), _err);
  4509. const uint8_t cdata[] = { 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0 };
  4510. // Order is always ABGR order because Photoshop and GIMP ignore these fields and
  4511. // assume it's in ABGR order.
  4512. total += bx::write(_writer, 'A', _err);
  4513. total += bx::write(_writer, cdata, BX_COUNTOF(cdata), _err);
  4514. total += bx::write(_writer, 'B', _err);
  4515. total += bx::write(_writer, cdata, BX_COUNTOF(cdata), _err);
  4516. total += bx::write(_writer, 'G', _err);
  4517. total += bx::write(_writer, cdata, BX_COUNTOF(cdata), _err);
  4518. total += bx::write(_writer, 'R', _err);
  4519. total += bx::write(_writer, cdata, BX_COUNTOF(cdata), _err);
  4520. total += bx::write(_writer, '\0', _err);
  4521. total += bx::write(_writer, "compression", _err);
  4522. total += bx::write(_writer, '\0', _err);
  4523. total += bx::write(_writer, "compression", _err);
  4524. total += bx::write(_writer, '\0', _err);
  4525. total += bx::writeLE(_writer, uint32_t(1), _err);
  4526. total += bx::write(_writer, '\0', _err); // no compression
  4527. total += bx::write(_writer, "dataWindow", _err);
  4528. total += bx::write(_writer, '\0', _err);
  4529. total += bx::write(_writer, "box2i", _err);
  4530. total += bx::write(_writer, '\0', _err);
  4531. total += bx::writeLE(_writer, uint32_t(16), _err);
  4532. total += bx::writeRep(_writer, '\0', 8, _err);
  4533. total += bx::writeLE(_writer, _width-1, _err);
  4534. total += bx::writeLE(_writer, _height-1, _err);
  4535. total += bx::write(_writer, "displayWindow", _err);
  4536. total += bx::write(_writer, '\0', _err);
  4537. total += bx::write(_writer, "box2i", _err);
  4538. total += bx::write(_writer, '\0', _err);
  4539. total += bx::writeLE(_writer, uint32_t(16), _err);
  4540. total += bx::writeRep(_writer, '\0', 8, _err);
  4541. total += bx::writeLE(_writer, _width-1, _err);
  4542. total += bx::writeLE(_writer, _height-1, _err);
  4543. total += bx::write(_writer, "lineOrder", _err);
  4544. total += bx::write(_writer, '\0', _err);
  4545. total += bx::write(_writer, "lineOrder", _err);
  4546. total += bx::write(_writer, '\0', _err);
  4547. total += bx::writeLE(_writer, uint32_t(1), _err);
  4548. total += bx::write(_writer, _yflip, _err);
  4549. total += bx::write(_writer, "pixelAspectRatio", _err);
  4550. total += bx::write(_writer, '\0', _err);
  4551. total += bx::write(_writer, "float", _err);
  4552. total += bx::write(_writer, '\0', _err);
  4553. total += bx::writeLE(_writer, uint32_t(4), _err);
  4554. total += bx::writeLE(_writer, 1.0f, _err);
  4555. total += bx::write(_writer, "screenWindowCenter", _err);
  4556. total += bx::write(_writer, '\0', _err);
  4557. total += bx::write(_writer, "v2f", _err);
  4558. total += bx::write(_writer, '\0', _err);
  4559. total += bx::writeLE(_writer, uint32_t(8), _err);
  4560. total += bx::writeRep(_writer, '\0', 8, _err);
  4561. total += bx::write(_writer, "screenWindowWidth", _err);
  4562. total += bx::write(_writer, '\0', _err);
  4563. total += bx::write(_writer, "float", _err);
  4564. total += bx::write(_writer, '\0', _err);
  4565. total += bx::writeLE(_writer, uint32_t(4), _err);
  4566. total += bx::writeLE(_writer, 1.0f, _err);
  4567. total += bx::write(_writer, '\0', _err);
  4568. const uint32_t exrStride = _width*bpp/8;
  4569. uint64_t offset = 0;
  4570. for (uint32_t yy = 0; yy < _height && _err->isOk(); ++yy)
  4571. {
  4572. total += bx::writeLE(_writer, (offset), _err);
  4573. offset += exrStride + 8 /* offset */;
  4574. }
  4575. const uint8_t* data = (const uint8_t*)_src;
  4576. for (uint32_t yy = 0; yy < _height && _err->isOk(); ++yy)
  4577. {
  4578. total += bx::writeLE(_writer, yy, _err);
  4579. total += bx::writeLE(_writer, exrStride, _err);
  4580. for (uint32_t xx = 0; xx < _width && _err->isOk(); ++xx)
  4581. {
  4582. total += bx::write(_writer, &data[xx*bpp/8+3*bytesPerChannel], bytesPerChannel, _err);
  4583. }
  4584. for (uint32_t xx = 0; xx < _width && _err->isOk(); ++xx)
  4585. {
  4586. total += bx::write(_writer, &data[xx*bpp/8+2*bytesPerChannel], bytesPerChannel, _err);
  4587. }
  4588. for (uint32_t xx = 0; xx < _width && _err->isOk(); ++xx)
  4589. {
  4590. total += bx::write(_writer, &data[xx*bpp/8+1*bytesPerChannel], bytesPerChannel, _err);
  4591. }
  4592. for (uint32_t xx = 0; xx < _width && _err->isOk(); ++xx)
  4593. {
  4594. total += bx::write(_writer, &data[xx*bpp/8+0*bytesPerChannel], bytesPerChannel, _err);
  4595. }
  4596. data += _srcPitch;
  4597. }
  4598. return total;
  4599. }
  4600. 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)
  4601. {
  4602. BX_ERROR_SCOPE(_err);
  4603. int32_t total = 0;
  4604. total += bx::write(_writer, "#?RADIANCE\n" , _err);
  4605. total += bx::write(_writer, "FORMAT=32-bit_rle_rgbe\n" , _err);
  4606. total += bx::write(_writer, '\n' , _err);
  4607. total += bx::write(_writer, _err, "%cY %d +X %d\n", _yflip ? '+' : '-', _height, _width);
  4608. UnpackFn unpack = getUnpack(_format);
  4609. const uint32_t bpp = getBitsPerPixel(_format);
  4610. const uint8_t* data = (const uint8_t*)_src;
  4611. for (uint32_t yy = 0; yy < _height && _err->isOk(); ++yy)
  4612. {
  4613. for (uint32_t xx = 0; xx < _width && _err->isOk(); ++xx)
  4614. {
  4615. float rgba[4];
  4616. unpack(rgba, &data[xx*bpp/8]);
  4617. const float maxVal = bx::max(rgba[0], rgba[1], rgba[2]);
  4618. const float exp = bx::ceil(bx::log2(maxVal) );
  4619. const float toRgb8 = 255.0f * 1.0f/bx::ldexp(1.0f, int(exp) );
  4620. uint8_t rgbe[4];
  4621. rgbe[0] = uint8_t(rgba[0] * toRgb8);
  4622. rgbe[1] = uint8_t(rgba[1] * toRgb8);
  4623. rgbe[2] = uint8_t(rgba[2] * toRgb8);
  4624. rgbe[3] = uint8_t(exp+128.0f);
  4625. total += bx::write(_writer, rgbe, 4, _err);
  4626. }
  4627. data += _srcPitch;
  4628. }
  4629. return total;
  4630. }
  4631. 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)
  4632. {
  4633. BX_ERROR_SCOPE(_err);
  4634. uint32_t ddspf = UINT32_MAX;
  4635. uint32_t dxgiFormat = UINT32_MAX;
  4636. uint32_t fourccFormat = UINT32_MAX;
  4637. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateDdsPixelFormat); ++ii)
  4638. {
  4639. if (s_translateDdsPixelFormat[ii].m_textureFormat == _format)
  4640. {
  4641. ddspf = ii;
  4642. break;
  4643. }
  4644. }
  4645. if (UINT32_MAX == ddspf)
  4646. {
  4647. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateDxgiFormat); ++ii)
  4648. {
  4649. if (s_translateDxgiFormat[ii].m_textureFormat == _format)
  4650. {
  4651. dxgiFormat = s_translateDxgiFormat[ii].m_format;
  4652. break;
  4653. }
  4654. }
  4655. }
  4656. if (UINT32_MAX == ddspf && UINT32_MAX == dxgiFormat)
  4657. {
  4658. for (uint32_t ii = 0; ii < BX_COUNTOF(s_translateDdsFourccFormat); ++ii)
  4659. {
  4660. if (s_translateDdsFourccFormat[ii].m_textureFormat == _format)
  4661. {
  4662. fourccFormat = s_translateDdsFourccFormat[ii].m_format;
  4663. break;
  4664. }
  4665. }
  4666. }
  4667. if (UINT32_MAX == ddspf
  4668. && UINT32_MAX == dxgiFormat
  4669. && UINT32_MAX == fourccFormat)
  4670. {
  4671. BX_ERROR_SET(_err, BIMG_ERROR, "DDS: output format not supported.");
  4672. return 0;
  4673. }
  4674. const uint32_t bpp = getBitsPerPixel(_format);
  4675. uint32_t total = 0;
  4676. total += bx::write(_writer, uint32_t(DDS_MAGIC), _err);
  4677. uint32_t headerStart = total;
  4678. total += bx::write(_writer, uint32_t(DDS_HEADER_SIZE), _err);
  4679. total += bx::write(_writer, uint32_t(0
  4680. | DDSD_HEIGHT
  4681. | DDSD_WIDTH
  4682. | DDSD_PIXELFORMAT
  4683. | DDSD_CAPS
  4684. | (1 < _depth ? DDSD_DEPTH : 0)
  4685. | (1 < _numMips ? DDSD_MIPMAPCOUNT : 0)
  4686. | (isCompressed(_format) ? DDSD_LINEARSIZE : DDSD_PITCH)
  4687. )
  4688. , _err
  4689. );
  4690. const uint32_t pitchOrLinearSize = isCompressed(_format)
  4691. ? _width*_height*bpp/8
  4692. : _width*bpp/8
  4693. ;
  4694. total += bx::write(_writer, _height, _err);
  4695. total += bx::write(_writer, _width, _err);
  4696. total += bx::write(_writer, pitchOrLinearSize, _err);
  4697. total += bx::write(_writer, _depth, _err);
  4698. total += bx::write(_writer, uint32_t(_numMips), _err);
  4699. total += bx::writeRep(_writer, 0, 44, _err); // reserved1
  4700. if (UINT32_MAX != ddspf)
  4701. {
  4702. const TranslateDdsPixelFormat& pf = s_translateDdsPixelFormat[ddspf];
  4703. total += bx::write(_writer, uint32_t(8*sizeof(uint32_t) ), _err); // pixelFormatSize
  4704. total += bx::write(_writer, pf.m_flags, _err);
  4705. total += bx::write(_writer, uint32_t(0), _err);
  4706. total += bx::write(_writer, pf.m_bitCount, _err);
  4707. total += bx::write(_writer, pf.m_bitmask, _err);
  4708. }
  4709. else
  4710. {
  4711. total += bx::write(_writer, uint32_t(8*sizeof(uint32_t) ), _err); // pixelFormatSize
  4712. total += bx::write(_writer, uint32_t(DDPF_FOURCC), _err);
  4713. if (UINT32_MAX != fourccFormat)
  4714. total += bx::write(_writer, fourccFormat, _err);
  4715. else
  4716. total += bx::write(_writer, uint32_t(DDS_DX10), _err);
  4717. total += bx::write(_writer, uint32_t(0), _err); // bitCount
  4718. total += bx::writeRep(_writer, 0, 4*sizeof(uint32_t), _err); // bitmask
  4719. }
  4720. uint32_t caps[4] =
  4721. {
  4722. uint32_t(DDSCAPS_TEXTURE | (1 < _numMips ? DDSCAPS_COMPLEX|DDSCAPS_MIPMAP : 0) ),
  4723. uint32_t(_cubeMap ? DDSCAPS2_CUBEMAP|DSCAPS2_CUBEMAP_ALLSIDES : 0),
  4724. 0,
  4725. 0,
  4726. };
  4727. total += bx::write(_writer, caps, sizeof(caps) );
  4728. total += bx::writeRep(_writer, 0, 4, _err); // reserved2
  4729. BX_WARN(total-headerStart == DDS_HEADER_SIZE
  4730. , "DDS: Failed to write header size %d (expected: %d)."
  4731. , total-headerStart
  4732. , DDS_HEADER_SIZE
  4733. );
  4734. if (UINT32_MAX != dxgiFormat)
  4735. {
  4736. total += bx::write(_writer, dxgiFormat);
  4737. total += bx::write(_writer, uint32_t(1 < _depth ? DDS_DX10_DIMENSION_TEXTURE3D : DDS_DX10_DIMENSION_TEXTURE2D), _err); // dims
  4738. total += bx::write(_writer, uint32_t(_cubeMap ? DDS_DX10_MISC_TEXTURECUBE : 0), _err); // miscFlags
  4739. total += bx::write(_writer, uint32_t(1), _err); // arraySize
  4740. total += bx::write(_writer, uint32_t(0), _err); // miscFlags2
  4741. BX_WARN(total-headerStart == DDS_HEADER_SIZE+20
  4742. , "DDS: Failed to write header size %d (expected: %d)."
  4743. , total-headerStart
  4744. , DDS_HEADER_SIZE+20
  4745. );
  4746. BX_UNUSED(headerStart);
  4747. }
  4748. return total;
  4749. }
  4750. int32_t imageWriteDds(bx::WriterI* _writer, ImageContainer& _imageContainer, const void* _data, uint32_t _size, bx::Error* _err)
  4751. {
  4752. BX_ERROR_SCOPE(_err);
  4753. int32_t total = 0;
  4754. total += imageWriteDdsHeader(_writer
  4755. , TextureFormat::Enum(_imageContainer.m_format)
  4756. , _imageContainer.m_cubeMap
  4757. , _imageContainer.m_width
  4758. , _imageContainer.m_height
  4759. , _imageContainer.m_depth
  4760. , _imageContainer.m_numMips
  4761. , _err
  4762. );
  4763. if (!_err->isOk() )
  4764. {
  4765. return total;
  4766. }
  4767. for (uint8_t side = 0, numSides = _imageContainer.m_cubeMap ? 6 : 1; side < numSides && _err->isOk(); ++side)
  4768. {
  4769. for (uint8_t lod = 0, num = _imageContainer.m_numMips; lod < num && _err->isOk(); ++lod)
  4770. {
  4771. ImageMip mip;
  4772. if (imageGetRawData(_imageContainer, side, lod, _data, _size, mip) )
  4773. {
  4774. total += bx::write(_writer, mip.m_data, mip.m_size, _err);
  4775. }
  4776. }
  4777. }
  4778. return total;
  4779. }
  4780. static int32_t imageWriteKtxHeader(bx::WriterI* _writer, TextureFormat::Enum _format, bool _cubeMap, uint32_t _width, uint32_t _height, uint32_t _depth, uint8_t _numMips, uint32_t _numLayers, bx::Error* _err)
  4781. {
  4782. BX_ERROR_SCOPE(_err);
  4783. const KtxFormatInfo& tfi = s_translateKtxFormat[_format];
  4784. int32_t total = 0;
  4785. total += bx::write(_writer, "\xabKTX 11\xbb\r\n\x1a\n", 12, _err);
  4786. total += bx::write(_writer, uint32_t(0x04030201), _err);
  4787. total += bx::write(_writer, uint32_t(0), _err); // glType
  4788. total += bx::write(_writer, uint32_t(1), _err); // glTypeSize
  4789. total += bx::write(_writer, uint32_t(0), _err); // glFormat
  4790. total += bx::write(_writer, tfi.m_internalFmt, _err); // glInternalFormat
  4791. total += bx::write(_writer, tfi.m_fmt, _err); // glBaseInternalFormat
  4792. total += bx::write(_writer, _width, _err);
  4793. total += bx::write(_writer, _height, _err);
  4794. total += bx::write(_writer, _depth, _err);
  4795. total += bx::write(_writer, _numLayers, _err); // numberOfArrayElements
  4796. total += bx::write(_writer, _cubeMap ? uint32_t(6) : uint32_t(0), _err);
  4797. total += bx::write(_writer, uint32_t(_numMips), _err);
  4798. total += bx::write(_writer, uint32_t(0), _err); // Meta-data size.
  4799. BX_WARN(total == 64, "KTX: Failed to write header size %d (expected: %d).", total, 64);
  4800. return total;
  4801. }
  4802. int32_t imageWriteKtx(bx::WriterI* _writer, TextureFormat::Enum _format, bool _cubeMap, uint32_t _width, uint32_t _height, uint32_t _depth, uint8_t _numMips, uint32_t _numLayers, const void* _src, bx::Error* _err)
  4803. {
  4804. BX_ERROR_SCOPE(_err);
  4805. int32_t total = 0;
  4806. total += imageWriteKtxHeader(_writer, _format, _cubeMap, _width, _height, _depth, _numMips, _numLayers, _err);
  4807. if (!_err->isOk() )
  4808. {
  4809. return total;
  4810. }
  4811. const ImageBlockInfo& blockInfo = s_imageBlockInfo[_format];
  4812. const uint32_t blockWidth = blockInfo.blockWidth;
  4813. const uint32_t blockHeight = blockInfo.blockHeight;
  4814. const uint32_t minBlockX = blockInfo.minBlockX;
  4815. const uint32_t minBlockY = blockInfo.minBlockY;
  4816. const uint8_t blockSize = blockInfo.blockSize;
  4817. const uint8_t* src = (const uint8_t*)_src;
  4818. const uint32_t numLayers = bx::max<uint32_t>(_numLayers, 1);
  4819. const uint32_t numSides = _cubeMap ? 6 : 1;
  4820. uint32_t width = _width;
  4821. uint32_t height = _height;
  4822. uint32_t depth = _depth;
  4823. for (uint8_t lod = 0; lod < _numMips && _err->isOk(); ++lod)
  4824. {
  4825. width = bx::max<uint32_t>(blockWidth * minBlockX, ( (width + blockWidth - 1) / blockWidth )*blockWidth);
  4826. height = bx::max<uint32_t>(blockHeight * minBlockY, ( (height + blockHeight - 1) / blockHeight)*blockHeight);
  4827. depth = bx::max<uint32_t>(1, depth);
  4828. const uint32_t mipSize = width/blockWidth * height/blockHeight * depth * blockSize;
  4829. const uint32_t size = mipSize * numLayers * numSides;
  4830. total += bx::write(_writer, size, _err);
  4831. for (uint32_t layer = 0; layer < numLayers && _err->isOk(); ++layer)
  4832. {
  4833. for (uint8_t side = 0; side < numSides && _err->isOk(); ++side)
  4834. {
  4835. total += bx::write(_writer, src, size, _err);
  4836. src += size;
  4837. }
  4838. }
  4839. width >>= 1;
  4840. height >>= 1;
  4841. depth >>= 1;
  4842. }
  4843. return total;
  4844. }
  4845. int32_t imageWriteKtx(bx::WriterI* _writer, ImageContainer& _imageContainer, const void* _data, uint32_t _size, bx::Error* _err)
  4846. {
  4847. BX_ERROR_SCOPE(_err);
  4848. int32_t total = 0;
  4849. total += imageWriteKtxHeader(_writer
  4850. , TextureFormat::Enum(_imageContainer.m_format)
  4851. , _imageContainer.m_cubeMap
  4852. , _imageContainer.m_width
  4853. , _imageContainer.m_height
  4854. , _imageContainer.m_depth
  4855. , _imageContainer.m_numMips
  4856. , _imageContainer.m_numLayers
  4857. , _err
  4858. );
  4859. if (!_err->isOk() )
  4860. {
  4861. return total;
  4862. }
  4863. const uint32_t numMips = _imageContainer.m_numMips;
  4864. const uint32_t numLayers = bx::max<uint32_t>(_imageContainer.m_numLayers, 1);
  4865. const uint32_t numSides = _imageContainer.m_cubeMap ? 6 : 1;
  4866. for (uint8_t lod = 0; lod < numMips && _err->isOk(); ++lod)
  4867. {
  4868. ImageMip mip;
  4869. imageGetRawData(_imageContainer, 0, lod, _data, _size, mip);
  4870. const uint32_t size = mip.m_size*numSides*numLayers;
  4871. total += bx::write(_writer, size, _err);
  4872. for (uint32_t layer = 0; layer < numLayers && _err->isOk(); ++layer)
  4873. {
  4874. for (uint8_t side = 0; side < numSides && _err->isOk(); ++side)
  4875. {
  4876. if (imageGetRawData(_imageContainer, uint16_t(layer*numSides + side), lod, _data, _size, mip) )
  4877. {
  4878. total += bx::write(_writer, mip.m_data, mip.m_size, _err);
  4879. }
  4880. }
  4881. }
  4882. }
  4883. return total;
  4884. }
  4885. } // namespace bimg