image.cpp 167 KB

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