image.cpp 214 KB

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