image.cpp 182 KB

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