image.cpp 201 KB

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