image.cpp 202 KB

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