basisu_transcoder.cpp 625 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602
  1. // basisu_transcoder.cpp
  2. // Copyright (C) 2019-2021 Binomial LLC. All Rights Reserved.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #include "basisu_transcoder.h"
  16. #include <limits.h>
  17. #include "basisu_containers_impl.h"
  18. #ifndef BASISD_IS_BIG_ENDIAN
  19. // TODO: This doesn't work on OSX. How can this be so difficult?
  20. //#if defined(__BIG_ENDIAN__) || defined(_BIG_ENDIAN) || defined(BIG_ENDIAN)
  21. // #define BASISD_IS_BIG_ENDIAN (1)
  22. //#else
  23. #define BASISD_IS_BIG_ENDIAN (0)
  24. //#endif
  25. #endif
  26. #ifndef BASISD_USE_UNALIGNED_WORD_READS
  27. #ifdef __EMSCRIPTEN__
  28. // Can't use unaligned loads/stores with WebAssembly.
  29. #define BASISD_USE_UNALIGNED_WORD_READS (0)
  30. #elif defined(_M_AMD64) || defined(_M_IX86) || defined(__i386__) || defined(__x86_64__)
  31. #define BASISD_USE_UNALIGNED_WORD_READS (1)
  32. #else
  33. #define BASISD_USE_UNALIGNED_WORD_READS (0)
  34. #endif
  35. #endif
  36. #define BASISD_SUPPORTED_BASIS_VERSION (0x13)
  37. #ifndef BASISD_SUPPORT_KTX2
  38. #error Must have defined BASISD_SUPPORT_KTX2
  39. #endif
  40. #ifndef BASISD_SUPPORT_KTX2_ZSTD
  41. #error Must have defined BASISD_SUPPORT_KTX2_ZSTD
  42. #endif
  43. // Set to 1 for fuzz testing. This will disable all CRC16 checks on headers and compressed data.
  44. #ifndef BASISU_NO_HEADER_OR_DATA_CRC16_CHECKS
  45. #define BASISU_NO_HEADER_OR_DATA_CRC16_CHECKS 0
  46. #endif
  47. #ifndef BASISD_SUPPORT_DXT1
  48. #define BASISD_SUPPORT_DXT1 1
  49. #endif
  50. #ifndef BASISD_SUPPORT_DXT5A
  51. #define BASISD_SUPPORT_DXT5A 1
  52. #endif
  53. // Disable all BC7 transcoders if necessary (useful when cross compiling to Javascript)
  54. #if defined(BASISD_SUPPORT_BC7) && !BASISD_SUPPORT_BC7
  55. #ifndef BASISD_SUPPORT_BC7_MODE5
  56. #define BASISD_SUPPORT_BC7_MODE5 0
  57. #endif
  58. #endif // !BASISD_SUPPORT_BC7
  59. // BC7 mode 5 supports both opaque and opaque+alpha textures, and uses less memory BC1.
  60. #ifndef BASISD_SUPPORT_BC7_MODE5
  61. #define BASISD_SUPPORT_BC7_MODE5 1
  62. #endif
  63. #ifndef BASISD_SUPPORT_PVRTC1
  64. #define BASISD_SUPPORT_PVRTC1 1
  65. #endif
  66. #ifndef BASISD_SUPPORT_ETC2_EAC_A8
  67. #define BASISD_SUPPORT_ETC2_EAC_A8 1
  68. #endif
  69. // Set BASISD_SUPPORT_UASTC to 0 to completely disable support for transcoding UASTC files.
  70. #ifndef BASISD_SUPPORT_UASTC
  71. #define BASISD_SUPPORT_UASTC 1
  72. #endif
  73. #ifndef BASISD_SUPPORT_ASTC
  74. #define BASISD_SUPPORT_ASTC 1
  75. #endif
  76. // Note that if BASISD_SUPPORT_ATC is enabled, BASISD_SUPPORT_DXT5A should also be enabled for alpha support.
  77. #ifndef BASISD_SUPPORT_ATC
  78. #define BASISD_SUPPORT_ATC 1
  79. #endif
  80. // Support for ETC2 EAC R11 and ETC2 EAC RG11
  81. #ifndef BASISD_SUPPORT_ETC2_EAC_RG11
  82. #define BASISD_SUPPORT_ETC2_EAC_RG11 1
  83. #endif
  84. // If BASISD_SUPPORT_ASTC_HIGHER_OPAQUE_QUALITY is 1, opaque blocks will be transcoded to ASTC at slightly higher quality (higher than BC1), but the transcoder tables will be 2x as large.
  85. // This impacts grayscale and grayscale+alpha textures the most.
  86. #ifndef BASISD_SUPPORT_ASTC_HIGHER_OPAQUE_QUALITY
  87. #ifdef __EMSCRIPTEN__
  88. // Let's assume size matters more than quality when compiling with emscripten.
  89. #define BASISD_SUPPORT_ASTC_HIGHER_OPAQUE_QUALITY 0
  90. #else
  91. // Compiling native, so an extra 64K lookup table is probably acceptable.
  92. #define BASISD_SUPPORT_ASTC_HIGHER_OPAQUE_QUALITY 1
  93. #endif
  94. #endif
  95. #ifndef BASISD_SUPPORT_FXT1
  96. #define BASISD_SUPPORT_FXT1 1
  97. #endif
  98. #ifndef BASISD_SUPPORT_PVRTC2
  99. #define BASISD_SUPPORT_PVRTC2 1
  100. #endif
  101. #if BASISD_SUPPORT_PVRTC2
  102. #if !BASISD_SUPPORT_ATC
  103. #error BASISD_SUPPORT_ATC must be 1 if BASISD_SUPPORT_PVRTC2 is 1
  104. #endif
  105. #endif
  106. #if BASISD_SUPPORT_ATC
  107. #if !BASISD_SUPPORT_DXT5A
  108. #error BASISD_SUPPORT_DXT5A must be 1 if BASISD_SUPPORT_ATC is 1
  109. #endif
  110. #endif
  111. #define BASISD_WRITE_NEW_BC7_MODE5_TABLES 0
  112. #define BASISD_WRITE_NEW_DXT1_TABLES 0
  113. #define BASISD_WRITE_NEW_ETC2_EAC_A8_TABLES 0
  114. #define BASISD_WRITE_NEW_ASTC_TABLES 0
  115. #define BASISD_WRITE_NEW_ATC_TABLES 0
  116. #define BASISD_WRITE_NEW_ETC2_EAC_R11_TABLES 0
  117. #ifndef BASISD_ENABLE_DEBUG_FLAGS
  118. #define BASISD_ENABLE_DEBUG_FLAGS 0
  119. #endif
  120. // If KTX2 support is enabled, we may need Zstd for decompression of supercompressed UASTC files. Include this header.
  121. #if BASISD_SUPPORT_KTX2
  122. // If BASISD_SUPPORT_KTX2_ZSTD is 0, UASTC files compressed with Zstd cannot be loaded.
  123. #if BASISD_SUPPORT_KTX2_ZSTD
  124. // We only use two Zstd API's: ZSTD_decompress() and ZSTD_isError()
  125. #include "../zstd/zstd.h"
  126. #endif
  127. #endif
  128. namespace basisu
  129. {
  130. bool g_debug_printf;
  131. void enable_debug_printf(bool enabled)
  132. {
  133. g_debug_printf = enabled;
  134. }
  135. void debug_printf(const char* pFmt, ...)
  136. {
  137. #if BASISU_FORCE_DEVEL_MESSAGES
  138. g_debug_printf = true;
  139. #endif
  140. if (g_debug_printf)
  141. {
  142. va_list args;
  143. va_start(args, pFmt);
  144. vprintf(pFmt, args);
  145. va_end(args);
  146. }
  147. }
  148. } // namespace basisu
  149. namespace basist
  150. {
  151. #if BASISD_ENABLE_DEBUG_FLAGS
  152. static uint32_t g_debug_flags = 0;
  153. #endif
  154. uint32_t get_debug_flags()
  155. {
  156. #if BASISD_ENABLE_DEBUG_FLAGS
  157. return g_debug_flags;
  158. #else
  159. return 0;
  160. #endif
  161. }
  162. void set_debug_flags(uint32_t f)
  163. {
  164. BASISU_NOTE_UNUSED(f);
  165. #if BASISD_ENABLE_DEBUG_FLAGS
  166. g_debug_flags = f;
  167. #endif
  168. }
  169. inline uint16_t byteswap_uint16(uint16_t v)
  170. {
  171. return static_cast<uint16_t>((v >> 8) | (v << 8));
  172. }
  173. static inline int32_t clampi(int32_t value, int32_t low, int32_t high) { if (value < low) value = low; else if (value > high) value = high; return value; }
  174. static inline float clampf(float value, float low, float high) { if (value < low) value = low; else if (value > high) value = high; return value; }
  175. static inline float saturate(float value) { return clampf(value, 0, 1.0f); }
  176. static inline uint8_t mul_8(uint32_t v, uint32_t q) { v = v * q + 128; return (uint8_t)((v + (v >> 8)) >> 8); }
  177. uint16_t crc16(const void* r, size_t size, uint16_t crc)
  178. {
  179. crc = ~crc;
  180. const uint8_t* p = static_cast<const uint8_t*>(r);
  181. for (; size; --size)
  182. {
  183. const uint16_t q = *p++ ^ (crc >> 8);
  184. uint16_t k = (q >> 4) ^ q;
  185. crc = (((crc << 8) ^ k) ^ (k << 5)) ^ (k << 12);
  186. }
  187. return static_cast<uint16_t>(~crc);
  188. }
  189. const uint32_t g_global_selector_cb[] =
  190. #include "basisu_global_selector_cb.h"
  191. ;
  192. const uint32_t g_global_selector_cb_size = sizeof(g_global_selector_cb) / sizeof(g_global_selector_cb[0]);
  193. void etc1_global_selector_codebook::init(uint32_t N, const uint32_t* pEntries)
  194. {
  195. m_palette.resize(N);
  196. for (uint32_t i = 0; i < N; i++)
  197. m_palette[i].set_uint32(pEntries[i]);
  198. }
  199. void etc1_global_selector_codebook::print_code(FILE* pFile)
  200. {
  201. fprintf(pFile, "{\n");
  202. for (uint32_t i = 0; i < m_palette.size(); i++)
  203. {
  204. fprintf(pFile, "0x%X,", m_palette[i].get_uint32());
  205. if ((i & 15) == 15)
  206. fprintf(pFile, "\n");
  207. }
  208. fprintf(pFile, "\n}\n");
  209. }
  210. enum etc_constants
  211. {
  212. cETC1BytesPerBlock = 8U,
  213. cETC1SelectorBits = 2U,
  214. cETC1SelectorValues = 1U << cETC1SelectorBits,
  215. cETC1SelectorMask = cETC1SelectorValues - 1U,
  216. cETC1BlockShift = 2U,
  217. cETC1BlockSize = 1U << cETC1BlockShift,
  218. cETC1LSBSelectorIndicesBitOffset = 0,
  219. cETC1MSBSelectorIndicesBitOffset = 16,
  220. cETC1FlipBitOffset = 32,
  221. cETC1DiffBitOffset = 33,
  222. cETC1IntenModifierNumBits = 3,
  223. cETC1IntenModifierValues = 1 << cETC1IntenModifierNumBits,
  224. cETC1RightIntenModifierTableBitOffset = 34,
  225. cETC1LeftIntenModifierTableBitOffset = 37,
  226. // Base+Delta encoding (5 bit bases, 3 bit delta)
  227. cETC1BaseColorCompNumBits = 5,
  228. cETC1BaseColorCompMax = 1 << cETC1BaseColorCompNumBits,
  229. cETC1DeltaColorCompNumBits = 3,
  230. cETC1DeltaColorComp = 1 << cETC1DeltaColorCompNumBits,
  231. cETC1DeltaColorCompMax = 1 << cETC1DeltaColorCompNumBits,
  232. cETC1BaseColor5RBitOffset = 59,
  233. cETC1BaseColor5GBitOffset = 51,
  234. cETC1BaseColor5BBitOffset = 43,
  235. cETC1DeltaColor3RBitOffset = 56,
  236. cETC1DeltaColor3GBitOffset = 48,
  237. cETC1DeltaColor3BBitOffset = 40,
  238. // Absolute (non-delta) encoding (two 4-bit per component bases)
  239. cETC1AbsColorCompNumBits = 4,
  240. cETC1AbsColorCompMax = 1 << cETC1AbsColorCompNumBits,
  241. cETC1AbsColor4R1BitOffset = 60,
  242. cETC1AbsColor4G1BitOffset = 52,
  243. cETC1AbsColor4B1BitOffset = 44,
  244. cETC1AbsColor4R2BitOffset = 56,
  245. cETC1AbsColor4G2BitOffset = 48,
  246. cETC1AbsColor4B2BitOffset = 40,
  247. cETC1ColorDeltaMin = -4,
  248. cETC1ColorDeltaMax = 3,
  249. // Delta3:
  250. // 0 1 2 3 4 5 6 7
  251. // 000 001 010 011 100 101 110 111
  252. // 0 1 2 3 -4 -3 -2 -1
  253. };
  254. #define DECLARE_ETC1_INTEN_TABLE(name, N) \
  255. static const int name[cETC1IntenModifierValues][cETC1SelectorValues] = \
  256. { \
  257. { N * -8, N * -2, N * 2, N * 8 },{ N * -17, N * -5, N * 5, N * 17 },{ N * -29, N * -9, N * 9, N * 29 },{ N * -42, N * -13, N * 13, N * 42 }, \
  258. { N * -60, N * -18, N * 18, N * 60 },{ N * -80, N * -24, N * 24, N * 80 },{ N * -106, N * -33, N * 33, N * 106 },{ N * -183, N * -47, N * 47, N * 183 } \
  259. };
  260. DECLARE_ETC1_INTEN_TABLE(g_etc1_inten_tables, 1);
  261. DECLARE_ETC1_INTEN_TABLE(g_etc1_inten_tables16, 16);
  262. DECLARE_ETC1_INTEN_TABLE(g_etc1_inten_tables48, 3 * 16);
  263. //const uint8_t g_etc1_to_selector_index[cETC1SelectorValues] = { 2, 3, 1, 0 };
  264. const uint8_t g_selector_index_to_etc1[cETC1SelectorValues] = { 3, 2, 0, 1 };
  265. static const uint8_t g_etc_5_to_8[32] = { 0, 8, 16, 24, 33, 41, 49, 57, 66, 74, 82, 90, 99, 107, 115, 123, 132, 140, 148, 156, 165, 173, 181, 189, 198, 206, 214, 222, 231, 239, 247, 255 };
  266. struct decoder_etc_block
  267. {
  268. // big endian uint64:
  269. // bit ofs: 56 48 40 32 24 16 8 0
  270. // byte ofs: b0, b1, b2, b3, b4, b5, b6, b7
  271. union
  272. {
  273. uint64_t m_uint64;
  274. uint32_t m_uint32[2];
  275. uint8_t m_bytes[8];
  276. struct
  277. {
  278. signed m_dred2 : 3;
  279. uint32_t m_red1 : 5;
  280. signed m_dgreen2 : 3;
  281. uint32_t m_green1 : 5;
  282. signed m_dblue2 : 3;
  283. uint32_t m_blue1 : 5;
  284. uint32_t m_flip : 1;
  285. uint32_t m_diff : 1;
  286. uint32_t m_cw2 : 3;
  287. uint32_t m_cw1 : 3;
  288. uint32_t m_selectors;
  289. } m_differential;
  290. };
  291. inline void clear()
  292. {
  293. assert(sizeof(*this) == 8);
  294. basisu::clear_obj(*this);
  295. }
  296. inline void set_byte_bits(uint32_t ofs, uint32_t num, uint32_t bits)
  297. {
  298. assert((ofs + num) <= 64U);
  299. assert(num && (num < 32U));
  300. assert((ofs >> 3) == ((ofs + num - 1) >> 3));
  301. assert(bits < (1U << num));
  302. const uint32_t byte_ofs = 7 - (ofs >> 3);
  303. const uint32_t byte_bit_ofs = ofs & 7;
  304. const uint32_t mask = (1 << num) - 1;
  305. m_bytes[byte_ofs] &= ~(mask << byte_bit_ofs);
  306. m_bytes[byte_ofs] |= (bits << byte_bit_ofs);
  307. }
  308. inline void set_flip_bit(bool flip)
  309. {
  310. m_bytes[3] &= ~1;
  311. m_bytes[3] |= static_cast<uint8_t>(flip);
  312. }
  313. inline void set_diff_bit(bool diff)
  314. {
  315. m_bytes[3] &= ~2;
  316. m_bytes[3] |= (static_cast<uint32_t>(diff) << 1);
  317. }
  318. // Sets intensity modifier table (0-7) used by subblock subblock_id (0 or 1)
  319. inline void set_inten_table(uint32_t subblock_id, uint32_t t)
  320. {
  321. assert(subblock_id < 2);
  322. assert(t < 8);
  323. const uint32_t ofs = subblock_id ? 2 : 5;
  324. m_bytes[3] &= ~(7 << ofs);
  325. m_bytes[3] |= (t << ofs);
  326. }
  327. // Selector "val" ranges from 0-3 and is a direct index into g_etc1_inten_tables.
  328. inline void set_selector(uint32_t x, uint32_t y, uint32_t val)
  329. {
  330. assert((x | y | val) < 4);
  331. const uint32_t bit_index = x * 4 + y;
  332. uint8_t* p = &m_bytes[7 - (bit_index >> 3)];
  333. const uint32_t byte_bit_ofs = bit_index & 7;
  334. const uint32_t mask = 1 << byte_bit_ofs;
  335. static const uint8_t s_selector_index_to_etc1[4] = { 3, 2, 0, 1 };
  336. const uint32_t etc1_val = s_selector_index_to_etc1[val];
  337. const uint32_t lsb = etc1_val & 1;
  338. const uint32_t msb = etc1_val >> 1;
  339. p[0] &= ~mask;
  340. p[0] |= (lsb << byte_bit_ofs);
  341. p[-2] &= ~mask;
  342. p[-2] |= (msb << byte_bit_ofs);
  343. }
  344. // Returned encoded selector value ranges from 0-3 (this is NOT a direct index into g_etc1_inten_tables, see get_selector())
  345. inline uint32_t get_raw_selector(uint32_t x, uint32_t y) const
  346. {
  347. assert((x | y) < 4);
  348. const uint32_t bit_index = x * 4 + y;
  349. const uint32_t byte_bit_ofs = bit_index & 7;
  350. const uint8_t* p = &m_bytes[7 - (bit_index >> 3)];
  351. const uint32_t lsb = (p[0] >> byte_bit_ofs) & 1;
  352. const uint32_t msb = (p[-2] >> byte_bit_ofs) & 1;
  353. const uint32_t val = lsb | (msb << 1);
  354. return val;
  355. }
  356. // Returned selector value ranges from 0-3 and is a direct index into g_etc1_inten_tables.
  357. inline uint32_t get_selector(uint32_t x, uint32_t y) const
  358. {
  359. static const uint8_t s_etc1_to_selector_index[cETC1SelectorValues] = { 2, 3, 1, 0 };
  360. return s_etc1_to_selector_index[get_raw_selector(x, y)];
  361. }
  362. inline void set_raw_selector_bits(uint32_t bits)
  363. {
  364. m_bytes[4] = static_cast<uint8_t>(bits);
  365. m_bytes[5] = static_cast<uint8_t>(bits >> 8);
  366. m_bytes[6] = static_cast<uint8_t>(bits >> 16);
  367. m_bytes[7] = static_cast<uint8_t>(bits >> 24);
  368. }
  369. inline bool are_all_selectors_the_same() const
  370. {
  371. uint32_t v = *reinterpret_cast<const uint32_t*>(&m_bytes[4]);
  372. if ((v == 0xFFFFFFFF) || (v == 0xFFFF) || (!v) || (v == 0xFFFF0000))
  373. return true;
  374. return false;
  375. }
  376. inline void set_raw_selector_bits(uint8_t byte0, uint8_t byte1, uint8_t byte2, uint8_t byte3)
  377. {
  378. m_bytes[4] = byte0;
  379. m_bytes[5] = byte1;
  380. m_bytes[6] = byte2;
  381. m_bytes[7] = byte3;
  382. }
  383. inline uint32_t get_raw_selector_bits() const
  384. {
  385. return m_bytes[4] | (m_bytes[5] << 8) | (m_bytes[6] << 16) | (m_bytes[7] << 24);
  386. }
  387. inline void set_base4_color(uint32_t idx, uint16_t c)
  388. {
  389. if (idx)
  390. {
  391. set_byte_bits(cETC1AbsColor4R2BitOffset, 4, (c >> 8) & 15);
  392. set_byte_bits(cETC1AbsColor4G2BitOffset, 4, (c >> 4) & 15);
  393. set_byte_bits(cETC1AbsColor4B2BitOffset, 4, c & 15);
  394. }
  395. else
  396. {
  397. set_byte_bits(cETC1AbsColor4R1BitOffset, 4, (c >> 8) & 15);
  398. set_byte_bits(cETC1AbsColor4G1BitOffset, 4, (c >> 4) & 15);
  399. set_byte_bits(cETC1AbsColor4B1BitOffset, 4, c & 15);
  400. }
  401. }
  402. inline void set_base5_color(uint16_t c)
  403. {
  404. set_byte_bits(cETC1BaseColor5RBitOffset, 5, (c >> 10) & 31);
  405. set_byte_bits(cETC1BaseColor5GBitOffset, 5, (c >> 5) & 31);
  406. set_byte_bits(cETC1BaseColor5BBitOffset, 5, c & 31);
  407. }
  408. void set_delta3_color(uint16_t c)
  409. {
  410. set_byte_bits(cETC1DeltaColor3RBitOffset, 3, (c >> 6) & 7);
  411. set_byte_bits(cETC1DeltaColor3GBitOffset, 3, (c >> 3) & 7);
  412. set_byte_bits(cETC1DeltaColor3BBitOffset, 3, c & 7);
  413. }
  414. void set_block_color4(const color32& c0_unscaled, const color32& c1_unscaled)
  415. {
  416. set_diff_bit(false);
  417. set_base4_color(0, pack_color4(c0_unscaled, false));
  418. set_base4_color(1, pack_color4(c1_unscaled, false));
  419. }
  420. void set_block_color5(const color32& c0_unscaled, const color32& c1_unscaled)
  421. {
  422. set_diff_bit(true);
  423. set_base5_color(pack_color5(c0_unscaled, false));
  424. int dr = c1_unscaled.r - c0_unscaled.r;
  425. int dg = c1_unscaled.g - c0_unscaled.g;
  426. int db = c1_unscaled.b - c0_unscaled.b;
  427. set_delta3_color(pack_delta3(dr, dg, db));
  428. }
  429. bool set_block_color5_check(const color32& c0_unscaled, const color32& c1_unscaled)
  430. {
  431. set_diff_bit(true);
  432. set_base5_color(pack_color5(c0_unscaled, false));
  433. int dr = c1_unscaled.r - c0_unscaled.r;
  434. int dg = c1_unscaled.g - c0_unscaled.g;
  435. int db = c1_unscaled.b - c0_unscaled.b;
  436. if (((dr < cETC1ColorDeltaMin) || (dr > cETC1ColorDeltaMax)) ||
  437. ((dg < cETC1ColorDeltaMin) || (dg > cETC1ColorDeltaMax)) ||
  438. ((db < cETC1ColorDeltaMin) || (db > cETC1ColorDeltaMax)))
  439. return false;
  440. set_delta3_color(pack_delta3(dr, dg, db));
  441. return true;
  442. }
  443. inline uint32_t get_byte_bits(uint32_t ofs, uint32_t num) const
  444. {
  445. assert((ofs + num) <= 64U);
  446. assert(num && (num <= 8U));
  447. assert((ofs >> 3) == ((ofs + num - 1) >> 3));
  448. const uint32_t byte_ofs = 7 - (ofs >> 3);
  449. const uint32_t byte_bit_ofs = ofs & 7;
  450. return (m_bytes[byte_ofs] >> byte_bit_ofs) & ((1 << num) - 1);
  451. }
  452. inline uint16_t get_base5_color() const
  453. {
  454. const uint32_t r = get_byte_bits(cETC1BaseColor5RBitOffset, 5);
  455. const uint32_t g = get_byte_bits(cETC1BaseColor5GBitOffset, 5);
  456. const uint32_t b = get_byte_bits(cETC1BaseColor5BBitOffset, 5);
  457. return static_cast<uint16_t>(b | (g << 5U) | (r << 10U));
  458. }
  459. inline uint16_t get_base4_color(uint32_t idx) const
  460. {
  461. uint32_t r, g, b;
  462. if (idx)
  463. {
  464. r = get_byte_bits(cETC1AbsColor4R2BitOffset, 4);
  465. g = get_byte_bits(cETC1AbsColor4G2BitOffset, 4);
  466. b = get_byte_bits(cETC1AbsColor4B2BitOffset, 4);
  467. }
  468. else
  469. {
  470. r = get_byte_bits(cETC1AbsColor4R1BitOffset, 4);
  471. g = get_byte_bits(cETC1AbsColor4G1BitOffset, 4);
  472. b = get_byte_bits(cETC1AbsColor4B1BitOffset, 4);
  473. }
  474. return static_cast<uint16_t>(b | (g << 4U) | (r << 8U));
  475. }
  476. inline color32 get_base5_color_unscaled() const
  477. {
  478. return color32(m_differential.m_red1, m_differential.m_green1, m_differential.m_blue1, 255);
  479. }
  480. inline bool get_flip_bit() const
  481. {
  482. return (m_bytes[3] & 1) != 0;
  483. }
  484. inline bool get_diff_bit() const
  485. {
  486. return (m_bytes[3] & 2) != 0;
  487. }
  488. inline uint32_t get_inten_table(uint32_t subblock_id) const
  489. {
  490. assert(subblock_id < 2);
  491. const uint32_t ofs = subblock_id ? 2 : 5;
  492. return (m_bytes[3] >> ofs) & 7;
  493. }
  494. inline uint16_t get_delta3_color() const
  495. {
  496. const uint32_t r = get_byte_bits(cETC1DeltaColor3RBitOffset, 3);
  497. const uint32_t g = get_byte_bits(cETC1DeltaColor3GBitOffset, 3);
  498. const uint32_t b = get_byte_bits(cETC1DeltaColor3BBitOffset, 3);
  499. return static_cast<uint16_t>(b | (g << 3U) | (r << 6U));
  500. }
  501. void get_block_colors(color32* pBlock_colors, uint32_t subblock_index) const
  502. {
  503. color32 b;
  504. if (get_diff_bit())
  505. {
  506. if (subblock_index)
  507. unpack_color5(b, get_base5_color(), get_delta3_color(), true, 255);
  508. else
  509. unpack_color5(b, get_base5_color(), true);
  510. }
  511. else
  512. {
  513. b = unpack_color4(get_base4_color(subblock_index), true, 255);
  514. }
  515. const int* pInten_table = g_etc1_inten_tables[get_inten_table(subblock_index)];
  516. pBlock_colors[0].set_noclamp_rgba(clamp255(b.r + pInten_table[0]), clamp255(b.g + pInten_table[0]), clamp255(b.b + pInten_table[0]), 255);
  517. pBlock_colors[1].set_noclamp_rgba(clamp255(b.r + pInten_table[1]), clamp255(b.g + pInten_table[1]), clamp255(b.b + pInten_table[1]), 255);
  518. pBlock_colors[2].set_noclamp_rgba(clamp255(b.r + pInten_table[2]), clamp255(b.g + pInten_table[2]), clamp255(b.b + pInten_table[2]), 255);
  519. pBlock_colors[3].set_noclamp_rgba(clamp255(b.r + pInten_table[3]), clamp255(b.g + pInten_table[3]), clamp255(b.b + pInten_table[3]), 255);
  520. }
  521. static uint16_t pack_color4(const color32& color, bool scaled, uint32_t bias = 127U)
  522. {
  523. return pack_color4(color.r, color.g, color.b, scaled, bias);
  524. }
  525. static uint16_t pack_color4(uint32_t r, uint32_t g, uint32_t b, bool scaled, uint32_t bias = 127U)
  526. {
  527. if (scaled)
  528. {
  529. r = (r * 15U + bias) / 255U;
  530. g = (g * 15U + bias) / 255U;
  531. b = (b * 15U + bias) / 255U;
  532. }
  533. r = basisu::minimum(r, 15U);
  534. g = basisu::minimum(g, 15U);
  535. b = basisu::minimum(b, 15U);
  536. return static_cast<uint16_t>(b | (g << 4U) | (r << 8U));
  537. }
  538. static uint16_t pack_color5(const color32& color, bool scaled, uint32_t bias = 127U)
  539. {
  540. return pack_color5(color.r, color.g, color.b, scaled, bias);
  541. }
  542. static uint16_t pack_color5(uint32_t r, uint32_t g, uint32_t b, bool scaled, uint32_t bias = 127U)
  543. {
  544. if (scaled)
  545. {
  546. r = (r * 31U + bias) / 255U;
  547. g = (g * 31U + bias) / 255U;
  548. b = (b * 31U + bias) / 255U;
  549. }
  550. r = basisu::minimum(r, 31U);
  551. g = basisu::minimum(g, 31U);
  552. b = basisu::minimum(b, 31U);
  553. return static_cast<uint16_t>(b | (g << 5U) | (r << 10U));
  554. }
  555. uint16_t pack_delta3(const color32& color)
  556. {
  557. return pack_delta3(color.r, color.g, color.b);
  558. }
  559. uint16_t pack_delta3(int r, int g, int b)
  560. {
  561. assert((r >= cETC1ColorDeltaMin) && (r <= cETC1ColorDeltaMax));
  562. assert((g >= cETC1ColorDeltaMin) && (g <= cETC1ColorDeltaMax));
  563. assert((b >= cETC1ColorDeltaMin) && (b <= cETC1ColorDeltaMax));
  564. if (r < 0) r += 8;
  565. if (g < 0) g += 8;
  566. if (b < 0) b += 8;
  567. return static_cast<uint16_t>(b | (g << 3) | (r << 6));
  568. }
  569. static void unpack_delta3(int& r, int& g, int& b, uint16_t packed_delta3)
  570. {
  571. r = (packed_delta3 >> 6) & 7;
  572. g = (packed_delta3 >> 3) & 7;
  573. b = packed_delta3 & 7;
  574. if (r >= 4) r -= 8;
  575. if (g >= 4) g -= 8;
  576. if (b >= 4) b -= 8;
  577. }
  578. static color32 unpack_color5(uint16_t packed_color5, bool scaled, uint32_t alpha)
  579. {
  580. uint32_t b = packed_color5 & 31U;
  581. uint32_t g = (packed_color5 >> 5U) & 31U;
  582. uint32_t r = (packed_color5 >> 10U) & 31U;
  583. if (scaled)
  584. {
  585. b = (b << 3U) | (b >> 2U);
  586. g = (g << 3U) | (g >> 2U);
  587. r = (r << 3U) | (r >> 2U);
  588. }
  589. assert(alpha <= 255);
  590. return color32(cNoClamp, r, g, b, alpha);
  591. }
  592. static void unpack_color5(uint32_t& r, uint32_t& g, uint32_t& b, uint16_t packed_color5, bool scaled)
  593. {
  594. color32 c(unpack_color5(packed_color5, scaled, 0));
  595. r = c.r;
  596. g = c.g;
  597. b = c.b;
  598. }
  599. static void unpack_color5(color32& result, uint16_t packed_color5, bool scaled)
  600. {
  601. result = unpack_color5(packed_color5, scaled, 255);
  602. }
  603. static bool unpack_color5(color32& result, uint16_t packed_color5, uint16_t packed_delta3, bool scaled, uint32_t alpha)
  604. {
  605. int dr, dg, db;
  606. unpack_delta3(dr, dg, db, packed_delta3);
  607. int r = ((packed_color5 >> 10U) & 31U) + dr;
  608. int g = ((packed_color5 >> 5U) & 31U) + dg;
  609. int b = (packed_color5 & 31U) + db;
  610. bool success = true;
  611. if (static_cast<uint32_t>(r | g | b) > 31U)
  612. {
  613. success = false;
  614. r = basisu::clamp<int>(r, 0, 31);
  615. g = basisu::clamp<int>(g, 0, 31);
  616. b = basisu::clamp<int>(b, 0, 31);
  617. }
  618. if (scaled)
  619. {
  620. b = (b << 3U) | (b >> 2U);
  621. g = (g << 3U) | (g >> 2U);
  622. r = (r << 3U) | (r >> 2U);
  623. }
  624. result.set_noclamp_rgba(r, g, b, basisu::minimum(alpha, 255U));
  625. return success;
  626. }
  627. static color32 unpack_color4(uint16_t packed_color4, bool scaled, uint32_t alpha)
  628. {
  629. uint32_t b = packed_color4 & 15U;
  630. uint32_t g = (packed_color4 >> 4U) & 15U;
  631. uint32_t r = (packed_color4 >> 8U) & 15U;
  632. if (scaled)
  633. {
  634. b = (b << 4U) | b;
  635. g = (g << 4U) | g;
  636. r = (r << 4U) | r;
  637. }
  638. return color32(cNoClamp, r, g, b, basisu::minimum(alpha, 255U));
  639. }
  640. static void unpack_color4(uint32_t& r, uint32_t& g, uint32_t& b, uint16_t packed_color4, bool scaled)
  641. {
  642. color32 c(unpack_color4(packed_color4, scaled, 0));
  643. r = c.r;
  644. g = c.g;
  645. b = c.b;
  646. }
  647. static void get_diff_subblock_colors(color32* pDst, uint16_t packed_color5, uint32_t table_idx)
  648. {
  649. assert(table_idx < cETC1IntenModifierValues);
  650. const int* pInten_modifer_table = &g_etc1_inten_tables[table_idx][0];
  651. uint32_t r, g, b;
  652. unpack_color5(r, g, b, packed_color5, true);
  653. const int ir = static_cast<int>(r), ig = static_cast<int>(g), ib = static_cast<int>(b);
  654. const int y0 = pInten_modifer_table[0];
  655. pDst[0].set(clamp255(ir + y0), clamp255(ig + y0), clamp255(ib + y0), 255);
  656. const int y1 = pInten_modifer_table[1];
  657. pDst[1].set(clamp255(ir + y1), clamp255(ig + y1), clamp255(ib + y1), 255);
  658. const int y2 = pInten_modifer_table[2];
  659. pDst[2].set(clamp255(ir + y2), clamp255(ig + y2), clamp255(ib + y2), 255);
  660. const int y3 = pInten_modifer_table[3];
  661. pDst[3].set(clamp255(ir + y3), clamp255(ig + y3), clamp255(ib + y3), 255);
  662. }
  663. static int clamp255(int x)
  664. {
  665. if (x & 0xFFFFFF00)
  666. {
  667. if (x < 0)
  668. x = 0;
  669. else if (x > 255)
  670. x = 255;
  671. }
  672. return x;
  673. }
  674. static void get_block_colors5(color32* pBlock_colors, const color32& base_color5, uint32_t inten_table)
  675. {
  676. color32 b(base_color5);
  677. b.r = (b.r << 3) | (b.r >> 2);
  678. b.g = (b.g << 3) | (b.g >> 2);
  679. b.b = (b.b << 3) | (b.b >> 2);
  680. const int* pInten_table = g_etc1_inten_tables[inten_table];
  681. pBlock_colors[0].set(clamp255(b.r + pInten_table[0]), clamp255(b.g + pInten_table[0]), clamp255(b.b + pInten_table[0]), 255);
  682. pBlock_colors[1].set(clamp255(b.r + pInten_table[1]), clamp255(b.g + pInten_table[1]), clamp255(b.b + pInten_table[1]), 255);
  683. pBlock_colors[2].set(clamp255(b.r + pInten_table[2]), clamp255(b.g + pInten_table[2]), clamp255(b.b + pInten_table[2]), 255);
  684. pBlock_colors[3].set(clamp255(b.r + pInten_table[3]), clamp255(b.g + pInten_table[3]), clamp255(b.b + pInten_table[3]), 255);
  685. }
  686. static void get_block_color5(const color32& base_color5, uint32_t inten_table, uint32_t index, uint32_t& r, uint32_t &g, uint32_t &b)
  687. {
  688. assert(index < 4);
  689. uint32_t br = (base_color5.r << 3) | (base_color5.r >> 2);
  690. uint32_t bg = (base_color5.g << 3) | (base_color5.g >> 2);
  691. uint32_t bb = (base_color5.b << 3) | (base_color5.b >> 2);
  692. const int* pInten_table = g_etc1_inten_tables[inten_table];
  693. r = clamp255(br + pInten_table[index]);
  694. g = clamp255(bg + pInten_table[index]);
  695. b = clamp255(bb + pInten_table[index]);
  696. }
  697. static void get_block_color5_r(const color32& base_color5, uint32_t inten_table, uint32_t index, uint32_t &r)
  698. {
  699. assert(index < 4);
  700. uint32_t br = (base_color5.r << 3) | (base_color5.r >> 2);
  701. const int* pInten_table = g_etc1_inten_tables[inten_table];
  702. r = clamp255(br + pInten_table[index]);
  703. }
  704. static void get_block_colors5_g(int* pBlock_colors, const color32& base_color5, uint32_t inten_table)
  705. {
  706. const int g = (base_color5.g << 3) | (base_color5.g >> 2);
  707. const int* pInten_table = g_etc1_inten_tables[inten_table];
  708. pBlock_colors[0] = clamp255(g + pInten_table[0]);
  709. pBlock_colors[1] = clamp255(g + pInten_table[1]);
  710. pBlock_colors[2] = clamp255(g + pInten_table[2]);
  711. pBlock_colors[3] = clamp255(g + pInten_table[3]);
  712. }
  713. static void get_block_colors5_bounds(color32* pBlock_colors, const color32& base_color5, uint32_t inten_table, uint32_t l = 0, uint32_t h = 3)
  714. {
  715. color32 b(base_color5);
  716. b.r = (b.r << 3) | (b.r >> 2);
  717. b.g = (b.g << 3) | (b.g >> 2);
  718. b.b = (b.b << 3) | (b.b >> 2);
  719. const int* pInten_table = g_etc1_inten_tables[inten_table];
  720. pBlock_colors[0].set(clamp255(b.r + pInten_table[l]), clamp255(b.g + pInten_table[l]), clamp255(b.b + pInten_table[l]), 255);
  721. pBlock_colors[1].set(clamp255(b.r + pInten_table[h]), clamp255(b.g + pInten_table[h]), clamp255(b.b + pInten_table[h]), 255);
  722. }
  723. static void get_block_colors5_bounds_g(uint32_t* pBlock_colors, const color32& base_color5, uint32_t inten_table, uint32_t l = 0, uint32_t h = 3)
  724. {
  725. color32 b(base_color5);
  726. b.g = (b.g << 3) | (b.g >> 2);
  727. const int* pInten_table = g_etc1_inten_tables[inten_table];
  728. pBlock_colors[0] = clamp255(b.g + pInten_table[l]);
  729. pBlock_colors[1] = clamp255(b.g + pInten_table[h]);
  730. }
  731. };
  732. enum dxt_constants
  733. {
  734. cDXT1SelectorBits = 2U, cDXT1SelectorValues = 1U << cDXT1SelectorBits, cDXT1SelectorMask = cDXT1SelectorValues - 1U,
  735. cDXT5SelectorBits = 3U, cDXT5SelectorValues = 1U << cDXT5SelectorBits, cDXT5SelectorMask = cDXT5SelectorValues - 1U,
  736. };
  737. static const uint8_t g_etc1_x_selector_unpack[4][256] =
  738. {
  739. {
  740. 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3,
  741. 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3,
  742. 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3,
  743. 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3,
  744. 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3,
  745. 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3,
  746. 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3,
  747. 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3, 2, 3,
  748. },
  749. {
  750. 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1,
  751. 2, 2, 3, 3, 2, 2, 3, 3, 2, 2, 3, 3, 2, 2, 3, 3, 2, 2, 3, 3, 2, 2, 3, 3, 2, 2, 3, 3, 2, 2, 3, 3,
  752. 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1,
  753. 2, 2, 3, 3, 2, 2, 3, 3, 2, 2, 3, 3, 2, 2, 3, 3, 2, 2, 3, 3, 2, 2, 3, 3, 2, 2, 3, 3, 2, 2, 3, 3,
  754. 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1,
  755. 2, 2, 3, 3, 2, 2, 3, 3, 2, 2, 3, 3, 2, 2, 3, 3, 2, 2, 3, 3, 2, 2, 3, 3, 2, 2, 3, 3, 2, 2, 3, 3,
  756. 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1,
  757. 2, 2, 3, 3, 2, 2, 3, 3, 2, 2, 3, 3, 2, 2, 3, 3, 2, 2, 3, 3, 2, 2, 3, 3, 2, 2, 3, 3, 2, 2, 3, 3,
  758. },
  759. {
  760. 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1,
  761. 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1,
  762. 2, 2, 2, 2, 3, 3, 3, 3, 2, 2, 2, 2, 3, 3, 3, 3, 2, 2, 2, 2, 3, 3, 3, 3, 2, 2, 2, 2, 3, 3, 3, 3,
  763. 2, 2, 2, 2, 3, 3, 3, 3, 2, 2, 2, 2, 3, 3, 3, 3, 2, 2, 2, 2, 3, 3, 3, 3, 2, 2, 2, 2, 3, 3, 3, 3,
  764. 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1,
  765. 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1,
  766. 2, 2, 2, 2, 3, 3, 3, 3, 2, 2, 2, 2, 3, 3, 3, 3, 2, 2, 2, 2, 3, 3, 3, 3, 2, 2, 2, 2, 3, 3, 3, 3,
  767. 2, 2, 2, 2, 3, 3, 3, 3, 2, 2, 2, 2, 3, 3, 3, 3, 2, 2, 2, 2, 3, 3, 3, 3, 2, 2, 2, 2, 3, 3, 3, 3,
  768. },
  769. {
  770. 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
  771. 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
  772. 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
  773. 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
  774. 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
  775. 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
  776. 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
  777. 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,
  778. }
  779. };
  780. struct dxt1_block
  781. {
  782. enum { cTotalEndpointBytes = 2, cTotalSelectorBytes = 4 };
  783. uint8_t m_low_color[cTotalEndpointBytes];
  784. uint8_t m_high_color[cTotalEndpointBytes];
  785. uint8_t m_selectors[cTotalSelectorBytes];
  786. inline void clear() { basisu::clear_obj(*this); }
  787. inline uint32_t get_high_color() const { return m_high_color[0] | (m_high_color[1] << 8U); }
  788. inline uint32_t get_low_color() const { return m_low_color[0] | (m_low_color[1] << 8U); }
  789. inline void set_low_color(uint16_t c) { m_low_color[0] = static_cast<uint8_t>(c & 0xFF); m_low_color[1] = static_cast<uint8_t>((c >> 8) & 0xFF); }
  790. inline void set_high_color(uint16_t c) { m_high_color[0] = static_cast<uint8_t>(c & 0xFF); m_high_color[1] = static_cast<uint8_t>((c >> 8) & 0xFF); }
  791. inline uint32_t get_selector(uint32_t x, uint32_t y) const { assert((x < 4U) && (y < 4U)); return (m_selectors[y] >> (x * cDXT1SelectorBits)) & cDXT1SelectorMask; }
  792. inline void set_selector(uint32_t x, uint32_t y, uint32_t val) { assert((x < 4U) && (y < 4U) && (val < 4U)); m_selectors[y] &= (~(cDXT1SelectorMask << (x * cDXT1SelectorBits))); m_selectors[y] |= (val << (x * cDXT1SelectorBits)); }
  793. static uint16_t pack_color(const color32& color, bool scaled, uint32_t bias = 127U)
  794. {
  795. uint32_t r = color.r, g = color.g, b = color.b;
  796. if (scaled)
  797. {
  798. r = (r * 31U + bias) / 255U;
  799. g = (g * 63U + bias) / 255U;
  800. b = (b * 31U + bias) / 255U;
  801. }
  802. return static_cast<uint16_t>(basisu::minimum(b, 31U) | (basisu::minimum(g, 63U) << 5U) | (basisu::minimum(r, 31U) << 11U));
  803. }
  804. static uint16_t pack_unscaled_color(uint32_t r, uint32_t g, uint32_t b) { return static_cast<uint16_t>(b | (g << 5U) | (r << 11U)); }
  805. };
  806. struct dxt_selector_range
  807. {
  808. uint32_t m_low;
  809. uint32_t m_high;
  810. };
  811. struct etc1_to_dxt1_56_solution
  812. {
  813. uint8_t m_lo;
  814. uint8_t m_hi;
  815. uint16_t m_err;
  816. };
  817. #if BASISD_SUPPORT_DXT1
  818. static dxt_selector_range g_etc1_to_dxt1_selector_ranges[] =
  819. {
  820. { 0, 3 },
  821. { 1, 3 },
  822. { 0, 2 },
  823. { 1, 2 },
  824. { 2, 3 },
  825. { 0, 1 },
  826. };
  827. const uint32_t NUM_ETC1_TO_DXT1_SELECTOR_RANGES = sizeof(g_etc1_to_dxt1_selector_ranges) / sizeof(g_etc1_to_dxt1_selector_ranges[0]);
  828. static uint32_t g_etc1_to_dxt1_selector_range_index[4][4];
  829. const uint32_t NUM_ETC1_TO_DXT1_SELECTOR_MAPPINGS = 10;
  830. static const uint8_t g_etc1_to_dxt1_selector_mappings[NUM_ETC1_TO_DXT1_SELECTOR_MAPPINGS][4] =
  831. {
  832. { 0, 0, 1, 1 },
  833. { 0, 0, 1, 2 },
  834. { 0, 0, 1, 3 },
  835. { 0, 0, 2, 3 },
  836. { 0, 1, 1, 1 },
  837. { 0, 1, 2, 2 },
  838. { 0, 1, 2, 3 },
  839. { 0, 2, 3, 3 },
  840. { 1, 2, 2, 2 },
  841. { 1, 2, 3, 3 },
  842. };
  843. static uint8_t g_etc1_to_dxt1_selector_mappings_raw_dxt1_256[NUM_ETC1_TO_DXT1_SELECTOR_MAPPINGS][256];
  844. static uint8_t g_etc1_to_dxt1_selector_mappings_raw_dxt1_inv_256[NUM_ETC1_TO_DXT1_SELECTOR_MAPPINGS][256];
  845. static const etc1_to_dxt1_56_solution g_etc1_to_dxt_6[32 * 8 * NUM_ETC1_TO_DXT1_SELECTOR_MAPPINGS * NUM_ETC1_TO_DXT1_SELECTOR_RANGES] = {
  846. #include "basisu_transcoder_tables_dxt1_6.inc"
  847. };
  848. static const etc1_to_dxt1_56_solution g_etc1_to_dxt_5[32 * 8 * NUM_ETC1_TO_DXT1_SELECTOR_MAPPINGS * NUM_ETC1_TO_DXT1_SELECTOR_RANGES] = {
  849. #include "basisu_transcoder_tables_dxt1_5.inc"
  850. };
  851. #endif // BASISD_SUPPORT_DXT1
  852. #if BASISD_SUPPORT_DXT1 || BASISD_SUPPORT_UASTC
  853. // First saw the idea for optimal BC1 single-color block encoding using lookup tables in ryg_dxt.
  854. struct bc1_match_entry
  855. {
  856. uint8_t m_hi;
  857. uint8_t m_lo;
  858. };
  859. static bc1_match_entry g_bc1_match5_equals_1[256], g_bc1_match6_equals_1[256]; // selector 1, allow equals hi/lo
  860. static bc1_match_entry g_bc1_match5_equals_0[256], g_bc1_match6_equals_0[256]; // selector 0, allow equals hi/lo
  861. static void prepare_bc1_single_color_table(bc1_match_entry* pTable, const uint8_t* pExpand, int size0, int size1, int sel)
  862. {
  863. for (int i = 0; i < 256; i++)
  864. {
  865. int lowest_e = 256;
  866. for (int lo = 0; lo < size0; lo++)
  867. {
  868. for (int hi = 0; hi < size1; hi++)
  869. {
  870. const int lo_e = pExpand[lo], hi_e = pExpand[hi];
  871. int e;
  872. if (sel == 1)
  873. {
  874. // Selector 1
  875. e = basisu::iabs(((hi_e * 2 + lo_e) / 3) - i);
  876. e += (basisu::iabs(hi_e - lo_e) * 3) / 100;
  877. }
  878. else
  879. {
  880. assert(sel == 0);
  881. // Selector 0
  882. e = basisu::iabs(hi_e - i);
  883. }
  884. if (e < lowest_e)
  885. {
  886. pTable[i].m_hi = static_cast<uint8_t>(hi);
  887. pTable[i].m_lo = static_cast<uint8_t>(lo);
  888. lowest_e = e;
  889. }
  890. } // hi
  891. } // lo
  892. }
  893. }
  894. #endif
  895. #if BASISD_WRITE_NEW_DXT1_TABLES
  896. static void create_etc1_to_dxt1_5_conversion_table()
  897. {
  898. FILE* pFile = nullptr;
  899. fopen_s(&pFile, "basisu_transcoder_tables_dxt1_5.inc", "w");
  900. uint32_t n = 0;
  901. for (int inten = 0; inten < 8; inten++)
  902. {
  903. for (uint32_t g = 0; g < 32; g++)
  904. {
  905. color32 block_colors[4];
  906. decoder_etc_block::get_diff_subblock_colors(block_colors, decoder_etc_block::pack_color5(color32(g, g, g, 255), false), inten);
  907. for (uint32_t sr = 0; sr < NUM_ETC1_TO_DXT1_SELECTOR_RANGES; sr++)
  908. {
  909. const uint32_t low_selector = g_etc1_to_dxt1_selector_ranges[sr].m_low;
  910. const uint32_t high_selector = g_etc1_to_dxt1_selector_ranges[sr].m_high;
  911. for (uint32_t m = 0; m < NUM_ETC1_TO_DXT1_SELECTOR_MAPPINGS; m++)
  912. {
  913. uint32_t best_lo = 0;
  914. uint32_t best_hi = 0;
  915. uint64_t best_err = UINT64_MAX;
  916. for (uint32_t hi = 0; hi <= 31; hi++)
  917. {
  918. for (uint32_t lo = 0; lo <= 31; lo++)
  919. {
  920. //if (lo == hi) continue;
  921. uint32_t colors[4];
  922. colors[0] = (lo << 3) | (lo >> 2);
  923. colors[3] = (hi << 3) | (hi >> 2);
  924. colors[1] = (colors[0] * 2 + colors[3]) / 3;
  925. colors[2] = (colors[3] * 2 + colors[0]) / 3;
  926. uint64_t total_err = 0;
  927. for (uint32_t s = low_selector; s <= high_selector; s++)
  928. {
  929. int err = block_colors[s].g - colors[g_etc1_to_dxt1_selector_mappings[m][s]];
  930. total_err += err * err;
  931. }
  932. if (total_err < best_err)
  933. {
  934. best_err = total_err;
  935. best_lo = lo;
  936. best_hi = hi;
  937. }
  938. }
  939. }
  940. assert(best_err <= 0xFFFF);
  941. //table[g + inten * 32].m_solutions[sr][m].m_lo = static_cast<uint8_t>(best_lo);
  942. //table[g + inten * 32].m_solutions[sr][m].m_hi = static_cast<uint8_t>(best_hi);
  943. //table[g + inten * 32].m_solutions[sr][m].m_err = static_cast<uint16_t>(best_err);
  944. //assert(best_lo != best_hi);
  945. fprintf(pFile, "{%u,%u,%u},", best_lo, best_hi, (uint32_t)best_err);
  946. n++;
  947. if ((n & 31) == 31)
  948. fprintf(pFile, "\n");
  949. } // m
  950. } // sr
  951. } // g
  952. } // inten
  953. fclose(pFile);
  954. }
  955. static void create_etc1_to_dxt1_6_conversion_table()
  956. {
  957. FILE* pFile = nullptr;
  958. fopen_s(&pFile, "basisu_transcoder_tables_dxt1_6.inc", "w");
  959. uint32_t n = 0;
  960. for (int inten = 0; inten < 8; inten++)
  961. {
  962. for (uint32_t g = 0; g < 32; g++)
  963. {
  964. color32 block_colors[4];
  965. decoder_etc_block::get_diff_subblock_colors(block_colors, decoder_etc_block::pack_color5(color32(g, g, g, 255), false), inten);
  966. for (uint32_t sr = 0; sr < NUM_ETC1_TO_DXT1_SELECTOR_RANGES; sr++)
  967. {
  968. const uint32_t low_selector = g_etc1_to_dxt1_selector_ranges[sr].m_low;
  969. const uint32_t high_selector = g_etc1_to_dxt1_selector_ranges[sr].m_high;
  970. for (uint32_t m = 0; m < NUM_ETC1_TO_DXT1_SELECTOR_MAPPINGS; m++)
  971. {
  972. uint32_t best_lo = 0;
  973. uint32_t best_hi = 0;
  974. uint64_t best_err = UINT64_MAX;
  975. for (uint32_t hi = 0; hi <= 63; hi++)
  976. {
  977. for (uint32_t lo = 0; lo <= 63; lo++)
  978. {
  979. //if (lo == hi) continue;
  980. uint32_t colors[4];
  981. colors[0] = (lo << 2) | (lo >> 4);
  982. colors[3] = (hi << 2) | (hi >> 4);
  983. colors[1] = (colors[0] * 2 + colors[3]) / 3;
  984. colors[2] = (colors[3] * 2 + colors[0]) / 3;
  985. uint64_t total_err = 0;
  986. for (uint32_t s = low_selector; s <= high_selector; s++)
  987. {
  988. int err = block_colors[s].g - colors[g_etc1_to_dxt1_selector_mappings[m][s]];
  989. total_err += err * err;
  990. }
  991. if (total_err < best_err)
  992. {
  993. best_err = total_err;
  994. best_lo = lo;
  995. best_hi = hi;
  996. }
  997. }
  998. }
  999. assert(best_err <= 0xFFFF);
  1000. //table[g + inten * 32].m_solutions[sr][m].m_lo = static_cast<uint8_t>(best_lo);
  1001. //table[g + inten * 32].m_solutions[sr][m].m_hi = static_cast<uint8_t>(best_hi);
  1002. //table[g + inten * 32].m_solutions[sr][m].m_err = static_cast<uint16_t>(best_err);
  1003. //assert(best_lo != best_hi);
  1004. fprintf(pFile, "{%u,%u,%u},", best_lo, best_hi, (uint32_t)best_err);
  1005. n++;
  1006. if ((n & 31) == 31)
  1007. fprintf(pFile, "\n");
  1008. } // m
  1009. } // sr
  1010. } // g
  1011. } // inten
  1012. fclose(pFile);
  1013. }
  1014. #endif
  1015. #if BASISD_SUPPORT_UASTC || BASISD_SUPPORT_ETC2_EAC_A8 || BASISD_SUPPORT_ETC2_EAC_RG11
  1016. static const int8_t g_eac_modifier_table[16][8] =
  1017. {
  1018. { -3, -6, -9, -15, 2, 5, 8, 14 },
  1019. { -3, -7, -10, -13, 2, 6, 9, 12 },
  1020. { -2, -5, -8, -13, 1, 4, 7, 12 },
  1021. { -2, -4, -6, -13, 1, 3, 5, 12 },
  1022. { -3, -6, -8, -12, 2, 5, 7, 11 },
  1023. { -3, -7, -9, -11, 2, 6, 8, 10 },
  1024. { -4, -7, -8, -11, 3, 6, 7, 10 },
  1025. { -3, -5, -8, -11, 2, 4, 7, 10 },
  1026. { -2, -6, -8, -10, 1, 5, 7, 9 },
  1027. { -2, -5, -8, -10, 1, 4, 7, 9 },
  1028. { -2, -4, -8, -10, 1, 3, 7, 9 },
  1029. { -2, -5, -7, -10, 1, 4, 6, 9 },
  1030. { -3, -4, -7, -10, 2, 3, 6, 9 },
  1031. { -1, -2, -3, -10, 0, 1, 2, 9 }, // entry 13
  1032. { -4, -6, -8, -9, 3, 5, 7, 8 },
  1033. { -3, -5, -7, -9, 2, 4, 6, 8 }
  1034. };
  1035. // Used by ETC2 EAC A8 and ETC2 EAC R11/RG11.
  1036. struct eac_block
  1037. {
  1038. uint16_t m_base : 8;
  1039. uint16_t m_table : 4;
  1040. uint16_t m_multiplier : 4;
  1041. uint8_t m_selectors[6];
  1042. uint32_t get_selector(uint32_t x, uint32_t y) const
  1043. {
  1044. assert((x < 4) && (y < 4));
  1045. const uint32_t ofs = 45 - (y + x * 4) * 3;
  1046. const uint64_t pixels = get_selector_bits();
  1047. return (pixels >> ofs) & 7;
  1048. }
  1049. void set_selector(uint32_t x, uint32_t y, uint32_t s)
  1050. {
  1051. assert((x < 4) && (y < 4) && (s < 8));
  1052. const uint32_t ofs = 45 - (y + x * 4) * 3;
  1053. uint64_t pixels = get_selector_bits();
  1054. pixels &= ~(7ULL << ofs);
  1055. pixels |= (static_cast<uint64_t>(s) << ofs);
  1056. set_selector_bits(pixels);
  1057. }
  1058. uint64_t get_selector_bits() const
  1059. {
  1060. uint64_t pixels = ((uint64_t)m_selectors[0] << 40) | ((uint64_t)m_selectors[1] << 32) |
  1061. ((uint64_t)m_selectors[2] << 24) |
  1062. ((uint64_t)m_selectors[3] << 16) | ((uint64_t)m_selectors[4] << 8) | m_selectors[5];
  1063. return pixels;
  1064. }
  1065. void set_selector_bits(uint64_t pixels)
  1066. {
  1067. m_selectors[0] = (uint8_t)(pixels >> 40);
  1068. m_selectors[1] = (uint8_t)(pixels >> 32);
  1069. m_selectors[2] = (uint8_t)(pixels >> 24);
  1070. m_selectors[3] = (uint8_t)(pixels >> 16);
  1071. m_selectors[4] = (uint8_t)(pixels >> 8);
  1072. m_selectors[5] = (uint8_t)(pixels);
  1073. }
  1074. };
  1075. #endif // #if BASISD_SUPPORT_UASTC BASISD_SUPPORT_ETC2_EAC_A8 || BASISD_SUPPORT_ETC2_EAC_RG11
  1076. #if BASISD_SUPPORT_ETC2_EAC_A8 || BASISD_SUPPORT_ETC2_EAC_RG11
  1077. static const dxt_selector_range s_etc2_eac_selector_ranges[] =
  1078. {
  1079. { 0, 3 },
  1080. { 1, 3 },
  1081. { 0, 2 },
  1082. { 1, 2 },
  1083. };
  1084. const uint32_t NUM_ETC2_EAC_SELECTOR_RANGES = sizeof(s_etc2_eac_selector_ranges) / sizeof(s_etc2_eac_selector_ranges[0]);
  1085. struct etc1_g_to_eac_conversion
  1086. {
  1087. uint8_t m_base;
  1088. uint8_t m_table_mul; // mul*16+table
  1089. uint16_t m_trans; // translates ETC1 selectors to ETC2_EAC_A8
  1090. };
  1091. #endif // BASISD_SUPPORT_ETC2_EAC_A8 || BASISD_SUPPORT_ETC2_EAC_RG11
  1092. #if BASISD_SUPPORT_ETC2_EAC_A8
  1093. #if BASISD_WRITE_NEW_ETC2_EAC_A8_TABLES
  1094. struct pack_eac_a8_results
  1095. {
  1096. uint32_t m_base;
  1097. uint32_t m_table;
  1098. uint32_t m_multiplier;
  1099. basisu::vector<uint8_t> m_selectors;
  1100. basisu::vector<uint8_t> m_selectors_temp;
  1101. };
  1102. static uint64_t pack_eac_a8_exhaustive(pack_eac_a8_results& results, const uint8_t* pPixels, uint32_t num_pixels)
  1103. {
  1104. results.m_selectors.resize(num_pixels);
  1105. results.m_selectors_temp.resize(num_pixels);
  1106. uint64_t best_err = UINT64_MAX;
  1107. for (uint32_t base_color = 0; base_color < 256; base_color++)
  1108. {
  1109. for (uint32_t multiplier = 1; multiplier < 16; multiplier++)
  1110. {
  1111. for (uint32_t table = 0; table < 16; table++)
  1112. {
  1113. uint64_t total_err = 0;
  1114. for (uint32_t i = 0; i < num_pixels; i++)
  1115. {
  1116. const int a = pPixels[i];
  1117. uint32_t best_s_err = UINT32_MAX;
  1118. uint32_t best_s = 0;
  1119. for (uint32_t s = 0; s < 8; s++)
  1120. {
  1121. int v = (int)multiplier * g_eac_modifier_table[table][s] + (int)base_color;
  1122. if (v < 0)
  1123. v = 0;
  1124. else if (v > 255)
  1125. v = 255;
  1126. uint32_t err = abs(a - v);
  1127. if (err < best_s_err)
  1128. {
  1129. best_s_err = err;
  1130. best_s = s;
  1131. }
  1132. }
  1133. results.m_selectors_temp[i] = static_cast<uint8_t>(best_s);
  1134. total_err += best_s_err * best_s_err;
  1135. if (total_err >= best_err)
  1136. break;
  1137. }
  1138. if (total_err < best_err)
  1139. {
  1140. best_err = total_err;
  1141. results.m_base = base_color;
  1142. results.m_multiplier = multiplier;
  1143. results.m_table = table;
  1144. results.m_selectors.swap(results.m_selectors_temp);
  1145. }
  1146. } // table
  1147. } // multiplier
  1148. } // base_color
  1149. return best_err;
  1150. }
  1151. #endif // BASISD_WRITE_NEW_ETC2_EAC_A8_TABLES
  1152. static
  1153. #if !BASISD_WRITE_NEW_ETC2_EAC_A8_TABLES
  1154. const
  1155. #endif
  1156. etc1_g_to_eac_conversion s_etc1_g_to_etc2_a8[32 * 8][NUM_ETC2_EAC_SELECTOR_RANGES] =
  1157. {
  1158. { { 0,1,3328 },{ 0,1,3328 },{ 0,1,256 },{ 0,1,256 } },
  1159. { { 0,226,3936 },{ 0,226,3936 },{ 0,81,488 },{ 0,81,488 } },
  1160. { { 6,178,4012 },{ 6,178,4008 },{ 0,146,501 },{ 0,130,496 } },
  1161. { { 14,178,4012 },{ 14,178,4008 },{ 8,146,501 },{ 6,82,496 } },
  1162. { { 23,178,4012 },{ 23,178,4008 },{ 17,146,501 },{ 3,228,496 } },
  1163. { { 31,178,4012 },{ 31,178,4008 },{ 25,146,501 },{ 11,228,496 } },
  1164. { { 39,178,4012 },{ 39,178,4008 },{ 33,146,501 },{ 19,228,496 } },
  1165. { { 47,178,4012 },{ 47,178,4008 },{ 41,146,501 },{ 27,228,496 } },
  1166. { { 56,178,4012 },{ 56,178,4008 },{ 50,146,501 },{ 36,228,496 } },
  1167. { { 64,178,4012 },{ 64,178,4008 },{ 58,146,501 },{ 44,228,496 } },
  1168. { { 72,178,4012 },{ 72,178,4008 },{ 66,146,501 },{ 52,228,496 } },
  1169. { { 80,178,4012 },{ 80,178,4008 },{ 74,146,501 },{ 60,228,496 } },
  1170. { { 89,178,4012 },{ 89,178,4008 },{ 83,146,501 },{ 69,228,496 } },
  1171. { { 97,178,4012 },{ 97,178,4008 },{ 91,146,501 },{ 77,228,496 } },
  1172. { { 105,178,4012 },{ 105,178,4008 },{ 99,146,501 },{ 85,228,496 } },
  1173. { { 113,178,4012 },{ 113,178,4008 },{ 107,146,501 },{ 93,228,496 } },
  1174. { { 122,178,4012 },{ 122,178,4008 },{ 116,146,501 },{ 102,228,496 } },
  1175. { { 130,178,4012 },{ 130,178,4008 },{ 124,146,501 },{ 110,228,496 } },
  1176. { { 138,178,4012 },{ 138,178,4008 },{ 132,146,501 },{ 118,228,496 } },
  1177. { { 146,178,4012 },{ 146,178,4008 },{ 140,146,501 },{ 126,228,496 } },
  1178. { { 155,178,4012 },{ 155,178,4008 },{ 149,146,501 },{ 135,228,496 } },
  1179. { { 163,178,4012 },{ 163,178,4008 },{ 157,146,501 },{ 143,228,496 } },
  1180. { { 171,178,4012 },{ 171,178,4008 },{ 165,146,501 },{ 151,228,496 } },
  1181. { { 179,178,4012 },{ 179,178,4008 },{ 173,146,501 },{ 159,228,496 } },
  1182. { { 188,178,4012 },{ 188,178,4008 },{ 182,146,501 },{ 168,228,496 } },
  1183. { { 196,178,4012 },{ 196,178,4008 },{ 190,146,501 },{ 176,228,496 } },
  1184. { { 204,178,4012 },{ 204,178,4008 },{ 198,146,501 },{ 184,228,496 } },
  1185. { { 212,178,4012 },{ 212,178,4008 },{ 206,146,501 },{ 192,228,496 } },
  1186. { { 221,178,4012 },{ 221,178,4008 },{ 215,146,501 },{ 201,228,496 } },
  1187. { { 229,178,4012 },{ 229,178,4008 },{ 223,146,501 },{ 209,228,496 } },
  1188. { { 235,66,4012 },{ 221,100,4008 },{ 231,146,501 },{ 217,228,496 } },
  1189. { { 211,102,4085 },{ 118,31,4080 },{ 211,102,501 },{ 118,31,496 } },
  1190. { { 1,2,3328 },{ 1,2,3328 },{ 0,1,320 },{ 0,1,320 } },
  1191. { { 7,162,3905 },{ 7,162,3904 },{ 1,17,480 },{ 1,17,480 } },
  1192. { { 15,162,3906 },{ 15,162,3904 },{ 1,117,352 },{ 1,117,352 } },
  1193. { { 23,162,3906 },{ 23,162,3904 },{ 5,34,500 },{ 4,53,424 } },
  1194. { { 32,162,3906 },{ 32,162,3904 },{ 14,34,500 },{ 3,69,424 } },
  1195. { { 40,162,3906 },{ 40,162,3904 },{ 22,34,500 },{ 1,133,496 } },
  1196. { { 48,162,3906 },{ 48,162,3904 },{ 30,34,500 },{ 4,85,496 } },
  1197. { { 56,162,3906 },{ 56,162,3904 },{ 38,34,500 },{ 12,85,496 } },
  1198. { { 65,162,3906 },{ 65,162,3904 },{ 47,34,500 },{ 1,106,424 } },
  1199. { { 73,162,3906 },{ 73,162,3904 },{ 55,34,500 },{ 9,106,424 } },
  1200. { { 81,162,3906 },{ 81,162,3904 },{ 63,34,500 },{ 7,234,496 } },
  1201. { { 89,162,3906 },{ 89,162,3904 },{ 71,34,500 },{ 15,234,496 } },
  1202. { { 98,162,3906 },{ 98,162,3904 },{ 80,34,500 },{ 24,234,496 } },
  1203. { { 106,162,3906 },{ 106,162,3904 },{ 88,34,500 },{ 32,234,496 } },
  1204. { { 114,162,3906 },{ 114,162,3904 },{ 96,34,500 },{ 40,234,496 } },
  1205. { { 122,162,3906 },{ 122,162,3904 },{ 104,34,500 },{ 48,234,496 } },
  1206. { { 131,162,3906 },{ 131,162,3904 },{ 113,34,500 },{ 57,234,496 } },
  1207. { { 139,162,3906 },{ 139,162,3904 },{ 121,34,500 },{ 65,234,496 } },
  1208. { { 147,162,3906 },{ 147,162,3904 },{ 129,34,500 },{ 73,234,496 } },
  1209. { { 155,162,3906 },{ 155,162,3904 },{ 137,34,500 },{ 81,234,496 } },
  1210. { { 164,162,3906 },{ 164,162,3904 },{ 146,34,500 },{ 90,234,496 } },
  1211. { { 172,162,3906 },{ 172,162,3904 },{ 154,34,500 },{ 98,234,496 } },
  1212. { { 180,162,3906 },{ 180,162,3904 },{ 162,34,500 },{ 106,234,496 } },
  1213. { { 188,162,3906 },{ 188,162,3904 },{ 170,34,500 },{ 114,234,496 } },
  1214. { { 197,162,3906 },{ 197,162,3904 },{ 179,34,500 },{ 123,234,496 } },
  1215. { { 205,162,3906 },{ 205,162,3904 },{ 187,34,500 },{ 131,234,496 } },
  1216. { { 213,162,3906 },{ 213,162,3904 },{ 195,34,500 },{ 139,234,496 } },
  1217. { { 221,162,3906 },{ 221,162,3904 },{ 203,34,500 },{ 147,234,496 } },
  1218. { { 230,162,3906 },{ 230,162,3904 },{ 212,34,500 },{ 156,234,496 } },
  1219. { { 238,162,3906 },{ 174,106,4008 },{ 220,34,500 },{ 164,234,496 } },
  1220. { { 240,178,4001 },{ 182,106,4008 },{ 228,34,500 },{ 172,234,496 } },
  1221. { { 166,108,4085 },{ 115,31,4080 },{ 166,108,501 },{ 115,31,496 } },
  1222. { { 1,68,3328 },{ 1,68,3328 },{ 0,17,384 },{ 0,17,384 } },
  1223. { { 1,148,3904 },{ 1,148,3904 },{ 1,2,384 },{ 1,2,384 } },
  1224. { { 21,18,3851 },{ 21,18,3848 },{ 1,50,488 },{ 1,50,488 } },
  1225. { { 27,195,3851 },{ 29,18,3848 },{ 0,67,488 },{ 0,67,488 } },
  1226. { { 34,195,3907 },{ 38,18,3848 },{ 20,66,482 },{ 0,3,496 } },
  1227. { { 42,195,3907 },{ 46,18,3848 },{ 28,66,482 },{ 2,6,424 } },
  1228. { { 50,195,3907 },{ 54,18,3848 },{ 36,66,482 },{ 4,22,424 } },
  1229. { { 58,195,3907 },{ 62,18,3848 },{ 44,66,482 },{ 3,73,424 } },
  1230. { { 67,195,3907 },{ 71,18,3848 },{ 53,66,482 },{ 3,22,496 } },
  1231. { { 75,195,3907 },{ 79,18,3848 },{ 61,66,482 },{ 2,137,496 } },
  1232. { { 83,195,3907 },{ 87,18,3848 },{ 69,66,482 },{ 1,89,496 } },
  1233. { { 91,195,3907 },{ 95,18,3848 },{ 77,66,482 },{ 9,89,496 } },
  1234. { { 100,195,3907 },{ 104,18,3848 },{ 86,66,482 },{ 18,89,496 } },
  1235. { { 108,195,3907 },{ 112,18,3848 },{ 94,66,482 },{ 26,89,496 } },
  1236. { { 116,195,3907 },{ 120,18,3848 },{ 102,66,482 },{ 34,89,496 } },
  1237. { { 124,195,3907 },{ 128,18,3848 },{ 110,66,482 },{ 42,89,496 } },
  1238. { { 133,195,3907 },{ 137,18,3848 },{ 119,66,482 },{ 51,89,496 } },
  1239. { { 141,195,3907 },{ 145,18,3848 },{ 127,66,482 },{ 59,89,496 } },
  1240. { { 149,195,3907 },{ 153,18,3848 },{ 135,66,482 },{ 67,89,496 } },
  1241. { { 157,195,3907 },{ 161,18,3848 },{ 143,66,482 },{ 75,89,496 } },
  1242. { { 166,195,3907 },{ 170,18,3848 },{ 152,66,482 },{ 84,89,496 } },
  1243. { { 174,195,3907 },{ 178,18,3848 },{ 160,66,482 },{ 92,89,496 } },
  1244. { { 182,195,3907 },{ 186,18,3848 },{ 168,66,482 },{ 100,89,496 } },
  1245. { { 190,195,3907 },{ 194,18,3848 },{ 176,66,482 },{ 108,89,496 } },
  1246. { { 199,195,3907 },{ 203,18,3848 },{ 185,66,482 },{ 117,89,496 } },
  1247. { { 207,195,3907 },{ 211,18,3848 },{ 193,66,482 },{ 125,89,496 } },
  1248. { { 215,195,3907 },{ 219,18,3848 },{ 201,66,482 },{ 133,89,496 } },
  1249. { { 223,195,3907 },{ 227,18,3848 },{ 209,66,482 },{ 141,89,496 } },
  1250. { { 231,195,3907 },{ 168,89,4008 },{ 218,66,482 },{ 150,89,496 } },
  1251. { { 236,18,3907 },{ 176,89,4008 },{ 226,66,482 },{ 158,89,496 } },
  1252. { { 158,90,4085 },{ 103,31,4080 },{ 158,90,501 },{ 103,31,496 } },
  1253. { { 166,90,4085 },{ 111,31,4080 },{ 166,90,501 },{ 111,31,496 } },
  1254. { { 0,70,3328 },{ 0,70,3328 },{ 0,45,256 },{ 0,45,256 } },
  1255. { { 0,117,3904 },{ 0,117,3904 },{ 0,35,384 },{ 0,35,384 } },
  1256. { { 13,165,3905 },{ 13,165,3904 },{ 3,221,416 },{ 3,221,416 } },
  1257. { { 21,165,3906 },{ 21,165,3904 },{ 11,221,416 },{ 11,221,416 } },
  1258. { { 30,165,3906 },{ 30,165,3904 },{ 7,61,352 },{ 7,61,352 } },
  1259. { { 38,165,3906 },{ 38,165,3904 },{ 2,125,352 },{ 2,125,352 } },
  1260. { { 46,165,3906 },{ 46,165,3904 },{ 2,37,500 },{ 10,125,352 } },
  1261. { { 54,165,3906 },{ 54,165,3904 },{ 10,37,500 },{ 5,61,424 } },
  1262. { { 63,165,3906 },{ 63,165,3904 },{ 19,37,500 },{ 1,189,424 } },
  1263. { { 4,254,4012 },{ 71,165,3904 },{ 27,37,500 },{ 9,189,424 } },
  1264. { { 12,254,4012 },{ 79,165,3904 },{ 35,37,500 },{ 4,77,424 } },
  1265. { { 20,254,4012 },{ 87,165,3904 },{ 43,37,500 },{ 12,77,424 } },
  1266. { { 29,254,4012 },{ 96,165,3904 },{ 52,37,500 },{ 8,93,424 } },
  1267. { { 37,254,4012 },{ 104,165,3904 },{ 60,37,500 },{ 3,141,496 } },
  1268. { { 45,254,4012 },{ 112,165,3904 },{ 68,37,500 },{ 11,141,496 } },
  1269. { { 53,254,4012 },{ 120,165,3904 },{ 76,37,500 },{ 6,93,496 } },
  1270. { { 62,254,4012 },{ 129,165,3904 },{ 85,37,500 },{ 15,93,496 } },
  1271. { { 70,254,4012 },{ 137,165,3904 },{ 93,37,500 },{ 23,93,496 } },
  1272. { { 78,254,4012 },{ 145,165,3904 },{ 101,37,500 },{ 31,93,496 } },
  1273. { { 86,254,4012 },{ 153,165,3904 },{ 109,37,500 },{ 39,93,496 } },
  1274. { { 95,254,4012 },{ 162,165,3904 },{ 118,37,500 },{ 48,93,496 } },
  1275. { { 103,254,4012 },{ 170,165,3904 },{ 126,37,500 },{ 56,93,496 } },
  1276. { { 111,254,4012 },{ 178,165,3904 },{ 134,37,500 },{ 64,93,496 } },
  1277. { { 119,254,4012 },{ 186,165,3904 },{ 142,37,500 },{ 72,93,496 } },
  1278. { { 128,254,4012 },{ 195,165,3904 },{ 151,37,500 },{ 81,93,496 } },
  1279. { { 136,254,4012 },{ 203,165,3904 },{ 159,37,500 },{ 89,93,496 } },
  1280. { { 212,165,3906 },{ 136,77,4008 },{ 167,37,500 },{ 97,93,496 } },
  1281. { { 220,165,3394 },{ 131,93,4008 },{ 175,37,500 },{ 105,93,496 } },
  1282. { { 214,181,4001 },{ 140,93,4008 },{ 184,37,500 },{ 114,93,496 } },
  1283. { { 222,181,4001 },{ 148,93,4008 },{ 192,37,500 },{ 122,93,496 } },
  1284. { { 114,95,4085 },{ 99,31,4080 },{ 114,95,501 },{ 99,31,496 } },
  1285. { { 122,95,4085 },{ 107,31,4080 },{ 122,95,501 },{ 107,31,496 } },
  1286. { { 0,102,3840 },{ 0,102,3840 },{ 0,18,384 },{ 0,18,384 } },
  1287. { { 5,167,3904 },{ 5,167,3904 },{ 0,13,256 },{ 0,13,256 } },
  1288. { { 4,54,3968 },{ 4,54,3968 },{ 1,67,448 },{ 1,67,448 } },
  1289. { { 30,198,3850 },{ 30,198,3848 },{ 0,3,480 },{ 0,3,480 } },
  1290. { { 39,198,3850 },{ 39,198,3848 },{ 3,52,488 },{ 3,52,488 } },
  1291. { { 47,198,3851 },{ 47,198,3848 },{ 3,4,488 },{ 3,4,488 } },
  1292. { { 55,198,3851 },{ 55,198,3848 },{ 1,70,488 },{ 1,70,488 } },
  1293. { { 54,167,3906 },{ 63,198,3848 },{ 3,22,488 },{ 3,22,488 } },
  1294. { { 62,167,3906 },{ 72,198,3848 },{ 24,118,488 },{ 0,6,496 } },
  1295. { { 70,167,3906 },{ 80,198,3848 },{ 32,118,488 },{ 2,89,488 } },
  1296. { { 78,167,3906 },{ 88,198,3848 },{ 40,118,488 },{ 1,73,496 } },
  1297. { { 86,167,3906 },{ 96,198,3848 },{ 48,118,488 },{ 0,28,424 } },
  1298. { { 95,167,3906 },{ 105,198,3848 },{ 57,118,488 },{ 9,28,424 } },
  1299. { { 103,167,3906 },{ 113,198,3848 },{ 65,118,488 },{ 5,108,496 } },
  1300. { { 111,167,3906 },{ 121,198,3848 },{ 73,118,488 },{ 13,108,496 } },
  1301. { { 119,167,3906 },{ 129,198,3848 },{ 81,118,488 },{ 21,108,496 } },
  1302. { { 128,167,3906 },{ 138,198,3848 },{ 90,118,488 },{ 6,28,496 } },
  1303. { { 136,167,3906 },{ 146,198,3848 },{ 98,118,488 },{ 14,28,496 } },
  1304. { { 144,167,3906 },{ 154,198,3848 },{ 106,118,488 },{ 22,28,496 } },
  1305. { { 152,167,3906 },{ 162,198,3848 },{ 114,118,488 },{ 30,28,496 } },
  1306. { { 161,167,3906 },{ 171,198,3848 },{ 123,118,488 },{ 39,28,496 } },
  1307. { { 169,167,3906 },{ 179,198,3848 },{ 131,118,488 },{ 47,28,496 } },
  1308. { { 177,167,3906 },{ 187,198,3848 },{ 139,118,488 },{ 55,28,496 } },
  1309. { { 185,167,3906 },{ 195,198,3848 },{ 147,118,488 },{ 63,28,496 } },
  1310. { { 194,167,3906 },{ 120,12,4008 },{ 156,118,488 },{ 72,28,496 } },
  1311. { { 206,198,3907 },{ 116,28,4008 },{ 164,118,488 },{ 80,28,496 } },
  1312. { { 214,198,3907 },{ 124,28,4008 },{ 172,118,488 },{ 88,28,496 } },
  1313. { { 222,198,3395 },{ 132,28,4008 },{ 180,118,488 },{ 96,28,496 } },
  1314. { { 207,134,4001 },{ 141,28,4008 },{ 189,118,488 },{ 105,28,496 } },
  1315. { { 95,30,4085 },{ 86,31,4080 },{ 95,30,501 },{ 86,31,496 } },
  1316. { { 103,30,4085 },{ 94,31,4080 },{ 103,30,501 },{ 94,31,496 } },
  1317. { { 111,30,4085 },{ 102,31,4080 },{ 111,30,501 },{ 102,31,496 } },
  1318. { { 0,104,3840 },{ 0,104,3840 },{ 0,18,448 },{ 0,18,448 } },
  1319. { { 4,39,3904 },{ 4,39,3904 },{ 0,4,384 },{ 0,4,384 } },
  1320. { { 0,56,3968 },{ 0,56,3968 },{ 0,84,448 },{ 0,84,448 } },
  1321. { { 6,110,3328 },{ 6,110,3328 },{ 0,20,448 },{ 0,20,448 } },
  1322. { { 41,200,3850 },{ 41,200,3848 },{ 1,4,480 },{ 1,4,480 } },
  1323. { { 49,200,3850 },{ 49,200,3848 },{ 1,8,416 },{ 1,8,416 } },
  1324. { { 57,200,3851 },{ 57,200,3848 },{ 1,38,488 },{ 1,38,488 } },
  1325. { { 65,200,3851 },{ 65,200,3848 },{ 1,120,488 },{ 1,120,488 } },
  1326. { { 74,200,3851 },{ 74,200,3848 },{ 2,72,488 },{ 2,72,488 } },
  1327. { { 69,6,3907 },{ 82,200,3848 },{ 2,24,488 },{ 2,24,488 } },
  1328. { { 77,6,3907 },{ 90,200,3848 },{ 26,120,488 },{ 10,24,488 } },
  1329. { { 97,63,3330 },{ 98,200,3848 },{ 34,120,488 },{ 2,8,496 } },
  1330. { { 106,63,3330 },{ 107,200,3848 },{ 43,120,488 },{ 3,92,488 } },
  1331. { { 114,63,3330 },{ 115,200,3848 },{ 51,120,488 },{ 11,92,488 } },
  1332. { { 122,63,3330 },{ 123,200,3848 },{ 59,120,488 },{ 7,76,496 } },
  1333. { { 130,63,3330 },{ 131,200,3848 },{ 67,120,488 },{ 15,76,496 } },
  1334. { { 139,63,3330 },{ 140,200,3848 },{ 76,120,488 },{ 24,76,496 } },
  1335. { { 147,63,3330 },{ 148,200,3848 },{ 84,120,488 },{ 32,76,496 } },
  1336. { { 155,63,3330 },{ 156,200,3848 },{ 92,120,488 },{ 40,76,496 } },
  1337. { { 163,63,3330 },{ 164,200,3848 },{ 100,120,488 },{ 48,76,496 } },
  1338. { { 172,63,3330 },{ 173,200,3848 },{ 109,120,488 },{ 57,76,496 } },
  1339. { { 184,6,3851 },{ 181,200,3848 },{ 117,120,488 },{ 65,76,496 } },
  1340. { { 192,6,3851 },{ 133,28,3936 },{ 125,120,488 },{ 73,76,496 } },
  1341. { { 189,200,3907 },{ 141,28,3936 },{ 133,120,488 },{ 81,76,496 } },
  1342. { { 198,200,3907 },{ 138,108,4000 },{ 142,120,488 },{ 90,76,496 } },
  1343. { { 206,200,3907 },{ 146,108,4000 },{ 150,120,488 },{ 98,76,496 } },
  1344. { { 214,200,3395 },{ 154,108,4000 },{ 158,120,488 },{ 106,76,496 } },
  1345. { { 190,136,4001 },{ 162,108,4000 },{ 166,120,488 },{ 114,76,496 } },
  1346. { { 123,30,4076 },{ 87,15,4080 },{ 123,30,492 },{ 87,15,496 } },
  1347. { { 117,110,4084 },{ 80,31,4080 },{ 117,110,500 },{ 80,31,496 } },
  1348. { { 125,110,4084 },{ 88,31,4080 },{ 125,110,500 },{ 88,31,496 } },
  1349. { { 133,110,4084 },{ 96,31,4080 },{ 133,110,500 },{ 96,31,496 } },
  1350. { { 9,56,3904 },{ 9,56,3904 },{ 0,67,448 },{ 0,67,448 } },
  1351. { { 1,8,3904 },{ 1,8,3904 },{ 1,84,448 },{ 1,84,448 } },
  1352. { { 1,124,3904 },{ 1,124,3904 },{ 0,39,384 },{ 0,39,384 } },
  1353. { { 9,124,3904 },{ 9,124,3904 },{ 1,4,448 },{ 1,4,448 } },
  1354. { { 6,76,3904 },{ 6,76,3904 },{ 0,70,448 },{ 0,70,448 } },
  1355. { { 62,6,3859 },{ 62,6,3856 },{ 2,38,480 },{ 2,38,480 } },
  1356. { { 70,6,3859 },{ 70,6,3856 },{ 5,43,416 },{ 5,43,416 } },
  1357. { { 78,6,3859 },{ 78,6,3856 },{ 2,11,416 },{ 2,11,416 } },
  1358. { { 87,6,3859 },{ 87,6,3856 },{ 0,171,488 },{ 0,171,488 } },
  1359. { { 67,8,3906 },{ 95,6,3856 },{ 8,171,488 },{ 8,171,488 } },
  1360. { { 75,8,3907 },{ 103,6,3856 },{ 5,123,488 },{ 5,123,488 } },
  1361. { { 83,8,3907 },{ 111,6,3856 },{ 2,75,488 },{ 2,75,488 } },
  1362. { { 92,8,3907 },{ 120,6,3856 },{ 0,27,488 },{ 0,27,488 } },
  1363. { { 100,8,3907 },{ 128,6,3856 },{ 8,27,488 },{ 8,27,488 } },
  1364. { { 120,106,3843 },{ 136,6,3856 },{ 100,6,387 },{ 16,27,488 } },
  1365. { { 128,106,3843 },{ 144,6,3856 },{ 108,6,387 },{ 2,11,496 } },
  1366. { { 137,106,3843 },{ 153,6,3856 },{ 117,6,387 },{ 11,11,496 } },
  1367. { { 145,106,3843 },{ 161,6,3856 },{ 125,6,387 },{ 19,11,496 } },
  1368. { { 163,8,3851 },{ 137,43,3904 },{ 133,6,387 },{ 27,11,496 } },
  1369. { { 171,8,3851 },{ 101,11,4000 },{ 141,6,387 },{ 35,11,496 } },
  1370. { { 180,8,3851 },{ 110,11,4000 },{ 150,6,387 },{ 44,11,496 } },
  1371. { { 188,8,3851 },{ 118,11,4000 },{ 158,6,387 },{ 52,11,496 } },
  1372. { { 172,72,3907 },{ 126,11,4000 },{ 166,6,387 },{ 60,11,496 } },
  1373. { { 174,6,3971 },{ 134,11,4000 },{ 174,6,387 },{ 68,11,496 } },
  1374. { { 183,6,3971 },{ 143,11,4000 },{ 183,6,387 },{ 77,11,496 } },
  1375. { { 191,6,3971 },{ 151,11,4000 },{ 191,6,387 },{ 85,11,496 } },
  1376. { { 199,6,3971 },{ 159,11,4000 },{ 199,6,387 },{ 93,11,496 } },
  1377. { { 92,12,4084 },{ 69,15,4080 },{ 92,12,500 },{ 69,15,496 } },
  1378. { { 101,12,4084 },{ 78,15,4080 },{ 101,12,500 },{ 78,15,496 } },
  1379. { { 109,12,4084 },{ 86,15,4080 },{ 109,12,500 },{ 86,15,496 } },
  1380. { { 117,12,4084 },{ 79,31,4080 },{ 117,12,500 },{ 79,31,496 } },
  1381. { { 125,12,4084 },{ 87,31,4080 },{ 125,12,500 },{ 87,31,496 } },
  1382. { { 71,8,3602 },{ 71,8,3600 },{ 2,21,384 },{ 2,21,384 } },
  1383. { { 79,8,3611 },{ 79,8,3608 },{ 0,69,448 },{ 0,69,448 } },
  1384. { { 87,8,3611 },{ 87,8,3608 },{ 0,23,384 },{ 0,23,384 } },
  1385. { { 95,8,3611 },{ 95,8,3608 },{ 1,5,448 },{ 1,5,448 } },
  1386. { { 104,8,3611 },{ 104,8,3608 },{ 0,88,448 },{ 0,88,448 } },
  1387. { { 112,8,3611 },{ 112,8,3608 },{ 0,72,448 },{ 0,72,448 } },
  1388. { { 120,8,3611 },{ 121,8,3608 },{ 36,21,458 },{ 36,21,456 } },
  1389. { { 133,47,3091 },{ 129,8,3608 },{ 44,21,458 },{ 44,21,456 } },
  1390. { { 142,47,3091 },{ 138,8,3608 },{ 53,21,459 },{ 53,21,456 } },
  1391. { { 98,12,3850 },{ 98,12,3848 },{ 61,21,459 },{ 61,21,456 } },
  1392. { { 106,12,3850 },{ 106,12,3848 },{ 10,92,480 },{ 69,21,456 } },
  1393. { { 114,12,3851 },{ 114,12,3848 },{ 18,92,480 },{ 77,21,456 } },
  1394. { { 87,12,3906 },{ 87,12,3904 },{ 3,44,488 },{ 86,21,456 } },
  1395. { { 95,12,3906 },{ 95,12,3904 },{ 11,44,488 },{ 94,21,456 } },
  1396. { { 103,12,3906 },{ 103,12,3904 },{ 19,44,488 },{ 102,21,456 } },
  1397. { { 111,12,3907 },{ 111,12,3904 },{ 27,44,489 },{ 110,21,456 } },
  1398. { { 120,12,3907 },{ 120,12,3904 },{ 36,44,489 },{ 119,21,456 } },
  1399. { { 128,12,3907 },{ 128,12,3904 },{ 44,44,489 },{ 127,21,456 } },
  1400. { { 136,12,3907 },{ 136,12,3904 },{ 52,44,489 },{ 135,21,456 } },
  1401. { { 144,12,3907 },{ 144,12,3904 },{ 60,44,489 },{ 143,21,456 } },
  1402. { { 153,12,3907 },{ 153,12,3904 },{ 69,44,490 },{ 152,21,456 } },
  1403. { { 161,12,3395 },{ 149,188,3968 },{ 77,44,490 },{ 160,21,456 } },
  1404. { { 169,12,3395 },{ 198,21,3928 },{ 85,44,490 },{ 168,21,456 } },
  1405. { { 113,95,4001 },{ 201,69,3992 },{ 125,8,483 },{ 176,21,456 } },
  1406. { { 122,95,4001 },{ 200,21,3984 },{ 134,8,483 },{ 185,21,456 } },
  1407. { { 142,8,4067 },{ 208,21,3984 },{ 142,8,483 },{ 193,21,456 } },
  1408. { { 151,8,4067 },{ 47,15,4080 },{ 151,8,483 },{ 47,15,496 } },
  1409. { { 159,8,4067 },{ 55,15,4080 },{ 159,8,483 },{ 55,15,496 } },
  1410. { { 168,8,4067 },{ 64,15,4080 },{ 168,8,483 },{ 64,15,496 } },
  1411. { { 160,40,4075 },{ 72,15,4080 },{ 160,40,491 },{ 72,15,496 } },
  1412. { { 168,40,4075 },{ 80,15,4080 },{ 168,40,491 },{ 80,15,496 } },
  1413. { { 144,8,4082 },{ 88,15,4080 },{ 144,8,498 },{ 88,15,496 } }
  1414. };
  1415. #endif // BASISD_SUPPORT_ETC2_EAC_A8
  1416. #if BASISD_WRITE_NEW_ETC2_EAC_A8_TABLES
  1417. static void create_etc2_eac_a8_conversion_table()
  1418. {
  1419. FILE* pFile = fopen("basisu_decoder_tables_etc2_eac_a8.inc", "w");
  1420. for (uint32_t inten = 0; inten < 8; inten++)
  1421. {
  1422. for (uint32_t base = 0; base < 32; base++)
  1423. {
  1424. color32 block_colors[4];
  1425. decoder_etc_block::get_diff_subblock_colors(block_colors, decoder_etc_block::pack_color5(color32(base, base, base, 255), false), inten);
  1426. fprintf(pFile, "{");
  1427. for (uint32_t sel_range = 0; sel_range < NUM_ETC2_EAC_SELECTOR_RANGES; sel_range++)
  1428. {
  1429. const uint32_t low_selector = s_etc2_eac_selector_ranges[sel_range].m_low;
  1430. const uint32_t high_selector = s_etc2_eac_selector_ranges[sel_range].m_high;
  1431. // We have a ETC1 base color and intensity, and a used selector range from low_selector-high_selector.
  1432. // Now find the best ETC2 EAC A8 base/table/multiplier that fits these colors.
  1433. uint8_t pixels[4];
  1434. uint32_t num_pixels = 0;
  1435. for (uint32_t s = low_selector; s <= high_selector; s++)
  1436. pixels[num_pixels++] = block_colors[s].g;
  1437. pack_eac_a8_results pack_results;
  1438. pack_eac_a8_exhaustive(pack_results, pixels, num_pixels);
  1439. etc1_g_to_eac_conversion& c = s_etc1_g_to_etc2_a8[base + inten * 32][sel_range];
  1440. c.m_base = pack_results.m_base;
  1441. c.m_table_mul = pack_results.m_table * 16 + pack_results.m_multiplier;
  1442. c.m_trans = 0;
  1443. for (uint32_t s = 0; s < 4; s++)
  1444. {
  1445. if ((s < low_selector) || (s > high_selector))
  1446. continue;
  1447. uint32_t etc2_selector = pack_results.m_selectors[s - low_selector];
  1448. c.m_trans |= (etc2_selector << (s * 3));
  1449. }
  1450. fprintf(pFile, "{%u,%u,%u}", c.m_base, c.m_table_mul, c.m_trans);
  1451. if (sel_range < (NUM_ETC2_EAC_SELECTOR_RANGES - 1))
  1452. fprintf(pFile, ",");
  1453. }
  1454. fprintf(pFile, "},\n");
  1455. }
  1456. }
  1457. fclose(pFile);
  1458. }
  1459. #endif
  1460. #if BASISD_WRITE_NEW_ETC2_EAC_R11_TABLES
  1461. struct pack_eac_r11_results
  1462. {
  1463. uint32_t m_base;
  1464. uint32_t m_table;
  1465. uint32_t m_multiplier;
  1466. basisu::vector<uint8_t> m_selectors;
  1467. basisu::vector<uint8_t> m_selectors_temp;
  1468. };
  1469. static uint64_t pack_eac_r11_exhaustive(pack_eac_r11_results& results, const uint8_t* pPixels, uint32_t num_pixels)
  1470. {
  1471. results.m_selectors.resize(num_pixels);
  1472. results.m_selectors_temp.resize(num_pixels);
  1473. uint64_t best_err = UINT64_MAX;
  1474. for (uint32_t base_color = 0; base_color < 256; base_color++)
  1475. {
  1476. for (uint32_t multiplier = 0; multiplier < 16; multiplier++)
  1477. {
  1478. for (uint32_t table = 0; table < 16; table++)
  1479. {
  1480. uint64_t total_err = 0;
  1481. for (uint32_t i = 0; i < num_pixels; i++)
  1482. {
  1483. // Convert 8-bit input to 11-bits
  1484. const int a = (pPixels[i] * 2047 + 128) / 255;
  1485. uint32_t best_s_err = UINT32_MAX;
  1486. uint32_t best_s = 0;
  1487. for (uint32_t s = 0; s < 8; s++)
  1488. {
  1489. int v = (int)(multiplier ? (multiplier * 8) : 1) * g_eac_modifier_table[table][s] + (int)base_color * 8 + 4;
  1490. if (v < 0)
  1491. v = 0;
  1492. else if (v > 2047)
  1493. v = 2047;
  1494. uint32_t err = abs(a - v);
  1495. if (err < best_s_err)
  1496. {
  1497. best_s_err = err;
  1498. best_s = s;
  1499. }
  1500. }
  1501. results.m_selectors_temp[i] = static_cast<uint8_t>(best_s);
  1502. total_err += best_s_err * best_s_err;
  1503. if (total_err >= best_err)
  1504. break;
  1505. }
  1506. if (total_err < best_err)
  1507. {
  1508. best_err = total_err;
  1509. results.m_base = base_color;
  1510. results.m_multiplier = multiplier;
  1511. results.m_table = table;
  1512. results.m_selectors.swap(results.m_selectors_temp);
  1513. }
  1514. } // table
  1515. } // multiplier
  1516. } // base_color
  1517. return best_err;
  1518. }
  1519. static void create_etc2_eac_r11_conversion_table()
  1520. {
  1521. FILE* pFile = nullptr;
  1522. fopen_s(&pFile, "basisu_decoder_tables_etc2_eac_r11.inc", "w");
  1523. for (uint32_t inten = 0; inten < 8; inten++)
  1524. {
  1525. for (uint32_t base = 0; base < 32; base++)
  1526. {
  1527. color32 block_colors[4];
  1528. decoder_etc_block::get_diff_subblock_colors(block_colors, decoder_etc_block::pack_color5(color32(base, base, base, 255), false), inten);
  1529. fprintf(pFile, "{");
  1530. for (uint32_t sel_range = 0; sel_range < NUM_ETC2_EAC_SELECTOR_RANGES; sel_range++)
  1531. {
  1532. const uint32_t low_selector = s_etc2_eac_selector_ranges[sel_range].m_low;
  1533. const uint32_t high_selector = s_etc2_eac_selector_ranges[sel_range].m_high;
  1534. // We have a ETC1 base color and intensity, and a used selector range from low_selector-high_selector.
  1535. // Now find the best ETC2 EAC R11 base/table/multiplier that fits these colors.
  1536. uint8_t pixels[4];
  1537. uint32_t num_pixels = 0;
  1538. for (uint32_t s = low_selector; s <= high_selector; s++)
  1539. pixels[num_pixels++] = block_colors[s].g;
  1540. pack_eac_r11_results pack_results;
  1541. pack_eac_r11_exhaustive(pack_results, pixels, num_pixels);
  1542. etc1_g_to_eac_conversion c;
  1543. c.m_base = (uint8_t)pack_results.m_base;
  1544. c.m_table_mul = (uint8_t)(pack_results.m_table * 16 + pack_results.m_multiplier);
  1545. c.m_trans = 0;
  1546. for (uint32_t s = 0; s < 4; s++)
  1547. {
  1548. if ((s < low_selector) || (s > high_selector))
  1549. continue;
  1550. uint32_t etc2_selector = pack_results.m_selectors[s - low_selector];
  1551. c.m_trans |= (etc2_selector << (s * 3));
  1552. }
  1553. fprintf(pFile, "{%u,%u,%u}", c.m_base, c.m_table_mul, c.m_trans);
  1554. if (sel_range < (NUM_ETC2_EAC_SELECTOR_RANGES - 1))
  1555. fprintf(pFile, ",");
  1556. }
  1557. fprintf(pFile, "},\n");
  1558. }
  1559. }
  1560. fclose(pFile);
  1561. }
  1562. #endif // BASISD_WRITE_NEW_ETC2_EAC_R11_TABLES
  1563. #if BASISD_WRITE_NEW_ASTC_TABLES
  1564. static void create_etc1_to_astc_conversion_table_0_47();
  1565. static void create_etc1_to_astc_conversion_table_0_255();
  1566. #endif
  1567. #if BASISD_SUPPORT_ASTC
  1568. static void transcoder_init_astc();
  1569. #endif
  1570. #if BASISD_WRITE_NEW_BC7_MODE5_TABLES
  1571. static void create_etc1_to_bc7_m5_color_conversion_table();
  1572. static void create_etc1_to_bc7_m5_alpha_conversion_table();
  1573. #endif
  1574. #if BASISD_SUPPORT_BC7_MODE5
  1575. static void transcoder_init_bc7_mode5();
  1576. #endif
  1577. #if BASISD_WRITE_NEW_ATC_TABLES
  1578. static void create_etc1s_to_atc_conversion_tables();
  1579. #endif
  1580. #if BASISD_SUPPORT_ATC
  1581. static void transcoder_init_atc();
  1582. #endif
  1583. #if BASISD_SUPPORT_PVRTC2
  1584. static void transcoder_init_pvrtc2();
  1585. #endif
  1586. #if BASISD_SUPPORT_UASTC
  1587. void uastc_init();
  1588. #endif
  1589. static bool g_transcoder_initialized;
  1590. // Library global initialization. Requires ~9 milliseconds when compiled and executed natively on a Core i7 2.2 GHz.
  1591. // If this is too slow, these computed tables can easilky be moved to be compiled in.
  1592. void basisu_transcoder_init()
  1593. {
  1594. if (g_transcoder_initialized)
  1595. {
  1596. BASISU_DEVEL_ERROR("basisu_transcoder::basisu_transcoder_init: Called more than once\n");
  1597. return;
  1598. }
  1599. BASISU_DEVEL_ERROR("basisu_transcoder::basisu_transcoder_init: Initializing (this is not an error)\n");
  1600. #if BASISD_SUPPORT_UASTC
  1601. uastc_init();
  1602. #endif
  1603. #if BASISD_SUPPORT_ASTC
  1604. transcoder_init_astc();
  1605. #endif
  1606. #if BASISD_WRITE_NEW_ASTC_TABLES
  1607. create_etc1_to_astc_conversion_table_0_47();
  1608. create_etc1_to_astc_conversion_table_0_255();
  1609. exit(0);
  1610. #endif
  1611. #if BASISD_WRITE_NEW_BC7_MODE5_TABLES
  1612. create_etc1_to_bc7_m5_color_conversion_table();
  1613. create_etc1_to_bc7_m5_alpha_conversion_table();
  1614. exit(0);
  1615. #endif
  1616. #if BASISD_WRITE_NEW_DXT1_TABLES
  1617. create_etc1_to_dxt1_5_conversion_table();
  1618. create_etc1_to_dxt1_6_conversion_table();
  1619. exit(0);
  1620. #endif
  1621. #if BASISD_WRITE_NEW_ETC2_EAC_A8_TABLES
  1622. create_etc2_eac_a8_conversion_table();
  1623. exit(0);
  1624. #endif
  1625. #if BASISD_WRITE_NEW_ATC_TABLES
  1626. create_etc1s_to_atc_conversion_tables();
  1627. exit(0);
  1628. #endif
  1629. #if BASISD_WRITE_NEW_ETC2_EAC_R11_TABLES
  1630. create_etc2_eac_r11_conversion_table();
  1631. exit(0);
  1632. #endif
  1633. #if BASISD_SUPPORT_DXT1 || BASISD_SUPPORT_UASTC
  1634. uint8_t bc1_expand5[32];
  1635. for (int i = 0; i < 32; i++)
  1636. bc1_expand5[i] = static_cast<uint8_t>((i << 3) | (i >> 2));
  1637. prepare_bc1_single_color_table(g_bc1_match5_equals_1, bc1_expand5, 32, 32, 1);
  1638. prepare_bc1_single_color_table(g_bc1_match5_equals_0, bc1_expand5, 1, 32, 0);
  1639. uint8_t bc1_expand6[64];
  1640. for (int i = 0; i < 64; i++)
  1641. bc1_expand6[i] = static_cast<uint8_t>((i << 2) | (i >> 4));
  1642. prepare_bc1_single_color_table(g_bc1_match6_equals_1, bc1_expand6, 64, 64, 1);
  1643. prepare_bc1_single_color_table(g_bc1_match6_equals_0, bc1_expand6, 1, 64, 0);
  1644. #if 0
  1645. for (uint32_t i = 0; i < 256; i++)
  1646. {
  1647. printf("%u %u %u\n", i, (i * 63 + 127) / 255, g_bc1_match6_equals_0[i].m_hi);
  1648. }
  1649. exit(0);
  1650. #endif
  1651. #endif
  1652. #if BASISD_SUPPORT_DXT1
  1653. for (uint32_t i = 0; i < NUM_ETC1_TO_DXT1_SELECTOR_RANGES; i++)
  1654. {
  1655. uint32_t l = g_etc1_to_dxt1_selector_ranges[i].m_low;
  1656. uint32_t h = g_etc1_to_dxt1_selector_ranges[i].m_high;
  1657. g_etc1_to_dxt1_selector_range_index[l][h] = i;
  1658. }
  1659. for (uint32_t sm = 0; sm < NUM_ETC1_TO_DXT1_SELECTOR_MAPPINGS; sm++)
  1660. {
  1661. uint8_t etc1_to_dxt1_selector_mappings_raw_dxt1[4];
  1662. uint8_t etc1_to_dxt1_selector_mappings_raw_dxt1_inv[4];
  1663. for (uint32_t j = 0; j < 4; j++)
  1664. {
  1665. static const uint8_t s_linear_dxt1_to_dxt1[4] = { 0, 2, 3, 1 };
  1666. static const uint8_t s_dxt1_inverted_xlat[4] = { 1, 0, 3, 2 };
  1667. etc1_to_dxt1_selector_mappings_raw_dxt1[j] = (uint8_t)s_linear_dxt1_to_dxt1[g_etc1_to_dxt1_selector_mappings[sm][j]];
  1668. etc1_to_dxt1_selector_mappings_raw_dxt1_inv[j] = (uint8_t)s_dxt1_inverted_xlat[etc1_to_dxt1_selector_mappings_raw_dxt1[j]];
  1669. }
  1670. for (uint32_t i = 0; i < 256; i++)
  1671. {
  1672. uint32_t k = 0, k_inv = 0;
  1673. for (uint32_t s = 0; s < 4; s++)
  1674. {
  1675. k |= (etc1_to_dxt1_selector_mappings_raw_dxt1[(i >> (s * 2)) & 3] << (s * 2));
  1676. k_inv |= (etc1_to_dxt1_selector_mappings_raw_dxt1_inv[(i >> (s * 2)) & 3] << (s * 2));
  1677. }
  1678. g_etc1_to_dxt1_selector_mappings_raw_dxt1_256[sm][i] = (uint8_t)k;
  1679. g_etc1_to_dxt1_selector_mappings_raw_dxt1_inv_256[sm][i] = (uint8_t)k_inv;
  1680. }
  1681. }
  1682. #endif
  1683. #if BASISD_SUPPORT_BC7_MODE5
  1684. transcoder_init_bc7_mode5();
  1685. #endif
  1686. #if BASISD_SUPPORT_ATC
  1687. transcoder_init_atc();
  1688. #endif
  1689. #if BASISD_SUPPORT_PVRTC2
  1690. transcoder_init_pvrtc2();
  1691. #endif
  1692. g_transcoder_initialized = true;
  1693. }
  1694. #if BASISD_SUPPORT_DXT1
  1695. static void convert_etc1s_to_dxt1(dxt1_block* pDst_block, const endpoint *pEndpoints, const selector* pSelector, bool use_threecolor_blocks)
  1696. {
  1697. #if !BASISD_WRITE_NEW_DXT1_TABLES
  1698. const uint32_t low_selector = pSelector->m_lo_selector;
  1699. const uint32_t high_selector = pSelector->m_hi_selector;
  1700. const color32& base_color = pEndpoints->m_color5;
  1701. const uint32_t inten_table = pEndpoints->m_inten5;
  1702. if (low_selector == high_selector)
  1703. {
  1704. uint32_t r, g, b;
  1705. decoder_etc_block::get_block_color5(base_color, inten_table, low_selector, r, g, b);
  1706. uint32_t mask = 0xAA;
  1707. uint32_t max16 = (g_bc1_match5_equals_1[r].m_hi << 11) | (g_bc1_match6_equals_1[g].m_hi << 5) | g_bc1_match5_equals_1[b].m_hi;
  1708. uint32_t min16 = (g_bc1_match5_equals_1[r].m_lo << 11) | (g_bc1_match6_equals_1[g].m_lo << 5) | g_bc1_match5_equals_1[b].m_lo;
  1709. if ((!use_threecolor_blocks) && (min16 == max16))
  1710. {
  1711. // This is an annoying edge case that impacts BC3.
  1712. // This is to guarantee that BC3 blocks never use punchthrough alpha (3 color) mode, which isn't supported on some (all?) GPU's.
  1713. mask = 0;
  1714. // Make l > h
  1715. if (min16 > 0)
  1716. min16--;
  1717. else
  1718. {
  1719. // l = h = 0
  1720. assert(min16 == max16 && max16 == 0);
  1721. max16 = 1;
  1722. min16 = 0;
  1723. mask = 0x55;
  1724. }
  1725. assert(max16 > min16);
  1726. }
  1727. if (max16 < min16)
  1728. {
  1729. std::swap(max16, min16);
  1730. mask ^= 0x55;
  1731. }
  1732. pDst_block->set_low_color(static_cast<uint16_t>(max16));
  1733. pDst_block->set_high_color(static_cast<uint16_t>(min16));
  1734. pDst_block->m_selectors[0] = static_cast<uint8_t>(mask);
  1735. pDst_block->m_selectors[1] = static_cast<uint8_t>(mask);
  1736. pDst_block->m_selectors[2] = static_cast<uint8_t>(mask);
  1737. pDst_block->m_selectors[3] = static_cast<uint8_t>(mask);
  1738. return;
  1739. }
  1740. else if ((inten_table >= 7) && (pSelector->m_num_unique_selectors == 2) && (pSelector->m_lo_selector == 0) && (pSelector->m_hi_selector == 3))
  1741. {
  1742. color32 block_colors[4];
  1743. decoder_etc_block::get_block_colors5(block_colors, base_color, inten_table);
  1744. const uint32_t r0 = block_colors[0].r;
  1745. const uint32_t g0 = block_colors[0].g;
  1746. const uint32_t b0 = block_colors[0].b;
  1747. const uint32_t r1 = block_colors[3].r;
  1748. const uint32_t g1 = block_colors[3].g;
  1749. const uint32_t b1 = block_colors[3].b;
  1750. uint32_t max16 = (g_bc1_match5_equals_0[r0].m_hi << 11) | (g_bc1_match6_equals_0[g0].m_hi << 5) | g_bc1_match5_equals_0[b0].m_hi;
  1751. uint32_t min16 = (g_bc1_match5_equals_0[r1].m_hi << 11) | (g_bc1_match6_equals_0[g1].m_hi << 5) | g_bc1_match5_equals_0[b1].m_hi;
  1752. uint32_t l = 0, h = 1;
  1753. if (min16 == max16)
  1754. {
  1755. // Make l > h
  1756. if (min16 > 0)
  1757. {
  1758. min16--;
  1759. l = 0;
  1760. h = 0;
  1761. }
  1762. else
  1763. {
  1764. // l = h = 0
  1765. assert(min16 == max16 && max16 == 0);
  1766. max16 = 1;
  1767. min16 = 0;
  1768. l = 1;
  1769. h = 1;
  1770. }
  1771. assert(max16 > min16);
  1772. }
  1773. if (max16 < min16)
  1774. {
  1775. std::swap(max16, min16);
  1776. l = 1;
  1777. h = 0;
  1778. }
  1779. pDst_block->set_low_color((uint16_t)max16);
  1780. pDst_block->set_high_color((uint16_t)min16);
  1781. for (uint32_t y = 0; y < 4; y++)
  1782. {
  1783. for (uint32_t x = 0; x < 4; x++)
  1784. {
  1785. uint32_t s = pSelector->get_selector(x, y);
  1786. pDst_block->set_selector(x, y, (s == 3) ? h : l);
  1787. }
  1788. }
  1789. return;
  1790. }
  1791. const uint32_t selector_range_table = g_etc1_to_dxt1_selector_range_index[low_selector][high_selector];
  1792. //[32][8][RANGES][MAPPING]
  1793. const etc1_to_dxt1_56_solution* pTable_r = &g_etc1_to_dxt_5[(inten_table * 32 + base_color.r) * (NUM_ETC1_TO_DXT1_SELECTOR_RANGES * NUM_ETC1_TO_DXT1_SELECTOR_MAPPINGS) + selector_range_table * NUM_ETC1_TO_DXT1_SELECTOR_MAPPINGS];
  1794. const etc1_to_dxt1_56_solution* pTable_g = &g_etc1_to_dxt_6[(inten_table * 32 + base_color.g) * (NUM_ETC1_TO_DXT1_SELECTOR_RANGES * NUM_ETC1_TO_DXT1_SELECTOR_MAPPINGS) + selector_range_table * NUM_ETC1_TO_DXT1_SELECTOR_MAPPINGS];
  1795. const etc1_to_dxt1_56_solution* pTable_b = &g_etc1_to_dxt_5[(inten_table * 32 + base_color.b) * (NUM_ETC1_TO_DXT1_SELECTOR_RANGES * NUM_ETC1_TO_DXT1_SELECTOR_MAPPINGS) + selector_range_table * NUM_ETC1_TO_DXT1_SELECTOR_MAPPINGS];
  1796. uint32_t best_err = UINT_MAX;
  1797. uint32_t best_mapping = 0;
  1798. assert(NUM_ETC1_TO_DXT1_SELECTOR_MAPPINGS == 10);
  1799. #define DO_ITER(m) { uint32_t total_err = pTable_r[m].m_err + pTable_g[m].m_err + pTable_b[m].m_err; if (total_err < best_err) { best_err = total_err; best_mapping = m; } }
  1800. DO_ITER(0); DO_ITER(1); DO_ITER(2); DO_ITER(3); DO_ITER(4);
  1801. DO_ITER(5); DO_ITER(6); DO_ITER(7); DO_ITER(8); DO_ITER(9);
  1802. #undef DO_ITER
  1803. uint32_t l = dxt1_block::pack_unscaled_color(pTable_r[best_mapping].m_lo, pTable_g[best_mapping].m_lo, pTable_b[best_mapping].m_lo);
  1804. uint32_t h = dxt1_block::pack_unscaled_color(pTable_r[best_mapping].m_hi, pTable_g[best_mapping].m_hi, pTable_b[best_mapping].m_hi);
  1805. const uint8_t* pSelectors_xlat_256 = &g_etc1_to_dxt1_selector_mappings_raw_dxt1_256[best_mapping][0];
  1806. if (l < h)
  1807. {
  1808. std::swap(l, h);
  1809. pSelectors_xlat_256 = &g_etc1_to_dxt1_selector_mappings_raw_dxt1_inv_256[best_mapping][0];
  1810. }
  1811. pDst_block->set_low_color(static_cast<uint16_t>(l));
  1812. pDst_block->set_high_color(static_cast<uint16_t>(h));
  1813. if (l == h)
  1814. {
  1815. uint8_t mask = 0;
  1816. if (!use_threecolor_blocks)
  1817. {
  1818. // This is an annoying edge case that impacts BC3.
  1819. // Make l > h
  1820. if (h > 0)
  1821. h--;
  1822. else
  1823. {
  1824. // l = h = 0
  1825. assert(l == h && h == 0);
  1826. h = 0;
  1827. l = 1;
  1828. mask = 0x55;
  1829. }
  1830. assert(l > h);
  1831. pDst_block->set_low_color(static_cast<uint16_t>(l));
  1832. pDst_block->set_high_color(static_cast<uint16_t>(h));
  1833. }
  1834. pDst_block->m_selectors[0] = mask;
  1835. pDst_block->m_selectors[1] = mask;
  1836. pDst_block->m_selectors[2] = mask;
  1837. pDst_block->m_selectors[3] = mask;
  1838. return;
  1839. }
  1840. pDst_block->m_selectors[0] = pSelectors_xlat_256[pSelector->m_selectors[0]];
  1841. pDst_block->m_selectors[1] = pSelectors_xlat_256[pSelector->m_selectors[1]];
  1842. pDst_block->m_selectors[2] = pSelectors_xlat_256[pSelector->m_selectors[2]];
  1843. pDst_block->m_selectors[3] = pSelectors_xlat_256[pSelector->m_selectors[3]];
  1844. #endif
  1845. }
  1846. #if BASISD_ENABLE_DEBUG_FLAGS
  1847. static void convert_etc1s_to_dxt1_vis(dxt1_block* pDst_block, const endpoint* pEndpoints, const selector* pSelector, bool use_threecolor_blocks)
  1848. {
  1849. convert_etc1s_to_dxt1(pDst_block, pEndpoints, pSelector, use_threecolor_blocks);
  1850. if (g_debug_flags & cDebugFlagVisBC1Sels)
  1851. {
  1852. uint32_t l = dxt1_block::pack_unscaled_color(31, 63, 31);
  1853. uint32_t h = dxt1_block::pack_unscaled_color(0, 0, 0);
  1854. pDst_block->set_low_color(static_cast<uint16_t>(l));
  1855. pDst_block->set_high_color(static_cast<uint16_t>(h));
  1856. }
  1857. else if (g_debug_flags & cDebugFlagVisBC1Endpoints)
  1858. {
  1859. for (uint32_t y = 0; y < 4; y++)
  1860. for (uint32_t x = 0; x < 4; x++)
  1861. pDst_block->set_selector(x, y, (y < 2) ? 0 : 1);
  1862. }
  1863. }
  1864. #endif
  1865. #endif
  1866. #if BASISD_SUPPORT_FXT1
  1867. struct fxt1_block
  1868. {
  1869. union
  1870. {
  1871. struct
  1872. {
  1873. uint64_t m_t00 : 2;
  1874. uint64_t m_t01 : 2;
  1875. uint64_t m_t02 : 2;
  1876. uint64_t m_t03 : 2;
  1877. uint64_t m_t04 : 2;
  1878. uint64_t m_t05 : 2;
  1879. uint64_t m_t06 : 2;
  1880. uint64_t m_t07 : 2;
  1881. uint64_t m_t08 : 2;
  1882. uint64_t m_t09 : 2;
  1883. uint64_t m_t10 : 2;
  1884. uint64_t m_t11 : 2;
  1885. uint64_t m_t12 : 2;
  1886. uint64_t m_t13 : 2;
  1887. uint64_t m_t14 : 2;
  1888. uint64_t m_t15 : 2;
  1889. uint64_t m_t16 : 2;
  1890. uint64_t m_t17 : 2;
  1891. uint64_t m_t18 : 2;
  1892. uint64_t m_t19 : 2;
  1893. uint64_t m_t20 : 2;
  1894. uint64_t m_t21 : 2;
  1895. uint64_t m_t22 : 2;
  1896. uint64_t m_t23 : 2;
  1897. uint64_t m_t24 : 2;
  1898. uint64_t m_t25 : 2;
  1899. uint64_t m_t26 : 2;
  1900. uint64_t m_t27 : 2;
  1901. uint64_t m_t28 : 2;
  1902. uint64_t m_t29 : 2;
  1903. uint64_t m_t30 : 2;
  1904. uint64_t m_t31 : 2;
  1905. } m_lo;
  1906. uint64_t m_lo_bits;
  1907. uint8_t m_sels[8];
  1908. };
  1909. union
  1910. {
  1911. struct
  1912. {
  1913. #ifdef BASISU_USE_ORIGINAL_3DFX_FXT1_ENCODING
  1914. uint64_t m_b1 : 5;
  1915. uint64_t m_g1 : 5;
  1916. uint64_t m_r1 : 5;
  1917. uint64_t m_b0 : 5;
  1918. uint64_t m_g0 : 5;
  1919. uint64_t m_r0 : 5;
  1920. uint64_t m_b3 : 5;
  1921. uint64_t m_g3 : 5;
  1922. uint64_t m_r3 : 5;
  1923. uint64_t m_b2 : 5;
  1924. uint64_t m_g2 : 5;
  1925. uint64_t m_r2 : 5;
  1926. #else
  1927. uint64_t m_b0 : 5;
  1928. uint64_t m_g0 : 5;
  1929. uint64_t m_r0 : 5;
  1930. uint64_t m_b1 : 5;
  1931. uint64_t m_g1 : 5;
  1932. uint64_t m_r1 : 5;
  1933. uint64_t m_b2 : 5;
  1934. uint64_t m_g2 : 5;
  1935. uint64_t m_r2 : 5;
  1936. uint64_t m_b3 : 5;
  1937. uint64_t m_g3 : 5;
  1938. uint64_t m_r3 : 5;
  1939. #endif
  1940. uint64_t m_alpha : 1;
  1941. uint64_t m_glsb : 2;
  1942. uint64_t m_mode : 1;
  1943. } m_hi;
  1944. uint64_t m_hi_bits;
  1945. };
  1946. };
  1947. static uint8_t conv_dxt1_to_fxt1_sels(uint32_t sels)
  1948. {
  1949. static uint8_t s_conv_table[16] = { 0, 3, 1, 2, 12, 15, 13, 14, 4, 7, 5, 6, 8, 11, 9, 10 };
  1950. return s_conv_table[sels & 15] | (s_conv_table[sels >> 4] << 4);
  1951. }
  1952. static void convert_etc1s_to_fxt1(void *pDst, const endpoint *pEndpoints, const selector *pSelectors, uint32_t fxt1_subblock)
  1953. {
  1954. fxt1_block* pBlock = static_cast<fxt1_block*>(pDst);
  1955. // CC_MIXED is basically DXT1 with different encoding tricks.
  1956. // So transcode ETC1S to DXT1, then transcode that to FXT1 which is easy and nearly lossless.
  1957. // (It's not completely lossless because FXT1 rounds in its color lerps while DXT1 doesn't, but it should be good enough.)
  1958. dxt1_block blk;
  1959. convert_etc1s_to_dxt1(&blk, pEndpoints, pSelectors, false);
  1960. const uint32_t l = blk.get_low_color();
  1961. const uint32_t h = blk.get_high_color();
  1962. color32 color0((l >> 11) & 31, (l >> 5) & 63, l & 31, 255);
  1963. color32 color1((h >> 11) & 31, (h >> 5) & 63, h & 31, 255);
  1964. uint32_t g0 = color0.g & 1;
  1965. uint32_t g1 = color1.g & 1;
  1966. color0.g >>= 1;
  1967. color1.g >>= 1;
  1968. blk.m_selectors[0] = conv_dxt1_to_fxt1_sels(blk.m_selectors[0]);
  1969. blk.m_selectors[1] = conv_dxt1_to_fxt1_sels(blk.m_selectors[1]);
  1970. blk.m_selectors[2] = conv_dxt1_to_fxt1_sels(blk.m_selectors[2]);
  1971. blk.m_selectors[3] = conv_dxt1_to_fxt1_sels(blk.m_selectors[3]);
  1972. if ((blk.get_selector(0, 0) >> 1) != (g0 ^ g1))
  1973. {
  1974. std::swap(color0, color1);
  1975. std::swap(g0, g1);
  1976. blk.m_selectors[0] ^= 0xFF;
  1977. blk.m_selectors[1] ^= 0xFF;
  1978. blk.m_selectors[2] ^= 0xFF;
  1979. blk.m_selectors[3] ^= 0xFF;
  1980. }
  1981. if (fxt1_subblock == 0)
  1982. {
  1983. pBlock->m_hi.m_mode = 1;
  1984. pBlock->m_hi.m_alpha = 0;
  1985. pBlock->m_hi.m_glsb = g1 | (g1 << 1);
  1986. pBlock->m_hi.m_r0 = color0.r;
  1987. pBlock->m_hi.m_g0 = color0.g;
  1988. pBlock->m_hi.m_b0 = color0.b;
  1989. pBlock->m_hi.m_r1 = color1.r;
  1990. pBlock->m_hi.m_g1 = color1.g;
  1991. pBlock->m_hi.m_b1 = color1.b;
  1992. pBlock->m_hi.m_r2 = color0.r;
  1993. pBlock->m_hi.m_g2 = color0.g;
  1994. pBlock->m_hi.m_b2 = color0.b;
  1995. pBlock->m_hi.m_r3 = color1.r;
  1996. pBlock->m_hi.m_g3 = color1.g;
  1997. pBlock->m_hi.m_b3 = color1.b;
  1998. pBlock->m_sels[0] = blk.m_selectors[0];
  1999. pBlock->m_sels[1] = blk.m_selectors[1];
  2000. pBlock->m_sels[2] = blk.m_selectors[2];
  2001. pBlock->m_sels[3] = blk.m_selectors[3];
  2002. static const uint8_t s_border_dup[4] = { 0, 85, 170, 255 };
  2003. pBlock->m_sels[4] = s_border_dup[blk.m_selectors[0] >> 6];
  2004. pBlock->m_sels[5] = s_border_dup[blk.m_selectors[1] >> 6];
  2005. pBlock->m_sels[6] = s_border_dup[blk.m_selectors[2] >> 6];
  2006. pBlock->m_sels[7] = s_border_dup[blk.m_selectors[3] >> 6];
  2007. }
  2008. else
  2009. {
  2010. pBlock->m_hi.m_glsb = (pBlock->m_hi.m_glsb & 1) | (g1 << 1);
  2011. pBlock->m_hi.m_r2 = color0.r;
  2012. pBlock->m_hi.m_g2 = color0.g;
  2013. pBlock->m_hi.m_b2 = color0.b;
  2014. pBlock->m_hi.m_r3 = color1.r;
  2015. pBlock->m_hi.m_g3 = color1.g;
  2016. pBlock->m_hi.m_b3 = color1.b;
  2017. pBlock->m_sels[4] = blk.m_selectors[0];
  2018. pBlock->m_sels[5] = blk.m_selectors[1];
  2019. pBlock->m_sels[6] = blk.m_selectors[2];
  2020. pBlock->m_sels[7] = blk.m_selectors[3];
  2021. }
  2022. }
  2023. #endif // BASISD_SUPPORT_FXT1
  2024. #if BASISD_SUPPORT_DXT5A
  2025. static dxt_selector_range s_dxt5a_selector_ranges[] =
  2026. {
  2027. { 0, 3 },
  2028. { 1, 3 },
  2029. { 0, 2 },
  2030. { 1, 2 },
  2031. };
  2032. const uint32_t NUM_DXT5A_SELECTOR_RANGES = sizeof(s_dxt5a_selector_ranges) / sizeof(s_dxt5a_selector_ranges[0]);
  2033. struct etc1_g_to_dxt5a_conversion
  2034. {
  2035. uint8_t m_lo, m_hi;
  2036. uint16_t m_trans;
  2037. };
  2038. static etc1_g_to_dxt5a_conversion g_etc1_g_to_dxt5a[32 * 8][NUM_DXT5A_SELECTOR_RANGES] =
  2039. {
  2040. { { 8, 0, 393 },{ 8, 0, 392 },{ 2, 0, 9 },{ 2, 0, 8 }, }, { { 6, 16, 710 },{ 16, 6, 328 },{ 0, 10, 96 },{ 10, 6, 8 }, },
  2041. { { 28, 5, 1327 },{ 24, 14, 328 },{ 8, 18, 96 },{ 18, 14, 8 }, }, { { 36, 13, 1327 },{ 32, 22, 328 },{ 16, 26, 96 },{ 26, 22, 8 }, },
  2042. { { 45, 22, 1327 },{ 41, 31, 328 },{ 25, 35, 96 },{ 35, 31, 8 }, }, { { 53, 30, 1327 },{ 49, 39, 328 },{ 33, 43, 96 },{ 43, 39, 8 }, },
  2043. { { 61, 38, 1327 },{ 57, 47, 328 },{ 41, 51, 96 },{ 51, 47, 8 }, }, { { 69, 46, 1327 },{ 65, 55, 328 },{ 49, 59, 96 },{ 59, 55, 8 }, },
  2044. { { 78, 55, 1327 },{ 74, 64, 328 },{ 58, 68, 96 },{ 68, 64, 8 }, }, { { 86, 63, 1327 },{ 82, 72, 328 },{ 66, 76, 96 },{ 76, 72, 8 }, },
  2045. { { 94, 71, 1327 },{ 90, 80, 328 },{ 74, 84, 96 },{ 84, 80, 8 }, }, { { 102, 79, 1327 },{ 98, 88, 328 },{ 82, 92, 96 },{ 92, 88, 8 }, },
  2046. { { 111, 88, 1327 },{ 107, 97, 328 },{ 91, 101, 96 },{ 101, 97, 8 }, }, { { 119, 96, 1327 },{ 115, 105, 328 },{ 99, 109, 96 },{ 109, 105, 8 }, },
  2047. { { 127, 104, 1327 },{ 123, 113, 328 },{ 107, 117, 96 },{ 117, 113, 8 }, }, { { 135, 112, 1327 },{ 131, 121, 328 },{ 115, 125, 96 },{ 125, 121, 8 }, },
  2048. { { 144, 121, 1327 },{ 140, 130, 328 },{ 124, 134, 96 },{ 134, 130, 8 }, }, { { 152, 129, 1327 },{ 148, 138, 328 },{ 132, 142, 96 },{ 142, 138, 8 }, },
  2049. { { 160, 137, 1327 },{ 156, 146, 328 },{ 140, 150, 96 },{ 150, 146, 8 }, }, { { 168, 145, 1327 },{ 164, 154, 328 },{ 148, 158, 96 },{ 158, 154, 8 }, },
  2050. { { 177, 154, 1327 },{ 173, 163, 328 },{ 157, 167, 96 },{ 167, 163, 8 }, }, { { 185, 162, 1327 },{ 181, 171, 328 },{ 165, 175, 96 },{ 175, 171, 8 }, },
  2051. { { 193, 170, 1327 },{ 189, 179, 328 },{ 173, 183, 96 },{ 183, 179, 8 }, }, { { 201, 178, 1327 },{ 197, 187, 328 },{ 181, 191, 96 },{ 191, 187, 8 }, },
  2052. { { 210, 187, 1327 },{ 206, 196, 328 },{ 190, 200, 96 },{ 200, 196, 8 }, }, { { 218, 195, 1327 },{ 214, 204, 328 },{ 198, 208, 96 },{ 208, 204, 8 }, },
  2053. { { 226, 203, 1327 },{ 222, 212, 328 },{ 206, 216, 96 },{ 216, 212, 8 }, }, { { 234, 211, 1327 },{ 230, 220, 328 },{ 214, 224, 96 },{ 224, 220, 8 }, },
  2054. { { 243, 220, 1327 },{ 239, 229, 328 },{ 223, 233, 96 },{ 233, 229, 8 }, }, { { 251, 228, 1327 },{ 247, 237, 328 },{ 231, 241, 96 },{ 241, 237, 8 }, },
  2055. { { 239, 249, 3680 },{ 245, 249, 3648 },{ 239, 249, 96 },{ 249, 245, 8 }, }, { { 247, 253, 4040 },{ 255, 253, 8 },{ 247, 253, 456 },{ 255, 253, 8 }, },
  2056. { { 5, 17, 566 },{ 5, 17, 560 },{ 5, 0, 9 },{ 5, 0, 8 }, }, { { 25, 0, 313 },{ 25, 3, 328 },{ 13, 0, 49 },{ 13, 3, 8 }, },
  2057. { { 39, 0, 1329 },{ 33, 11, 328 },{ 11, 21, 70 },{ 21, 11, 8 }, }, { { 47, 7, 1329 },{ 41, 19, 328 },{ 29, 7, 33 },{ 29, 19, 8 }, },
  2058. { { 50, 11, 239 },{ 50, 28, 328 },{ 38, 16, 33 },{ 38, 28, 8 }, }, { { 92, 13, 2423 },{ 58, 36, 328 },{ 46, 24, 33 },{ 46, 36, 8 }, },
  2059. { { 100, 21, 2423 },{ 66, 44, 328 },{ 54, 32, 33 },{ 54, 44, 8 }, }, { { 86, 7, 1253 },{ 74, 52, 328 },{ 62, 40, 33 },{ 62, 52, 8 }, },
  2060. { { 95, 16, 1253 },{ 83, 61, 328 },{ 71, 49, 33 },{ 71, 61, 8 }, }, { { 103, 24, 1253 },{ 91, 69, 328 },{ 79, 57, 33 },{ 79, 69, 8 }, },
  2061. { { 111, 32, 1253 },{ 99, 77, 328 },{ 87, 65, 33 },{ 87, 77, 8 }, }, { { 119, 40, 1253 },{ 107, 85, 328 },{ 95, 73, 33 },{ 95, 85, 8 }, },
  2062. { { 128, 49, 1253 },{ 116, 94, 328 },{ 104, 82, 33 },{ 104, 94, 8 }, }, { { 136, 57, 1253 },{ 124, 102, 328 },{ 112, 90, 33 },{ 112, 102, 8 }, },
  2063. { { 144, 65, 1253 },{ 132, 110, 328 },{ 120, 98, 33 },{ 120, 110, 8 }, }, { { 152, 73, 1253 },{ 140, 118, 328 },{ 128, 106, 33 },{ 128, 118, 8 }, },
  2064. { { 161, 82, 1253 },{ 149, 127, 328 },{ 137, 115, 33 },{ 137, 127, 8 }, }, { { 169, 90, 1253 },{ 157, 135, 328 },{ 145, 123, 33 },{ 145, 135, 8 }, },
  2065. { { 177, 98, 1253 },{ 165, 143, 328 },{ 153, 131, 33 },{ 153, 143, 8 }, }, { { 185, 106, 1253 },{ 173, 151, 328 },{ 161, 139, 33 },{ 161, 151, 8 }, },
  2066. { { 194, 115, 1253 },{ 182, 160, 328 },{ 170, 148, 33 },{ 170, 160, 8 }, }, { { 202, 123, 1253 },{ 190, 168, 328 },{ 178, 156, 33 },{ 178, 168, 8 }, },
  2067. { { 210, 131, 1253 },{ 198, 176, 328 },{ 186, 164, 33 },{ 186, 176, 8 }, }, { { 218, 139, 1253 },{ 206, 184, 328 },{ 194, 172, 33 },{ 194, 184, 8 }, },
  2068. { { 227, 148, 1253 },{ 215, 193, 328 },{ 203, 181, 33 },{ 203, 193, 8 }, }, { { 235, 156, 1253 },{ 223, 201, 328 },{ 211, 189, 33 },{ 211, 201, 8 }, },
  2069. { { 243, 164, 1253 },{ 231, 209, 328 },{ 219, 197, 33 },{ 219, 209, 8 }, }, { { 183, 239, 867 },{ 239, 217, 328 },{ 227, 205, 33 },{ 227, 217, 8 }, },
  2070. { { 254, 214, 1329 },{ 248, 226, 328 },{ 236, 214, 33 },{ 236, 226, 8 }, }, { { 222, 244, 3680 },{ 234, 244, 3648 },{ 244, 222, 33 },{ 244, 234, 8 }, },
  2071. { { 230, 252, 3680 },{ 242, 252, 3648 },{ 252, 230, 33 },{ 252, 242, 8 }, }, { { 238, 250, 4040 },{ 255, 250, 8 },{ 238, 250, 456 },{ 255, 250, 8 }, },
  2072. { { 9, 29, 566 },{ 9, 29, 560 },{ 9, 0, 9 },{ 9, 0, 8 }, }, { { 17, 37, 566 },{ 17, 37, 560 },{ 17, 0, 9 },{ 17, 0, 8 }, },
  2073. { { 45, 0, 313 },{ 45, 0, 312 },{ 25, 0, 49 },{ 25, 7, 8 }, }, { { 14, 63, 2758 },{ 5, 53, 784 },{ 15, 33, 70 },{ 33, 15, 8 }, },
  2074. { { 71, 6, 1329 },{ 72, 4, 1328 },{ 42, 4, 33 },{ 42, 24, 8 }, }, { { 70, 3, 239 },{ 70, 2, 232 },{ 50, 12, 33 },{ 50, 32, 8 }, },
  2075. { { 0, 98, 2842 },{ 78, 10, 232 },{ 58, 20, 33 },{ 58, 40, 8 }, }, { { 97, 27, 1329 },{ 86, 18, 232 },{ 66, 28, 33 },{ 66, 48, 8 }, },
  2076. { { 0, 94, 867 },{ 95, 27, 232 },{ 75, 37, 33 },{ 75, 57, 8 }, }, { { 8, 102, 867 },{ 103, 35, 232 },{ 83, 45, 33 },{ 83, 65, 8 }, },
  2077. { { 12, 112, 867 },{ 111, 43, 232 },{ 91, 53, 33 },{ 91, 73, 8 }, }, { { 139, 2, 1253 },{ 119, 51, 232 },{ 99, 61, 33 },{ 99, 81, 8 }, },
  2078. { { 148, 13, 1253 },{ 128, 60, 232 },{ 108, 70, 33 },{ 108, 90, 8 }, }, { { 156, 21, 1253 },{ 136, 68, 232 },{ 116, 78, 33 },{ 116, 98, 8 }, },
  2079. { { 164, 29, 1253 },{ 144, 76, 232 },{ 124, 86, 33 },{ 124, 106, 8 }, }, { { 172, 37, 1253 },{ 152, 84, 232 },{ 132, 94, 33 },{ 132, 114, 8 }, },
  2080. { { 181, 46, 1253 },{ 161, 93, 232 },{ 141, 103, 33 },{ 141, 123, 8 }, }, { { 189, 54, 1253 },{ 169, 101, 232 },{ 149, 111, 33 },{ 149, 131, 8 }, },
  2081. { { 197, 62, 1253 },{ 177, 109, 232 },{ 157, 119, 33 },{ 157, 139, 8 }, }, { { 205, 70, 1253 },{ 185, 117, 232 },{ 165, 127, 33 },{ 165, 147, 8 }, },
  2082. { { 214, 79, 1253 },{ 194, 126, 232 },{ 174, 136, 33 },{ 174, 156, 8 }, }, { { 222, 87, 1253 },{ 202, 134, 232 },{ 182, 144, 33 },{ 182, 164, 8 }, },
  2083. { { 230, 95, 1253 },{ 210, 142, 232 },{ 190, 152, 33 },{ 190, 172, 8 }, }, { { 238, 103, 1253 },{ 218, 150, 232 },{ 198, 160, 33 },{ 198, 180, 8 }, },
  2084. { { 247, 112, 1253 },{ 227, 159, 232 },{ 207, 169, 33 },{ 207, 189, 8 }, }, { { 255, 120, 1253 },{ 235, 167, 232 },{ 215, 177, 33 },{ 215, 197, 8 }, },
  2085. { { 146, 243, 867 },{ 243, 175, 232 },{ 223, 185, 33 },{ 223, 205, 8 }, }, { { 184, 231, 3682 },{ 203, 251, 784 },{ 231, 193, 33 },{ 231, 213, 8 }, },
  2086. { { 193, 240, 3682 },{ 222, 240, 3648 },{ 240, 202, 33 },{ 240, 222, 8 }, }, { { 255, 210, 169 },{ 230, 248, 3648 },{ 248, 210, 33 },{ 248, 230, 8 }, },
  2087. { { 218, 238, 4040 },{ 255, 238, 8 },{ 218, 238, 456 },{ 255, 238, 8 }, }, { { 226, 246, 4040 },{ 255, 246, 8 },{ 226, 246, 456 },{ 255, 246, 8 }, },
  2088. { { 13, 42, 566 },{ 13, 42, 560 },{ 13, 0, 9 },{ 13, 0, 8 }, }, { { 50, 0, 329 },{ 50, 0, 328 },{ 21, 0, 9 },{ 21, 0, 8 }, },
  2089. { { 29, 58, 566 },{ 67, 2, 1352 },{ 3, 29, 70 },{ 29, 3, 8 }, }, { { 10, 79, 2758 },{ 76, 11, 1352 },{ 11, 37, 70 },{ 37, 11, 8 }, },
  2090. { { 7, 75, 790 },{ 7, 75, 784 },{ 20, 46, 70 },{ 46, 20, 8 }, }, { { 15, 83, 790 },{ 97, 1, 1328 },{ 28, 54, 70 },{ 54, 28, 8 }, },
  2091. { { 101, 7, 1329 },{ 105, 9, 1328 },{ 62, 0, 39 },{ 62, 36, 8 }, }, { { 99, 1, 239 },{ 99, 3, 232 },{ 1, 71, 98 },{ 70, 44, 8 }, },
  2092. { { 107, 11, 239 },{ 108, 12, 232 },{ 10, 80, 98 },{ 79, 53, 8 }, }, { { 115, 19, 239 },{ 116, 20, 232 },{ 18, 88, 98 },{ 87, 61, 8 }, },
  2093. { { 123, 27, 239 },{ 124, 28, 232 },{ 26, 96, 98 },{ 95, 69, 8 }, }, { { 131, 35, 239 },{ 132, 36, 232 },{ 34, 104, 98 },{ 103, 77, 8 }, },
  2094. { { 140, 44, 239 },{ 141, 45, 232 },{ 43, 113, 98 },{ 112, 86, 8 }, }, { { 148, 52, 239 },{ 149, 53, 232 },{ 51, 121, 98 },{ 120, 94, 8 }, },
  2095. { { 156, 60, 239 },{ 157, 61, 232 },{ 59, 129, 98 },{ 128, 102, 8 }, }, { { 164, 68, 239 },{ 165, 69, 232 },{ 67, 137, 98 },{ 136, 110, 8 }, },
  2096. { { 173, 77, 239 },{ 174, 78, 232 },{ 76, 146, 98 },{ 145, 119, 8 }, }, { { 181, 85, 239 },{ 182, 86, 232 },{ 84, 154, 98 },{ 153, 127, 8 }, },
  2097. { { 189, 93, 239 },{ 190, 94, 232 },{ 92, 162, 98 },{ 161, 135, 8 }, }, { { 197, 101, 239 },{ 198, 102, 232 },{ 100, 170, 98 },{ 169, 143, 8 }, },
  2098. { { 206, 110, 239 },{ 207, 111, 232 },{ 109, 179, 98 },{ 178, 152, 8 }, }, { { 214, 118, 239 },{ 215, 119, 232 },{ 117, 187, 98 },{ 186, 160, 8 }, },
  2099. { { 222, 126, 239 },{ 223, 127, 232 },{ 125, 195, 98 },{ 194, 168, 8 }, }, { { 230, 134, 239 },{ 231, 135, 232 },{ 133, 203, 98 },{ 202, 176, 8 }, },
  2100. { { 239, 143, 239 },{ 240, 144, 232 },{ 142, 212, 98 },{ 211, 185, 8 }, }, { { 247, 151, 239 },{ 180, 248, 784 },{ 150, 220, 98 },{ 219, 193, 8 }, },
  2101. { { 159, 228, 3682 },{ 201, 227, 3648 },{ 158, 228, 98 },{ 227, 201, 8 }, }, { { 181, 249, 3928 },{ 209, 235, 3648 },{ 166, 236, 98 },{ 235, 209, 8 }, },
  2102. { { 255, 189, 169 },{ 218, 244, 3648 },{ 175, 245, 98 },{ 244, 218, 8 }, }, { { 197, 226, 4040 },{ 226, 252, 3648 },{ 183, 253, 98 },{ 252, 226, 8 }, },
  2103. { { 205, 234, 4040 },{ 255, 234, 8 },{ 205, 234, 456 },{ 255, 234, 8 }, }, { { 213, 242, 4040 },{ 255, 242, 8 },{ 213, 242, 456 },{ 255, 242, 8 }, },
  2104. { { 18, 60, 566 },{ 18, 60, 560 },{ 18, 0, 9 },{ 18, 0, 8 }, }, { { 26, 68, 566 },{ 26, 68, 560 },{ 26, 0, 9 },{ 26, 0, 8 }, },
  2105. { { 34, 76, 566 },{ 34, 76, 560 },{ 34, 0, 9 },{ 34, 0, 8 }, }, { { 5, 104, 2758 },{ 98, 5, 1352 },{ 42, 0, 57 },{ 42, 6, 8 }, },
  2106. { { 92, 0, 313 },{ 93, 1, 312 },{ 15, 51, 70 },{ 51, 15, 8 }, }, { { 3, 101, 790 },{ 3, 101, 784 },{ 0, 59, 88 },{ 59, 23, 8 }, },
  2107. { { 14, 107, 790 },{ 11, 109, 784 },{ 31, 67, 70 },{ 67, 31, 8 }, }, { { 19, 117, 790 },{ 19, 117, 784 },{ 39, 75, 70 },{ 75, 39, 8 }, },
  2108. { { 28, 126, 790 },{ 28, 126, 784 },{ 83, 5, 33 },{ 84, 48, 8 }, }, { { 132, 0, 239 },{ 36, 134, 784 },{ 91, 13, 33 },{ 92, 56, 8 }, },
  2109. { { 142, 4, 239 },{ 44, 142, 784 },{ 99, 21, 33 },{ 100, 64, 8 }, }, { { 150, 12, 239 },{ 52, 150, 784 },{ 107, 29, 33 },{ 108, 72, 8 }, },
  2110. { { 159, 21, 239 },{ 61, 159, 784 },{ 116, 38, 33 },{ 117, 81, 8 }, }, { { 167, 29, 239 },{ 69, 167, 784 },{ 124, 46, 33 },{ 125, 89, 8 }, },
  2111. { { 175, 37, 239 },{ 77, 175, 784 },{ 132, 54, 33 },{ 133, 97, 8 }, }, { { 183, 45, 239 },{ 85, 183, 784 },{ 140, 62, 33 },{ 141, 105, 8 }, },
  2112. { { 192, 54, 239 },{ 94, 192, 784 },{ 149, 71, 33 },{ 150, 114, 8 }, }, { { 200, 62, 239 },{ 102, 200, 784 },{ 157, 79, 33 },{ 158, 122, 8 }, },
  2113. { { 208, 70, 239 },{ 110, 208, 784 },{ 165, 87, 33 },{ 166, 130, 8 }, }, { { 216, 78, 239 },{ 118, 216, 784 },{ 173, 95, 33 },{ 174, 138, 8 }, },
  2114. { { 225, 87, 239 },{ 127, 225, 784 },{ 182, 104, 33 },{ 183, 147, 8 }, }, { { 233, 95, 239 },{ 135, 233, 784 },{ 190, 112, 33 },{ 191, 155, 8 }, },
  2115. { { 241, 103, 239 },{ 143, 241, 784 },{ 198, 120, 33 },{ 199, 163, 8 }, }, { { 111, 208, 3682 },{ 151, 249, 784 },{ 206, 128, 33 },{ 207, 171, 8 }, },
  2116. { { 120, 217, 3682 },{ 180, 216, 3648 },{ 215, 137, 33 },{ 216, 180, 8 }, }, { { 128, 225, 3682 },{ 188, 224, 3648 },{ 223, 145, 33 },{ 224, 188, 8 }, },
  2117. { { 155, 253, 3928 },{ 196, 232, 3648 },{ 231, 153, 33 },{ 232, 196, 8 }, }, { { 144, 241, 3682 },{ 204, 240, 3648 },{ 239, 161, 33 },{ 240, 204, 8 }, },
  2118. { { 153, 250, 3682 },{ 213, 249, 3648 },{ 248, 170, 33 },{ 249, 213, 8 }, }, { { 179, 221, 4040 },{ 255, 221, 8 },{ 179, 221, 456 },{ 255, 221, 8 }, },
  2119. { { 187, 229, 4040 },{ 255, 229, 8 },{ 187, 229, 456 },{ 255, 229, 8 }, }, { { 195, 237, 4040 },{ 255, 237, 8 },{ 195, 237, 456 },{ 255, 237, 8 }, },
  2120. { { 24, 80, 566 },{ 24, 80, 560 },{ 24, 0, 9 },{ 24, 0, 8 }, }, { { 32, 88, 566 },{ 32, 88, 560 },{ 32, 0, 9 },{ 32, 0, 8 }, },
  2121. { { 40, 96, 566 },{ 40, 96, 560 },{ 40, 0, 9 },{ 40, 0, 8 }, }, { { 48, 104, 566 },{ 48, 104, 560 },{ 48, 0, 9 },{ 48, 0, 8 }, },
  2122. { { 9, 138, 2758 },{ 130, 7, 1352 },{ 9, 57, 70 },{ 57, 9, 8 }, }, { { 119, 0, 313 },{ 120, 0, 312 },{ 17, 65, 70 },{ 65, 17, 8 }, },
  2123. { { 0, 128, 784 },{ 128, 6, 312 },{ 25, 73, 70 },{ 73, 25, 8 }, }, { { 6, 137, 790 },{ 5, 136, 784 },{ 33, 81, 70 },{ 81, 33, 8 }, },
  2124. { { 42, 171, 2758 },{ 14, 145, 784 },{ 42, 90, 70 },{ 90, 42, 8 }, }, { { 50, 179, 2758 },{ 22, 153, 784 },{ 50, 98, 70 },{ 98, 50, 8 }, },
  2125. { { 58, 187, 2758 },{ 30, 161, 784 },{ 58, 106, 70 },{ 106, 58, 8 }, }, { { 191, 18, 1329 },{ 38, 169, 784 },{ 112, 9, 33 },{ 114, 66, 8 }, },
  2126. { { 176, 0, 239 },{ 47, 178, 784 },{ 121, 18, 33 },{ 123, 75, 8 }, }, { { 187, 1, 239 },{ 55, 186, 784 },{ 129, 26, 33 },{ 131, 83, 8 }, },
  2127. { { 195, 10, 239 },{ 63, 194, 784 },{ 137, 34, 33 },{ 139, 91, 8 }, }, { { 203, 18, 239 },{ 71, 202, 784 },{ 145, 42, 33 },{ 147, 99, 8 }, },
  2128. { { 212, 27, 239 },{ 80, 211, 784 },{ 154, 51, 33 },{ 156, 108, 8 }, }, { { 220, 35, 239 },{ 88, 219, 784 },{ 162, 59, 33 },{ 164, 116, 8 }, },
  2129. { { 228, 43, 239 },{ 96, 227, 784 },{ 170, 67, 33 },{ 172, 124, 8 }, }, { { 236, 51, 239 },{ 104, 235, 784 },{ 178, 75, 33 },{ 180, 132, 8 }, },
  2130. { { 245, 60, 239 },{ 113, 244, 784 },{ 187, 84, 33 },{ 189, 141, 8 }, }, { { 91, 194, 3680 },{ 149, 197, 3648 },{ 195, 92, 33 },{ 197, 149, 8 }, },
  2131. { { 99, 202, 3680 },{ 157, 205, 3648 },{ 203, 100, 33 },{ 205, 157, 8 }, }, { { 107, 210, 3680 },{ 165, 213, 3648 },{ 211, 108, 33 },{ 213, 165, 8 }, },
  2132. { { 119, 249, 3928 },{ 174, 222, 3648 },{ 220, 117, 33 },{ 222, 174, 8 }, }, { { 127, 255, 856 },{ 182, 230, 3648 },{ 228, 125, 33 },{ 230, 182, 8 }, },
  2133. { { 255, 135, 169 },{ 190, 238, 3648 },{ 236, 133, 33 },{ 238, 190, 8 }, }, { { 140, 243, 3680 },{ 198, 246, 3648 },{ 244, 141, 33 },{ 246, 198, 8 }, },
  2134. { { 151, 207, 4040 },{ 255, 207, 8 },{ 151, 207, 456 },{ 255, 207, 8 }, }, { { 159, 215, 4040 },{ 255, 215, 8 },{ 159, 215, 456 },{ 255, 215, 8 }, },
  2135. { { 167, 223, 4040 },{ 255, 223, 8 },{ 167, 223, 456 },{ 255, 223, 8 }, }, { { 175, 231, 4040 },{ 255, 231, 8 },{ 175, 231, 456 },{ 255, 231, 8 }, },
  2136. { { 33, 106, 566 },{ 33, 106, 560 },{ 33, 0, 9 },{ 33, 0, 8 }, }, { { 41, 114, 566 },{ 41, 114, 560 },{ 41, 0, 9 },{ 41, 0, 8 }, },
  2137. { { 49, 122, 566 },{ 49, 122, 560 },{ 49, 0, 9 },{ 49, 0, 8 }, }, { { 57, 130, 566 },{ 57, 130, 560 },{ 57, 0, 9 },{ 57, 0, 8 }, },
  2138. { { 66, 139, 566 },{ 66, 139, 560 },{ 66, 0, 9 },{ 66, 0, 8 }, }, { { 74, 147, 566 },{ 170, 7, 1352 },{ 8, 74, 70 },{ 74, 8, 8 }, },
  2139. { { 152, 0, 313 },{ 178, 15, 1352 },{ 0, 82, 80 },{ 82, 16, 8 }, }, { { 162, 0, 313 },{ 186, 23, 1352 },{ 24, 90, 70 },{ 90, 24, 8 }, },
  2140. { { 0, 171, 784 },{ 195, 32, 1352 },{ 33, 99, 70 },{ 99, 33, 8 }, }, { { 6, 179, 790 },{ 203, 40, 1352 },{ 41, 107, 70 },{ 107, 41, 8 }, },
  2141. { { 15, 187, 790 },{ 211, 48, 1352 },{ 115, 0, 41 },{ 115, 49, 8 }, }, { { 61, 199, 710 },{ 219, 56, 1352 },{ 57, 123, 70 },{ 123, 57, 8 }, },
  2142. { { 70, 208, 710 },{ 228, 65, 1352 },{ 66, 132, 70 },{ 132, 66, 8 }, }, { { 78, 216, 710 },{ 236, 73, 1352 },{ 74, 140, 70 },{ 140, 74, 8 }, },
  2143. { { 86, 224, 710 },{ 244, 81, 1352 },{ 145, 7, 33 },{ 148, 82, 8 }, }, { { 222, 8, 233 },{ 252, 89, 1352 },{ 153, 15, 33 },{ 156, 90, 8 }, },
  2144. { { 235, 0, 239 },{ 241, 101, 328 },{ 166, 6, 39 },{ 165, 99, 8 }, }, { { 32, 170, 3680 },{ 249, 109, 328 },{ 0, 175, 98 },{ 173, 107, 8 }, },
  2145. { { 40, 178, 3680 },{ 115, 181, 3648 },{ 8, 183, 98 },{ 181, 115, 8 }, }, { { 48, 186, 3680 },{ 123, 189, 3648 },{ 16, 191, 98 },{ 189, 123, 8 }, },
  2146. { { 57, 195, 3680 },{ 132, 198, 3648 },{ 25, 200, 98 },{ 198, 132, 8 }, }, { { 67, 243, 3928 },{ 140, 206, 3648 },{ 33, 208, 98 },{ 206, 140, 8 }, },
  2147. { { 76, 251, 3928 },{ 148, 214, 3648 },{ 41, 216, 98 },{ 214, 148, 8 }, }, { { 86, 255, 856 },{ 156, 222, 3648 },{ 49, 224, 98 },{ 222, 156, 8 }, },
  2148. { { 255, 93, 169 },{ 165, 231, 3648 },{ 58, 233, 98 },{ 231, 165, 8 }, }, { { 98, 236, 3680 },{ 173, 239, 3648 },{ 66, 241, 98 },{ 239, 173, 8 }, },
  2149. { { 108, 181, 4040 },{ 181, 247, 3648 },{ 74, 249, 98 },{ 247, 181, 8 }, }, { { 116, 189, 4040 },{ 255, 189, 8 },{ 116, 189, 456 },{ 255, 189, 8 }, },
  2150. { { 125, 198, 4040 },{ 255, 198, 8 },{ 125, 198, 456 },{ 255, 198, 8 }, }, { { 133, 206, 4040 },{ 255, 206, 8 },{ 133, 206, 456 },{ 255, 206, 8 }, },
  2151. { { 141, 214, 4040 },{ 255, 214, 8 },{ 141, 214, 456 },{ 255, 214, 8 }, }, { { 149, 222, 4040 },{ 255, 222, 8 },{ 149, 222, 456 },{ 255, 222, 8 }, },
  2152. { { 47, 183, 566 },{ 47, 183, 560 },{ 47, 0, 9 },{ 47, 0, 8 }, }, { { 55, 191, 566 },{ 55, 191, 560 },{ 55, 0, 9 },{ 55, 0, 8 }, },
  2153. { { 63, 199, 566 },{ 63, 199, 560 },{ 63, 0, 9 },{ 63, 0, 8 }, }, { { 71, 207, 566 },{ 71, 207, 560 },{ 71, 0, 9 },{ 71, 0, 8 }, },
  2154. { { 80, 216, 566 },{ 80, 216, 560 },{ 80, 0, 9 },{ 80, 0, 8 }, }, { { 88, 224, 566 },{ 88, 224, 560 },{ 88, 0, 9 },{ 88, 0, 8 }, },
  2155. { { 3, 233, 710 },{ 3, 233, 704 },{ 2, 96, 70 },{ 96, 2, 8 }, }, { { 11, 241, 710 },{ 11, 241, 704 },{ 10, 104, 70 },{ 104, 10, 8 }, },
  2156. { { 20, 250, 710 },{ 20, 250, 704 },{ 19, 113, 70 },{ 113, 19, 8 }, }, { { 27, 121, 3654 },{ 27, 121, 3648 },{ 27, 121, 70 },{ 121, 27, 8 }, },
  2157. { { 35, 129, 3654 },{ 35, 129, 3648 },{ 35, 129, 70 },{ 129, 35, 8 }, }, { { 43, 137, 3654 },{ 43, 137, 3648 },{ 43, 137, 70 },{ 137, 43, 8 }, },
  2158. { { 52, 146, 3654 },{ 52, 146, 3648 },{ 52, 146, 70 },{ 146, 52, 8 }, }, { { 60, 154, 3654 },{ 60, 154, 3648 },{ 60, 154, 70 },{ 154, 60, 8 }, },
  2159. { { 68, 162, 3654 },{ 68, 162, 3648 },{ 68, 162, 70 },{ 162, 68, 8 }, }, { { 76, 170, 3654 },{ 76, 170, 3648 },{ 76, 170, 70 },{ 170, 76, 8 }, },
  2160. { { 85, 179, 3654 },{ 85, 179, 3648 },{ 85, 179, 70 },{ 179, 85, 8 }, }, { { 93, 187, 3654 },{ 93, 187, 3648 },{ 93, 187, 70 },{ 187, 93, 8 }, },
  2161. { { 101, 195, 3654 },{ 101, 195, 3648 },{ 101, 195, 70 },{ 195, 101, 8 }, }, { { 109, 203, 3654 },{ 109, 203, 3648 },{ 109, 203, 70 },{ 203, 109, 8 }, },
  2162. { { 118, 212, 3654 },{ 118, 212, 3648 },{ 118, 212, 70 },{ 212, 118, 8 }, }, { { 126, 220, 3654 },{ 126, 220, 3648 },{ 126, 220, 70 },{ 220, 126, 8 }, },
  2163. { { 134, 228, 3654 },{ 134, 228, 3648 },{ 134, 228, 70 },{ 228, 134, 8 }, }, { { 5, 236, 3680 },{ 142, 236, 3648 },{ 5, 236, 96 },{ 236, 142, 8 }, },
  2164. { { 14, 245, 3680 },{ 151, 245, 3648 },{ 14, 245, 96 },{ 245, 151, 8 }, }, { { 23, 159, 4040 },{ 159, 253, 3648 },{ 23, 159, 456 },{ 253, 159, 8 }, },
  2165. { { 31, 167, 4040 },{ 255, 167, 8 },{ 31, 167, 456 },{ 255, 167, 8 }, }, { { 39, 175, 4040 },{ 255, 175, 8 },{ 39, 175, 456 },{ 255, 175, 8 }, },
  2166. { { 48, 184, 4040 },{ 255, 184, 8 },{ 48, 184, 456 },{ 255, 184, 8 }, }, { { 56, 192, 4040 },{ 255, 192, 8 },{ 56, 192, 456 },{ 255, 192, 8 }, },
  2167. { { 64, 200, 4040 },{ 255, 200, 8 },{ 64, 200, 456 },{ 255, 200, 8 }, },{ { 72, 208, 4040 },{ 255, 208, 8 },{ 72, 208, 456 },{ 255, 208, 8 }, },
  2168. };
  2169. struct dxt5a_block
  2170. {
  2171. uint8_t m_endpoints[2];
  2172. enum { cTotalSelectorBytes = 6 };
  2173. uint8_t m_selectors[cTotalSelectorBytes];
  2174. inline void clear()
  2175. {
  2176. basisu::clear_obj(*this);
  2177. }
  2178. inline uint32_t get_low_alpha() const
  2179. {
  2180. return m_endpoints[0];
  2181. }
  2182. inline uint32_t get_high_alpha() const
  2183. {
  2184. return m_endpoints[1];
  2185. }
  2186. inline void set_low_alpha(uint32_t i)
  2187. {
  2188. assert(i <= UINT8_MAX);
  2189. m_endpoints[0] = static_cast<uint8_t>(i);
  2190. }
  2191. inline void set_high_alpha(uint32_t i)
  2192. {
  2193. assert(i <= UINT8_MAX);
  2194. m_endpoints[1] = static_cast<uint8_t>(i);
  2195. }
  2196. inline bool is_alpha6_block() const { return get_low_alpha() <= get_high_alpha(); }
  2197. uint32_t get_endpoints_as_word() const { return m_endpoints[0] | (m_endpoints[1] << 8); }
  2198. uint32_t get_selectors_as_word(uint32_t index) { assert(index < 3); return m_selectors[index * 2] | (m_selectors[index * 2 + 1] << 8); }
  2199. inline uint32_t get_selector(uint32_t x, uint32_t y) const
  2200. {
  2201. assert((x < 4U) && (y < 4U));
  2202. uint32_t selector_index = (y * 4) + x;
  2203. uint32_t bit_index = selector_index * cDXT5SelectorBits;
  2204. uint32_t byte_index = bit_index >> 3;
  2205. uint32_t bit_ofs = bit_index & 7;
  2206. uint32_t v = m_selectors[byte_index];
  2207. if (byte_index < (cTotalSelectorBytes - 1))
  2208. v |= (m_selectors[byte_index + 1] << 8);
  2209. return (v >> bit_ofs) & 7;
  2210. }
  2211. inline void set_selector(uint32_t x, uint32_t y, uint32_t val)
  2212. {
  2213. assert((x < 4U) && (y < 4U) && (val < 8U));
  2214. uint32_t selector_index = (y * 4) + x;
  2215. uint32_t bit_index = selector_index * cDXT5SelectorBits;
  2216. uint32_t byte_index = bit_index >> 3;
  2217. uint32_t bit_ofs = bit_index & 7;
  2218. uint32_t v = m_selectors[byte_index];
  2219. if (byte_index < (cTotalSelectorBytes - 1))
  2220. v |= (m_selectors[byte_index + 1] << 8);
  2221. v &= (~(7 << bit_ofs));
  2222. v |= (val << bit_ofs);
  2223. m_selectors[byte_index] = static_cast<uint8_t>(v);
  2224. if (byte_index < (cTotalSelectorBytes - 1))
  2225. m_selectors[byte_index + 1] = static_cast<uint8_t>(v >> 8);
  2226. }
  2227. enum { cMaxSelectorValues = 8 };
  2228. static uint32_t get_block_values6(color32* pDst, uint32_t l, uint32_t h)
  2229. {
  2230. pDst[0].a = static_cast<uint8_t>(l);
  2231. pDst[1].a = static_cast<uint8_t>(h);
  2232. pDst[2].a = static_cast<uint8_t>((l * 4 + h) / 5);
  2233. pDst[3].a = static_cast<uint8_t>((l * 3 + h * 2) / 5);
  2234. pDst[4].a = static_cast<uint8_t>((l * 2 + h * 3) / 5);
  2235. pDst[5].a = static_cast<uint8_t>((l + h * 4) / 5);
  2236. pDst[6].a = 0;
  2237. pDst[7].a = 255;
  2238. return 6;
  2239. }
  2240. static uint32_t get_block_values8(color32* pDst, uint32_t l, uint32_t h)
  2241. {
  2242. pDst[0].a = static_cast<uint8_t>(l);
  2243. pDst[1].a = static_cast<uint8_t>(h);
  2244. pDst[2].a = static_cast<uint8_t>((l * 6 + h) / 7);
  2245. pDst[3].a = static_cast<uint8_t>((l * 5 + h * 2) / 7);
  2246. pDst[4].a = static_cast<uint8_t>((l * 4 + h * 3) / 7);
  2247. pDst[5].a = static_cast<uint8_t>((l * 3 + h * 4) / 7);
  2248. pDst[6].a = static_cast<uint8_t>((l * 2 + h * 5) / 7);
  2249. pDst[7].a = static_cast<uint8_t>((l + h * 6) / 7);
  2250. return 8;
  2251. }
  2252. static uint32_t get_block_values(color32* pDst, uint32_t l, uint32_t h)
  2253. {
  2254. if (l > h)
  2255. return get_block_values8(pDst, l, h);
  2256. else
  2257. return get_block_values6(pDst, l, h);
  2258. }
  2259. };
  2260. static void convert_etc1s_to_dxt5a(dxt5a_block* pDst_block, const endpoint* pEndpoints, const selector* pSelector)
  2261. {
  2262. const uint32_t low_selector = pSelector->m_lo_selector;
  2263. const uint32_t high_selector = pSelector->m_hi_selector;
  2264. const color32& base_color = pEndpoints->m_color5;
  2265. const uint32_t inten_table = pEndpoints->m_inten5;
  2266. if (low_selector == high_selector)
  2267. {
  2268. uint32_t r;
  2269. decoder_etc_block::get_block_color5_r(base_color, inten_table, low_selector, r);
  2270. pDst_block->set_low_alpha(r);
  2271. pDst_block->set_high_alpha(r);
  2272. pDst_block->m_selectors[0] = 0;
  2273. pDst_block->m_selectors[1] = 0;
  2274. pDst_block->m_selectors[2] = 0;
  2275. pDst_block->m_selectors[3] = 0;
  2276. pDst_block->m_selectors[4] = 0;
  2277. pDst_block->m_selectors[5] = 0;
  2278. return;
  2279. }
  2280. else if (pSelector->m_num_unique_selectors == 2)
  2281. {
  2282. color32 block_colors[4];
  2283. decoder_etc_block::get_block_colors5(block_colors, base_color, inten_table);
  2284. const uint32_t r0 = block_colors[low_selector].r;
  2285. const uint32_t r1 = block_colors[high_selector].r;
  2286. pDst_block->set_low_alpha(r0);
  2287. pDst_block->set_high_alpha(r1);
  2288. // TODO: Optimize this
  2289. for (uint32_t y = 0; y < 4; y++)
  2290. {
  2291. for (uint32_t x = 0; x < 4; x++)
  2292. {
  2293. uint32_t s = pSelector->get_selector(x, y);
  2294. pDst_block->set_selector(x, y, (s == high_selector) ? 1 : 0);
  2295. }
  2296. }
  2297. return;
  2298. }
  2299. uint32_t selector_range_table = 0;
  2300. for (selector_range_table = 0; selector_range_table < NUM_DXT5A_SELECTOR_RANGES; selector_range_table++)
  2301. if ((low_selector == s_dxt5a_selector_ranges[selector_range_table].m_low) && (high_selector == s_dxt5a_selector_ranges[selector_range_table].m_high))
  2302. break;
  2303. if (selector_range_table >= NUM_DXT5A_SELECTOR_RANGES)
  2304. selector_range_table = 0;
  2305. const etc1_g_to_dxt5a_conversion* pTable_entry = &g_etc1_g_to_dxt5a[base_color.r + inten_table * 32][selector_range_table];
  2306. pDst_block->set_low_alpha(pTable_entry->m_lo);
  2307. pDst_block->set_high_alpha(pTable_entry->m_hi);
  2308. // TODO: Optimize this (like ETC1->BC1)
  2309. for (uint32_t y = 0; y < 4; y++)
  2310. {
  2311. for (uint32_t x = 0; x < 4; x++)
  2312. {
  2313. uint32_t s = pSelector->get_selector(x, y);
  2314. uint32_t ds = (pTable_entry->m_trans >> (s * 3)) & 7;
  2315. pDst_block->set_selector(x, y, ds);
  2316. }
  2317. }
  2318. }
  2319. #endif //BASISD_SUPPORT_DXT5A
  2320. // PVRTC
  2321. #if BASISD_SUPPORT_PVRTC1 || BASISD_SUPPORT_UASTC
  2322. static const uint16_t g_pvrtc_swizzle_table[256] =
  2323. {
  2324. 0x0000, 0x0001, 0x0004, 0x0005, 0x0010, 0x0011, 0x0014, 0x0015, 0x0040, 0x0041, 0x0044, 0x0045, 0x0050, 0x0051, 0x0054, 0x0055, 0x0100, 0x0101, 0x0104, 0x0105, 0x0110, 0x0111, 0x0114, 0x0115, 0x0140, 0x0141, 0x0144, 0x0145, 0x0150, 0x0151, 0x0154, 0x0155,
  2325. 0x0400, 0x0401, 0x0404, 0x0405, 0x0410, 0x0411, 0x0414, 0x0415, 0x0440, 0x0441, 0x0444, 0x0445, 0x0450, 0x0451, 0x0454, 0x0455, 0x0500, 0x0501, 0x0504, 0x0505, 0x0510, 0x0511, 0x0514, 0x0515, 0x0540, 0x0541, 0x0544, 0x0545, 0x0550, 0x0551, 0x0554, 0x0555,
  2326. 0x1000, 0x1001, 0x1004, 0x1005, 0x1010, 0x1011, 0x1014, 0x1015, 0x1040, 0x1041, 0x1044, 0x1045, 0x1050, 0x1051, 0x1054, 0x1055, 0x1100, 0x1101, 0x1104, 0x1105, 0x1110, 0x1111, 0x1114, 0x1115, 0x1140, 0x1141, 0x1144, 0x1145, 0x1150, 0x1151, 0x1154, 0x1155,
  2327. 0x1400, 0x1401, 0x1404, 0x1405, 0x1410, 0x1411, 0x1414, 0x1415, 0x1440, 0x1441, 0x1444, 0x1445, 0x1450, 0x1451, 0x1454, 0x1455, 0x1500, 0x1501, 0x1504, 0x1505, 0x1510, 0x1511, 0x1514, 0x1515, 0x1540, 0x1541, 0x1544, 0x1545, 0x1550, 0x1551, 0x1554, 0x1555,
  2328. 0x4000, 0x4001, 0x4004, 0x4005, 0x4010, 0x4011, 0x4014, 0x4015, 0x4040, 0x4041, 0x4044, 0x4045, 0x4050, 0x4051, 0x4054, 0x4055, 0x4100, 0x4101, 0x4104, 0x4105, 0x4110, 0x4111, 0x4114, 0x4115, 0x4140, 0x4141, 0x4144, 0x4145, 0x4150, 0x4151, 0x4154, 0x4155,
  2329. 0x4400, 0x4401, 0x4404, 0x4405, 0x4410, 0x4411, 0x4414, 0x4415, 0x4440, 0x4441, 0x4444, 0x4445, 0x4450, 0x4451, 0x4454, 0x4455, 0x4500, 0x4501, 0x4504, 0x4505, 0x4510, 0x4511, 0x4514, 0x4515, 0x4540, 0x4541, 0x4544, 0x4545, 0x4550, 0x4551, 0x4554, 0x4555,
  2330. 0x5000, 0x5001, 0x5004, 0x5005, 0x5010, 0x5011, 0x5014, 0x5015, 0x5040, 0x5041, 0x5044, 0x5045, 0x5050, 0x5051, 0x5054, 0x5055, 0x5100, 0x5101, 0x5104, 0x5105, 0x5110, 0x5111, 0x5114, 0x5115, 0x5140, 0x5141, 0x5144, 0x5145, 0x5150, 0x5151, 0x5154, 0x5155,
  2331. 0x5400, 0x5401, 0x5404, 0x5405, 0x5410, 0x5411, 0x5414, 0x5415, 0x5440, 0x5441, 0x5444, 0x5445, 0x5450, 0x5451, 0x5454, 0x5455, 0x5500, 0x5501, 0x5504, 0x5505, 0x5510, 0x5511, 0x5514, 0x5515, 0x5540, 0x5541, 0x5544, 0x5545, 0x5550, 0x5551, 0x5554, 0x5555
  2332. };
  2333. // Note we can't use simple calculations to convert PVRTC1 encoded endpoint components to/from 8-bits, due to hardware approximations.
  2334. static const uint8_t g_pvrtc_5[32] = { 0,8,16,24,33,41,49,57,66,74,82,90,99,107,115,123,132,140,148,156,165,173,181,189,198,206,214,222,231,239,247,255 };
  2335. static const uint8_t g_pvrtc_4[16] = { 0,16,33,49,66,82,99,115,140,156,173,189,206,222,239,255 };
  2336. static const uint8_t g_pvrtc_3[8] = { 0,33,74,107,148,181,222,255 };
  2337. static const uint8_t g_pvrtc_alpha[9] = { 0,34,68,102,136,170,204,238,255 };
  2338. static const uint8_t g_pvrtc_5_floor[256] =
  2339. {
  2340. 0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,
  2341. 3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6,7,7,7,7,7,7,7,
  2342. 7,7,8,8,8,8,8,8,8,8,9,9,9,9,9,9,9,9,10,10,10,10,10,10,10,10,11,11,11,11,11,11,
  2343. 11,11,11,12,12,12,12,12,12,12,12,13,13,13,13,13,13,13,13,14,14,14,14,14,14,14,14,15,15,15,15,15,
  2344. 15,15,15,15,16,16,16,16,16,16,16,16,17,17,17,17,17,17,17,17,18,18,18,18,18,18,18,18,19,19,19,19,
  2345. 19,19,19,19,19,20,20,20,20,20,20,20,20,21,21,21,21,21,21,21,21,22,22,22,22,22,22,22,22,23,23,23,
  2346. 23,23,23,23,23,23,24,24,24,24,24,24,24,24,25,25,25,25,25,25,25,25,26,26,26,26,26,26,26,26,27,27,
  2347. 27,27,27,27,27,27,27,28,28,28,28,28,28,28,28,29,29,29,29,29,29,29,29,30,30,30,30,30,30,30,30,31
  2348. };
  2349. static const uint8_t g_pvrtc_5_ceil[256] =
  2350. {
  2351. 0,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,
  2352. 4,4,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6,7,7,7,7,7,7,7,7,8,8,8,8,8,8,
  2353. 8,8,8,9,9,9,9,9,9,9,9,10,10,10,10,10,10,10,10,11,11,11,11,11,11,11,11,12,12,12,12,12,
  2354. 12,12,12,12,13,13,13,13,13,13,13,13,14,14,14,14,14,14,14,14,15,15,15,15,15,15,15,15,16,16,16,16,
  2355. 16,16,16,16,16,17,17,17,17,17,17,17,17,18,18,18,18,18,18,18,18,19,19,19,19,19,19,19,19,20,20,20,
  2356. 20,20,20,20,20,20,21,21,21,21,21,21,21,21,22,22,22,22,22,22,22,22,23,23,23,23,23,23,23,23,24,24,
  2357. 24,24,24,24,24,24,24,25,25,25,25,25,25,25,25,26,26,26,26,26,26,26,26,27,27,27,27,27,27,27,27,28,
  2358. 28,28,28,28,28,28,28,28,29,29,29,29,29,29,29,29,30,30,30,30,30,30,30,30,31,31,31,31,31,31,31,31
  2359. };
  2360. static const uint8_t g_pvrtc_4_floor[256] =
  2361. {
  2362. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  2363. 1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
  2364. 3,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
  2365. 5,5,5,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,7,7,7,7,7,7,7,7,7,7,7,7,7,
  2366. 7,7,7,7,7,7,7,7,7,7,7,7,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,9,9,9,9,
  2367. 9,9,9,9,9,9,9,9,9,9,9,9,9,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,10,11,11,11,
  2368. 11,11,11,11,11,11,11,11,11,11,11,11,11,11,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,13,13,
  2369. 13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,15
  2370. };
  2371. static const uint8_t g_pvrtc_4_ceil[256] =
  2372. {
  2373. 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
  2374. 2,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
  2375. 4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6,6,6,6,6,6,
  2376. 6,6,6,6,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,8,8,8,8,8,8,8,8,8,8,8,8,
  2377. 8,8,8,8,8,8,8,8,8,8,8,8,8,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,10,10,10,
  2378. 10,10,10,10,10,10,10,10,10,10,10,10,10,10,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,11,12,12,
  2379. 12,12,12,12,12,12,12,12,12,12,12,12,12,12,12,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,14,
  2380. 14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,14,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15,15
  2381. };
  2382. static const uint8_t g_pvrtc_3_floor[256] =
  2383. {
  2384. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  2385. 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  2386. 1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
  2387. 2,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
  2388. 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,4,
  2389. 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,
  2390. 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6,6,
  2391. 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,7
  2392. };
  2393. static const uint8_t g_pvrtc_3_ceil[256] =
  2394. {
  2395. 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  2396. 1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
  2397. 2,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
  2398. 3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
  2399. 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,
  2400. 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6,6,6,
  2401. 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,7,
  2402. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7
  2403. };
  2404. static const uint8_t g_pvrtc_alpha_floor[256] =
  2405. {
  2406. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  2407. 0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  2408. 1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
  2409. 2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
  2410. 3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
  2411. 4,4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
  2412. 5,5,5,5,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  2413. 6,6,6,6,6,6,6,6,6,6,6,6,6,6,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,8
  2414. };
  2415. static const uint8_t g_pvrtc_alpha_ceil[256] =
  2416. {
  2417. 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  2418. 1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
  2419. 2,2,2,2,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,
  2420. 3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
  2421. 4,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
  2422. 5,5,5,5,5,5,5,5,5,5,5,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  2423. 6,6,6,6,6,6,6,6,6,6,6,6,6,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  2424. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8
  2425. };
  2426. struct pvrtc4_block
  2427. {
  2428. uint32_t m_modulation;
  2429. uint32_t m_endpoints;
  2430. pvrtc4_block() : m_modulation(0), m_endpoints(0) { }
  2431. inline bool operator== (const pvrtc4_block& rhs) const
  2432. {
  2433. return (m_modulation == rhs.m_modulation) && (m_endpoints == rhs.m_endpoints);
  2434. }
  2435. inline void clear()
  2436. {
  2437. m_modulation = 0;
  2438. m_endpoints = 0;
  2439. }
  2440. inline bool get_block_uses_transparent_modulation() const
  2441. {
  2442. return (m_endpoints & 1) != 0;
  2443. }
  2444. inline void set_block_uses_transparent_modulation(bool m)
  2445. {
  2446. m_endpoints = (m_endpoints & ~1U) | static_cast<uint32_t>(m);
  2447. }
  2448. inline bool is_endpoint_opaque(uint32_t endpoint_index) const
  2449. {
  2450. static const uint32_t s_bitmasks[2] = { 0x8000U, 0x80000000U };
  2451. return (m_endpoints & s_bitmasks[basisu::open_range_check(endpoint_index, 2U)]) != 0;
  2452. }
  2453. inline void set_endpoint_opaque(uint32_t endpoint_index, bool opaque)
  2454. {
  2455. assert(endpoint_index < 2);
  2456. static const uint32_t s_bitmasks[2] = { 0x8000U, 0x80000000U };
  2457. if (opaque)
  2458. m_endpoints |= s_bitmasks[endpoint_index];
  2459. else
  2460. m_endpoints &= ~s_bitmasks[endpoint_index];
  2461. }
  2462. inline color32 get_endpoint_5554(uint32_t endpoint_index) const
  2463. {
  2464. assert(endpoint_index < 2);
  2465. static const uint32_t s_endpoint_mask[2] = { 0xFFFE, 0xFFFF };
  2466. uint32_t packed = (m_endpoints >> (basisu::open_range_check(endpoint_index, 2U) ? 16 : 0)) & s_endpoint_mask[endpoint_index];
  2467. uint32_t r, g, b, a;
  2468. if (packed & 0x8000)
  2469. {
  2470. // opaque 554 or 555
  2471. r = (packed >> 10) & 31;
  2472. g = (packed >> 5) & 31;
  2473. b = packed & 31;
  2474. if (!endpoint_index)
  2475. b |= (b >> 4);
  2476. a = 0xF;
  2477. }
  2478. else
  2479. {
  2480. // translucent 4433 or 4443
  2481. r = (packed >> 7) & 0x1E;
  2482. g = (packed >> 3) & 0x1E;
  2483. b = (packed & 0xF) << 1;
  2484. r |= (r >> 4);
  2485. g |= (g >> 4);
  2486. if (!endpoint_index)
  2487. b |= (b >> 3);
  2488. else
  2489. b |= (b >> 4);
  2490. a = (packed >> 11) & 0xE;
  2491. }
  2492. assert((r < 32) && (g < 32) && (b < 32) && (a < 16));
  2493. return color32(r, g, b, a);
  2494. }
  2495. inline color32 get_endpoint_8888(uint32_t endpoint_index) const
  2496. {
  2497. assert(endpoint_index < 2);
  2498. static const uint32_t s_endpoint_mask[2] = { 0xFFFE, 0xFFFF };
  2499. uint32_t packed = (m_endpoints >> (basisu::open_range_check(endpoint_index, 2U) ? 16 : 0)) & s_endpoint_mask[endpoint_index];
  2500. uint32_t r, g, b, a;
  2501. if (packed & 0x8000)
  2502. {
  2503. // opaque 554 or 555
  2504. // 1RRRRRGGGGGBBBBM
  2505. // 1RRRRRGGGGGBBBBB
  2506. r = (packed >> 10) & 31;
  2507. g = (packed >> 5) & 31;
  2508. b = packed & 31;
  2509. r = g_pvrtc_5[r];
  2510. g = g_pvrtc_5[g];
  2511. if (!endpoint_index)
  2512. b = g_pvrtc_4[b >> 1];
  2513. else
  2514. b = g_pvrtc_5[b];
  2515. a = 255;
  2516. }
  2517. else
  2518. {
  2519. // translucent 4433 or 4443
  2520. // 0AAA RRRR GGGG BBBM
  2521. // 0AAA RRRR GGGG BBBB
  2522. r = (packed >> 8) & 0xF;
  2523. g = (packed >> 4) & 0xF;
  2524. b = packed & 0xF;
  2525. a = (packed >> 12) & 7;
  2526. r = g_pvrtc_4[r];
  2527. g = g_pvrtc_4[g];
  2528. if (!endpoint_index)
  2529. b = g_pvrtc_3[b >> 1];
  2530. else
  2531. b = g_pvrtc_4[b];
  2532. a = g_pvrtc_alpha[a];
  2533. }
  2534. return color32(r, g, b, a);
  2535. }
  2536. inline uint32_t get_endpoint_l8(uint32_t endpoint_index) const
  2537. {
  2538. color32 c(get_endpoint_8888(endpoint_index));
  2539. return c.r + c.g + c.b + c.a;
  2540. }
  2541. inline uint32_t get_opaque_endpoint_l0() const
  2542. {
  2543. uint32_t packed = m_endpoints & 0xFFFE;
  2544. uint32_t r, g, b;
  2545. assert(packed & 0x8000);
  2546. // opaque 554 or 555
  2547. r = (packed >> 10) & 31;
  2548. g = (packed >> 5) & 31;
  2549. b = packed & 31;
  2550. b |= (b >> 4);
  2551. return r + g + b;
  2552. }
  2553. inline uint32_t get_opaque_endpoint_l1() const
  2554. {
  2555. uint32_t packed = m_endpoints >> 16;
  2556. uint32_t r, g, b;
  2557. assert(packed & 0x8000);
  2558. // opaque 554 or 555
  2559. r = (packed >> 10) & 31;
  2560. g = (packed >> 5) & 31;
  2561. b = packed & 31;
  2562. return r + g + b;
  2563. }
  2564. static uint32_t get_component_precision_in_bits(uint32_t c, uint32_t endpoint_index, bool opaque_endpoint)
  2565. {
  2566. static const uint32_t s_comp_prec[4][4] =
  2567. {
  2568. // R0 G0 B0 A0 R1 G1 B1 A1
  2569. { 4, 4, 3, 3 },{ 4, 4, 4, 3 }, // transparent endpoint
  2570. { 5, 5, 4, 0 },{ 5, 5, 5, 0 } // opaque endpoint
  2571. };
  2572. return s_comp_prec[basisu::open_range_check(endpoint_index, 2U) + (opaque_endpoint * 2)][basisu::open_range_check(c, 4U)];
  2573. }
  2574. static color32 get_color_precision_in_bits(uint32_t endpoint_index, bool opaque_endpoint)
  2575. {
  2576. static const color32 s_color_prec[4] =
  2577. {
  2578. color32(4, 4, 3, 3), color32(4, 4, 4, 3), // transparent endpoint
  2579. color32(5, 5, 4, 0), color32(5, 5, 5, 0) // opaque endpoint
  2580. };
  2581. return s_color_prec[basisu::open_range_check(endpoint_index, 2U) + (opaque_endpoint * 2)];
  2582. }
  2583. inline void set_opaque_endpoint_floor(uint32_t endpoint_index, const color32& c)
  2584. {
  2585. assert(endpoint_index < 2);
  2586. const uint32_t m = m_endpoints & 1;
  2587. uint32_t r = g_pvrtc_5_floor[c[0]], g = g_pvrtc_5_floor[c[1]], b = c[2];
  2588. if (!endpoint_index)
  2589. b = g_pvrtc_4_floor[b] << 1;
  2590. else
  2591. b = g_pvrtc_5_floor[b];
  2592. // rgba=555 here
  2593. assert((r < 32) && (g < 32) && (b < 32));
  2594. // 1RRRRRGGGGGBBBBM
  2595. // 1RRRRRGGGGGBBBBB
  2596. // opaque 554 or 555
  2597. uint32_t packed = 0x8000 | (r << 10) | (g << 5) | b;
  2598. if (!endpoint_index)
  2599. packed = (packed & ~1) | m;
  2600. assert(packed <= 0xFFFF);
  2601. if (endpoint_index)
  2602. m_endpoints = (m_endpoints & 0xFFFFU) | (packed << 16);
  2603. else
  2604. m_endpoints = (m_endpoints & 0xFFFF0000U) | packed;
  2605. }
  2606. inline void set_opaque_endpoint_ceil(uint32_t endpoint_index, const color32& c)
  2607. {
  2608. assert(endpoint_index < 2);
  2609. const uint32_t m = m_endpoints & 1;
  2610. uint32_t r = g_pvrtc_5_ceil[c[0]], g = g_pvrtc_5_ceil[c[1]], b = c[2];
  2611. if (!endpoint_index)
  2612. b = g_pvrtc_4_ceil[b] << 1;
  2613. else
  2614. b = g_pvrtc_5_ceil[b];
  2615. // rgba=555 here
  2616. assert((r < 32) && (g < 32) && (b < 32));
  2617. // 1RRRRRGGGGGBBBBM
  2618. // 1RRRRRGGGGGBBBBB
  2619. // opaque 554 or 555
  2620. uint32_t packed = 0x8000 | (r << 10) | (g << 5) | b;
  2621. if (!endpoint_index)
  2622. packed |= m;
  2623. assert(packed <= 0xFFFF);
  2624. if (endpoint_index)
  2625. m_endpoints = (m_endpoints & 0xFFFFU) | (packed << 16);
  2626. else
  2627. m_endpoints = (m_endpoints & 0xFFFF0000U) | packed;
  2628. }
  2629. // opaque endpoints: 554 or 555
  2630. // transparent endpoints: 3443 or 3444
  2631. inline void set_endpoint_raw(uint32_t endpoint_index, const color32& c, bool opaque_endpoint)
  2632. {
  2633. assert(endpoint_index < 2);
  2634. const uint32_t m = m_endpoints & 1;
  2635. uint32_t r = c[0], g = c[1], b = c[2], a = c[3];
  2636. uint32_t packed;
  2637. if (opaque_endpoint)
  2638. {
  2639. if (!endpoint_index)
  2640. {
  2641. // 554
  2642. // 1RRRRRGGGGGBBBBM
  2643. assert((r < 32) && (g < 32) && (b < 16));
  2644. packed = 0x8000 | (r << 10) | (g << 5) | (b << 1) | m;
  2645. }
  2646. else
  2647. {
  2648. // 555
  2649. // 1RRRRRGGGGGBBBBB
  2650. assert((r < 32) && (g < 32) && (b < 32));
  2651. packed = 0x8000 | (r << 10) | (g << 5) | b;
  2652. }
  2653. }
  2654. else
  2655. {
  2656. if (!endpoint_index)
  2657. {
  2658. // 3443
  2659. // 0AAA RRRR GGGG BBBM
  2660. assert((r < 16) && (g < 16) && (b < 8) && (a < 8));
  2661. packed = (a << 12) | (r << 8) | (g << 4) | (b << 1) | m;
  2662. }
  2663. else
  2664. {
  2665. // 3444
  2666. // 0AAA RRRR GGGG BBBB
  2667. assert((r < 16) && (g < 16) && (b < 16) && (a < 8));
  2668. packed = (a << 12) | (r << 8) | (g << 4) | b;
  2669. }
  2670. }
  2671. assert(packed <= 0xFFFF);
  2672. if (endpoint_index)
  2673. m_endpoints = (m_endpoints & 0xFFFFU) | (packed << 16);
  2674. else
  2675. m_endpoints = (m_endpoints & 0xFFFF0000U) | packed;
  2676. }
  2677. inline void set_endpoint_floor(uint32_t endpoint_index, const color32& c)
  2678. {
  2679. assert(endpoint_index < 2);
  2680. int a = g_pvrtc_alpha_floor[c.a];
  2681. if (a == 8)
  2682. {
  2683. // 554 or 555
  2684. uint32_t r = g_pvrtc_5_floor[c[0]], g = g_pvrtc_5_floor[c[1]], b = c[2];
  2685. if (!endpoint_index)
  2686. b = g_pvrtc_4_floor[b];
  2687. else
  2688. b = g_pvrtc_5_floor[b];
  2689. set_endpoint_raw(endpoint_index, color32(r, g, b, a), true);
  2690. }
  2691. else
  2692. {
  2693. // 4433 or 4443
  2694. uint32_t r = g_pvrtc_4_floor[c[0]], g = g_pvrtc_4_floor[c[1]], b = c[2];
  2695. if (!endpoint_index)
  2696. b = g_pvrtc_3_floor[b];
  2697. else
  2698. b = g_pvrtc_4_floor[b];
  2699. set_endpoint_raw(endpoint_index, color32(r, g, b, a), false);
  2700. }
  2701. }
  2702. inline void set_endpoint_ceil(uint32_t endpoint_index, const color32& c)
  2703. {
  2704. assert(endpoint_index < 2);
  2705. int a = g_pvrtc_alpha_ceil[c.a];
  2706. if (a == 8)
  2707. {
  2708. // 554 or 555
  2709. uint32_t r = g_pvrtc_5_ceil[c[0]], g = g_pvrtc_5_ceil[c[1]], b = c[2];
  2710. if (!endpoint_index)
  2711. b = g_pvrtc_4_ceil[b];
  2712. else
  2713. b = g_pvrtc_5_ceil[b];
  2714. set_endpoint_raw(endpoint_index, color32(r, g, b, a), true);
  2715. }
  2716. else
  2717. {
  2718. // 4433 or 4443
  2719. uint32_t r = g_pvrtc_4_ceil[c[0]], g = g_pvrtc_4_ceil[c[1]], b = c[2];
  2720. if (!endpoint_index)
  2721. b = g_pvrtc_3_ceil[b];
  2722. else
  2723. b = g_pvrtc_4_ceil[b];
  2724. set_endpoint_raw(endpoint_index, color32(r, g, b, a), false);
  2725. }
  2726. }
  2727. inline uint32_t get_modulation(uint32_t x, uint32_t y) const
  2728. {
  2729. assert((x < 4) && (y < 4));
  2730. return (m_modulation >> ((y * 4 + x) * 2)) & 3;
  2731. }
  2732. // Scaled by 8
  2733. inline const uint32_t* get_scaled_modulation_values(bool block_uses_transparent_modulation) const
  2734. {
  2735. static const uint32_t s_block_scales[2][4] = { { 0, 3, 5, 8 },{ 0, 4, 4, 8 } };
  2736. return s_block_scales[block_uses_transparent_modulation];
  2737. }
  2738. // Scaled by 8
  2739. inline uint32_t get_scaled_modulation(uint32_t x, uint32_t y) const
  2740. {
  2741. return get_scaled_modulation_values(get_block_uses_transparent_modulation())[get_modulation(x, y)];
  2742. }
  2743. inline void set_modulation(uint32_t x, uint32_t y, uint32_t s)
  2744. {
  2745. assert((x < 4) && (y < 4) && (s < 4));
  2746. uint32_t n = (y * 4 + x) * 2;
  2747. m_modulation = (m_modulation & (~(3 << n))) | (s << n);
  2748. assert(get_modulation(x, y) == s);
  2749. }
  2750. // Assumes modulation was initialized to 0
  2751. inline void set_modulation_fast(uint32_t x, uint32_t y, uint32_t s)
  2752. {
  2753. assert((x < 4) && (y < 4) && (s < 4));
  2754. uint32_t n = (y * 4 + x) * 2;
  2755. m_modulation |= (s << n);
  2756. assert(get_modulation(x, y) == s);
  2757. }
  2758. };
  2759. #if 0
  2760. static const uint8_t g_pvrtc_bilinear_weights[16][4] =
  2761. {
  2762. { 4, 4, 4, 4 }, { 2, 6, 2, 6 }, { 8, 0, 8, 0 }, { 6, 2, 6, 2 },
  2763. { 2, 2, 6, 6 }, { 1, 3, 3, 9 }, { 4, 0, 12, 0 }, { 3, 1, 9, 3 },
  2764. { 8, 8, 0, 0 }, { 4, 12, 0, 0 }, { 16, 0, 0, 0 }, { 12, 4, 0, 0 },
  2765. { 6, 6, 2, 2 }, { 3, 9, 1, 3 }, { 12, 0, 4, 0 }, { 9, 3, 3, 1 },
  2766. };
  2767. #endif
  2768. struct pvrtc1_temp_block
  2769. {
  2770. decoder_etc_block m_etc1_block;
  2771. uint32_t m_pvrtc_endpoints;
  2772. };
  2773. static inline uint32_t get_opaque_endpoint_l0(uint32_t endpoints)
  2774. {
  2775. uint32_t packed = endpoints;
  2776. uint32_t r, g, b;
  2777. assert(packed & 0x8000);
  2778. r = (packed >> 10) & 31;
  2779. g = (packed >> 5) & 31;
  2780. b = packed & 30;
  2781. b |= (b >> 4);
  2782. return r + g + b;
  2783. }
  2784. static inline uint32_t get_opaque_endpoint_l1(uint32_t endpoints)
  2785. {
  2786. uint32_t packed = endpoints >> 16;
  2787. uint32_t r, g, b;
  2788. assert(packed & 0x8000);
  2789. r = (packed >> 10) & 31;
  2790. g = (packed >> 5) & 31;
  2791. b = packed & 31;
  2792. return r + g + b;
  2793. }
  2794. static color32 get_endpoint_8888(uint32_t endpoints, uint32_t endpoint_index)
  2795. {
  2796. assert(endpoint_index < 2);
  2797. static const uint32_t s_endpoint_mask[2] = { 0xFFFE, 0xFFFF };
  2798. uint32_t packed = (endpoints >> (basisu::open_range_check(endpoint_index, 2U) ? 16 : 0)) & s_endpoint_mask[endpoint_index];
  2799. uint32_t r, g, b, a;
  2800. if (packed & 0x8000)
  2801. {
  2802. // opaque 554 or 555
  2803. // 1RRRRRGGGGGBBBBM
  2804. // 1RRRRRGGGGGBBBBB
  2805. r = (packed >> 10) & 31;
  2806. g = (packed >> 5) & 31;
  2807. b = packed & 31;
  2808. r = g_pvrtc_5[r];
  2809. g = g_pvrtc_5[g];
  2810. if (!endpoint_index)
  2811. b = g_pvrtc_4[b >> 1];
  2812. else
  2813. b = g_pvrtc_5[b];
  2814. a = 255;
  2815. }
  2816. else
  2817. {
  2818. // translucent 4433 or 4443
  2819. // 0AAA RRRR GGGG BBBM
  2820. // 0AAA RRRR GGGG BBBB
  2821. r = (packed >> 8) & 0xF;
  2822. g = (packed >> 4) & 0xF;
  2823. b = packed & 0xF;
  2824. a = (packed >> 12) & 7;
  2825. r = g_pvrtc_4[r];
  2826. g = g_pvrtc_4[g];
  2827. if (!endpoint_index)
  2828. b = g_pvrtc_3[b >> 1];
  2829. else
  2830. b = g_pvrtc_4[b];
  2831. a = g_pvrtc_alpha[a];
  2832. }
  2833. return color32(r, g, b, a);
  2834. }
  2835. static uint32_t get_endpoint_l8(uint32_t endpoints, uint32_t endpoint_index)
  2836. {
  2837. color32 c(get_endpoint_8888(endpoints, endpoint_index));
  2838. return c.r + c.g + c.b + c.a;
  2839. }
  2840. #endif
  2841. #if BASISD_SUPPORT_PVRTC1
  2842. // TODO: Support decoding a non-pow2 ETC1S texture into the next larger pow2 PVRTC texture.
  2843. static void fixup_pvrtc1_4_modulation_rgb(const decoder_etc_block* pETC_Blocks, const uint32_t* pPVRTC_endpoints, void* pDst_blocks, uint32_t num_blocks_x, uint32_t num_blocks_y)
  2844. {
  2845. const uint32_t x_mask = num_blocks_x - 1;
  2846. const uint32_t y_mask = num_blocks_y - 1;
  2847. const uint32_t x_bits = basisu::total_bits(x_mask);
  2848. const uint32_t y_bits = basisu::total_bits(y_mask);
  2849. const uint32_t min_bits = basisu::minimum(x_bits, y_bits);
  2850. //const uint32_t max_bits = basisu::maximum(x_bits, y_bits);
  2851. const uint32_t swizzle_mask = (1 << (min_bits * 2)) - 1;
  2852. uint32_t block_index = 0;
  2853. // really 3x3
  2854. int e0[4][4], e1[4][4];
  2855. for (int y = 0; y < static_cast<int>(num_blocks_y); y++)
  2856. {
  2857. const uint32_t* pE_rows[3];
  2858. for (int ey = 0; ey < 3; ey++)
  2859. {
  2860. int by = y + ey - 1;
  2861. const uint32_t* pE = &pPVRTC_endpoints[(by & y_mask) * num_blocks_x];
  2862. pE_rows[ey] = pE;
  2863. for (int ex = 0; ex < 3; ex++)
  2864. {
  2865. int bx = 0 + ex - 1;
  2866. const uint32_t e = pE[bx & x_mask];
  2867. e0[ex][ey] = (get_opaque_endpoint_l0(e) * 255) / 31;
  2868. e1[ex][ey] = (get_opaque_endpoint_l1(e) * 255) / 31;
  2869. }
  2870. }
  2871. const uint32_t y_swizzle = (g_pvrtc_swizzle_table[y >> 8] << 16) | g_pvrtc_swizzle_table[y & 0xFF];
  2872. for (int x = 0; x < static_cast<int>(num_blocks_x); x++, block_index++)
  2873. {
  2874. const decoder_etc_block& src_block = pETC_Blocks[block_index];
  2875. const uint32_t x_swizzle = (g_pvrtc_swizzle_table[x >> 8] << 17) | (g_pvrtc_swizzle_table[x & 0xFF] << 1);
  2876. uint32_t swizzled = x_swizzle | y_swizzle;
  2877. if (num_blocks_x != num_blocks_y)
  2878. {
  2879. swizzled &= swizzle_mask;
  2880. if (num_blocks_x > num_blocks_y)
  2881. swizzled |= ((x >> min_bits) << (min_bits * 2));
  2882. else
  2883. swizzled |= ((y >> min_bits) << (min_bits * 2));
  2884. }
  2885. pvrtc4_block* pDst_block = static_cast<pvrtc4_block*>(pDst_blocks) + swizzled;
  2886. pDst_block->m_endpoints = pPVRTC_endpoints[block_index];
  2887. uint32_t base_r = g_etc_5_to_8[src_block.m_differential.m_red1];
  2888. uint32_t base_g = g_etc_5_to_8[src_block.m_differential.m_green1];
  2889. uint32_t base_b = g_etc_5_to_8[src_block.m_differential.m_blue1];
  2890. const int* pInten_table48 = g_etc1_inten_tables48[src_block.m_differential.m_cw1];
  2891. int by = (base_r + base_g + base_b) * 16;
  2892. int block_colors_y_x16[4];
  2893. block_colors_y_x16[0] = by + pInten_table48[2];
  2894. block_colors_y_x16[1] = by + pInten_table48[3];
  2895. block_colors_y_x16[2] = by + pInten_table48[1];
  2896. block_colors_y_x16[3] = by + pInten_table48[0];
  2897. {
  2898. const uint32_t ex = 2;
  2899. int bx = x + ex - 1;
  2900. bx &= x_mask;
  2901. #define DO_ROW(ey) \
  2902. { \
  2903. const uint32_t e = pE_rows[ey][bx]; \
  2904. e0[ex][ey] = (get_opaque_endpoint_l0(e) * 255) / 31; \
  2905. e1[ex][ey] = (get_opaque_endpoint_l1(e) * 255) / 31; \
  2906. }
  2907. DO_ROW(0);
  2908. DO_ROW(1);
  2909. DO_ROW(2);
  2910. #undef DO_ROW
  2911. }
  2912. uint32_t mod = 0;
  2913. uint32_t lookup_x[4];
  2914. #define DO_LOOKUP(lx) { \
  2915. const uint32_t byte_ofs = 7 - (((lx) * 4) >> 3); \
  2916. const uint32_t lsb_bits = src_block.m_bytes[byte_ofs] >> (((lx) & 1) * 4); \
  2917. const uint32_t msb_bits = src_block.m_bytes[byte_ofs - 2] >> (((lx) & 1) * 4); \
  2918. lookup_x[lx] = (lsb_bits & 0xF) | ((msb_bits & 0xF) << 4); }
  2919. DO_LOOKUP(0);
  2920. DO_LOOKUP(1);
  2921. DO_LOOKUP(2);
  2922. DO_LOOKUP(3);
  2923. #undef DO_LOOKUP
  2924. #define DO_PIX(lx, ly, w0, w1, w2, w3) \
  2925. { \
  2926. int ca_l = a0 * w0 + a1 * w1 + a2 * w2 + a3 * w3; \
  2927. int cb_l = b0 * w0 + b1 * w1 + b2 * w2 + b3 * w3; \
  2928. int cl = block_colors_y_x16[g_etc1_x_selector_unpack[ly][lookup_x[lx]]]; \
  2929. int dl = cb_l - ca_l; \
  2930. int vl = cl - ca_l; \
  2931. int p = vl * 16; \
  2932. if (ca_l > cb_l) { p = -p; dl = -dl; } \
  2933. uint32_t m = 0; \
  2934. if (p > 3 * dl) m = (uint32_t)(1 << ((ly) * 8 + (lx) * 2)); \
  2935. if (p > 8 * dl) m = (uint32_t)(2 << ((ly) * 8 + (lx) * 2)); \
  2936. if (p > 13 * dl) m = (uint32_t)(3 << ((ly) * 8 + (lx) * 2)); \
  2937. mod |= m; \
  2938. }
  2939. {
  2940. const uint32_t ex = 0, ey = 0;
  2941. const int a0 = e0[ex][ey], a1 = e0[ex + 1][ey], a2 = e0[ex][ey + 1], a3 = e0[ex + 1][ey + 1];
  2942. const int b0 = e1[ex][ey], b1 = e1[ex + 1][ey], b2 = e1[ex][ey + 1], b3 = e1[ex + 1][ey + 1];
  2943. DO_PIX(0, 0, 4, 4, 4, 4);
  2944. DO_PIX(1, 0, 2, 6, 2, 6);
  2945. DO_PIX(0, 1, 2, 2, 6, 6);
  2946. DO_PIX(1, 1, 1, 3, 3, 9);
  2947. }
  2948. {
  2949. const uint32_t ex = 1, ey = 0;
  2950. const int a0 = e0[ex][ey], a1 = e0[ex + 1][ey], a2 = e0[ex][ey + 1], a3 = e0[ex + 1][ey + 1];
  2951. const int b0 = e1[ex][ey], b1 = e1[ex + 1][ey], b2 = e1[ex][ey + 1], b3 = e1[ex + 1][ey + 1];
  2952. DO_PIX(2, 0, 8, 0, 8, 0);
  2953. DO_PIX(3, 0, 6, 2, 6, 2);
  2954. DO_PIX(2, 1, 4, 0, 12, 0);
  2955. DO_PIX(3, 1, 3, 1, 9, 3);
  2956. }
  2957. {
  2958. const uint32_t ex = 0, ey = 1;
  2959. const int a0 = e0[ex][ey], a1 = e0[ex + 1][ey], a2 = e0[ex][ey + 1], a3 = e0[ex + 1][ey + 1];
  2960. const int b0 = e1[ex][ey], b1 = e1[ex + 1][ey], b2 = e1[ex][ey + 1], b3 = e1[ex + 1][ey + 1];
  2961. DO_PIX(0, 2, 8, 8, 0, 0);
  2962. DO_PIX(1, 2, 4, 12, 0, 0);
  2963. DO_PIX(0, 3, 6, 6, 2, 2);
  2964. DO_PIX(1, 3, 3, 9, 1, 3);
  2965. }
  2966. {
  2967. const uint32_t ex = 1, ey = 1;
  2968. const int a0 = e0[ex][ey], a1 = e0[ex + 1][ey], a2 = e0[ex][ey + 1], a3 = e0[ex + 1][ey + 1];
  2969. const int b0 = e1[ex][ey], b1 = e1[ex + 1][ey], b2 = e1[ex][ey + 1], b3 = e1[ex + 1][ey + 1];
  2970. DO_PIX(2, 2, 16, 0, 0, 0);
  2971. DO_PIX(3, 2, 12, 4, 0, 0);
  2972. DO_PIX(2, 3, 12, 0, 4, 0);
  2973. DO_PIX(3, 3, 9, 3, 3, 1);
  2974. }
  2975. #undef DO_PIX
  2976. pDst_block->m_modulation = mod;
  2977. e0[0][0] = e0[1][0]; e0[1][0] = e0[2][0];
  2978. e0[0][1] = e0[1][1]; e0[1][1] = e0[2][1];
  2979. e0[0][2] = e0[1][2]; e0[1][2] = e0[2][2];
  2980. e1[0][0] = e1[1][0]; e1[1][0] = e1[2][0];
  2981. e1[0][1] = e1[1][1]; e1[1][1] = e1[2][1];
  2982. e1[0][2] = e1[1][2]; e1[1][2] = e1[2][2];
  2983. } // x
  2984. } // y
  2985. }
  2986. static void fixup_pvrtc1_4_modulation_rgba(
  2987. const decoder_etc_block* pETC_Blocks,
  2988. const uint32_t* pPVRTC_endpoints,
  2989. void* pDst_blocks, uint32_t num_blocks_x, uint32_t num_blocks_y, void *pAlpha_blocks,
  2990. const endpoint* pEndpoints, const selector* pSelectors)
  2991. {
  2992. const uint32_t x_mask = num_blocks_x - 1;
  2993. const uint32_t y_mask = num_blocks_y - 1;
  2994. const uint32_t x_bits = basisu::total_bits(x_mask);
  2995. const uint32_t y_bits = basisu::total_bits(y_mask);
  2996. const uint32_t min_bits = basisu::minimum(x_bits, y_bits);
  2997. //const uint32_t max_bits = basisu::maximum(x_bits, y_bits);
  2998. const uint32_t swizzle_mask = (1 << (min_bits * 2)) - 1;
  2999. uint32_t block_index = 0;
  3000. // really 3x3
  3001. int e0[4][4], e1[4][4];
  3002. for (int y = 0; y < static_cast<int>(num_blocks_y); y++)
  3003. {
  3004. const uint32_t* pE_rows[3];
  3005. for (int ey = 0; ey < 3; ey++)
  3006. {
  3007. int by = y + ey - 1;
  3008. const uint32_t* pE = &pPVRTC_endpoints[(by & y_mask) * num_blocks_x];
  3009. pE_rows[ey] = pE;
  3010. for (int ex = 0; ex < 3; ex++)
  3011. {
  3012. int bx = 0 + ex - 1;
  3013. const uint32_t e = pE[bx & x_mask];
  3014. e0[ex][ey] = get_endpoint_l8(e, 0);
  3015. e1[ex][ey] = get_endpoint_l8(e, 1);
  3016. }
  3017. }
  3018. const uint32_t y_swizzle = (g_pvrtc_swizzle_table[y >> 8] << 16) | g_pvrtc_swizzle_table[y & 0xFF];
  3019. for (int x = 0; x < static_cast<int>(num_blocks_x); x++, block_index++)
  3020. {
  3021. const decoder_etc_block& src_block = pETC_Blocks[block_index];
  3022. const uint16_t* pSrc_alpha_block = reinterpret_cast<const uint16_t*>(static_cast<const uint32_t*>(pAlpha_blocks) + x + (y * num_blocks_x));
  3023. const endpoint* pAlpha_endpoints = &pEndpoints[pSrc_alpha_block[0]];
  3024. const selector* pAlpha_selectors = &pSelectors[pSrc_alpha_block[1]];
  3025. const uint32_t x_swizzle = (g_pvrtc_swizzle_table[x >> 8] << 17) | (g_pvrtc_swizzle_table[x & 0xFF] << 1);
  3026. uint32_t swizzled = x_swizzle | y_swizzle;
  3027. if (num_blocks_x != num_blocks_y)
  3028. {
  3029. swizzled &= swizzle_mask;
  3030. if (num_blocks_x > num_blocks_y)
  3031. swizzled |= ((x >> min_bits) << (min_bits * 2));
  3032. else
  3033. swizzled |= ((y >> min_bits) << (min_bits * 2));
  3034. }
  3035. pvrtc4_block* pDst_block = static_cast<pvrtc4_block*>(pDst_blocks) + swizzled;
  3036. pDst_block->m_endpoints = pPVRTC_endpoints[block_index];
  3037. uint32_t base_r = g_etc_5_to_8[src_block.m_differential.m_red1];
  3038. uint32_t base_g = g_etc_5_to_8[src_block.m_differential.m_green1];
  3039. uint32_t base_b = g_etc_5_to_8[src_block.m_differential.m_blue1];
  3040. const int* pInten_table48 = g_etc1_inten_tables48[src_block.m_differential.m_cw1];
  3041. int by = (base_r + base_g + base_b) * 16;
  3042. int block_colors_y_x16[4];
  3043. block_colors_y_x16[0] = basisu::clamp<int>(by + pInten_table48[0], 0, 48 * 255);
  3044. block_colors_y_x16[1] = basisu::clamp<int>(by + pInten_table48[1], 0, 48 * 255);
  3045. block_colors_y_x16[2] = basisu::clamp<int>(by + pInten_table48[2], 0, 48 * 255);
  3046. block_colors_y_x16[3] = basisu::clamp<int>(by + pInten_table48[3], 0, 48 * 255);
  3047. uint32_t alpha_base_g = g_etc_5_to_8[pAlpha_endpoints->m_color5.g] * 16;
  3048. const int* pInten_table16 = g_etc1_inten_tables16[pAlpha_endpoints->m_inten5];
  3049. int alpha_block_colors_x16[4];
  3050. alpha_block_colors_x16[0] = basisu::clamp<int>(alpha_base_g + pInten_table16[0], 0, 16 * 255);
  3051. alpha_block_colors_x16[1] = basisu::clamp<int>(alpha_base_g + pInten_table16[1], 0, 16 * 255);
  3052. alpha_block_colors_x16[2] = basisu::clamp<int>(alpha_base_g + pInten_table16[2], 0, 16 * 255);
  3053. alpha_block_colors_x16[3] = basisu::clamp<int>(alpha_base_g + pInten_table16[3], 0, 16 * 255);
  3054. // clamp((base_r + base_g + base_b) * 16 + color_inten[s] * 48) + clamp(alpha_base_g * 16 + alpha_inten[as] * 16)
  3055. {
  3056. const uint32_t ex = 2;
  3057. int bx = x + ex - 1;
  3058. bx &= x_mask;
  3059. #define DO_ROW(ey) \
  3060. { \
  3061. const uint32_t e = pE_rows[ey][bx]; \
  3062. e0[ex][ey] = get_endpoint_l8(e, 0); \
  3063. e1[ex][ey] = get_endpoint_l8(e, 1); \
  3064. }
  3065. DO_ROW(0);
  3066. DO_ROW(1);
  3067. DO_ROW(2);
  3068. #undef DO_ROW
  3069. }
  3070. uint32_t mod = 0;
  3071. #define DO_PIX(lx, ly, w0, w1, w2, w3) \
  3072. { \
  3073. int ca_l = a0 * w0 + a1 * w1 + a2 * w2 + a3 * w3; \
  3074. int cb_l = b0 * w0 + b1 * w1 + b2 * w2 + b3 * w3; \
  3075. int cl = block_colors_y_x16[(src_block.m_bytes[4 + ly] >> (lx * 2)) & 3] + alpha_block_colors_x16[(pAlpha_selectors->m_selectors[ly] >> (lx * 2)) & 3]; \
  3076. int dl = cb_l - ca_l; \
  3077. int vl = cl - ca_l; \
  3078. int p = vl * 16; \
  3079. if (ca_l > cb_l) { p = -p; dl = -dl; } \
  3080. uint32_t m = 0; \
  3081. if (p > 3 * dl) m = (uint32_t)(1 << ((ly) * 8 + (lx) * 2)); \
  3082. if (p > 8 * dl) m = (uint32_t)(2 << ((ly) * 8 + (lx) * 2)); \
  3083. if (p > 13 * dl) m = (uint32_t)(3 << ((ly) * 8 + (lx) * 2)); \
  3084. mod |= m; \
  3085. }
  3086. {
  3087. const uint32_t ex = 0, ey = 0;
  3088. const int a0 = e0[ex][ey], a1 = e0[ex + 1][ey], a2 = e0[ex][ey + 1], a3 = e0[ex + 1][ey + 1];
  3089. const int b0 = e1[ex][ey], b1 = e1[ex + 1][ey], b2 = e1[ex][ey + 1], b3 = e1[ex + 1][ey + 1];
  3090. DO_PIX(0, 0, 4, 4, 4, 4);
  3091. DO_PIX(1, 0, 2, 6, 2, 6);
  3092. DO_PIX(0, 1, 2, 2, 6, 6);
  3093. DO_PIX(1, 1, 1, 3, 3, 9);
  3094. }
  3095. {
  3096. const uint32_t ex = 1, ey = 0;
  3097. const int a0 = e0[ex][ey], a1 = e0[ex + 1][ey], a2 = e0[ex][ey + 1], a3 = e0[ex + 1][ey + 1];
  3098. const int b0 = e1[ex][ey], b1 = e1[ex + 1][ey], b2 = e1[ex][ey + 1], b3 = e1[ex + 1][ey + 1];
  3099. DO_PIX(2, 0, 8, 0, 8, 0);
  3100. DO_PIX(3, 0, 6, 2, 6, 2);
  3101. DO_PIX(2, 1, 4, 0, 12, 0);
  3102. DO_PIX(3, 1, 3, 1, 9, 3);
  3103. }
  3104. {
  3105. const uint32_t ex = 0, ey = 1;
  3106. const int a0 = e0[ex][ey], a1 = e0[ex + 1][ey], a2 = e0[ex][ey + 1], a3 = e0[ex + 1][ey + 1];
  3107. const int b0 = e1[ex][ey], b1 = e1[ex + 1][ey], b2 = e1[ex][ey + 1], b3 = e1[ex + 1][ey + 1];
  3108. DO_PIX(0, 2, 8, 8, 0, 0);
  3109. DO_PIX(1, 2, 4, 12, 0, 0);
  3110. DO_PIX(0, 3, 6, 6, 2, 2);
  3111. DO_PIX(1, 3, 3, 9, 1, 3);
  3112. }
  3113. {
  3114. const uint32_t ex = 1, ey = 1;
  3115. const int a0 = e0[ex][ey], a1 = e0[ex + 1][ey], a2 = e0[ex][ey + 1], a3 = e0[ex + 1][ey + 1];
  3116. const int b0 = e1[ex][ey], b1 = e1[ex + 1][ey], b2 = e1[ex][ey + 1], b3 = e1[ex + 1][ey + 1];
  3117. DO_PIX(2, 2, 16, 0, 0, 0);
  3118. DO_PIX(3, 2, 12, 4, 0, 0);
  3119. DO_PIX(2, 3, 12, 0, 4, 0);
  3120. DO_PIX(3, 3, 9, 3, 3, 1);
  3121. }
  3122. #undef DO_PIX
  3123. pDst_block->m_modulation = mod;
  3124. e0[0][0] = e0[1][0]; e0[1][0] = e0[2][0];
  3125. e0[0][1] = e0[1][1]; e0[1][1] = e0[2][1];
  3126. e0[0][2] = e0[1][2]; e0[1][2] = e0[2][2];
  3127. e1[0][0] = e1[1][0]; e1[1][0] = e1[2][0];
  3128. e1[0][1] = e1[1][1]; e1[1][1] = e1[2][1];
  3129. e1[0][2] = e1[1][2]; e1[1][2] = e1[2][2];
  3130. } // x
  3131. } // y
  3132. }
  3133. #endif // BASISD_SUPPORT_PVRTC1
  3134. #if BASISD_SUPPORT_BC7_MODE5
  3135. static dxt_selector_range g_etc1_to_bc7_m5_selector_ranges[] =
  3136. {
  3137. { 0, 3 },
  3138. { 1, 3 },
  3139. { 0, 2 },
  3140. { 1, 2 },
  3141. { 2, 3 },
  3142. { 0, 1 },
  3143. };
  3144. const uint32_t NUM_ETC1_TO_BC7_M5_SELECTOR_RANGES = sizeof(g_etc1_to_bc7_m5_selector_ranges) / sizeof(g_etc1_to_bc7_m5_selector_ranges[0]);
  3145. static uint32_t g_etc1_to_bc7_m5_selector_range_index[4][4];
  3146. const uint32_t NUM_ETC1_TO_BC7_M5_SELECTOR_MAPPINGS = 10;
  3147. static const uint8_t g_etc1_to_bc7_m5_selector_mappings[NUM_ETC1_TO_BC7_M5_SELECTOR_MAPPINGS][4] =
  3148. {
  3149. { 0, 0, 1, 1 },
  3150. { 0, 0, 1, 2 },
  3151. { 0, 0, 1, 3 },
  3152. { 0, 0, 2, 3 },
  3153. { 0, 1, 1, 1 },
  3154. { 0, 1, 2, 2 },
  3155. { 0, 1, 2, 3 },
  3156. { 0, 2, 3, 3 },
  3157. { 1, 2, 2, 2 },
  3158. { 1, 2, 3, 3 },
  3159. };
  3160. struct etc1_to_bc7_m5_solution
  3161. {
  3162. uint8_t m_lo;
  3163. uint8_t m_hi;
  3164. uint16_t m_err;
  3165. };
  3166. static const etc1_to_bc7_m5_solution g_etc1_to_bc7_m5_color[32 * 8 * NUM_ETC1_TO_BC7_M5_SELECTOR_MAPPINGS * NUM_ETC1_TO_BC7_M5_SELECTOR_RANGES] = {
  3167. #include "basisu_transcoder_tables_bc7_m5_color.inc"
  3168. };
  3169. static dxt_selector_range g_etc1_to_bc7_m5a_selector_ranges[] =
  3170. {
  3171. { 0, 3 },
  3172. { 1, 3 },
  3173. { 0, 2 },
  3174. { 1, 2 },
  3175. { 2, 3 },
  3176. { 0, 1 }
  3177. };
  3178. const uint32_t NUM_ETC1_TO_BC7_M5A_SELECTOR_RANGES = sizeof(g_etc1_to_bc7_m5a_selector_ranges) / sizeof(g_etc1_to_bc7_m5a_selector_ranges[0]);
  3179. static uint32_t g_etc1_to_bc7_m5a_selector_range_index[4][4];
  3180. struct etc1_g_to_bc7_m5a_conversion
  3181. {
  3182. uint8_t m_lo, m_hi;
  3183. uint8_t m_trans;
  3184. };
  3185. static etc1_g_to_bc7_m5a_conversion g_etc1_g_to_bc7_m5a[8 * 32 * NUM_ETC1_TO_BC7_M5A_SELECTOR_RANGES] =
  3186. {
  3187. #include "basisu_transcoder_tables_bc7_m5_alpha.inc"
  3188. };
  3189. static inline uint32_t set_block_bits(uint8_t* pBytes, uint32_t val, uint32_t num_bits, uint32_t cur_ofs)
  3190. {
  3191. assert(num_bits < 32);
  3192. assert(val < (1ULL << num_bits));
  3193. uint32_t mask = static_cast<uint32_t>((1ULL << num_bits) - 1);
  3194. while (num_bits)
  3195. {
  3196. const uint32_t n = basisu::minimum<uint32_t>(8 - (cur_ofs & 7), num_bits);
  3197. pBytes[cur_ofs >> 3] &= ~static_cast<uint8_t>(mask << (cur_ofs & 7));
  3198. pBytes[cur_ofs >> 3] |= static_cast<uint8_t>(val << (cur_ofs & 7));
  3199. val >>= n;
  3200. mask >>= n;
  3201. num_bits -= n;
  3202. cur_ofs += n;
  3203. }
  3204. return cur_ofs;
  3205. }
  3206. struct bc7_mode_5
  3207. {
  3208. union
  3209. {
  3210. struct
  3211. {
  3212. uint64_t m_mode : 6;
  3213. uint64_t m_rot : 2;
  3214. uint64_t m_r0 : 7;
  3215. uint64_t m_r1 : 7;
  3216. uint64_t m_g0 : 7;
  3217. uint64_t m_g1 : 7;
  3218. uint64_t m_b0 : 7;
  3219. uint64_t m_b1 : 7;
  3220. uint64_t m_a0 : 8;
  3221. uint64_t m_a1_0 : 6;
  3222. } m_lo;
  3223. uint64_t m_lo_bits;
  3224. };
  3225. union
  3226. {
  3227. struct
  3228. {
  3229. uint64_t m_a1_1 : 2;
  3230. // bit 2
  3231. uint64_t m_c00 : 1;
  3232. uint64_t m_c10 : 2;
  3233. uint64_t m_c20 : 2;
  3234. uint64_t m_c30 : 2;
  3235. uint64_t m_c01 : 2;
  3236. uint64_t m_c11 : 2;
  3237. uint64_t m_c21 : 2;
  3238. uint64_t m_c31 : 2;
  3239. uint64_t m_c02 : 2;
  3240. uint64_t m_c12 : 2;
  3241. uint64_t m_c22 : 2;
  3242. uint64_t m_c32 : 2;
  3243. uint64_t m_c03 : 2;
  3244. uint64_t m_c13 : 2;
  3245. uint64_t m_c23 : 2;
  3246. uint64_t m_c33 : 2;
  3247. // bit 33
  3248. uint64_t m_a00 : 1;
  3249. uint64_t m_a10 : 2;
  3250. uint64_t m_a20 : 2;
  3251. uint64_t m_a30 : 2;
  3252. uint64_t m_a01 : 2;
  3253. uint64_t m_a11 : 2;
  3254. uint64_t m_a21 : 2;
  3255. uint64_t m_a31 : 2;
  3256. uint64_t m_a02 : 2;
  3257. uint64_t m_a12 : 2;
  3258. uint64_t m_a22 : 2;
  3259. uint64_t m_a32 : 2;
  3260. uint64_t m_a03 : 2;
  3261. uint64_t m_a13 : 2;
  3262. uint64_t m_a23 : 2;
  3263. uint64_t m_a33 : 2;
  3264. } m_hi;
  3265. uint64_t m_hi_bits;
  3266. };
  3267. };
  3268. #if BASISD_WRITE_NEW_BC7_MODE5_TABLES
  3269. static void create_etc1_to_bc7_m5_color_conversion_table()
  3270. {
  3271. FILE* pFile = nullptr;
  3272. fopen_s(&pFile, "basisu_transcoder_tables_bc7_m5_color.inc", "w");
  3273. uint32_t n = 0;
  3274. for (int inten = 0; inten < 8; inten++)
  3275. {
  3276. for (uint32_t g = 0; g < 32; g++)
  3277. {
  3278. color32 block_colors[4];
  3279. decoder_etc_block::get_diff_subblock_colors(block_colors, decoder_etc_block::pack_color5(color32(g, g, g, 255), false), inten);
  3280. for (uint32_t sr = 0; sr < NUM_ETC1_TO_BC7_M5_SELECTOR_RANGES; sr++)
  3281. {
  3282. const uint32_t low_selector = g_etc1_to_bc7_m5_selector_ranges[sr].m_low;
  3283. const uint32_t high_selector = g_etc1_to_bc7_m5_selector_ranges[sr].m_high;
  3284. for (uint32_t m = 0; m < NUM_ETC1_TO_BC7_M5_SELECTOR_MAPPINGS; m++)
  3285. {
  3286. uint32_t best_lo = 0;
  3287. uint32_t best_hi = 0;
  3288. uint64_t best_err = UINT64_MAX;
  3289. for (uint32_t hi = 0; hi <= 127; hi++)
  3290. {
  3291. for (uint32_t lo = 0; lo <= 127; lo++)
  3292. {
  3293. uint32_t colors[4];
  3294. colors[0] = (lo << 1) | (lo >> 6);
  3295. colors[3] = (hi << 1) | (hi >> 6);
  3296. colors[1] = (colors[0] * (64 - 21) + colors[3] * 21 + 32) / 64;
  3297. colors[2] = (colors[0] * (64 - 43) + colors[3] * 43 + 32) / 64;
  3298. uint64_t total_err = 0;
  3299. for (uint32_t s = low_selector; s <= high_selector; s++)
  3300. {
  3301. int err = block_colors[s].g - colors[g_etc1_to_bc7_m5_selector_mappings[m][s]];
  3302. int err_scale = 1;
  3303. // Special case when the intensity table is 7, low_selector is 0, and high_selector is 3. In this extreme case, it's likely the encoder is trying to strongly favor
  3304. // the low/high selectors which are clamping to either 0 or 255.
  3305. if (((inten == 7) && (low_selector == 0) && (high_selector == 3)) && ((s == 0) || (s == 3)))
  3306. err_scale = 5;
  3307. total_err += (err * err) * err_scale;
  3308. }
  3309. if (total_err < best_err)
  3310. {
  3311. best_err = total_err;
  3312. best_lo = lo;
  3313. best_hi = hi;
  3314. }
  3315. }
  3316. }
  3317. best_err = basisu::minimum<uint32_t>(best_err, 0xFFFF);
  3318. fprintf(pFile, "{%u,%u,%u},", best_lo, best_hi, (uint32_t)best_err);
  3319. n++;
  3320. if ((n & 31) == 31)
  3321. fprintf(pFile, "\n");
  3322. } // m
  3323. } // sr
  3324. } // g
  3325. } // inten
  3326. fclose(pFile);
  3327. }
  3328. static void create_etc1_to_bc7_m5_alpha_conversion_table()
  3329. {
  3330. FILE* pFile = nullptr;
  3331. fopen_s(&pFile, "basisu_transcoder_tables_bc7_m5_alpha.inc", "w");
  3332. uint32_t n = 0;
  3333. for (int inten = 0; inten < 8; inten++)
  3334. {
  3335. for (uint32_t g = 0; g < 32; g++)
  3336. {
  3337. color32 block_colors[4];
  3338. decoder_etc_block::get_diff_subblock_colors(block_colors, decoder_etc_block::pack_color5(color32(g, g, g, 255), false), inten);
  3339. for (uint32_t sr = 0; sr < NUM_ETC1_TO_BC7_M5A_SELECTOR_RANGES; sr++)
  3340. {
  3341. const uint32_t low_selector = g_etc1_to_bc7_m5a_selector_ranges[sr].m_low;
  3342. const uint32_t high_selector = g_etc1_to_bc7_m5a_selector_ranges[sr].m_high;
  3343. uint32_t best_lo = 0;
  3344. uint32_t best_hi = 0;
  3345. uint64_t best_err = UINT64_MAX;
  3346. uint32_t best_output_selectors = 0;
  3347. for (uint32_t hi = 0; hi <= 255; hi++)
  3348. {
  3349. for (uint32_t lo = 0; lo <= 255; lo++)
  3350. {
  3351. uint32_t colors[4];
  3352. colors[0] = lo;
  3353. colors[3] = hi;
  3354. colors[1] = (colors[0] * (64 - 21) + colors[3] * 21 + 32) / 64;
  3355. colors[2] = (colors[0] * (64 - 43) + colors[3] * 43 + 32) / 64;
  3356. uint64_t total_err = 0;
  3357. uint32_t output_selectors = 0;
  3358. for (uint32_t s = low_selector; s <= high_selector; s++)
  3359. {
  3360. int best_mapping_err = INT_MAX;
  3361. int best_k = 0;
  3362. for (int k = 0; k < 4; k++)
  3363. {
  3364. int mapping_err = block_colors[s].g - colors[k];
  3365. mapping_err *= mapping_err;
  3366. // Special case when the intensity table is 7, low_selector is 0, and high_selector is 3. In this extreme case, it's likely the encoder is trying to strongly favor
  3367. // the low/high selectors which are clamping to either 0 or 255.
  3368. if (((inten == 7) && (low_selector == 0) && (high_selector == 3)) && ((s == 0) || (s == 3)))
  3369. mapping_err *= 5;
  3370. if (mapping_err < best_mapping_err)
  3371. {
  3372. best_mapping_err = mapping_err;
  3373. best_k = k;
  3374. }
  3375. } // k
  3376. total_err += best_mapping_err;
  3377. output_selectors |= (best_k << (s * 2));
  3378. } // s
  3379. if (total_err < best_err)
  3380. {
  3381. best_err = total_err;
  3382. best_lo = lo;
  3383. best_hi = hi;
  3384. best_output_selectors = output_selectors;
  3385. }
  3386. } // lo
  3387. } // hi
  3388. fprintf(pFile, "{%u,%u,%u},", best_lo, best_hi, best_output_selectors);
  3389. n++;
  3390. if ((n & 31) == 31)
  3391. fprintf(pFile, "\n");
  3392. } // sr
  3393. } // g
  3394. } // inten
  3395. fclose(pFile);
  3396. }
  3397. #endif // BASISD_WRITE_NEW_BC7_MODE5_TABLES
  3398. struct bc7_m5_match_entry
  3399. {
  3400. uint8_t m_hi;
  3401. uint8_t m_lo;
  3402. };
  3403. static bc7_m5_match_entry g_bc7_m5_equals_1[256] =
  3404. {
  3405. {0,0},{1,0},{3,0},{4,0},{6,0},{7,0},{9,0},{10,0},{12,0},{13,0},{15,0},{16,0},{18,0},{20,0},{21,0},{23,0},
  3406. {24,0},{26,0},{27,0},{29,0},{30,0},{32,0},{33,0},{35,0},{36,0},{38,0},{39,0},{41,0},{42,0},{44,0},{45,0},{47,0},
  3407. {48,0},{50,0},{52,0},{53,0},{55,0},{56,0},{58,0},{59,0},{61,0},{62,0},{64,0},{65,0},{66,0},{68,0},{69,0},{71,0},
  3408. {72,0},{74,0},{75,0},{77,0},{78,0},{80,0},{82,0},{83,0},{85,0},{86,0},{88,0},{89,0},{91,0},{92,0},{94,0},{95,0},
  3409. {97,0},{98,0},{100,0},{101,0},{103,0},{104,0},{106,0},{107,0},{109,0},{110,0},{112,0},{114,0},{115,0},{117,0},{118,0},{120,0},
  3410. {121,0},{123,0},{124,0},{126,0},{127,0},{127,1},{126,2},{126,3},{127,3},{127,4},{126,5},{126,6},{127,6},{127,7},{126,8},{126,9},
  3411. {127,9},{127,10},{126,11},{126,12},{127,12},{127,13},{126,14},{125,15},{127,15},{126,16},{126,17},{127,17},{127,18},{126,19},{126,20},{127,20},
  3412. {127,21},{126,22},{126,23},{127,23},{127,24},{126,25},{126,26},{127,26},{127,27},{126,28},{126,29},{127,29},{127,30},{126,31},{126,32},{127,32},
  3413. {127,33},{126,34},{126,35},{127,35},{127,36},{126,37},{126,38},{127,38},{127,39},{126,40},{126,41},{127,41},{127,42},{126,43},{126,44},{127,44},
  3414. {127,45},{126,46},{125,47},{127,47},{126,48},{126,49},{127,49},{127,50},{126,51},{126,52},{127,52},{127,53},{126,54},{126,55},{127,55},{127,56},
  3415. {126,57},{126,58},{127,58},{127,59},{126,60},{126,61},{127,61},{127,62},{126,63},{125,64},{126,64},{126,65},{127,65},{127,66},{126,67},{126,68},
  3416. {127,68},{127,69},{126,70},{126,71},{127,71},{127,72},{126,73},{126,74},{127,74},{127,75},{126,76},{125,77},{127,77},{126,78},{126,79},{127,79},
  3417. {127,80},{126,81},{126,82},{127,82},{127,83},{126,84},{126,85},{127,85},{127,86},{126,87},{126,88},{127,88},{127,89},{126,90},{126,91},{127,91},
  3418. {127,92},{126,93},{126,94},{127,94},{127,95},{126,96},{126,97},{127,97},{127,98},{126,99},{126,100},{127,100},{127,101},{126,102},{126,103},{127,103},
  3419. {127,104},{126,105},{126,106},{127,106},{127,107},{126,108},{125,109},{127,109},{126,110},{126,111},{127,111},{127,112},{126,113},{126,114},{127,114},{127,115},
  3420. {126,116},{126,117},{127,117},{127,118},{126,119},{126,120},{127,120},{127,121},{126,122},{126,123},{127,123},{127,124},{126,125},{126,126},{127,126},{127,127}
  3421. };
  3422. static void transcoder_init_bc7_mode5()
  3423. {
  3424. #if 0
  3425. // This is a little too much work to do at init time, so precompute it.
  3426. for (int i = 0; i < 256; i++)
  3427. {
  3428. int lowest_e = 256;
  3429. for (int lo = 0; lo < 128; lo++)
  3430. {
  3431. for (int hi = 0; hi < 128; hi++)
  3432. {
  3433. const int lo_e = (lo << 1) | (lo >> 6);
  3434. const int hi_e = (hi << 1) | (hi >> 6);
  3435. // Selector 1
  3436. int v = (lo_e * (64 - 21) + hi_e * 21 + 32) >> 6;
  3437. int e = abs(v - i);
  3438. if (e < lowest_e)
  3439. {
  3440. g_bc7_m5_equals_1[i].m_hi = static_cast<uint8_t>(hi);
  3441. g_bc7_m5_equals_1[i].m_lo = static_cast<uint8_t>(lo);
  3442. lowest_e = e;
  3443. }
  3444. } // hi
  3445. } // lo
  3446. printf("{%u,%u},", g_bc7_m5_equals_1[i].m_hi, g_bc7_m5_equals_1[i].m_lo);
  3447. if ((i & 15) == 15) printf("\n");
  3448. }
  3449. #endif
  3450. for (uint32_t i = 0; i < NUM_ETC1_TO_BC7_M5_SELECTOR_RANGES; i++)
  3451. {
  3452. uint32_t l = g_etc1_to_bc7_m5_selector_ranges[i].m_low;
  3453. uint32_t h = g_etc1_to_bc7_m5_selector_ranges[i].m_high;
  3454. g_etc1_to_bc7_m5_selector_range_index[l][h] = i;
  3455. }
  3456. for (uint32_t i = 0; i < NUM_ETC1_TO_BC7_M5A_SELECTOR_RANGES; i++)
  3457. {
  3458. uint32_t l = g_etc1_to_bc7_m5a_selector_ranges[i].m_low;
  3459. uint32_t h = g_etc1_to_bc7_m5a_selector_ranges[i].m_high;
  3460. g_etc1_to_bc7_m5a_selector_range_index[l][h] = i;
  3461. }
  3462. }
  3463. static void convert_etc1s_to_bc7_m5_color(void* pDst, const endpoint* pEndpoints, const selector* pSelector)
  3464. {
  3465. bc7_mode_5* pDst_block = static_cast<bc7_mode_5*>(pDst);
  3466. // First ensure the block is cleared to all 0's
  3467. static_cast<uint64_t*>(pDst)[0] = 0;
  3468. static_cast<uint64_t*>(pDst)[1] = 0;
  3469. // Set alpha to 255
  3470. pDst_block->m_lo.m_mode = 1 << 5;
  3471. pDst_block->m_lo.m_a0 = 255;
  3472. pDst_block->m_lo.m_a1_0 = 63;
  3473. pDst_block->m_hi.m_a1_1 = 3;
  3474. const uint32_t low_selector = pSelector->m_lo_selector;
  3475. const uint32_t high_selector = pSelector->m_hi_selector;
  3476. const uint32_t base_color_r = pEndpoints->m_color5.r;
  3477. const uint32_t base_color_g = pEndpoints->m_color5.g;
  3478. const uint32_t base_color_b = pEndpoints->m_color5.b;
  3479. const uint32_t inten_table = pEndpoints->m_inten5;
  3480. if (pSelector->m_num_unique_selectors == 1)
  3481. {
  3482. // Solid color block - use precomputed tables and set selectors to 1.
  3483. uint32_t r, g, b;
  3484. decoder_etc_block::get_block_color5(pEndpoints->m_color5, inten_table, low_selector, r, g, b);
  3485. pDst_block->m_lo.m_r0 = g_bc7_m5_equals_1[r].m_lo;
  3486. pDst_block->m_lo.m_g0 = g_bc7_m5_equals_1[g].m_lo;
  3487. pDst_block->m_lo.m_b0 = g_bc7_m5_equals_1[b].m_lo;
  3488. pDst_block->m_lo.m_r1 = g_bc7_m5_equals_1[r].m_hi;
  3489. pDst_block->m_lo.m_g1 = g_bc7_m5_equals_1[g].m_hi;
  3490. pDst_block->m_lo.m_b1 = g_bc7_m5_equals_1[b].m_hi;
  3491. set_block_bits((uint8_t*)pDst, 0x2aaaaaab, 31, 66);
  3492. return;
  3493. }
  3494. else if (pSelector->m_num_unique_selectors == 2)
  3495. {
  3496. // Only one or two unique selectors, so just switch to block truncation coding (BTC) to avoid quality issues on extreme blocks.
  3497. color32 block_colors[4];
  3498. decoder_etc_block::get_block_colors5(block_colors, color32(base_color_r, base_color_g, base_color_b, 255), inten_table);
  3499. const uint32_t r0 = block_colors[low_selector].r;
  3500. const uint32_t g0 = block_colors[low_selector].g;
  3501. const uint32_t b0 = block_colors[low_selector].b;
  3502. const uint32_t r1 = block_colors[high_selector].r;
  3503. const uint32_t g1 = block_colors[high_selector].g;
  3504. const uint32_t b1 = block_colors[high_selector].b;
  3505. pDst_block->m_lo.m_r0 = r0 >> 1;
  3506. pDst_block->m_lo.m_g0 = g0 >> 1;
  3507. pDst_block->m_lo.m_b0 = b0 >> 1;
  3508. pDst_block->m_lo.m_r1 = r1 >> 1;
  3509. pDst_block->m_lo.m_g1 = g1 >> 1;
  3510. pDst_block->m_lo.m_b1 = b1 >> 1;
  3511. uint32_t output_low_selector = 0, output_bit_offset = 0, output_bits = 0;
  3512. for (uint32_t y = 0; y < 4; y++)
  3513. {
  3514. for (uint32_t x = 0; x < 4; x++)
  3515. {
  3516. uint32_t s = pSelector->get_selector(x, y);
  3517. uint32_t os = (s == low_selector) ? output_low_selector : (3 ^ output_low_selector);
  3518. uint32_t num_bits = 2;
  3519. if ((x | y) == 0)
  3520. {
  3521. if (os & 2)
  3522. {
  3523. pDst_block->m_lo.m_r0 = r1 >> 1;
  3524. pDst_block->m_lo.m_g0 = g1 >> 1;
  3525. pDst_block->m_lo.m_b0 = b1 >> 1;
  3526. pDst_block->m_lo.m_r1 = r0 >> 1;
  3527. pDst_block->m_lo.m_g1 = g0 >> 1;
  3528. pDst_block->m_lo.m_b1 = b0 >> 1;
  3529. output_low_selector = 3;
  3530. os = 0;
  3531. }
  3532. num_bits = 1;
  3533. }
  3534. output_bits |= (os << output_bit_offset);
  3535. output_bit_offset += num_bits;
  3536. }
  3537. }
  3538. set_block_bits((uint8_t*)pDst, output_bits, 31, 66);
  3539. return;
  3540. }
  3541. const uint32_t selector_range_table = g_etc1_to_bc7_m5_selector_range_index[low_selector][high_selector];
  3542. //[32][8][RANGES][MAPPING]
  3543. const etc1_to_bc7_m5_solution* pTable_r = &g_etc1_to_bc7_m5_color[(inten_table * 32 + base_color_r) * (NUM_ETC1_TO_BC7_M5_SELECTOR_RANGES * NUM_ETC1_TO_BC7_M5_SELECTOR_MAPPINGS) + selector_range_table * NUM_ETC1_TO_BC7_M5_SELECTOR_MAPPINGS];
  3544. const etc1_to_bc7_m5_solution* pTable_g = &g_etc1_to_bc7_m5_color[(inten_table * 32 + base_color_g) * (NUM_ETC1_TO_BC7_M5_SELECTOR_RANGES * NUM_ETC1_TO_BC7_M5_SELECTOR_MAPPINGS) + selector_range_table * NUM_ETC1_TO_BC7_M5_SELECTOR_MAPPINGS];
  3545. const etc1_to_bc7_m5_solution* pTable_b = &g_etc1_to_bc7_m5_color[(inten_table * 32 + base_color_b) * (NUM_ETC1_TO_BC7_M5_SELECTOR_RANGES * NUM_ETC1_TO_BC7_M5_SELECTOR_MAPPINGS) + selector_range_table * NUM_ETC1_TO_BC7_M5_SELECTOR_MAPPINGS];
  3546. uint32_t best_err = UINT_MAX;
  3547. uint32_t best_mapping = 0;
  3548. assert(NUM_ETC1_TO_BC7_M5_SELECTOR_MAPPINGS == 10);
  3549. #define DO_ITER(m) { uint32_t total_err = pTable_r[m].m_err + pTable_g[m].m_err + pTable_b[m].m_err; if (total_err < best_err) { best_err = total_err; best_mapping = m; } }
  3550. DO_ITER(0); DO_ITER(1); DO_ITER(2); DO_ITER(3); DO_ITER(4);
  3551. DO_ITER(5); DO_ITER(6); DO_ITER(7); DO_ITER(8); DO_ITER(9);
  3552. #undef DO_ITER
  3553. const uint8_t* pSelectors_xlat = &g_etc1_to_bc7_m5_selector_mappings[best_mapping][0];
  3554. uint32_t s_inv = 0;
  3555. if (pSelectors_xlat[pSelector->get_selector(0, 0)] & 2)
  3556. {
  3557. pDst_block->m_lo.m_r0 = pTable_r[best_mapping].m_hi;
  3558. pDst_block->m_lo.m_g0 = pTable_g[best_mapping].m_hi;
  3559. pDst_block->m_lo.m_b0 = pTable_b[best_mapping].m_hi;
  3560. pDst_block->m_lo.m_r1 = pTable_r[best_mapping].m_lo;
  3561. pDst_block->m_lo.m_g1 = pTable_g[best_mapping].m_lo;
  3562. pDst_block->m_lo.m_b1 = pTable_b[best_mapping].m_lo;
  3563. s_inv = 3;
  3564. }
  3565. else
  3566. {
  3567. pDst_block->m_lo.m_r0 = pTable_r[best_mapping].m_lo;
  3568. pDst_block->m_lo.m_g0 = pTable_g[best_mapping].m_lo;
  3569. pDst_block->m_lo.m_b0 = pTable_b[best_mapping].m_lo;
  3570. pDst_block->m_lo.m_r1 = pTable_r[best_mapping].m_hi;
  3571. pDst_block->m_lo.m_g1 = pTable_g[best_mapping].m_hi;
  3572. pDst_block->m_lo.m_b1 = pTable_b[best_mapping].m_hi;
  3573. }
  3574. uint32_t output_bits = 0, output_bit_ofs = 0;
  3575. for (uint32_t y = 0; y < 4; y++)
  3576. {
  3577. for (uint32_t x = 0; x < 4; x++)
  3578. {
  3579. const uint32_t s = pSelector->get_selector(x, y);
  3580. const uint32_t os = pSelectors_xlat[s] ^ s_inv;
  3581. output_bits |= (os << output_bit_ofs);
  3582. output_bit_ofs += (((x | y) == 0) ? 1 : 2);
  3583. }
  3584. }
  3585. set_block_bits((uint8_t*)pDst, output_bits, 31, 66);
  3586. }
  3587. static void convert_etc1s_to_bc7_m5_alpha(void* pDst, const endpoint* pEndpoints, const selector* pSelector)
  3588. {
  3589. bc7_mode_5* pDst_block = static_cast<bc7_mode_5*>(pDst);
  3590. const uint32_t low_selector = pSelector->m_lo_selector;
  3591. const uint32_t high_selector = pSelector->m_hi_selector;
  3592. const uint32_t base_color_r = pEndpoints->m_color5.r;
  3593. const uint32_t inten_table = pEndpoints->m_inten5;
  3594. if (pSelector->m_num_unique_selectors == 1)
  3595. {
  3596. uint32_t r;
  3597. decoder_etc_block::get_block_color5_r(pEndpoints->m_color5, inten_table, low_selector, r);
  3598. pDst_block->m_lo.m_a0 = r;
  3599. pDst_block->m_lo.m_a1_0 = r & 63;
  3600. pDst_block->m_hi.m_a1_1 = r >> 6;
  3601. return;
  3602. }
  3603. else if (pSelector->m_num_unique_selectors == 2)
  3604. {
  3605. // Only one or two unique selectors, so just switch to block truncation coding (BTC) to avoid quality issues on extreme blocks.
  3606. int block_colors[4];
  3607. decoder_etc_block::get_block_colors5_g(block_colors, pEndpoints->m_color5, inten_table);
  3608. pDst_block->m_lo.m_a0 = block_colors[low_selector];
  3609. pDst_block->m_lo.m_a1_0 = block_colors[high_selector] & 63;
  3610. pDst_block->m_hi.m_a1_1 = block_colors[high_selector] >> 6;
  3611. uint32_t output_low_selector = 0, output_bit_offset = 0, output_bits = 0;
  3612. for (uint32_t y = 0; y < 4; y++)
  3613. {
  3614. for (uint32_t x = 0; x < 4; x++)
  3615. {
  3616. const uint32_t s = pSelector->get_selector(x, y);
  3617. uint32_t os = (s == low_selector) ? output_low_selector : (3 ^ output_low_selector);
  3618. uint32_t num_bits = 2;
  3619. if ((x | y) == 0)
  3620. {
  3621. if (os & 2)
  3622. {
  3623. pDst_block->m_lo.m_a0 = block_colors[high_selector];
  3624. pDst_block->m_lo.m_a1_0 = block_colors[low_selector] & 63;
  3625. pDst_block->m_hi.m_a1_1 = block_colors[low_selector] >> 6;
  3626. output_low_selector = 3;
  3627. os = 0;
  3628. }
  3629. num_bits = 1;
  3630. }
  3631. output_bits |= (os << output_bit_offset);
  3632. output_bit_offset += num_bits;
  3633. }
  3634. }
  3635. set_block_bits((uint8_t*)pDst, output_bits, 31, 97);
  3636. return;
  3637. }
  3638. const uint32_t selector_range_table = g_etc1_to_bc7_m5a_selector_range_index[low_selector][high_selector];
  3639. const etc1_g_to_bc7_m5a_conversion* pTable = &g_etc1_g_to_bc7_m5a[inten_table * (32 * NUM_ETC1_TO_BC7_M5A_SELECTOR_RANGES) + base_color_r * NUM_ETC1_TO_BC7_M5A_SELECTOR_RANGES + selector_range_table];
  3640. pDst_block->m_lo.m_a0 = pTable->m_lo;
  3641. pDst_block->m_lo.m_a1_0 = pTable->m_hi & 63;
  3642. pDst_block->m_hi.m_a1_1 = pTable->m_hi >> 6;
  3643. uint32_t output_bit_offset = 0, output_bits = 0, selector_trans = pTable->m_trans;
  3644. for (uint32_t y = 0; y < 4; y++)
  3645. {
  3646. for (uint32_t x = 0; x < 4; x++)
  3647. {
  3648. const uint32_t s = pSelector->get_selector(x, y);
  3649. uint32_t os = (selector_trans >> (s * 2)) & 3;
  3650. uint32_t num_bits = 2;
  3651. if ((x | y) == 0)
  3652. {
  3653. if (os & 2)
  3654. {
  3655. pDst_block->m_lo.m_a0 = pTable->m_hi;
  3656. pDst_block->m_lo.m_a1_0 = pTable->m_lo & 63;
  3657. pDst_block->m_hi.m_a1_1 = pTable->m_lo >> 6;
  3658. selector_trans ^= 0xFF;
  3659. os ^= 3;
  3660. }
  3661. num_bits = 1;
  3662. }
  3663. output_bits |= (os << output_bit_offset);
  3664. output_bit_offset += num_bits;
  3665. }
  3666. }
  3667. set_block_bits((uint8_t*)pDst, output_bits, 31, 97);
  3668. }
  3669. #endif // BASISD_SUPPORT_BC7_MODE5
  3670. #if BASISD_SUPPORT_ETC2_EAC_A8 || BASISD_SUPPORT_UASTC
  3671. static const uint8_t g_etc2_eac_a8_sel4[6] = { 0x92, 0x49, 0x24, 0x92, 0x49, 0x24 };
  3672. #endif
  3673. #if BASISD_SUPPORT_ETC2_EAC_A8
  3674. static void convert_etc1s_to_etc2_eac_a8(eac_block* pDst_block, const endpoint* pEndpoints, const selector* pSelector)
  3675. {
  3676. const uint32_t low_selector = pSelector->m_lo_selector;
  3677. const uint32_t high_selector = pSelector->m_hi_selector;
  3678. const color32& base_color = pEndpoints->m_color5;
  3679. const uint32_t inten_table = pEndpoints->m_inten5;
  3680. if (low_selector == high_selector)
  3681. {
  3682. uint32_t r;
  3683. decoder_etc_block::get_block_color5_r(base_color, inten_table, low_selector, r);
  3684. // Constant alpha block
  3685. // Select table 13, use selector 4 (0), set multiplier to 1 and base color g
  3686. pDst_block->m_base = r;
  3687. pDst_block->m_table = 13;
  3688. pDst_block->m_multiplier = 1;
  3689. // selectors are all 4's
  3690. memcpy(pDst_block->m_selectors, g_etc2_eac_a8_sel4, sizeof(g_etc2_eac_a8_sel4));
  3691. return;
  3692. }
  3693. uint32_t selector_range_table = 0;
  3694. for (selector_range_table = 0; selector_range_table < NUM_ETC2_EAC_SELECTOR_RANGES; selector_range_table++)
  3695. if ((low_selector == s_etc2_eac_selector_ranges[selector_range_table].m_low) && (high_selector == s_etc2_eac_selector_ranges[selector_range_table].m_high))
  3696. break;
  3697. if (selector_range_table >= NUM_ETC2_EAC_SELECTOR_RANGES)
  3698. selector_range_table = 0;
  3699. const etc1_g_to_eac_conversion* pTable_entry = &s_etc1_g_to_etc2_a8[base_color.r + inten_table * 32][selector_range_table];
  3700. pDst_block->m_base = pTable_entry->m_base;
  3701. pDst_block->m_table = pTable_entry->m_table_mul >> 4;
  3702. pDst_block->m_multiplier = pTable_entry->m_table_mul & 15;
  3703. uint64_t selector_bits = 0;
  3704. for (uint32_t y = 0; y < 4; y++)
  3705. {
  3706. for (uint32_t x = 0; x < 4; x++)
  3707. {
  3708. uint32_t s = pSelector->get_selector(x, y);
  3709. uint32_t ds = (pTable_entry->m_trans >> (s * 3)) & 7;
  3710. const uint32_t dst_ofs = 45 - (y + x * 4) * 3;
  3711. selector_bits |= (static_cast<uint64_t>(ds) << dst_ofs);
  3712. }
  3713. }
  3714. pDst_block->set_selector_bits(selector_bits);
  3715. }
  3716. #endif // BASISD_SUPPORT_ETC2_EAC_A8
  3717. #if BASISD_SUPPORT_ETC2_EAC_RG11
  3718. static const etc1_g_to_eac_conversion s_etc1_g_to_etc2_r11[32 * 8][NUM_ETC2_EAC_SELECTOR_RANGES] =
  3719. {
  3720. {{0,1,3328},{0,1,3328},{0,16,457},{0,16,456}},
  3721. {{0,226,3936},{0,226,3936},{0,17,424},{8,0,472}},
  3722. {{6,178,4012},{6,178,4008},{0,146,501},{16,0,472}},
  3723. {{14,178,4012},{14,178,4008},{8,146,501},{24,0,472}},
  3724. {{23,178,4012},{23,178,4008},{17,146,501},{33,0,472}},
  3725. {{31,178,4012},{31,178,4008},{25,146,501},{41,0,472}},
  3726. {{39,178,4012},{39,178,4008},{33,146,501},{49,0,472}},
  3727. {{47,178,4012},{47,178,4008},{41,146,501},{27,228,496}},
  3728. {{56,178,4012},{56,178,4008},{50,146,501},{36,228,496}},
  3729. {{64,178,4012},{64,178,4008},{58,146,501},{44,228,496}},
  3730. {{72,178,4012},{72,178,4008},{66,146,501},{52,228,496}},
  3731. {{80,178,4012},{80,178,4008},{74,146,501},{60,228,496}},
  3732. {{89,178,4012},{89,178,4008},{83,146,501},{69,228,496}},
  3733. {{97,178,4012},{97,178,4008},{91,146,501},{77,228,496}},
  3734. {{105,178,4012},{105,178,4008},{99,146,501},{85,228,496}},
  3735. {{113,178,4012},{113,178,4008},{107,146,501},{93,228,496}},
  3736. {{122,178,4012},{122,178,4008},{116,146,501},{102,228,496}},
  3737. {{130,178,4012},{130,178,4008},{124,146,501},{110,228,496}},
  3738. {{138,178,4012},{138,178,4008},{132,146,501},{118,228,496}},
  3739. {{146,178,4012},{146,178,4008},{140,146,501},{126,228,496}},
  3740. {{155,178,4012},{155,178,4008},{149,146,501},{135,228,496}},
  3741. {{163,178,4012},{163,178,4008},{157,146,501},{143,228,496}},
  3742. {{171,178,4012},{171,178,4008},{165,146,501},{151,228,496}},
  3743. {{179,178,4012},{179,178,4008},{173,146,501},{159,228,496}},
  3744. {{188,178,4012},{188,178,4008},{182,146,501},{168,228,496}},
  3745. {{196,178,4012},{196,178,4008},{190,146,501},{176,228,496}},
  3746. {{204,178,4012},{204,178,4008},{198,146,501},{184,228,496}},
  3747. {{212,178,4012},{212,178,4008},{206,146,501},{192,228,496}},
  3748. {{221,178,4012},{221,178,4008},{215,146,501},{201,228,496}},
  3749. {{229,178,4012},{229,178,4008},{223,146,501},{209,228,496}},
  3750. {{235,66,4012},{221,100,4008},{231,146,501},{217,228,496}},
  3751. {{211,102,4085},{254,32,4040},{211,102,501},{254,32,456}},
  3752. {{0,2,3328},{0,2,3328},{0,1,320},{0,1,320}},
  3753. {{7,162,3905},{7,162,3904},{0,17,480},{0,17,480}},
  3754. {{15,162,3906},{15,162,3904},{1,117,352},{1,117,352}},
  3755. {{23,162,3906},{23,162,3904},{5,34,500},{4,53,424}},
  3756. {{32,162,3906},{32,162,3904},{14,34,500},{3,69,424}},
  3757. {{40,162,3906},{40,162,3904},{22,34,500},{1,133,496}},
  3758. {{48,162,3906},{48,162,3904},{30,34,500},{4,85,496}},
  3759. {{56,162,3906},{56,162,3904},{38,34,500},{12,85,496}},
  3760. {{65,162,3906},{65,162,3904},{47,34,500},{1,106,424}},
  3761. {{73,162,3906},{73,162,3904},{55,34,500},{9,106,424}},
  3762. {{81,162,3906},{81,162,3904},{63,34,500},{7,234,496}},
  3763. {{89,162,3906},{89,162,3904},{71,34,500},{15,234,496}},
  3764. {{98,162,3906},{98,162,3904},{80,34,500},{24,234,496}},
  3765. {{106,162,3906},{106,162,3904},{88,34,500},{32,234,496}},
  3766. {{114,162,3906},{114,162,3904},{96,34,500},{40,234,496}},
  3767. {{122,162,3906},{122,162,3904},{104,34,500},{48,234,496}},
  3768. {{131,162,3906},{131,162,3904},{113,34,500},{57,234,496}},
  3769. {{139,162,3906},{139,162,3904},{121,34,500},{65,234,496}},
  3770. {{147,162,3906},{147,162,3904},{129,34,500},{73,234,496}},
  3771. {{155,162,3906},{155,162,3904},{137,34,500},{81,234,496}},
  3772. {{164,162,3906},{164,162,3904},{146,34,500},{90,234,496}},
  3773. {{172,162,3906},{172,162,3904},{154,34,500},{98,234,496}},
  3774. {{180,162,3906},{180,162,3904},{162,34,500},{106,234,496}},
  3775. {{188,162,3906},{188,162,3904},{170,34,500},{114,234,496}},
  3776. {{197,162,3906},{197,162,3904},{179,34,500},{123,234,496}},
  3777. {{205,162,3906},{205,162,3904},{187,34,500},{131,234,496}},
  3778. {{213,162,3906},{213,162,3904},{195,34,500},{139,234,496}},
  3779. {{221,162,3906},{221,162,3904},{203,34,500},{147,234,496}},
  3780. {{230,162,3906},{230,162,3904},{212,34,500},{156,234,496}},
  3781. {{238,162,3906},{174,106,4008},{220,34,500},{164,234,496}},
  3782. {{240,178,4001},{182,106,4008},{228,34,500},{172,234,496}},
  3783. {{166,108,4085},{115,31,4080},{166,108,501},{115,31,496}},
  3784. {{1,68,3328},{1,68,3328},{0,1,384},{0,1,384}},
  3785. {{1,51,3968},{1,51,3968},{0,2,384},{0,2,384}},
  3786. {{21,18,3851},{21,18,3848},{1,50,488},{1,50,488}},
  3787. {{26,195,3851},{29,18,3848},{0,67,488},{0,67,488}},
  3788. {{35,195,3851},{38,18,3848},{12,115,488},{0,3,496}},
  3789. {{43,195,3851},{46,18,3848},{20,115,488},{2,6,424}},
  3790. {{51,195,3851},{54,18,3848},{36,66,482},{4,22,424}},
  3791. {{59,195,3851},{62,18,3848},{44,66,482},{3,73,424}},
  3792. {{68,195,3851},{71,18,3848},{53,66,482},{3,22,496}},
  3793. {{76,195,3851},{79,18,3848},{61,66,482},{2,137,496}},
  3794. {{84,195,3851},{87,18,3848},{69,66,482},{1,89,496}},
  3795. {{92,195,3851},{95,18,3848},{77,66,482},{9,89,496}},
  3796. {{101,195,3851},{104,18,3848},{86,66,482},{18,89,496}},
  3797. {{109,195,3851},{112,18,3848},{94,66,482},{26,89,496}},
  3798. {{117,195,3851},{120,18,3848},{102,66,482},{34,89,496}},
  3799. {{125,195,3851},{128,18,3848},{110,66,482},{42,89,496}},
  3800. {{134,195,3851},{137,18,3848},{119,66,482},{51,89,496}},
  3801. {{141,195,3907},{145,18,3848},{127,66,482},{59,89,496}},
  3802. {{149,195,3907},{153,18,3848},{135,66,482},{67,89,496}},
  3803. {{157,195,3907},{161,18,3848},{143,66,482},{75,89,496}},
  3804. {{166,195,3907},{170,18,3848},{152,66,482},{84,89,496}},
  3805. {{174,195,3907},{178,18,3848},{160,66,482},{92,89,496}},
  3806. {{182,195,3907},{186,18,3848},{168,66,482},{100,89,496}},
  3807. {{190,195,3907},{194,18,3848},{176,66,482},{108,89,496}},
  3808. {{199,195,3907},{203,18,3848},{185,66,482},{117,89,496}},
  3809. {{207,195,3907},{211,18,3848},{193,66,482},{125,89,496}},
  3810. {{215,195,3907},{219,18,3848},{201,66,482},{133,89,496}},
  3811. {{223,195,3907},{227,18,3848},{209,66,482},{141,89,496}},
  3812. {{232,195,3907},{168,89,4008},{218,66,482},{150,89,496}},
  3813. {{236,18,3907},{176,89,4008},{226,66,482},{158,89,496}},
  3814. {{158,90,4085},{103,31,4080},{158,90,501},{103,31,496}},
  3815. {{166,90,4085},{111,31,4080},{166,90,501},{111,31,496}},
  3816. {{0,70,3328},{0,70,3328},{0,17,448},{0,17,448}},
  3817. {{0,117,3904},{0,117,3904},{0,35,384},{0,35,384}},
  3818. {{13,165,3905},{13,165,3904},{2,211,480},{2,211,480}},
  3819. {{21,165,3906},{21,165,3904},{1,51,488},{1,51,488}},
  3820. {{30,165,3906},{30,165,3904},{7,61,352},{7,61,352}},
  3821. {{38,165,3906},{38,165,3904},{2,125,352},{2,125,352}},
  3822. {{46,165,3906},{46,165,3904},{1,37,500},{10,125,352}},
  3823. {{54,165,3906},{54,165,3904},{9,37,500},{5,61,424}},
  3824. {{63,165,3906},{63,165,3904},{18,37,500},{1,189,424}},
  3825. {{71,165,3906},{71,165,3904},{26,37,500},{9,189,424}},
  3826. {{79,165,3906},{79,165,3904},{34,37,500},{4,77,424}},
  3827. {{87,165,3906},{87,165,3904},{42,37,500},{12,77,424}},
  3828. {{96,165,3906},{96,165,3904},{51,37,500},{8,93,424}},
  3829. {{104,165,3906},{104,165,3904},{59,37,500},{3,141,496}},
  3830. {{112,165,3906},{112,165,3904},{68,37,500},{11,141,496}},
  3831. {{120,165,3906},{120,165,3904},{76,37,500},{6,93,496}},
  3832. {{129,165,3906},{129,165,3904},{85,37,500},{15,93,496}},
  3833. {{70,254,4012},{137,165,3904},{93,37,500},{23,93,496}},
  3834. {{145,165,3906},{145,165,3904},{101,37,500},{31,93,496}},
  3835. {{86,254,4012},{153,165,3904},{109,37,500},{39,93,496}},
  3836. {{163,165,3906},{162,165,3904},{118,37,500},{48,93,496}},
  3837. {{171,165,3906},{170,165,3904},{126,37,500},{56,93,496}},
  3838. {{179,165,3906},{178,165,3904},{134,37,500},{64,93,496}},
  3839. {{187,165,3906},{187,165,3904},{142,37,500},{72,93,496}},
  3840. {{196,165,3906},{196,165,3904},{151,37,500},{81,93,496}},
  3841. {{204,165,3906},{204,165,3904},{159,37,500},{89,93,496}},
  3842. {{212,165,3906},{136,77,4008},{167,37,500},{97,93,496}},
  3843. {{220,165,3906},{131,93,4008},{175,37,500},{105,93,496}},
  3844. {{214,181,4001},{140,93,4008},{184,37,500},{114,93,496}},
  3845. {{222,181,4001},{148,93,4008},{192,37,500},{122,93,496}},
  3846. {{115,95,4085},{99,31,4080},{115,95,501},{99,31,496}},
  3847. {{123,95,4085},{107,31,4080},{123,95,501},{107,31,496}},
  3848. {{0,102,3840},{0,102,3840},{0,18,384},{0,18,384}},
  3849. {{5,167,3904},{5,167,3904},{0,13,256},{0,13,256}},
  3850. {{4,54,3968},{4,54,3968},{1,67,448},{1,67,448}},
  3851. {{30,198,3850},{30,198,3848},{0,3,480},{0,3,480}},
  3852. {{39,198,3850},{39,198,3848},{3,52,488},{3,52,488}},
  3853. {{47,198,3851},{47,198,3848},{3,4,488},{3,4,488}},
  3854. {{55,198,3851},{55,198,3848},{1,70,488},{1,70,488}},
  3855. {{53,167,3906},{63,198,3848},{3,22,488},{3,22,488}},
  3856. {{62,167,3906},{72,198,3848},{24,118,488},{0,6,496}},
  3857. {{70,167,3906},{80,198,3848},{32,118,488},{2,89,488}},
  3858. {{78,167,3906},{88,198,3848},{40,118,488},{1,73,496}},
  3859. {{86,167,3906},{96,198,3848},{48,118,488},{0,28,424}},
  3860. {{95,167,3906},{105,198,3848},{57,118,488},{9,28,424}},
  3861. {{103,167,3906},{113,198,3848},{65,118,488},{5,108,496}},
  3862. {{111,167,3906},{121,198,3848},{73,118,488},{13,108,496}},
  3863. {{119,167,3906},{129,198,3848},{81,118,488},{21,108,496}},
  3864. {{128,167,3906},{138,198,3848},{90,118,488},{6,28,496}},
  3865. {{136,167,3906},{146,198,3848},{98,118,488},{14,28,496}},
  3866. {{145,167,3906},{154,198,3848},{106,118,488},{22,28,496}},
  3867. {{153,167,3906},{162,198,3848},{114,118,488},{30,28,496}},
  3868. {{162,167,3906},{171,198,3848},{123,118,488},{39,28,496}},
  3869. {{170,167,3906},{179,198,3848},{131,118,488},{47,28,496}},
  3870. {{178,167,3906},{187,198,3848},{139,118,488},{55,28,496}},
  3871. {{186,167,3906},{195,198,3848},{147,118,488},{63,28,496}},
  3872. {{194,167,3906},{120,12,4008},{156,118,488},{72,28,496}},
  3873. {{206,198,3907},{116,28,4008},{164,118,488},{80,28,496}},
  3874. {{214,198,3907},{124,28,4008},{172,118,488},{88,28,496}},
  3875. {{222,198,3395},{132,28,4008},{180,118,488},{96,28,496}},
  3876. {{207,134,4001},{141,28,4008},{189,118,488},{105,28,496}},
  3877. {{95,30,4085},{86,31,4080},{95,30,501},{86,31,496}},
  3878. {{103,30,4085},{94,31,4080},{103,30,501},{94,31,496}},
  3879. {{111,30,4085},{102,31,4080},{111,30,501},{102,31,496}},
  3880. {{0,104,3840},{0,104,3840},{0,18,448},{0,18,448}},
  3881. {{4,39,3904},{4,39,3904},{0,4,384},{0,4,384}},
  3882. {{0,56,3968},{0,56,3968},{0,84,448},{0,84,448}},
  3883. {{6,110,3328},{6,110,3328},{0,20,448},{0,20,448}},
  3884. {{41,200,3850},{41,200,3848},{1,4,480},{1,4,480}},
  3885. {{49,200,3850},{49,200,3848},{1,8,416},{1,8,416}},
  3886. {{57,200,3851},{57,200,3848},{1,38,488},{1,38,488}},
  3887. {{65,200,3851},{65,200,3848},{1,120,488},{1,120,488}},
  3888. {{74,200,3851},{74,200,3848},{2,72,488},{2,72,488}},
  3889. {{68,6,3907},{82,200,3848},{2,24,488},{2,24,488}},
  3890. {{77,6,3907},{90,200,3848},{26,120,488},{10,24,488}},
  3891. {{97,63,3330},{98,200,3848},{34,120,488},{2,8,496}},
  3892. {{106,63,3330},{107,200,3848},{43,120,488},{3,92,488}},
  3893. {{114,63,3330},{115,200,3848},{51,120,488},{11,92,488}},
  3894. {{122,63,3330},{123,200,3848},{59,120,488},{7,76,496}},
  3895. {{130,63,3330},{131,200,3848},{67,120,488},{15,76,496}},
  3896. {{139,63,3330},{140,200,3848},{76,120,488},{24,76,496}},
  3897. {{147,63,3330},{148,200,3848},{84,120,488},{32,76,496}},
  3898. {{155,63,3330},{156,200,3848},{92,120,488},{40,76,496}},
  3899. {{164,63,3330},{164,200,3848},{100,120,488},{48,76,496}},
  3900. {{173,63,3330},{173,200,3848},{109,120,488},{57,76,496}},
  3901. {{184,6,3851},{181,200,3848},{117,120,488},{65,76,496}},
  3902. {{192,6,3851},{133,28,3936},{125,120,488},{73,76,496}},
  3903. {{189,200,3907},{141,28,3936},{133,120,488},{81,76,496}},
  3904. {{198,200,3907},{138,108,4000},{142,120,488},{90,76,496}},
  3905. {{206,200,3907},{146,108,4000},{150,120,488},{98,76,496}},
  3906. {{214,200,3395},{154,108,4000},{158,120,488},{106,76,496}},
  3907. {{190,136,4001},{162,108,4000},{166,120,488},{114,76,496}},
  3908. {{123,30,4076},{87,15,4080},{123,30,492},{87,15,496}},
  3909. {{117,110,4084},{80,31,4080},{117,110,500},{80,31,496}},
  3910. {{125,110,4084},{88,31,4080},{125,110,500},{88,31,496}},
  3911. {{133,110,4084},{96,31,4080},{133,110,500},{96,31,496}},
  3912. {{9,56,3904},{9,56,3904},{0,67,448},{0,67,448}},
  3913. {{1,8,3904},{1,8,3904},{1,84,448},{1,84,448}},
  3914. {{1,124,3904},{1,124,3904},{0,39,384},{0,39,384}},
  3915. {{9,124,3904},{9,124,3904},{1,4,448},{1,4,448}},
  3916. {{6,76,3904},{6,76,3904},{0,70,448},{0,70,448}},
  3917. {{62,6,3859},{62,6,3856},{2,38,480},{2,38,480}},
  3918. {{70,6,3859},{70,6,3856},{5,43,416},{5,43,416}},
  3919. {{78,6,3859},{78,6,3856},{2,11,416},{2,11,416}},
  3920. {{87,6,3859},{87,6,3856},{0,171,488},{0,171,488}},
  3921. {{67,8,3906},{95,6,3856},{8,171,488},{8,171,488}},
  3922. {{75,8,3907},{103,6,3856},{5,123,488},{5,123,488}},
  3923. {{83,8,3907},{111,6,3856},{2,75,488},{2,75,488}},
  3924. {{92,8,3907},{120,6,3856},{0,27,488},{0,27,488}},
  3925. {{100,8,3907},{128,6,3856},{8,27,488},{8,27,488}},
  3926. {{120,106,3843},{136,6,3856},{99,6,387},{16,27,488}},
  3927. {{128,106,3843},{144,6,3856},{107,6,387},{2,11,496}},
  3928. {{137,106,3843},{153,6,3856},{117,6,387},{11,11,496}},
  3929. {{145,106,3843},{161,6,3856},{125,6,387},{19,11,496}},
  3930. {{163,8,3851},{137,43,3904},{133,6,387},{27,11,496}},
  3931. {{171,8,3851},{145,43,3904},{141,6,387},{35,11,496}},
  3932. {{180,8,3851},{110,11,4000},{150,6,387},{44,11,496}},
  3933. {{188,8,3851},{118,11,4000},{158,6,387},{52,11,496}},
  3934. {{172,72,3907},{126,11,4000},{166,6,387},{60,11,496}},
  3935. {{174,6,3971},{134,11,4000},{174,6,387},{68,11,496}},
  3936. {{183,6,3971},{143,11,4000},{183,6,387},{77,11,496}},
  3937. {{191,6,3971},{151,11,4000},{191,6,387},{85,11,496}},
  3938. {{199,6,3971},{159,11,4000},{199,6,387},{93,11,496}},
  3939. {{92,12,4084},{69,15,4080},{92,12,500},{69,15,496}},
  3940. {{101,12,4084},{78,15,4080},{101,12,500},{78,15,496}},
  3941. {{110,12,4084},{86,15,4080},{110,12,500},{86,15,496}},
  3942. {{118,12,4084},{79,31,4080},{118,12,500},{79,31,496}},
  3943. {{126,12,4084},{87,31,4080},{126,12,500},{87,31,496}},
  3944. {{71,8,3602},{71,8,3600},{2,21,384},{2,21,384}},
  3945. {{79,8,3611},{79,8,3608},{0,69,448},{0,69,448}},
  3946. {{87,8,3611},{87,8,3608},{0,23,384},{0,23,384}},
  3947. {{95,8,3611},{95,8,3608},{1,5,448},{1,5,448}},
  3948. {{104,8,3611},{104,8,3608},{0,88,448},{0,88,448}},
  3949. {{112,8,3611},{112,8,3608},{0,72,448},{0,72,448}},
  3950. {{120,8,3611},{121,8,3608},{36,21,458},{36,21,456}},
  3951. {{133,47,3091},{129,8,3608},{44,21,458},{44,21,456}},
  3952. {{142,47,3091},{138,8,3608},{53,21,459},{53,21,456}},
  3953. {{98,12,3850},{98,12,3848},{61,21,459},{61,21,456}},
  3954. {{106,12,3850},{106,12,3848},{10,92,480},{69,21,456}},
  3955. {{114,12,3851},{114,12,3848},{18,92,480},{77,21,456}},
  3956. {{123,12,3851},{123,12,3848},{3,44,488},{86,21,456}},
  3957. {{95,12,3906},{95,12,3904},{11,44,488},{94,21,456}},
  3958. {{103,12,3906},{103,12,3904},{19,44,488},{102,21,456}},
  3959. {{111,12,3907},{111,12,3904},{27,44,489},{110,21,456}},
  3960. {{120,12,3907},{120,12,3904},{36,44,489},{119,21,456}},
  3961. {{128,12,3907},{128,12,3904},{44,44,489},{127,21,456}},
  3962. {{136,12,3907},{136,12,3904},{52,44,489},{135,21,456}},
  3963. {{144,12,3907},{144,12,3904},{60,44,490},{144,21,456}},
  3964. {{153,12,3907},{153,12,3904},{69,44,490},{153,21,456}},
  3965. {{161,12,3395},{149,188,3968},{77,44,490},{161,21,456}},
  3966. {{169,12,3395},{199,21,3928},{85,44,490},{169,21,456}},
  3967. {{113,95,4001},{202,69,3992},{125,8,483},{177,21,456}},
  3968. {{122,95,4001},{201,21,3984},{134,8,483},{186,21,456}},
  3969. {{143,8,4067},{209,21,3984},{142,8,483},{194,21,456}},
  3970. {{151,8,4067},{47,15,4080},{151,8,483},{47,15,496}},
  3971. {{159,8,4067},{55,15,4080},{159,8,483},{55,15,496}},
  3972. {{168,8,4067},{64,15,4080},{168,8,483},{64,15,496}},
  3973. {{160,40,4075},{72,15,4080},{160,40,491},{72,15,496}},
  3974. {{168,40,4075},{80,15,4080},{168,40,491},{80,15,496}},
  3975. {{144,8,4082},{88,15,4080},{144,8,498},{88,15,496}},
  3976. };
  3977. static void convert_etc1s_to_etc2_eac_r11(eac_block* pDst_block, const endpoint* pEndpoints, const selector* pSelector)
  3978. {
  3979. const uint32_t low_selector = pSelector->m_lo_selector;
  3980. const uint32_t high_selector = pSelector->m_hi_selector;
  3981. const color32& base_color = pEndpoints->m_color5;
  3982. const uint32_t inten_table = pEndpoints->m_inten5;
  3983. if (low_selector == high_selector)
  3984. {
  3985. uint32_t r;
  3986. decoder_etc_block::get_block_color5_r(base_color, inten_table, low_selector, r);
  3987. // Constant alpha block
  3988. // Select table 13, use selector 4 (0), set multiplier to 1 and base color r
  3989. pDst_block->m_base = r;
  3990. pDst_block->m_table = 13;
  3991. pDst_block->m_multiplier = 1;
  3992. // selectors are all 4's
  3993. static const uint8_t s_etc2_eac_r11_sel4[6] = { 0x92, 0x49, 0x24, 0x92, 0x49, 0x24 };
  3994. memcpy(pDst_block->m_selectors, s_etc2_eac_r11_sel4, sizeof(s_etc2_eac_r11_sel4));
  3995. return;
  3996. }
  3997. uint32_t selector_range_table = 0;
  3998. for (selector_range_table = 0; selector_range_table < NUM_ETC2_EAC_SELECTOR_RANGES; selector_range_table++)
  3999. if ((low_selector == s_etc2_eac_selector_ranges[selector_range_table].m_low) && (high_selector == s_etc2_eac_selector_ranges[selector_range_table].m_high))
  4000. break;
  4001. if (selector_range_table >= NUM_ETC2_EAC_SELECTOR_RANGES)
  4002. selector_range_table = 0;
  4003. const etc1_g_to_eac_conversion* pTable_entry = &s_etc1_g_to_etc2_r11[base_color.r + inten_table * 32][selector_range_table];
  4004. pDst_block->m_base = pTable_entry->m_base;
  4005. pDst_block->m_table = pTable_entry->m_table_mul >> 4;
  4006. pDst_block->m_multiplier = pTable_entry->m_table_mul & 15;
  4007. uint64_t selector_bits = 0;
  4008. for (uint32_t y = 0; y < 4; y++)
  4009. {
  4010. for (uint32_t x = 0; x < 4; x++)
  4011. {
  4012. uint32_t s = pSelector->get_selector(x, y);
  4013. uint32_t ds = (pTable_entry->m_trans >> (s * 3)) & 7;
  4014. const uint32_t dst_ofs = 45 - (y + x * 4) * 3;
  4015. selector_bits |= (static_cast<uint64_t>(ds) << dst_ofs);
  4016. }
  4017. }
  4018. pDst_block->set_selector_bits(selector_bits);
  4019. }
  4020. #endif // BASISD_SUPPORT_ETC2_EAC_RG11
  4021. // ASTC
  4022. struct etc1_to_astc_solution
  4023. {
  4024. uint8_t m_lo;
  4025. uint8_t m_hi;
  4026. uint16_t m_err;
  4027. };
  4028. #if BASISD_SUPPORT_ASTC
  4029. static dxt_selector_range g_etc1_to_astc_selector_ranges[] =
  4030. {
  4031. { 0, 3 },
  4032. { 1, 3 },
  4033. { 0, 2 },
  4034. { 1, 2 },
  4035. { 2, 3 },
  4036. { 0, 1 },
  4037. };
  4038. const uint32_t NUM_ETC1_TO_ASTC_SELECTOR_RANGES = sizeof(g_etc1_to_astc_selector_ranges) / sizeof(g_etc1_to_astc_selector_ranges[0]);
  4039. static uint32_t g_etc1_to_astc_selector_range_index[4][4];
  4040. const uint32_t NUM_ETC1_TO_ASTC_SELECTOR_MAPPINGS = 10;
  4041. static const uint8_t g_etc1_to_astc_selector_mappings[NUM_ETC1_TO_ASTC_SELECTOR_MAPPINGS][4] =
  4042. {
  4043. { 0, 0, 1, 1 },
  4044. { 0, 0, 1, 2 },
  4045. { 0, 0, 1, 3 },
  4046. { 0, 0, 2, 3 },
  4047. { 0, 1, 1, 1 },
  4048. { 0, 1, 2, 2 },
  4049. { 0, 1, 2, 3 },
  4050. { 0, 2, 3, 3 },
  4051. { 1, 2, 2, 2 },
  4052. { 1, 2, 3, 3 },
  4053. };
  4054. static const etc1_to_astc_solution g_etc1_to_astc[32 * 8 * NUM_ETC1_TO_ASTC_SELECTOR_MAPPINGS * NUM_ETC1_TO_ASTC_SELECTOR_RANGES] = {
  4055. #include "basisu_transcoder_tables_astc.inc"
  4056. };
  4057. // The best selector mapping to use given a base base+inten table and used selector range for converting grayscale data.
  4058. static uint8_t g_etc1_to_astc_best_grayscale_mapping[32][8][NUM_ETC1_TO_ASTC_SELECTOR_RANGES];
  4059. #if BASISD_SUPPORT_ASTC_HIGHER_OPAQUE_QUALITY
  4060. static const etc1_to_astc_solution g_etc1_to_astc_0_255[32 * 8 * NUM_ETC1_TO_ASTC_SELECTOR_MAPPINGS * NUM_ETC1_TO_ASTC_SELECTOR_RANGES] = {
  4061. #include "basisu_transcoder_tables_astc_0_255.inc"
  4062. };
  4063. static uint8_t g_etc1_to_astc_best_grayscale_mapping_0_255[32][8][NUM_ETC1_TO_ASTC_SELECTOR_RANGES];
  4064. #endif
  4065. static uint32_t g_ise_to_unquant[48];
  4066. #if BASISD_WRITE_NEW_ASTC_TABLES
  4067. static void create_etc1_to_astc_conversion_table_0_47()
  4068. {
  4069. FILE* pFile = nullptr;
  4070. fopen_s(&pFile, "basisu_transcoder_tables_astc.inc", "w");
  4071. uint32_t n = 0;
  4072. for (int inten = 0; inten < 8; inten++)
  4073. {
  4074. for (uint32_t g = 0; g < 32; g++)
  4075. {
  4076. color32 block_colors[4];
  4077. decoder_etc_block::get_diff_subblock_colors(block_colors, decoder_etc_block::pack_color5(color32(g, g, g, 255), false), inten);
  4078. for (uint32_t sr = 0; sr < NUM_ETC1_TO_ASTC_SELECTOR_RANGES; sr++)
  4079. {
  4080. const uint32_t low_selector = g_etc1_to_astc_selector_ranges[sr].m_low;
  4081. const uint32_t high_selector = g_etc1_to_astc_selector_ranges[sr].m_high;
  4082. uint32_t mapping_best_low[NUM_ETC1_TO_ASTC_SELECTOR_MAPPINGS];
  4083. uint32_t mapping_best_high[NUM_ETC1_TO_ASTC_SELECTOR_MAPPINGS];
  4084. uint64_t mapping_best_err[NUM_ETC1_TO_ASTC_SELECTOR_MAPPINGS];
  4085. uint64_t highest_best_err = 0;
  4086. for (uint32_t m = 0; m < NUM_ETC1_TO_ASTC_SELECTOR_MAPPINGS; m++)
  4087. {
  4088. uint32_t best_lo = 0;
  4089. uint32_t best_hi = 0;
  4090. uint64_t best_err = UINT64_MAX;
  4091. for (uint32_t hi = 0; hi <= 47; hi++)
  4092. {
  4093. for (uint32_t lo = 0; lo <= 47; lo++)
  4094. {
  4095. uint32_t colors[4];
  4096. for (uint32_t s = 0; s < 4; s++)
  4097. {
  4098. uint32_t s_scaled = s | (s << 2) | (s << 4);
  4099. if (s_scaled > 32)
  4100. s_scaled++;
  4101. uint32_t c0 = g_ise_to_unquant[lo] | (g_ise_to_unquant[lo] << 8);
  4102. uint32_t c1 = g_ise_to_unquant[hi] | (g_ise_to_unquant[hi] << 8);
  4103. colors[s] = ((c0 * (64 - s_scaled) + c1 * s_scaled + 32) / 64) >> 8;
  4104. }
  4105. uint64_t total_err = 0;
  4106. for (uint32_t s = low_selector; s <= high_selector; s++)
  4107. {
  4108. int err = block_colors[s].g - colors[g_etc1_to_astc_selector_mappings[m][s]];
  4109. int err_scale = 1;
  4110. // Special case when the intensity table is 7, low_selector is 0, and high_selector is 3. In this extreme case, it's likely the encoder is trying to strongly favor
  4111. // the low/high selectors which are clamping to either 0 or 255.
  4112. if (((inten == 7) && (low_selector == 0) && (high_selector == 3)) && ((s == 0) || (s == 3)))
  4113. err_scale = 8;
  4114. total_err += (err * err) * err_scale;
  4115. }
  4116. if (total_err < best_err)
  4117. {
  4118. best_err = total_err;
  4119. best_lo = lo;
  4120. best_hi = hi;
  4121. }
  4122. }
  4123. }
  4124. mapping_best_low[m] = best_lo;
  4125. mapping_best_high[m] = best_hi;
  4126. mapping_best_err[m] = best_err;
  4127. highest_best_err = basisu::maximum(highest_best_err, best_err);
  4128. } // m
  4129. for (uint32_t m = 0; m < NUM_ETC1_TO_ASTC_SELECTOR_MAPPINGS; m++)
  4130. {
  4131. uint64_t err = mapping_best_err[m];
  4132. err = basisu::minimum<uint64_t>(err, 0xFFFF);
  4133. fprintf(pFile, "{%u,%u,%u},", mapping_best_low[m], mapping_best_high[m], (uint32_t)err);
  4134. n++;
  4135. if ((n & 31) == 31)
  4136. fprintf(pFile, "\n");
  4137. } // m
  4138. } // sr
  4139. } // g
  4140. } // inten
  4141. fclose(pFile);
  4142. }
  4143. static void create_etc1_to_astc_conversion_table_0_255()
  4144. {
  4145. FILE* pFile = nullptr;
  4146. fopen_s(&pFile, "basisu_transcoder_tables_astc_0_255.inc", "w");
  4147. uint32_t n = 0;
  4148. for (int inten = 0; inten < 8; inten++)
  4149. {
  4150. for (uint32_t g = 0; g < 32; g++)
  4151. {
  4152. color32 block_colors[4];
  4153. decoder_etc_block::get_diff_subblock_colors(block_colors, decoder_etc_block::pack_color5(color32(g, g, g, 255), false), inten);
  4154. for (uint32_t sr = 0; sr < NUM_ETC1_TO_ASTC_SELECTOR_RANGES; sr++)
  4155. {
  4156. const uint32_t low_selector = g_etc1_to_astc_selector_ranges[sr].m_low;
  4157. const uint32_t high_selector = g_etc1_to_astc_selector_ranges[sr].m_high;
  4158. uint32_t mapping_best_low[NUM_ETC1_TO_ASTC_SELECTOR_MAPPINGS];
  4159. uint32_t mapping_best_high[NUM_ETC1_TO_ASTC_SELECTOR_MAPPINGS];
  4160. uint64_t mapping_best_err[NUM_ETC1_TO_ASTC_SELECTOR_MAPPINGS];
  4161. uint64_t highest_best_err = 0;
  4162. for (uint32_t m = 0; m < NUM_ETC1_TO_ASTC_SELECTOR_MAPPINGS; m++)
  4163. {
  4164. uint32_t best_lo = 0;
  4165. uint32_t best_hi = 0;
  4166. uint64_t best_err = UINT64_MAX;
  4167. for (uint32_t hi = 0; hi <= 255; hi++)
  4168. {
  4169. for (uint32_t lo = 0; lo <= 255; lo++)
  4170. {
  4171. uint32_t colors[4];
  4172. for (uint32_t s = 0; s < 4; s++)
  4173. {
  4174. uint32_t s_scaled = s | (s << 2) | (s << 4);
  4175. if (s_scaled > 32)
  4176. s_scaled++;
  4177. uint32_t c0 = lo | (lo << 8);
  4178. uint32_t c1 = hi | (hi << 8);
  4179. colors[s] = ((c0 * (64 - s_scaled) + c1 * s_scaled + 32) / 64) >> 8;
  4180. }
  4181. uint64_t total_err = 0;
  4182. for (uint32_t s = low_selector; s <= high_selector; s++)
  4183. {
  4184. int err = block_colors[s].g - colors[g_etc1_to_astc_selector_mappings[m][s]];
  4185. // Special case when the intensity table is 7, low_selector is 0, and high_selector is 3. In this extreme case, it's likely the encoder is trying to strongly favor
  4186. // the low/high selectors which are clamping to either 0 or 255.
  4187. int err_scale = 1;
  4188. if (((inten == 7) && (low_selector == 0) && (high_selector == 3)) && ((s == 0) || (s == 3)))
  4189. err_scale = 8;
  4190. total_err += (err * err) * err_scale;
  4191. }
  4192. if (total_err < best_err)
  4193. {
  4194. best_err = total_err;
  4195. best_lo = lo;
  4196. best_hi = hi;
  4197. }
  4198. }
  4199. }
  4200. mapping_best_low[m] = best_lo;
  4201. mapping_best_high[m] = best_hi;
  4202. mapping_best_err[m] = best_err;
  4203. highest_best_err = basisu::maximum(highest_best_err, best_err);
  4204. } // m
  4205. for (uint32_t m = 0; m < NUM_ETC1_TO_ASTC_SELECTOR_MAPPINGS; m++)
  4206. {
  4207. uint64_t err = mapping_best_err[m];
  4208. err = basisu::minimum<uint64_t>(err, 0xFFFF);
  4209. fprintf(pFile, "{%u,%u,%u},", mapping_best_low[m], mapping_best_high[m], (uint32_t)err);
  4210. n++;
  4211. if ((n & 31) == 31)
  4212. fprintf(pFile, "\n");
  4213. } // m
  4214. } // sr
  4215. } // g
  4216. } // inten
  4217. fclose(pFile);
  4218. }
  4219. #endif
  4220. #endif
  4221. #if BASISD_SUPPORT_UASTC || BASISD_SUPPORT_ASTC
  4222. // Table encodes 5 trits to 8 output bits. 3^5 entries.
  4223. // Inverse of the trit bit manipulation process in https://www.khronos.org/registry/DataFormat/specs/1.2/dataformat.1.2.html#astc-integer-sequence-encoding
  4224. static const uint8_t g_astc_trit_encode[243] = { 0, 1, 2, 4, 5, 6, 8, 9, 10, 16, 17, 18, 20, 21, 22, 24, 25, 26, 3, 7, 11, 19, 23, 27, 12, 13, 14, 32, 33, 34, 36, 37, 38, 40, 41, 42, 48, 49, 50, 52, 53, 54, 56, 57, 58, 35, 39,
  4225. 43, 51, 55, 59, 44, 45, 46, 64, 65, 66, 68, 69, 70, 72, 73, 74, 80, 81, 82, 84, 85, 86, 88, 89, 90, 67, 71, 75, 83, 87, 91, 76, 77, 78, 128, 129, 130, 132, 133, 134, 136, 137, 138, 144, 145, 146, 148, 149, 150, 152, 153, 154,
  4226. 131, 135, 139, 147, 151, 155, 140, 141, 142, 160, 161, 162, 164, 165, 166, 168, 169, 170, 176, 177, 178, 180, 181, 182, 184, 185, 186, 163, 167, 171, 179, 183, 187, 172, 173, 174, 192, 193, 194, 196, 197, 198, 200, 201, 202,
  4227. 208, 209, 210, 212, 213, 214, 216, 217, 218, 195, 199, 203, 211, 215, 219, 204, 205, 206, 96, 97, 98, 100, 101, 102, 104, 105, 106, 112, 113, 114, 116, 117, 118, 120, 121, 122, 99, 103, 107, 115, 119, 123, 108, 109, 110, 224,
  4228. 225, 226, 228, 229, 230, 232, 233, 234, 240, 241, 242, 244, 245, 246, 248, 249, 250, 227, 231, 235, 243, 247, 251, 236, 237, 238, 28, 29, 30, 60, 61, 62, 92, 93, 94, 156, 157, 158, 188, 189, 190, 220, 221, 222, 31, 63, 95, 159,
  4229. 191, 223, 124, 125, 126 };
  4230. // Extracts bits [low,high]
  4231. static inline uint32_t astc_extract_bits(uint32_t bits, int low, int high)
  4232. {
  4233. return (bits >> low) & ((1 << (high - low + 1)) - 1);
  4234. }
  4235. // Writes bits to output in an endian safe way
  4236. static inline void astc_set_bits(uint32_t* pOutput, int& bit_pos, uint32_t value, uint32_t total_bits)
  4237. {
  4238. uint8_t* pBytes = reinterpret_cast<uint8_t*>(pOutput);
  4239. while (total_bits)
  4240. {
  4241. const uint32_t bits_to_write = basisu::minimum<int>(total_bits, 8 - (bit_pos & 7));
  4242. pBytes[bit_pos >> 3] |= static_cast<uint8_t>(value << (bit_pos & 7));
  4243. bit_pos += bits_to_write;
  4244. total_bits -= bits_to_write;
  4245. value >>= bits_to_write;
  4246. }
  4247. }
  4248. // Encodes 5 values to output, usable for any range that uses trits and bits
  4249. static void astc_encode_trits(uint32_t* pOutput, const uint8_t* pValues, int& bit_pos, int n)
  4250. {
  4251. // First extract the trits and the bits from the 5 input values
  4252. int trits = 0, bits[5];
  4253. const uint32_t bit_mask = (1 << n) - 1;
  4254. for (int i = 0; i < 5; i++)
  4255. {
  4256. static const int s_muls[5] = { 1, 3, 9, 27, 81 };
  4257. const int t = pValues[i] >> n;
  4258. trits += t * s_muls[i];
  4259. bits[i] = pValues[i] & bit_mask;
  4260. }
  4261. // Encode the trits, by inverting the bit manipulations done by the decoder, converting 5 trits into 8-bits.
  4262. // See https://www.khronos.org/registry/DataFormat/specs/1.2/dataformat.1.2.html#astc-integer-sequence-encoding
  4263. assert(trits < 243);
  4264. const int T = g_astc_trit_encode[trits];
  4265. // Now interleave the 8 encoded trit bits with the bits to form the encoded output. See table 94.
  4266. astc_set_bits(pOutput, bit_pos, bits[0] | (astc_extract_bits(T, 0, 1) << n) | (bits[1] << (2 + n)), n * 2 + 2);
  4267. astc_set_bits(pOutput, bit_pos, astc_extract_bits(T, 2, 3) | (bits[2] << 2) | (astc_extract_bits(T, 4, 4) << (2 + n)) | (bits[3] << (3 + n)) | (astc_extract_bits(T, 5, 6) << (3 + n * 2)) |
  4268. (bits[4] << (5 + n * 2)) | (astc_extract_bits(T, 7, 7) << (5 + n * 3)), n * 3 + 6);
  4269. }
  4270. #endif // #if BASISD_SUPPORT_UASTC || BASISD_SUPPORT_ASTC
  4271. #if BASISD_SUPPORT_ASTC
  4272. struct astc_block_params
  4273. {
  4274. // 2 groups of 5, but only a max of 8 are used (RRGGBBAA00)
  4275. uint8_t m_endpoints[10];
  4276. uint8_t m_weights[32];
  4277. };
  4278. // Packs a single format ASTC block using Color Endpoint Mode 12 (LDR RGBA direct), endpoint BISE range 13, 2-bit weights (range 2).
  4279. // We're always going to output blocks containing alpha, even if the input doesn't have alpha, for simplicity.
  4280. // Each block always has 4x4 weights, uses range 13 BISE encoding on the endpoints (0-47), and each weight ranges from 0-3. This encoding should be roughly equal in quality vs. BC1 for color.
  4281. // 8 total endpoints, stored as RGBA LH LH LH LH order, each ranging from 0-47.
  4282. // Note the input [0,47] endpoint values are not linear - they are encoded as outlined in the ASTC spec:
  4283. // https://www.khronos.org/registry/DataFormat/specs/1.2/dataformat.1.2.html#astc-endpoint-unquantization
  4284. // 32 total weights, stored as 16 CA CA, each ranging from 0-3.
  4285. static void astc_pack_block_cem_12_weight_range2(uint32_t *pOutput, const astc_block_params* pBlock)
  4286. {
  4287. uint8_t* pBytes = reinterpret_cast<uint8_t*>(pOutput);
  4288. // Write constant block mode, color component selector, number of partitions, color endpoint mode
  4289. // https://www.khronos.org/registry/DataFormat/specs/1.2/dataformat.1.2.html#_block_mode
  4290. pBytes[0] = 0x42; pBytes[1] = 0x84; pBytes[2] = 0x01; pBytes[3] = 0x00;
  4291. pBytes[4] = 0x00; pBytes[5] = 0x00; pBytes[6] = 0x00; pBytes[7] = 0xc0;
  4292. pOutput[2] = 0;
  4293. pOutput[3] = 0;
  4294. // Pack 8 endpoints (each ranging between [0,47]) using BISE starting at bit 17
  4295. int bit_pos = 17;
  4296. astc_encode_trits(pOutput, pBlock->m_endpoints, bit_pos, 4);
  4297. astc_encode_trits(pOutput, pBlock->m_endpoints + 5, bit_pos, 4);
  4298. // Pack 32 2-bit weights, which are stored from the top down into the block in opposite bit order.
  4299. for (uint32_t i = 0; i < 32; i++)
  4300. {
  4301. static const uint8_t s_reverse_bits[4] = { 0, 2, 1, 3 };
  4302. const uint32_t ofs = 126 - (i * 2);
  4303. pBytes[ofs >> 3] |= (s_reverse_bits[pBlock->m_weights[i]] << (ofs & 7));
  4304. }
  4305. }
  4306. // CEM mode 12 (LDR RGBA Direct), 8-bit endpoints, 1-bit weights
  4307. // This ASTC mode is basically block truncation coding (BTC) using 1-bit weights and 8-bit/component endpoints - very convenient.
  4308. static void astc_pack_block_cem_12_weight_range0(uint32_t* pOutput, const astc_block_params* pBlock)
  4309. {
  4310. uint8_t* pBytes = reinterpret_cast<uint8_t*>(pOutput);
  4311. // Write constant block mode, color component selector, number of partitions, color endpoint mode
  4312. // https://www.khronos.org/registry/DataFormat/specs/1.2/dataformat.1.2.html#_block_mode
  4313. pBytes[0] = 0x41; pBytes[1] = 0x84; pBytes[2] = 0x01; pBytes[3] = 0x00;
  4314. pOutput[1] = 0;
  4315. pBytes[8] = 0x00; pBytes[9] = 0x00; pBytes[10] = 0x00; pBytes[11] = 0xc0;
  4316. pOutput[3] = 0;
  4317. // Pack 8 endpoints (each ranging between [0,255]) as 8-bits starting at bit 17
  4318. int bit_pos = 17;
  4319. for (uint32_t i = 0; i < 8; i++)
  4320. astc_set_bits(pOutput, bit_pos, pBlock->m_endpoints[i], 8);
  4321. // Pack 32 1-bit weights, which are stored from the top down into the block in opposite bit order.
  4322. for (uint32_t i = 0; i < 32; i++)
  4323. {
  4324. const uint32_t ofs = 127 - i;
  4325. pBytes[ofs >> 3] |= (pBlock->m_weights[i] << (ofs & 7));
  4326. }
  4327. }
  4328. #if BASISD_SUPPORT_ASTC_HIGHER_OPAQUE_QUALITY
  4329. // Optional 8-bit endpoint packing functions.
  4330. // CEM mode 4 (LDR Luminance+Alpha Direct), 8-bit endpoints, 2 bit weights
  4331. static void astc_pack_block_cem_4_weight_range2(uint32_t* pOutput, const astc_block_params* pBlock)
  4332. {
  4333. uint8_t* pBytes = reinterpret_cast<uint8_t*>(pOutput);
  4334. // Write constant block mode, color component selector, number of partitions, color endpoint mode
  4335. // https://www.khronos.org/registry/DataFormat/specs/1.2/dataformat.1.2.html#_block_mode
  4336. pBytes[0] = 0x42; pBytes[1] = 0x84; pBytes[2] = 0x00; pBytes[3] = 0x00;
  4337. pBytes[4] = 0x00; pBytes[5] = 0x00; pBytes[6] = 0x00; pBytes[7] = 0xc0;
  4338. pOutput[2] = 0;
  4339. pOutput[3] = 0;
  4340. // Pack 4 endpoints (each ranging between [0,255]) as 8-bits starting at bit 17
  4341. int bit_pos = 17;
  4342. for (uint32_t i = 0; i < 4; i++)
  4343. astc_set_bits(pOutput, bit_pos, pBlock->m_endpoints[i], 8);
  4344. // Pack 32 2-bit weights, which are stored from the top down into the block in opposite bit order.
  4345. for (uint32_t i = 0; i < 32; i++)
  4346. {
  4347. static const uint8_t s_reverse_bits[4] = { 0, 2, 1, 3 };
  4348. const uint32_t ofs = 126 - (i * 2);
  4349. pBytes[ofs >> 3] |= (s_reverse_bits[pBlock->m_weights[i]] << (ofs & 7));
  4350. }
  4351. }
  4352. // CEM mode 8 (LDR RGB Direct), 8-bit endpoints, 2 bit weights
  4353. static void astc_pack_block_cem_8_weight_range2(uint32_t* pOutput, const astc_block_params* pBlock)
  4354. {
  4355. uint8_t* pBytes = reinterpret_cast<uint8_t*>(pOutput);
  4356. // Write constant block mode, color component selector, number of partitions, color endpoint mode
  4357. // https://www.khronos.org/registry/DataFormat/specs/1.2/dataformat.1.2.html#_block_mode
  4358. pBytes[0] = 0x42; pBytes[1] = 0x00; pBytes[2] = 0x01; pBytes[3] = 0x00;
  4359. pOutput[1] = 0;
  4360. pOutput[2] = 0;
  4361. pOutput[3] = 0;
  4362. // Pack 6 endpoints (each ranging between [0,255]) as 8-bits starting at bit 17
  4363. int bit_pos = 17;
  4364. for (uint32_t i = 0; i < 6; i++)
  4365. astc_set_bits(pOutput, bit_pos, pBlock->m_endpoints[i], 8);
  4366. // Pack 16 2-bit weights, which are stored from the top down into the block in opposite bit order.
  4367. for (uint32_t i = 0; i < 16; i++)
  4368. {
  4369. static const uint8_t s_reverse_bits[4] = { 0, 2, 1, 3 };
  4370. const uint32_t ofs = 126 - (i * 2);
  4371. pBytes[ofs >> 3] |= (s_reverse_bits[pBlock->m_weights[i]] << (ofs & 7));
  4372. }
  4373. }
  4374. #endif
  4375. // Optimal quantized [0,47] entry to use given [0,255] input
  4376. static uint8_t g_astc_single_color_encoding_0[256];
  4377. // Optimal quantized [0,47] low/high values given [0,255] input assuming a selector of 1
  4378. static struct
  4379. {
  4380. uint8_t m_lo, m_hi;
  4381. } g_astc_single_color_encoding_1[256];
  4382. static void transcoder_init_astc()
  4383. {
  4384. for (uint32_t base_color = 0; base_color < 32; base_color++)
  4385. {
  4386. for (uint32_t inten_table = 0; inten_table < 8; inten_table++)
  4387. {
  4388. for (uint32_t range_index = 0; range_index < NUM_ETC1_TO_ASTC_SELECTOR_RANGES; range_index++)
  4389. {
  4390. const etc1_to_astc_solution* pTable_g = &g_etc1_to_astc[(inten_table * 32 + base_color) * (NUM_ETC1_TO_ASTC_SELECTOR_RANGES * NUM_ETC1_TO_ASTC_SELECTOR_MAPPINGS) + range_index * NUM_ETC1_TO_ASTC_SELECTOR_MAPPINGS];
  4391. uint32_t best_mapping = 0;
  4392. uint32_t best_err = UINT32_MAX;
  4393. for (uint32_t mapping_index = 0; mapping_index < NUM_ETC1_TO_ASTC_SELECTOR_MAPPINGS; mapping_index++)
  4394. {
  4395. if (pTable_g[mapping_index].m_err < best_err)
  4396. {
  4397. best_err = pTable_g[mapping_index].m_err;
  4398. best_mapping = mapping_index;
  4399. }
  4400. }
  4401. g_etc1_to_astc_best_grayscale_mapping[base_color][inten_table][range_index] = static_cast<uint8_t>(best_mapping);
  4402. }
  4403. }
  4404. }
  4405. #if BASISD_SUPPORT_ASTC_HIGHER_OPAQUE_QUALITY
  4406. for (uint32_t base_color = 0; base_color < 32; base_color++)
  4407. {
  4408. for (uint32_t inten_table = 0; inten_table < 8; inten_table++)
  4409. {
  4410. for (uint32_t range_index = 0; range_index < NUM_ETC1_TO_ASTC_SELECTOR_RANGES; range_index++)
  4411. {
  4412. const etc1_to_astc_solution* pTable_g = &g_etc1_to_astc_0_255[(inten_table * 32 + base_color) * (NUM_ETC1_TO_ASTC_SELECTOR_RANGES * NUM_ETC1_TO_ASTC_SELECTOR_MAPPINGS) + range_index * NUM_ETC1_TO_ASTC_SELECTOR_MAPPINGS];
  4413. uint32_t best_mapping = 0;
  4414. uint32_t best_err = UINT32_MAX;
  4415. for (uint32_t mapping_index = 0; mapping_index < NUM_ETC1_TO_ASTC_SELECTOR_MAPPINGS; mapping_index++)
  4416. {
  4417. if (pTable_g[mapping_index].m_err < best_err)
  4418. {
  4419. best_err = pTable_g[mapping_index].m_err;
  4420. best_mapping = mapping_index;
  4421. }
  4422. }
  4423. g_etc1_to_astc_best_grayscale_mapping_0_255[base_color][inten_table][range_index] = static_cast<uint8_t>(best_mapping);
  4424. }
  4425. }
  4426. }
  4427. #endif
  4428. for (uint32_t i = 0; i < NUM_ETC1_TO_ASTC_SELECTOR_RANGES; i++)
  4429. {
  4430. uint32_t l = g_etc1_to_astc_selector_ranges[i].m_low;
  4431. uint32_t h = g_etc1_to_astc_selector_ranges[i].m_high;
  4432. g_etc1_to_astc_selector_range_index[l][h] = i;
  4433. }
  4434. // Endpoint dequantization, see:
  4435. // https://www.khronos.org/registry/DataFormat/specs/1.2/dataformat.1.2.html#astc-endpoint-unquantization
  4436. for (uint32_t trit = 0; trit < 3; trit++)
  4437. {
  4438. for (uint32_t bit = 0; bit < 16; bit++)
  4439. {
  4440. const uint32_t A = (bit & 1) ? 511 : 0;
  4441. const uint32_t B = (bit >> 1) | ((bit >> 1) << 6);
  4442. const uint32_t C = 22;
  4443. const uint32_t D = trit;
  4444. uint32_t unq = D * C + B;
  4445. unq = unq ^ A;
  4446. unq = (A & 0x80) | (unq >> 2);
  4447. g_ise_to_unquant[bit | (trit << 4)] = unq;
  4448. }
  4449. }
  4450. // Compute table used for optimal single color encoding.
  4451. for (int i = 0; i < 256; i++)
  4452. {
  4453. int lowest_e = INT_MAX;
  4454. for (int lo = 0; lo < 48; lo++)
  4455. {
  4456. for (int hi = 0; hi < 48; hi++)
  4457. {
  4458. const int lo_v = g_ise_to_unquant[lo];
  4459. const int hi_v = g_ise_to_unquant[hi];
  4460. int l = lo_v | (lo_v << 8);
  4461. int h = hi_v | (hi_v << 8);
  4462. int v = ((l * (64 - 21) + (h * 21) + 32) / 64) >> 8;
  4463. int e = abs(v - i);
  4464. if (e < lowest_e)
  4465. {
  4466. g_astc_single_color_encoding_1[i].m_hi = static_cast<uint8_t>(hi);
  4467. g_astc_single_color_encoding_1[i].m_lo = static_cast<uint8_t>(lo);
  4468. lowest_e = e;
  4469. }
  4470. } // hi
  4471. } // lo
  4472. }
  4473. for (int i = 0; i < 256; i++)
  4474. {
  4475. int lowest_e = INT_MAX;
  4476. for (int lo = 0; lo < 48; lo++)
  4477. {
  4478. const int lo_v = g_ise_to_unquant[lo];
  4479. int e = abs(lo_v - i);
  4480. if (e < lowest_e)
  4481. {
  4482. g_astc_single_color_encoding_0[i] = static_cast<uint8_t>(lo);
  4483. lowest_e = e;
  4484. }
  4485. } // lo
  4486. }
  4487. }
  4488. // Converts opaque or color+alpha ETC1S block to ASTC 4x4.
  4489. // This function tries to use the best ASTC mode given the block's actual contents.
  4490. static void convert_etc1s_to_astc_4x4(void* pDst_block, const endpoint* pEndpoints, const selector* pSelector,
  4491. bool transcode_alpha, const endpoint *pEndpoint_codebook, const selector *pSelector_codebook)
  4492. {
  4493. astc_block_params blk;
  4494. blk.m_endpoints[8] = 0;
  4495. blk.m_endpoints[9] = 0;
  4496. int constant_alpha_val = 255;
  4497. int num_unique_alpha_selectors = 1;
  4498. if (transcode_alpha)
  4499. {
  4500. const selector& alpha_selectors = pSelector_codebook[((uint16_t*)pDst_block)[1]];
  4501. num_unique_alpha_selectors = alpha_selectors.m_num_unique_selectors;
  4502. if (num_unique_alpha_selectors == 1)
  4503. {
  4504. const endpoint& alpha_endpoint = pEndpoint_codebook[((uint16_t*)pDst_block)[0]];
  4505. const color32& alpha_base_color = alpha_endpoint.m_color5;
  4506. const uint32_t alpha_inten_table = alpha_endpoint.m_inten5;
  4507. int alpha_block_colors[4];
  4508. decoder_etc_block::get_block_colors5_g(alpha_block_colors, alpha_base_color, alpha_inten_table);
  4509. constant_alpha_val = alpha_block_colors[alpha_selectors.m_lo_selector];
  4510. }
  4511. }
  4512. const color32& base_color = pEndpoints->m_color5;
  4513. const uint32_t inten_table = pEndpoints->m_inten5;
  4514. const uint32_t low_selector = pSelector->m_lo_selector;
  4515. const uint32_t high_selector = pSelector->m_hi_selector;
  4516. // Handle solid color or BTC blocks, which can always be encoded from ETC1S to ASTC losslessly.
  4517. if ((pSelector->m_num_unique_selectors == 1) && (num_unique_alpha_selectors == 1))
  4518. {
  4519. // Both color and alpha are constant, write a solid color block and exit.
  4520. // See https://www.khronos.org/registry/DataFormat/specs/1.2/dataformat.1.2.html#astc-void-extent-blocks
  4521. uint32_t r, g, b;
  4522. decoder_etc_block::get_block_color5(base_color, inten_table, low_selector, r, g, b);
  4523. uint32_t* pOutput = static_cast<uint32_t*>(pDst_block);
  4524. uint8_t* pBytes = reinterpret_cast<uint8_t*>(pDst_block);
  4525. pBytes[0] = 0xfc; pBytes[1] = 0xfd; pBytes[2] = 0xff; pBytes[3] = 0xff;
  4526. pOutput[1] = 0xffffffff;
  4527. pOutput[2] = 0;
  4528. pOutput[3] = 0;
  4529. int bit_pos = 64;
  4530. astc_set_bits(pOutput, bit_pos, r | (r << 8), 16);
  4531. astc_set_bits(pOutput, bit_pos, g | (g << 8), 16);
  4532. astc_set_bits(pOutput, bit_pos, b | (b << 8), 16);
  4533. astc_set_bits(pOutput, bit_pos, constant_alpha_val | (constant_alpha_val << 8), 16);
  4534. return;
  4535. }
  4536. else if ((pSelector->m_num_unique_selectors <= 2) && (num_unique_alpha_selectors <= 2))
  4537. {
  4538. // Both color and alpha use <= 2 unique selectors each.
  4539. // Use block truncation coding, which is lossless with ASTC (8-bit endpoints, 1-bit weights).
  4540. color32 block_colors[4];
  4541. decoder_etc_block::get_block_colors5(block_colors, base_color, inten_table);
  4542. blk.m_endpoints[0] = block_colors[low_selector].r;
  4543. blk.m_endpoints[2] = block_colors[low_selector].g;
  4544. blk.m_endpoints[4] = block_colors[low_selector].b;
  4545. blk.m_endpoints[1] = block_colors[high_selector].r;
  4546. blk.m_endpoints[3] = block_colors[high_selector].g;
  4547. blk.m_endpoints[5] = block_colors[high_selector].b;
  4548. int s0 = blk.m_endpoints[0] + blk.m_endpoints[2] + blk.m_endpoints[4];
  4549. int s1 = blk.m_endpoints[1] + blk.m_endpoints[3] + blk.m_endpoints[5];
  4550. bool invert = false;
  4551. if (s1 < s0)
  4552. {
  4553. std::swap(blk.m_endpoints[0], blk.m_endpoints[1]);
  4554. std::swap(blk.m_endpoints[2], blk.m_endpoints[3]);
  4555. std::swap(blk.m_endpoints[4], blk.m_endpoints[5]);
  4556. invert = true;
  4557. }
  4558. if (transcode_alpha)
  4559. {
  4560. const endpoint& alpha_endpoint = pEndpoint_codebook[((uint16_t*)pDst_block)[0]];
  4561. const selector& alpha_selectors = pSelector_codebook[((uint16_t*)pDst_block)[1]];
  4562. const color32& alpha_base_color = alpha_endpoint.m_color5;
  4563. const uint32_t alpha_inten_table = alpha_endpoint.m_inten5;
  4564. const uint32_t alpha_low_selector = alpha_selectors.m_lo_selector;
  4565. const uint32_t alpha_high_selector = alpha_selectors.m_hi_selector;
  4566. int alpha_block_colors[4];
  4567. decoder_etc_block::get_block_colors5_g(alpha_block_colors, alpha_base_color, alpha_inten_table);
  4568. blk.m_endpoints[6] = static_cast<uint8_t>(alpha_block_colors[alpha_low_selector]);
  4569. blk.m_endpoints[7] = static_cast<uint8_t>(alpha_block_colors[alpha_high_selector]);
  4570. for (uint32_t y = 0; y < 4; y++)
  4571. {
  4572. for (uint32_t x = 0; x < 4; x++)
  4573. {
  4574. uint32_t s = alpha_selectors.get_selector(x, y);
  4575. s = (s == alpha_high_selector) ? 1 : 0;
  4576. blk.m_weights[(x + y * 4) * 2 + 1] = static_cast<uint8_t>(s);
  4577. } // x
  4578. } // y
  4579. }
  4580. else
  4581. {
  4582. blk.m_endpoints[6] = 255;
  4583. blk.m_endpoints[7] = 255;
  4584. for (uint32_t i = 0; i < 16; i++)
  4585. blk.m_weights[i * 2 + 1] = 0;
  4586. }
  4587. for (uint32_t y = 0; y < 4; y++)
  4588. {
  4589. for (uint32_t x = 0; x < 4; x++)
  4590. {
  4591. uint32_t s = pSelector->get_selector(x, y);
  4592. s = (s == high_selector) ? 1 : 0;
  4593. if (invert)
  4594. s = 1 - s;
  4595. blk.m_weights[(x + y * 4) * 2] = static_cast<uint8_t>(s);
  4596. } // x
  4597. } // y
  4598. astc_pack_block_cem_12_weight_range0(reinterpret_cast<uint32_t*>(pDst_block), &blk);
  4599. return;
  4600. }
  4601. // Either alpha and/or color use > 2 unique selectors each, so we must do something more complex.
  4602. #if BASISD_SUPPORT_ASTC_HIGHER_OPAQUE_QUALITY
  4603. // The optional higher quality modes use 8-bits endpoints vs. [0,47] endpoints.
  4604. // If the block's base color is grayscale, all pixels are grayscale, so encode the block as Luminance+Alpha.
  4605. if ((base_color.r == base_color.g) && (base_color.r == base_color.b))
  4606. {
  4607. if (transcode_alpha)
  4608. {
  4609. const endpoint& alpha_endpoint = pEndpoint_codebook[((uint16_t*)pDst_block)[0]];
  4610. const selector& alpha_selectors = pSelector_codebook[((uint16_t*)pDst_block)[1]];
  4611. const color32& alpha_base_color = alpha_endpoint.m_color5;
  4612. const uint32_t alpha_inten_table = alpha_endpoint.m_inten5;
  4613. const uint32_t alpha_low_selector = alpha_selectors.m_lo_selector;
  4614. const uint32_t alpha_high_selector = alpha_selectors.m_hi_selector;
  4615. if (num_unique_alpha_selectors <= 2)
  4616. {
  4617. // Simple alpha block with only 1 or 2 unique values, so use BTC. This is lossless.
  4618. int alpha_block_colors[4];
  4619. decoder_etc_block::get_block_colors5_g(alpha_block_colors, alpha_base_color, alpha_inten_table);
  4620. blk.m_endpoints[2] = static_cast<uint8_t>(alpha_block_colors[alpha_low_selector]);
  4621. blk.m_endpoints[3] = static_cast<uint8_t>(alpha_block_colors[alpha_high_selector]);
  4622. for (uint32_t i = 0; i < 16; i++)
  4623. {
  4624. uint32_t s = alpha_selectors.get_selector(i & 3, i >> 2);
  4625. blk.m_weights[i * 2 + 1] = (s == alpha_high_selector) ? 3 : 0;
  4626. }
  4627. }
  4628. else
  4629. {
  4630. // Convert ETC1S alpha
  4631. const uint32_t alpha_selector_range_table = g_etc1_to_astc_selector_range_index[alpha_low_selector][alpha_high_selector];
  4632. //[32][8][RANGES][MAPPING]
  4633. const etc1_to_astc_solution* pTable_g = &g_etc1_to_astc_0_255[(alpha_inten_table * 32 + alpha_base_color.g) * (NUM_ETC1_TO_ASTC_SELECTOR_RANGES * NUM_ETC1_TO_ASTC_SELECTOR_MAPPINGS) + alpha_selector_range_table * NUM_ETC1_TO_ASTC_SELECTOR_MAPPINGS];
  4634. const uint32_t best_mapping = g_etc1_to_astc_best_grayscale_mapping_0_255[alpha_base_color.g][alpha_inten_table][alpha_selector_range_table];
  4635. blk.m_endpoints[2] = pTable_g[best_mapping].m_lo;
  4636. blk.m_endpoints[3] = pTable_g[best_mapping].m_hi;
  4637. const uint8_t* pSelectors_xlat = &g_etc1_to_astc_selector_mappings[best_mapping][0];
  4638. for (uint32_t y = 0; y < 4; y++)
  4639. {
  4640. for (uint32_t x = 0; x < 4; x++)
  4641. {
  4642. uint32_t s = alpha_selectors.get_selector(x, y);
  4643. uint32_t as = pSelectors_xlat[s];
  4644. blk.m_weights[(x + y * 4) * 2 + 1] = static_cast<uint8_t>(as);
  4645. } // x
  4646. } // y
  4647. }
  4648. }
  4649. else
  4650. {
  4651. // No alpha slice - set output alpha to all 255's
  4652. blk.m_endpoints[2] = 255;
  4653. blk.m_endpoints[3] = 255;
  4654. for (uint32_t i = 0; i < 16; i++)
  4655. blk.m_weights[i * 2 + 1] = 0;
  4656. }
  4657. if (pSelector->m_num_unique_selectors <= 2)
  4658. {
  4659. // Simple color block with only 1 or 2 unique values, so use BTC. This is lossless.
  4660. int block_colors[4];
  4661. decoder_etc_block::get_block_colors5_g(block_colors, base_color, inten_table);
  4662. blk.m_endpoints[0] = static_cast<uint8_t>(block_colors[low_selector]);
  4663. blk.m_endpoints[1] = static_cast<uint8_t>(block_colors[high_selector]);
  4664. for (uint32_t i = 0; i < 16; i++)
  4665. {
  4666. uint32_t s = pSelector->get_selector(i & 3, i >> 2);
  4667. blk.m_weights[i * 2] = (s == high_selector) ? 3 : 0;
  4668. }
  4669. }
  4670. else
  4671. {
  4672. // Convert ETC1S alpha
  4673. const uint32_t selector_range_table = g_etc1_to_astc_selector_range_index[low_selector][high_selector];
  4674. //[32][8][RANGES][MAPPING]
  4675. const etc1_to_astc_solution* pTable_g = &g_etc1_to_astc_0_255[(inten_table * 32 + base_color.g) * (NUM_ETC1_TO_ASTC_SELECTOR_RANGES * NUM_ETC1_TO_ASTC_SELECTOR_MAPPINGS) + selector_range_table * NUM_ETC1_TO_ASTC_SELECTOR_MAPPINGS];
  4676. const uint32_t best_mapping = g_etc1_to_astc_best_grayscale_mapping_0_255[base_color.g][inten_table][selector_range_table];
  4677. blk.m_endpoints[0] = pTable_g[best_mapping].m_lo;
  4678. blk.m_endpoints[1] = pTable_g[best_mapping].m_hi;
  4679. const uint8_t* pSelectors_xlat = &g_etc1_to_astc_selector_mappings[best_mapping][0];
  4680. for (uint32_t y = 0; y < 4; y++)
  4681. {
  4682. for (uint32_t x = 0; x < 4; x++)
  4683. {
  4684. uint32_t s = pSelector->get_selector(x, y);
  4685. uint32_t as = pSelectors_xlat[s];
  4686. blk.m_weights[(x + y * 4) * 2] = static_cast<uint8_t>(as);
  4687. } // x
  4688. } // y
  4689. }
  4690. astc_pack_block_cem_4_weight_range2(reinterpret_cast<uint32_t*>(pDst_block), &blk);
  4691. return;
  4692. }
  4693. // The block isn't grayscale and it uses > 2 unique selectors for opaque and/or alpha.
  4694. // Check for fully opaque blocks, if so use 8-bit endpoints for slightly higher opaque quality (higher than BC1, but lower than BC7 mode 6 opaque).
  4695. if ((num_unique_alpha_selectors == 1) && (constant_alpha_val == 255))
  4696. {
  4697. // Convert ETC1S color
  4698. const uint32_t selector_range_table = g_etc1_to_astc_selector_range_index[low_selector][high_selector];
  4699. //[32][8][RANGES][MAPPING]
  4700. const etc1_to_astc_solution* pTable_r = &g_etc1_to_astc_0_255[(inten_table * 32 + base_color.r) * (NUM_ETC1_TO_ASTC_SELECTOR_RANGES * NUM_ETC1_TO_ASTC_SELECTOR_MAPPINGS) + selector_range_table * NUM_ETC1_TO_ASTC_SELECTOR_MAPPINGS];
  4701. const etc1_to_astc_solution* pTable_g = &g_etc1_to_astc_0_255[(inten_table * 32 + base_color.g) * (NUM_ETC1_TO_ASTC_SELECTOR_RANGES * NUM_ETC1_TO_ASTC_SELECTOR_MAPPINGS) + selector_range_table * NUM_ETC1_TO_ASTC_SELECTOR_MAPPINGS];
  4702. const etc1_to_astc_solution* pTable_b = &g_etc1_to_astc_0_255[(inten_table * 32 + base_color.b) * (NUM_ETC1_TO_ASTC_SELECTOR_RANGES * NUM_ETC1_TO_ASTC_SELECTOR_MAPPINGS) + selector_range_table * NUM_ETC1_TO_ASTC_SELECTOR_MAPPINGS];
  4703. uint32_t best_err = UINT_MAX;
  4704. uint32_t best_mapping = 0;
  4705. assert(NUM_ETC1_TO_ASTC_SELECTOR_MAPPINGS == 10);
  4706. #define DO_ITER(m) { uint32_t total_err = pTable_r[m].m_err + pTable_g[m].m_err + pTable_b[m].m_err; if (total_err < best_err) { best_err = total_err; best_mapping = m; } }
  4707. DO_ITER(0); DO_ITER(1); DO_ITER(2); DO_ITER(3); DO_ITER(4);
  4708. DO_ITER(5); DO_ITER(6); DO_ITER(7); DO_ITER(8); DO_ITER(9);
  4709. #undef DO_ITER
  4710. blk.m_endpoints[0] = pTable_r[best_mapping].m_lo;
  4711. blk.m_endpoints[1] = pTable_r[best_mapping].m_hi;
  4712. blk.m_endpoints[2] = pTable_g[best_mapping].m_lo;
  4713. blk.m_endpoints[3] = pTable_g[best_mapping].m_hi;
  4714. blk.m_endpoints[4] = pTable_b[best_mapping].m_lo;
  4715. blk.m_endpoints[5] = pTable_b[best_mapping].m_hi;
  4716. int s0 = blk.m_endpoints[0] + blk.m_endpoints[2] + blk.m_endpoints[4];
  4717. int s1 = blk.m_endpoints[1] + blk.m_endpoints[3] + blk.m_endpoints[5];
  4718. bool invert = false;
  4719. if (s1 < s0)
  4720. {
  4721. std::swap(blk.m_endpoints[0], blk.m_endpoints[1]);
  4722. std::swap(blk.m_endpoints[2], blk.m_endpoints[3]);
  4723. std::swap(blk.m_endpoints[4], blk.m_endpoints[5]);
  4724. invert = true;
  4725. }
  4726. const uint8_t* pSelectors_xlat = &g_etc1_to_astc_selector_mappings[best_mapping][0];
  4727. for (uint32_t y = 0; y < 4; y++)
  4728. {
  4729. for (uint32_t x = 0; x < 4; x++)
  4730. {
  4731. uint32_t s = pSelector->get_selector(x, y);
  4732. uint32_t as = pSelectors_xlat[s];
  4733. if (invert)
  4734. as = 3 - as;
  4735. blk.m_weights[x + y * 4] = static_cast<uint8_t>(as);
  4736. } // x
  4737. } // y
  4738. // Now pack to ASTC
  4739. astc_pack_block_cem_8_weight_range2(reinterpret_cast<uint32_t*>(pDst_block), &blk);
  4740. return;
  4741. }
  4742. #endif //#if BASISD_SUPPORT_ASTC_HIGHER_OPAQUE_QUALITY
  4743. // Nothing else worked, so fall back to CEM Mode 12 (LDR RGBA Direct), [0,47] endpoints, weight range 2 (2-bit weights), dual planes.
  4744. // This mode can handle everything, but at slightly less quality than BC1.
  4745. if (transcode_alpha)
  4746. {
  4747. const endpoint& alpha_endpoint = pEndpoint_codebook[((uint16_t*)pDst_block)[0]];
  4748. const selector& alpha_selectors = pSelector_codebook[((uint16_t*)pDst_block)[1]];
  4749. const color32& alpha_base_color = alpha_endpoint.m_color5;
  4750. const uint32_t alpha_inten_table = alpha_endpoint.m_inten5;
  4751. const uint32_t alpha_low_selector = alpha_selectors.m_lo_selector;
  4752. const uint32_t alpha_high_selector = alpha_selectors.m_hi_selector;
  4753. if (alpha_low_selector == alpha_high_selector)
  4754. {
  4755. // Solid alpha block - use precomputed tables.
  4756. int alpha_block_colors[4];
  4757. decoder_etc_block::get_block_colors5_g(alpha_block_colors, alpha_base_color, alpha_inten_table);
  4758. const uint32_t g = alpha_block_colors[alpha_low_selector];
  4759. blk.m_endpoints[6] = g_astc_single_color_encoding_1[g].m_lo;
  4760. blk.m_endpoints[7] = g_astc_single_color_encoding_1[g].m_hi;
  4761. for (uint32_t i = 0; i < 16; i++)
  4762. blk.m_weights[i * 2 + 1] = 1;
  4763. }
  4764. else if ((alpha_inten_table >= 7) && (alpha_selectors.m_num_unique_selectors == 2) && (alpha_low_selector == 0) && (alpha_high_selector == 3))
  4765. {
  4766. // Handle outlier case where only the two outer colors are used with inten table 7.
  4767. color32 alpha_block_colors[4];
  4768. decoder_etc_block::get_block_colors5(alpha_block_colors, alpha_base_color, alpha_inten_table);
  4769. const uint32_t g0 = alpha_block_colors[0].g;
  4770. const uint32_t g1 = alpha_block_colors[3].g;
  4771. blk.m_endpoints[6] = g_astc_single_color_encoding_0[g0];
  4772. blk.m_endpoints[7] = g_astc_single_color_encoding_0[g1];
  4773. for (uint32_t y = 0; y < 4; y++)
  4774. {
  4775. for (uint32_t x = 0; x < 4; x++)
  4776. {
  4777. uint32_t s = alpha_selectors.get_selector(x, y);
  4778. uint32_t as = (s == alpha_high_selector) ? 3 : 0;
  4779. blk.m_weights[(x + y * 4) * 2 + 1] = static_cast<uint8_t>(as);
  4780. } // x
  4781. } // y
  4782. }
  4783. else
  4784. {
  4785. // Convert ETC1S alpha
  4786. const uint32_t alpha_selector_range_table = g_etc1_to_astc_selector_range_index[alpha_low_selector][alpha_high_selector];
  4787. //[32][8][RANGES][MAPPING]
  4788. const etc1_to_astc_solution* pTable_g = &g_etc1_to_astc[(alpha_inten_table * 32 + alpha_base_color.g) * (NUM_ETC1_TO_ASTC_SELECTOR_RANGES * NUM_ETC1_TO_ASTC_SELECTOR_MAPPINGS) + alpha_selector_range_table * NUM_ETC1_TO_ASTC_SELECTOR_MAPPINGS];
  4789. const uint32_t best_mapping = g_etc1_to_astc_best_grayscale_mapping[alpha_base_color.g][alpha_inten_table][alpha_selector_range_table];
  4790. blk.m_endpoints[6] = pTable_g[best_mapping].m_lo;
  4791. blk.m_endpoints[7] = pTable_g[best_mapping].m_hi;
  4792. const uint8_t* pSelectors_xlat = &g_etc1_to_astc_selector_mappings[best_mapping][0];
  4793. for (uint32_t y = 0; y < 4; y++)
  4794. {
  4795. for (uint32_t x = 0; x < 4; x++)
  4796. {
  4797. uint32_t s = alpha_selectors.get_selector(x, y);
  4798. uint32_t as = pSelectors_xlat[s];
  4799. blk.m_weights[(x + y * 4) * 2 + 1] = static_cast<uint8_t>(as);
  4800. } // x
  4801. } // y
  4802. }
  4803. }
  4804. else
  4805. {
  4806. // No alpha slice - set output alpha to all 255's
  4807. // 1 is 255 when dequantized
  4808. blk.m_endpoints[6] = 1;
  4809. blk.m_endpoints[7] = 1;
  4810. for (uint32_t i = 0; i < 16; i++)
  4811. blk.m_weights[i * 2 + 1] = 0;
  4812. }
  4813. if (low_selector == high_selector)
  4814. {
  4815. // Solid color block - use precomputed tables of optimal endpoints assuming selector weights are all 1.
  4816. color32 block_colors[4];
  4817. decoder_etc_block::get_block_colors5(block_colors, base_color, inten_table);
  4818. const uint32_t r = block_colors[low_selector].r;
  4819. const uint32_t g = block_colors[low_selector].g;
  4820. const uint32_t b = block_colors[low_selector].b;
  4821. blk.m_endpoints[0] = g_astc_single_color_encoding_1[r].m_lo;
  4822. blk.m_endpoints[1] = g_astc_single_color_encoding_1[r].m_hi;
  4823. blk.m_endpoints[2] = g_astc_single_color_encoding_1[g].m_lo;
  4824. blk.m_endpoints[3] = g_astc_single_color_encoding_1[g].m_hi;
  4825. blk.m_endpoints[4] = g_astc_single_color_encoding_1[b].m_lo;
  4826. blk.m_endpoints[5] = g_astc_single_color_encoding_1[b].m_hi;
  4827. int s0 = g_ise_to_unquant[blk.m_endpoints[0]] + g_ise_to_unquant[blk.m_endpoints[2]] + g_ise_to_unquant[blk.m_endpoints[4]];
  4828. int s1 = g_ise_to_unquant[blk.m_endpoints[1]] + g_ise_to_unquant[blk.m_endpoints[3]] + g_ise_to_unquant[blk.m_endpoints[5]];
  4829. bool invert = false;
  4830. if (s1 < s0)
  4831. {
  4832. std::swap(blk.m_endpoints[0], blk.m_endpoints[1]);
  4833. std::swap(blk.m_endpoints[2], blk.m_endpoints[3]);
  4834. std::swap(blk.m_endpoints[4], blk.m_endpoints[5]);
  4835. invert = true;
  4836. }
  4837. for (uint32_t i = 0; i < 16; i++)
  4838. blk.m_weights[i * 2] = invert ? 2 : 1;
  4839. }
  4840. else if ((inten_table >= 7) && (pSelector->m_num_unique_selectors == 2) && (pSelector->m_lo_selector == 0) && (pSelector->m_hi_selector == 3))
  4841. {
  4842. // Handle outlier case where only the two outer colors are used with inten table 7.
  4843. color32 block_colors[4];
  4844. decoder_etc_block::get_block_colors5(block_colors, base_color, inten_table);
  4845. const uint32_t r0 = block_colors[0].r;
  4846. const uint32_t g0 = block_colors[0].g;
  4847. const uint32_t b0 = block_colors[0].b;
  4848. const uint32_t r1 = block_colors[3].r;
  4849. const uint32_t g1 = block_colors[3].g;
  4850. const uint32_t b1 = block_colors[3].b;
  4851. blk.m_endpoints[0] = g_astc_single_color_encoding_0[r0];
  4852. blk.m_endpoints[1] = g_astc_single_color_encoding_0[r1];
  4853. blk.m_endpoints[2] = g_astc_single_color_encoding_0[g0];
  4854. blk.m_endpoints[3] = g_astc_single_color_encoding_0[g1];
  4855. blk.m_endpoints[4] = g_astc_single_color_encoding_0[b0];
  4856. blk.m_endpoints[5] = g_astc_single_color_encoding_0[b1];
  4857. int s0 = g_ise_to_unquant[blk.m_endpoints[0]] + g_ise_to_unquant[blk.m_endpoints[2]] + g_ise_to_unquant[blk.m_endpoints[4]];
  4858. int s1 = g_ise_to_unquant[blk.m_endpoints[1]] + g_ise_to_unquant[blk.m_endpoints[3]] + g_ise_to_unquant[blk.m_endpoints[5]];
  4859. bool invert = false;
  4860. if (s1 < s0)
  4861. {
  4862. std::swap(blk.m_endpoints[0], blk.m_endpoints[1]);
  4863. std::swap(blk.m_endpoints[2], blk.m_endpoints[3]);
  4864. std::swap(blk.m_endpoints[4], blk.m_endpoints[5]);
  4865. invert = true;
  4866. }
  4867. for (uint32_t y = 0; y < 4; y++)
  4868. {
  4869. for (uint32_t x = 0; x < 4; x++)
  4870. {
  4871. uint32_t s = pSelector->get_selector(x, y);
  4872. uint32_t as = (s == low_selector) ? 0 : 3;
  4873. if (invert)
  4874. as = 3 - as;
  4875. blk.m_weights[(x + y * 4) * 2] = static_cast<uint8_t>(as);
  4876. } // x
  4877. } // y
  4878. }
  4879. else
  4880. {
  4881. // Convert ETC1S color
  4882. const uint32_t selector_range_table = g_etc1_to_astc_selector_range_index[low_selector][high_selector];
  4883. //[32][8][RANGES][MAPPING]
  4884. const etc1_to_astc_solution* pTable_r = &g_etc1_to_astc[(inten_table * 32 + base_color.r) * (NUM_ETC1_TO_ASTC_SELECTOR_RANGES * NUM_ETC1_TO_ASTC_SELECTOR_MAPPINGS) + selector_range_table * NUM_ETC1_TO_ASTC_SELECTOR_MAPPINGS];
  4885. const etc1_to_astc_solution* pTable_g = &g_etc1_to_astc[(inten_table * 32 + base_color.g) * (NUM_ETC1_TO_ASTC_SELECTOR_RANGES * NUM_ETC1_TO_ASTC_SELECTOR_MAPPINGS) + selector_range_table * NUM_ETC1_TO_ASTC_SELECTOR_MAPPINGS];
  4886. const etc1_to_astc_solution* pTable_b = &g_etc1_to_astc[(inten_table * 32 + base_color.b) * (NUM_ETC1_TO_ASTC_SELECTOR_RANGES * NUM_ETC1_TO_ASTC_SELECTOR_MAPPINGS) + selector_range_table * NUM_ETC1_TO_ASTC_SELECTOR_MAPPINGS];
  4887. uint32_t best_err = UINT_MAX;
  4888. uint32_t best_mapping = 0;
  4889. assert(NUM_ETC1_TO_ASTC_SELECTOR_MAPPINGS == 10);
  4890. #define DO_ITER(m) { uint32_t total_err = pTable_r[m].m_err + pTable_g[m].m_err + pTable_b[m].m_err; if (total_err < best_err) { best_err = total_err; best_mapping = m; } }
  4891. DO_ITER(0); DO_ITER(1); DO_ITER(2); DO_ITER(3); DO_ITER(4);
  4892. DO_ITER(5); DO_ITER(6); DO_ITER(7); DO_ITER(8); DO_ITER(9);
  4893. #undef DO_ITER
  4894. blk.m_endpoints[0] = pTable_r[best_mapping].m_lo;
  4895. blk.m_endpoints[1] = pTable_r[best_mapping].m_hi;
  4896. blk.m_endpoints[2] = pTable_g[best_mapping].m_lo;
  4897. blk.m_endpoints[3] = pTable_g[best_mapping].m_hi;
  4898. blk.m_endpoints[4] = pTable_b[best_mapping].m_lo;
  4899. blk.m_endpoints[5] = pTable_b[best_mapping].m_hi;
  4900. int s0 = g_ise_to_unquant[blk.m_endpoints[0]] + g_ise_to_unquant[blk.m_endpoints[2]] + g_ise_to_unquant[blk.m_endpoints[4]];
  4901. int s1 = g_ise_to_unquant[blk.m_endpoints[1]] + g_ise_to_unquant[blk.m_endpoints[3]] + g_ise_to_unquant[blk.m_endpoints[5]];
  4902. bool invert = false;
  4903. if (s1 < s0)
  4904. {
  4905. std::swap(blk.m_endpoints[0], blk.m_endpoints[1]);
  4906. std::swap(blk.m_endpoints[2], blk.m_endpoints[3]);
  4907. std::swap(blk.m_endpoints[4], blk.m_endpoints[5]);
  4908. invert = true;
  4909. }
  4910. const uint8_t* pSelectors_xlat = &g_etc1_to_astc_selector_mappings[best_mapping][0];
  4911. for (uint32_t y = 0; y < 4; y++)
  4912. {
  4913. for (uint32_t x = 0; x < 4; x++)
  4914. {
  4915. uint32_t s = pSelector->get_selector(x, y);
  4916. uint32_t as = pSelectors_xlat[s];
  4917. if (invert)
  4918. as = 3 - as;
  4919. blk.m_weights[(x + y * 4) * 2] = static_cast<uint8_t>(as);
  4920. } // x
  4921. } // y
  4922. }
  4923. // Now pack to ASTC
  4924. astc_pack_block_cem_12_weight_range2(reinterpret_cast<uint32_t *>(pDst_block), &blk);
  4925. }
  4926. #endif
  4927. #if BASISD_SUPPORT_ATC
  4928. // ATC and PVRTC2 both use these tables.
  4929. struct etc1s_to_atc_solution
  4930. {
  4931. uint8_t m_lo;
  4932. uint8_t m_hi;
  4933. uint16_t m_err;
  4934. };
  4935. static dxt_selector_range g_etc1s_to_atc_selector_ranges[] =
  4936. {
  4937. { 0, 3 },
  4938. { 1, 3 },
  4939. { 0, 2 },
  4940. { 1, 2 },
  4941. { 2, 3 },
  4942. { 0, 1 },
  4943. };
  4944. const uint32_t NUM_ETC1S_TO_ATC_SELECTOR_RANGES = sizeof(g_etc1s_to_atc_selector_ranges) / sizeof(g_etc1s_to_atc_selector_ranges[0]);
  4945. static uint32_t g_etc1s_to_atc_selector_range_index[4][4];
  4946. const uint32_t NUM_ETC1S_TO_ATC_SELECTOR_MAPPINGS = 10;
  4947. static const uint8_t g_etc1s_to_atc_selector_mappings[NUM_ETC1S_TO_ATC_SELECTOR_MAPPINGS][4] =
  4948. {
  4949. { 0, 0, 1, 1 },
  4950. { 0, 0, 1, 2 },
  4951. { 0, 0, 1, 3 },
  4952. { 0, 0, 2, 3 },
  4953. { 0, 1, 1, 1 },
  4954. { 0, 1, 2, 2 },
  4955. { 0, 1, 2, 3 }, //6 - identity
  4956. { 0, 2, 3, 3 },
  4957. { 1, 2, 2, 2 },
  4958. { 1, 2, 3, 3 },
  4959. };
  4960. const uint32_t ATC_IDENTITY_SELECTOR_MAPPING_INDEX = 6;
  4961. #if BASISD_SUPPORT_PVRTC2
  4962. static const etc1s_to_atc_solution g_etc1s_to_pvrtc2_45[32 * 8 * NUM_ETC1S_TO_ATC_SELECTOR_MAPPINGS * NUM_ETC1S_TO_ATC_SELECTOR_RANGES] = {
  4963. #include "basisu_transcoder_tables_pvrtc2_45.inc"
  4964. };
  4965. #if 0
  4966. static const etc1s_to_atc_solution g_etc1s_to_pvrtc2_alpha_33[32 * 8 * NUM_ETC1S_TO_ATC_SELECTOR_MAPPINGS * NUM_ETC1S_TO_ATC_SELECTOR_RANGES] = {
  4967. #include "basisu_transcoder_tables_pvrtc2_alpha_33.inc"
  4968. };
  4969. #endif
  4970. #endif
  4971. static const etc1s_to_atc_solution g_etc1s_to_atc_55[32 * 8 * NUM_ETC1S_TO_ATC_SELECTOR_MAPPINGS * NUM_ETC1S_TO_ATC_SELECTOR_RANGES] = {
  4972. #include "basisu_transcoder_tables_atc_55.inc"
  4973. };
  4974. static const etc1s_to_atc_solution g_etc1s_to_atc_56[32 * 8 * NUM_ETC1S_TO_ATC_SELECTOR_MAPPINGS * NUM_ETC1S_TO_ATC_SELECTOR_RANGES] = {
  4975. #include "basisu_transcoder_tables_atc_56.inc"
  4976. };
  4977. struct atc_match_entry
  4978. {
  4979. uint8_t m_lo;
  4980. uint8_t m_hi;
  4981. };
  4982. static atc_match_entry g_pvrtc2_match45_equals_1[256], g_atc_match55_equals_1[256], g_atc_match56_equals_1[256]; // selector 1
  4983. static atc_match_entry g_pvrtc2_match4[256], g_atc_match5[256], g_atc_match6[256];
  4984. static void prepare_atc_single_color_table(atc_match_entry* pTable, int size0, int size1, int sel)
  4985. {
  4986. for (int i = 0; i < 256; i++)
  4987. {
  4988. int lowest_e = 256;
  4989. for (int lo = 0; lo < size0; lo++)
  4990. {
  4991. int lo_e = lo;
  4992. if (size0 == 16)
  4993. {
  4994. lo_e = (lo_e << 1) | (lo_e >> 3);
  4995. lo_e = (lo_e << 3) | (lo_e >> 2);
  4996. }
  4997. else if (size0 == 32)
  4998. lo_e = (lo_e << 3) | (lo_e >> 2);
  4999. else
  5000. lo_e = (lo_e << 2) | (lo_e >> 4);
  5001. for (int hi = 0; hi < size1; hi++)
  5002. {
  5003. int hi_e = hi;
  5004. if (size1 == 16)
  5005. {
  5006. // This is only for PVRTC2 - expand to 5 then 8
  5007. hi_e = (hi_e << 1) | (hi_e >> 3);
  5008. hi_e = (hi_e << 3) | (hi_e >> 2);
  5009. }
  5010. else if (size1 == 32)
  5011. hi_e = (hi_e << 3) | (hi_e >> 2);
  5012. else
  5013. hi_e = (hi_e << 2) | (hi_e >> 4);
  5014. int e;
  5015. if (sel == 1)
  5016. {
  5017. // Selector 1
  5018. e = abs(((lo_e * 5 + hi_e * 3) / 8) - i);
  5019. }
  5020. else
  5021. {
  5022. assert(sel == 3);
  5023. // Selector 3
  5024. e = abs(hi_e - i);
  5025. }
  5026. if (e < lowest_e)
  5027. {
  5028. pTable[i].m_lo = static_cast<uint8_t>(lo);
  5029. pTable[i].m_hi = static_cast<uint8_t>(hi);
  5030. lowest_e = e;
  5031. }
  5032. } // hi
  5033. } // lo
  5034. } // i
  5035. }
  5036. static void transcoder_init_atc()
  5037. {
  5038. prepare_atc_single_color_table(g_pvrtc2_match45_equals_1, 16, 32, 1);
  5039. prepare_atc_single_color_table(g_atc_match55_equals_1, 32, 32, 1);
  5040. prepare_atc_single_color_table(g_atc_match56_equals_1, 32, 64, 1);
  5041. prepare_atc_single_color_table(g_pvrtc2_match4, 1, 16, 3);
  5042. prepare_atc_single_color_table(g_atc_match5, 1, 32, 3);
  5043. prepare_atc_single_color_table(g_atc_match6, 1, 64, 3);
  5044. for (uint32_t i = 0; i < NUM_ETC1S_TO_ATC_SELECTOR_RANGES; i++)
  5045. {
  5046. uint32_t l = g_etc1s_to_atc_selector_ranges[i].m_low;
  5047. uint32_t h = g_etc1s_to_atc_selector_ranges[i].m_high;
  5048. g_etc1s_to_atc_selector_range_index[l][h] = i;
  5049. }
  5050. }
  5051. struct atc_block
  5052. {
  5053. uint8_t m_lo[2];
  5054. uint8_t m_hi[2];
  5055. uint8_t m_sels[4];
  5056. void set_low_color(uint32_t r, uint32_t g, uint32_t b)
  5057. {
  5058. assert((r < 32) && (g < 32) && (b < 32));
  5059. uint32_t x = (r << 10) | (g << 5) | b;
  5060. m_lo[0] = x & 0xFF;
  5061. m_lo[1] = (x >> 8) & 0xFF;
  5062. }
  5063. void set_high_color(uint32_t r, uint32_t g, uint32_t b)
  5064. {
  5065. assert((r < 32) && (g < 64) && (b < 32));
  5066. uint32_t x = (r << 11) | (g << 5) | b;
  5067. m_hi[0] = x & 0xFF;
  5068. m_hi[1] = (x >> 8) & 0xFF;
  5069. }
  5070. };
  5071. static void convert_etc1s_to_atc(void* pDst, const endpoint* pEndpoints, const selector* pSelector)
  5072. {
  5073. atc_block* pBlock = static_cast<atc_block*>(pDst);
  5074. const uint32_t low_selector = pSelector->m_lo_selector;
  5075. const uint32_t high_selector = pSelector->m_hi_selector;
  5076. const color32& base_color = pEndpoints->m_color5;
  5077. const uint32_t inten_table = pEndpoints->m_inten5;
  5078. if (low_selector == high_selector)
  5079. {
  5080. uint32_t r, g, b;
  5081. decoder_etc_block::get_block_color5(base_color, inten_table, low_selector, r, g, b);
  5082. pBlock->set_low_color(g_atc_match55_equals_1[r].m_lo, g_atc_match56_equals_1[g].m_lo, g_atc_match55_equals_1[b].m_lo);
  5083. pBlock->set_high_color(g_atc_match55_equals_1[r].m_hi, g_atc_match56_equals_1[g].m_hi, g_atc_match55_equals_1[b].m_hi);
  5084. pBlock->m_sels[0] = 0x55;
  5085. pBlock->m_sels[1] = 0x55;
  5086. pBlock->m_sels[2] = 0x55;
  5087. pBlock->m_sels[3] = 0x55;
  5088. return;
  5089. }
  5090. else if ((inten_table >= 7) && (pSelector->m_num_unique_selectors == 2) && (pSelector->m_lo_selector == 0) && (pSelector->m_hi_selector == 3))
  5091. {
  5092. color32 block_colors[4];
  5093. decoder_etc_block::get_block_colors5(block_colors, base_color, inten_table);
  5094. const uint32_t r0 = block_colors[0].r;
  5095. const uint32_t g0 = block_colors[0].g;
  5096. const uint32_t b0 = block_colors[0].b;
  5097. const uint32_t r1 = block_colors[3].r;
  5098. const uint32_t g1 = block_colors[3].g;
  5099. const uint32_t b1 = block_colors[3].b;
  5100. pBlock->set_low_color(g_atc_match5[r0].m_hi, g_atc_match5[g0].m_hi, g_atc_match5[b0].m_hi);
  5101. pBlock->set_high_color(g_atc_match5[r1].m_hi, g_atc_match6[g1].m_hi, g_atc_match5[b1].m_hi);
  5102. pBlock->m_sels[0] = pSelector->m_selectors[0];
  5103. pBlock->m_sels[1] = pSelector->m_selectors[1];
  5104. pBlock->m_sels[2] = pSelector->m_selectors[2];
  5105. pBlock->m_sels[3] = pSelector->m_selectors[3];
  5106. return;
  5107. }
  5108. const uint32_t selector_range_table = g_etc1s_to_atc_selector_range_index[low_selector][high_selector];
  5109. //[32][8][RANGES][MAPPING]
  5110. const etc1s_to_atc_solution* pTable_r = &g_etc1s_to_atc_55[(inten_table * 32 + base_color.r) * (NUM_ETC1S_TO_ATC_SELECTOR_RANGES * NUM_ETC1S_TO_ATC_SELECTOR_MAPPINGS) + selector_range_table * NUM_ETC1S_TO_ATC_SELECTOR_MAPPINGS];
  5111. const etc1s_to_atc_solution* pTable_g = &g_etc1s_to_atc_56[(inten_table * 32 + base_color.g) * (NUM_ETC1S_TO_ATC_SELECTOR_RANGES * NUM_ETC1S_TO_ATC_SELECTOR_MAPPINGS) + selector_range_table * NUM_ETC1S_TO_ATC_SELECTOR_MAPPINGS];
  5112. const etc1s_to_atc_solution* pTable_b = &g_etc1s_to_atc_55[(inten_table * 32 + base_color.b) * (NUM_ETC1S_TO_ATC_SELECTOR_RANGES * NUM_ETC1S_TO_ATC_SELECTOR_MAPPINGS) + selector_range_table * NUM_ETC1S_TO_ATC_SELECTOR_MAPPINGS];
  5113. uint32_t best_err = UINT_MAX;
  5114. uint32_t best_mapping = 0;
  5115. assert(NUM_ETC1S_TO_ATC_SELECTOR_MAPPINGS == 10);
  5116. #define DO_ITER(m) { uint32_t total_err = pTable_r[m].m_err + pTable_g[m].m_err + pTable_b[m].m_err; if (total_err < best_err) { best_err = total_err; best_mapping = m; } }
  5117. DO_ITER(0); DO_ITER(1); DO_ITER(2); DO_ITER(3); DO_ITER(4);
  5118. DO_ITER(5); DO_ITER(6); DO_ITER(7); DO_ITER(8); DO_ITER(9);
  5119. #undef DO_ITER
  5120. pBlock->set_low_color(pTable_r[best_mapping].m_lo, pTable_g[best_mapping].m_lo, pTable_b[best_mapping].m_lo);
  5121. pBlock->set_high_color(pTable_r[best_mapping].m_hi, pTable_g[best_mapping].m_hi, pTable_b[best_mapping].m_hi);
  5122. if (ATC_IDENTITY_SELECTOR_MAPPING_INDEX == best_mapping)
  5123. {
  5124. pBlock->m_sels[0] = pSelector->m_selectors[0];
  5125. pBlock->m_sels[1] = pSelector->m_selectors[1];
  5126. pBlock->m_sels[2] = pSelector->m_selectors[2];
  5127. pBlock->m_sels[3] = pSelector->m_selectors[3];
  5128. }
  5129. else
  5130. {
  5131. const uint8_t* pSelectors_xlat = &g_etc1s_to_atc_selector_mappings[best_mapping][0];
  5132. const uint32_t sel_bits0 = pSelector->m_selectors[0];
  5133. const uint32_t sel_bits1 = pSelector->m_selectors[1];
  5134. const uint32_t sel_bits2 = pSelector->m_selectors[2];
  5135. const uint32_t sel_bits3 = pSelector->m_selectors[3];
  5136. uint32_t atc_sels0 = 0, atc_sels1 = 0, atc_sels2 = 0, atc_sels3 = 0;
  5137. #define DO_X(x) { \
  5138. const uint32_t x_shift = (x) * 2; \
  5139. atc_sels0 |= (pSelectors_xlat[(sel_bits0 >> x_shift) & 3] << x_shift); \
  5140. atc_sels1 |= (pSelectors_xlat[(sel_bits1 >> x_shift) & 3] << x_shift); \
  5141. atc_sels2 |= (pSelectors_xlat[(sel_bits2 >> x_shift) & 3] << x_shift); \
  5142. atc_sels3 |= (pSelectors_xlat[(sel_bits3 >> x_shift) & 3] << x_shift); }
  5143. DO_X(0);
  5144. DO_X(1);
  5145. DO_X(2);
  5146. DO_X(3);
  5147. #undef DO_X
  5148. pBlock->m_sels[0] = (uint8_t)atc_sels0;
  5149. pBlock->m_sels[1] = (uint8_t)atc_sels1;
  5150. pBlock->m_sels[2] = (uint8_t)atc_sels2;
  5151. pBlock->m_sels[3] = (uint8_t)atc_sels3;
  5152. }
  5153. }
  5154. #if BASISD_WRITE_NEW_ATC_TABLES
  5155. static void create_etc1s_to_atc_conversion_tables()
  5156. {
  5157. // ATC 55
  5158. FILE* pFile = nullptr;
  5159. fopen_s(&pFile, "basisu_transcoder_tables_atc_55.inc", "w");
  5160. uint32_t n = 0;
  5161. for (int inten = 0; inten < 8; inten++)
  5162. {
  5163. for (uint32_t g = 0; g < 32; g++)
  5164. {
  5165. color32 block_colors[4];
  5166. decoder_etc_block::get_diff_subblock_colors(block_colors, decoder_etc_block::pack_color5(color32(g, g, g, 255), false), inten);
  5167. for (uint32_t sr = 0; sr < NUM_ETC1S_TO_ATC_SELECTOR_RANGES; sr++)
  5168. {
  5169. const uint32_t low_selector = g_etc1s_to_atc_selector_ranges[sr].m_low;
  5170. const uint32_t high_selector = g_etc1s_to_atc_selector_ranges[sr].m_high;
  5171. for (uint32_t m = 0; m < NUM_ETC1S_TO_ATC_SELECTOR_MAPPINGS; m++)
  5172. {
  5173. uint32_t best_lo = 0;
  5174. uint32_t best_hi = 0;
  5175. uint64_t best_err = UINT64_MAX;
  5176. for (uint32_t hi = 0; hi <= 31; hi++)
  5177. {
  5178. for (uint32_t lo = 0; lo <= 31; lo++)
  5179. {
  5180. uint32_t colors[4];
  5181. colors[0] = (lo << 3) | (lo >> 2);
  5182. colors[3] = (hi << 3) | (hi >> 2);
  5183. colors[1] = (colors[0] * 5 + colors[3] * 3) / 8;
  5184. colors[2] = (colors[3] * 5 + colors[0] * 3) / 8;
  5185. uint64_t total_err = 0;
  5186. for (uint32_t s = low_selector; s <= high_selector; s++)
  5187. {
  5188. int err = block_colors[s].g - colors[g_etc1s_to_atc_selector_mappings[m][s]];
  5189. int err_scale = 1;
  5190. // Special case when the intensity table is 7, low_selector is 0, and high_selector is 3. In this extreme case, it's likely the encoder is trying to strongly favor
  5191. // the low/high selectors which are clamping to either 0 or 255.
  5192. if (((inten == 7) && (low_selector == 0) && (high_selector == 3)) && ((s == 0) || (s == 3)))
  5193. err_scale = 5;
  5194. total_err += (err * err) * err_scale;
  5195. }
  5196. if (total_err < best_err)
  5197. {
  5198. best_err = total_err;
  5199. best_lo = lo;
  5200. best_hi = hi;
  5201. }
  5202. }
  5203. }
  5204. //assert(best_err <= 0xFFFF);
  5205. best_err = basisu::minimum<uint32_t>(best_err, 0xFFFF);
  5206. fprintf(pFile, "{%u,%u,%u},", best_lo, best_hi, (uint32_t)best_err);
  5207. n++;
  5208. if ((n & 31) == 31)
  5209. fprintf(pFile, "\n");
  5210. } // m
  5211. } // sr
  5212. } // g
  5213. } // inten
  5214. fclose(pFile);
  5215. pFile = nullptr;
  5216. // ATC 56
  5217. fopen_s(&pFile, "basisu_transcoder_tables_atc_56.inc", "w");
  5218. n = 0;
  5219. for (int inten = 0; inten < 8; inten++)
  5220. {
  5221. for (uint32_t g = 0; g < 32; g++)
  5222. {
  5223. color32 block_colors[4];
  5224. decoder_etc_block::get_diff_subblock_colors(block_colors, decoder_etc_block::pack_color5(color32(g, g, g, 255), false), inten);
  5225. for (uint32_t sr = 0; sr < NUM_ETC1S_TO_ATC_SELECTOR_RANGES; sr++)
  5226. {
  5227. const uint32_t low_selector = g_etc1s_to_atc_selector_ranges[sr].m_low;
  5228. const uint32_t high_selector = g_etc1s_to_atc_selector_ranges[sr].m_high;
  5229. for (uint32_t m = 0; m < NUM_ETC1S_TO_ATC_SELECTOR_MAPPINGS; m++)
  5230. {
  5231. uint32_t best_lo = 0;
  5232. uint32_t best_hi = 0;
  5233. uint64_t best_err = UINT64_MAX;
  5234. for (uint32_t hi = 0; hi <= 63; hi++)
  5235. {
  5236. for (uint32_t lo = 0; lo <= 31; lo++)
  5237. {
  5238. uint32_t colors[4];
  5239. colors[0] = (lo << 3) | (lo >> 2);
  5240. colors[3] = (hi << 2) | (hi >> 4);
  5241. colors[1] = (colors[0] * 5 + colors[3] * 3) / 8;
  5242. colors[2] = (colors[3] * 5 + colors[0] * 3) / 8;
  5243. uint64_t total_err = 0;
  5244. for (uint32_t s = low_selector; s <= high_selector; s++)
  5245. {
  5246. int err = block_colors[s].g - colors[g_etc1s_to_atc_selector_mappings[m][s]];
  5247. int err_scale = 1;
  5248. // Special case when the intensity table is 7, low_selector is 0, and high_selector is 3. In this extreme case, it's likely the encoder is trying to strongly favor
  5249. // the low/high selectors which are clamping to either 0 or 255.
  5250. if (((inten == 7) && (low_selector == 0) && (high_selector == 3)) && ((s == 0) || (s == 3)))
  5251. err_scale = 5;
  5252. total_err += (err * err) * err_scale;
  5253. }
  5254. if (total_err < best_err)
  5255. {
  5256. best_err = total_err;
  5257. best_lo = lo;
  5258. best_hi = hi;
  5259. }
  5260. }
  5261. }
  5262. //assert(best_err <= 0xFFFF);
  5263. best_err = basisu::minimum<uint32_t>(best_err, 0xFFFF);
  5264. fprintf(pFile, "{%u,%u,%u},", best_lo, best_hi, (uint32_t)best_err);
  5265. n++;
  5266. if ((n & 31) == 31)
  5267. fprintf(pFile, "\n");
  5268. } // m
  5269. } // sr
  5270. } // g
  5271. } // inten
  5272. fclose(pFile);
  5273. // PVRTC2 45
  5274. fopen_s(&pFile, "basisu_transcoder_tables_pvrtc2_45.inc", "w");
  5275. n = 0;
  5276. for (int inten = 0; inten < 8; inten++)
  5277. {
  5278. for (uint32_t g = 0; g < 32; g++)
  5279. {
  5280. color32 block_colors[4];
  5281. decoder_etc_block::get_diff_subblock_colors(block_colors, decoder_etc_block::pack_color5(color32(g, g, g, 255), false), inten);
  5282. for (uint32_t sr = 0; sr < NUM_ETC1S_TO_ATC_SELECTOR_RANGES; sr++)
  5283. {
  5284. const uint32_t low_selector = g_etc1s_to_atc_selector_ranges[sr].m_low;
  5285. const uint32_t high_selector = g_etc1s_to_atc_selector_ranges[sr].m_high;
  5286. for (uint32_t m = 0; m < NUM_ETC1S_TO_ATC_SELECTOR_MAPPINGS; m++)
  5287. {
  5288. uint32_t best_lo = 0;
  5289. uint32_t best_hi = 0;
  5290. uint64_t best_err = UINT64_MAX;
  5291. for (uint32_t hi = 0; hi <= 31; hi++)
  5292. {
  5293. for (uint32_t lo = 0; lo <= 15; lo++)
  5294. {
  5295. uint32_t colors[4];
  5296. colors[0] = (lo << 1) | (lo >> 3);
  5297. colors[0] = (colors[0] << 3) | (colors[0] >> 2);
  5298. colors[3] = (hi << 3) | (hi >> 2);
  5299. colors[1] = (colors[0] * 5 + colors[3] * 3) / 8;
  5300. colors[2] = (colors[3] * 5 + colors[0] * 3) / 8;
  5301. uint64_t total_err = 0;
  5302. for (uint32_t s = low_selector; s <= high_selector; s++)
  5303. {
  5304. int err = block_colors[s].g - colors[g_etc1s_to_atc_selector_mappings[m][s]];
  5305. int err_scale = 1;
  5306. // Special case when the intensity table is 7, low_selector is 0, and high_selector is 3. In this extreme case, it's likely the encoder is trying to strongly favor
  5307. // the low/high selectors which are clamping to either 0 or 255.
  5308. if (((inten == 7) && (low_selector == 0) && (high_selector == 3)) && ((s == 0) || (s == 3)))
  5309. err_scale = 5;
  5310. total_err += (err * err) * err_scale;
  5311. }
  5312. if (total_err < best_err)
  5313. {
  5314. best_err = total_err;
  5315. best_lo = lo;
  5316. best_hi = hi;
  5317. }
  5318. }
  5319. }
  5320. //assert(best_err <= 0xFFFF);
  5321. best_err = basisu::minimum<uint32_t>(best_err, 0xFFFF);
  5322. fprintf(pFile, "{%u,%u,%u},", best_lo, best_hi, (uint32_t)best_err);
  5323. n++;
  5324. if ((n & 31) == 31)
  5325. fprintf(pFile, "\n");
  5326. } // m
  5327. } // sr
  5328. } // g
  5329. } // inten
  5330. fclose(pFile);
  5331. #if 0
  5332. // PVRTC2 34
  5333. fopen_s(&pFile, "basisu_transcoder_tables_pvrtc2_34.inc", "w");
  5334. n = 0;
  5335. for (int inten = 0; inten < 8; inten++)
  5336. {
  5337. for (uint32_t g = 0; g < 32; g++)
  5338. {
  5339. color32 block_colors[4];
  5340. decoder_etc_block::get_diff_subblock_colors(block_colors, decoder_etc_block::pack_color5(color32(g, g, g, 255), false), inten);
  5341. for (uint32_t sr = 0; sr < NUM_ETC1S_TO_ATC_SELECTOR_RANGES; sr++)
  5342. {
  5343. const uint32_t low_selector = g_etc1s_to_atc_selector_ranges[sr].m_low;
  5344. const uint32_t high_selector = g_etc1s_to_atc_selector_ranges[sr].m_high;
  5345. for (uint32_t m = 0; m < NUM_ETC1S_TO_ATC_SELECTOR_MAPPINGS; m++)
  5346. {
  5347. uint32_t best_lo = 0;
  5348. uint32_t best_hi = 0;
  5349. uint64_t best_err = UINT64_MAX;
  5350. for (uint32_t hi = 0; hi <= 15; hi++)
  5351. {
  5352. for (uint32_t lo = 0; lo <= 7; lo++)
  5353. {
  5354. uint32_t colors[4];
  5355. colors[0] = (lo << 2) | (lo >> 1);
  5356. colors[0] = (colors[0] << 3) | (colors[0] >> 2);
  5357. colors[3] = (hi << 1) | (hi >> 3);
  5358. colors[3] = (colors[3] << 3) | (colors[3] >> 2);
  5359. colors[1] = (colors[0] * 5 + colors[3] * 3) / 8;
  5360. colors[2] = (colors[3] * 5 + colors[0] * 3) / 8;
  5361. uint64_t total_err = 0;
  5362. for (uint32_t s = low_selector; s <= high_selector; s++)
  5363. {
  5364. int err = block_colors[s].g - colors[g_etc1s_to_atc_selector_mappings[m][s]];
  5365. int err_scale = 1;
  5366. // Special case when the intensity table is 7, low_selector is 0, and high_selector is 3. In this extreme case, it's likely the encoder is trying to strongly favor
  5367. // the low/high selectors which are clamping to either 0 or 255.
  5368. if (((inten == 7) && (low_selector == 0) && (high_selector == 3)) && ((s == 0) || (s == 3)))
  5369. err_scale = 5;
  5370. total_err += (err * err) * err_scale;
  5371. }
  5372. if (total_err < best_err)
  5373. {
  5374. best_err = total_err;
  5375. best_lo = lo;
  5376. best_hi = hi;
  5377. }
  5378. }
  5379. }
  5380. //assert(best_err <= 0xFFFF);
  5381. best_err = basisu::minimum<uint32_t>(best_err, 0xFFFF);
  5382. fprintf(pFile, "{%u,%u,%u},", best_lo, best_hi, (uint32_t)best_err);
  5383. n++;
  5384. if ((n & 31) == 31)
  5385. fprintf(pFile, "\n");
  5386. } // m
  5387. } // sr
  5388. } // g
  5389. } // inten
  5390. fclose(pFile);
  5391. #endif
  5392. #if 0
  5393. // PVRTC2 44
  5394. fopen_s(&pFile, "basisu_transcoder_tables_pvrtc2_44.inc", "w");
  5395. n = 0;
  5396. for (int inten = 0; inten < 8; inten++)
  5397. {
  5398. for (uint32_t g = 0; g < 32; g++)
  5399. {
  5400. color32 block_colors[4];
  5401. decoder_etc_block::get_diff_subblock_colors(block_colors, decoder_etc_block::pack_color5(color32(g, g, g, 255), false), inten);
  5402. for (uint32_t sr = 0; sr < NUM_ETC1S_TO_ATC_SELECTOR_RANGES; sr++)
  5403. {
  5404. const uint32_t low_selector = g_etc1s_to_atc_selector_ranges[sr].m_low;
  5405. const uint32_t high_selector = g_etc1s_to_atc_selector_ranges[sr].m_high;
  5406. for (uint32_t m = 0; m < NUM_ETC1S_TO_ATC_SELECTOR_MAPPINGS; m++)
  5407. {
  5408. uint32_t best_lo = 0;
  5409. uint32_t best_hi = 0;
  5410. uint64_t best_err = UINT64_MAX;
  5411. for (uint32_t hi = 0; hi <= 15; hi++)
  5412. {
  5413. for (uint32_t lo = 0; lo <= 15; lo++)
  5414. {
  5415. uint32_t colors[4];
  5416. colors[0] = (lo << 1) | (lo >> 3);
  5417. colors[0] = (colors[0] << 3) | (colors[0] >> 2);
  5418. colors[3] = (hi << 1) | (hi >> 3);
  5419. colors[3] = (colors[3] << 3) | (colors[3] >> 2);
  5420. colors[1] = (colors[0] * 5 + colors[3] * 3) / 8;
  5421. colors[2] = (colors[3] * 5 + colors[0] * 3) / 8;
  5422. uint64_t total_err = 0;
  5423. for (uint32_t s = low_selector; s <= high_selector; s++)
  5424. {
  5425. int err = block_colors[s].g - colors[g_etc1s_to_atc_selector_mappings[m][s]];
  5426. int err_scale = 1;
  5427. // Special case when the intensity table is 7, low_selector is 0, and high_selector is 3. In this extreme case, it's likely the encoder is trying to strongly favor
  5428. // the low/high selectors which are clamping to either 0 or 255.
  5429. if (((inten == 7) && (low_selector == 0) && (high_selector == 3)) && ((s == 0) || (s == 3)))
  5430. err_scale = 5;
  5431. total_err += (err * err) * err_scale;
  5432. }
  5433. if (total_err < best_err)
  5434. {
  5435. best_err = total_err;
  5436. best_lo = lo;
  5437. best_hi = hi;
  5438. }
  5439. }
  5440. }
  5441. //assert(best_err <= 0xFFFF);
  5442. best_err = basisu::minimum<uint32_t>(best_err, 0xFFFF);
  5443. fprintf(pFile, "{%u,%u,%u},", best_lo, best_hi, (uint32_t)best_err);
  5444. n++;
  5445. if ((n & 31) == 31)
  5446. fprintf(pFile, "\n");
  5447. } // m
  5448. } // sr
  5449. } // g
  5450. } // inten
  5451. fclose(pFile);
  5452. #endif
  5453. // PVRTC2 alpha 33
  5454. fopen_s(&pFile, "basisu_transcoder_tables_pvrtc2_alpha_33.inc", "w");
  5455. n = 0;
  5456. for (int inten = 0; inten < 8; inten++)
  5457. {
  5458. for (uint32_t g = 0; g < 32; g++)
  5459. {
  5460. color32 block_colors[4];
  5461. decoder_etc_block::get_diff_subblock_colors(block_colors, decoder_etc_block::pack_color5(color32(g, g, g, 255), false), inten);
  5462. for (uint32_t sr = 0; sr < NUM_ETC1S_TO_ATC_SELECTOR_RANGES; sr++)
  5463. {
  5464. const uint32_t low_selector = g_etc1s_to_atc_selector_ranges[sr].m_low;
  5465. const uint32_t high_selector = g_etc1s_to_atc_selector_ranges[sr].m_high;
  5466. for (uint32_t m = 0; m < NUM_ETC1S_TO_ATC_SELECTOR_MAPPINGS; m++)
  5467. {
  5468. uint32_t best_lo = 0;
  5469. uint32_t best_hi = 0;
  5470. uint64_t best_err = UINT64_MAX;
  5471. for (uint32_t hi = 0; hi <= 7; hi++)
  5472. {
  5473. for (uint32_t lo = 0; lo <= 7; lo++)
  5474. {
  5475. uint32_t colors[4];
  5476. colors[0] = (lo << 1);
  5477. colors[0] = (colors[0] << 4) | colors[0];
  5478. colors[3] = (hi << 1) | 1;
  5479. colors[3] = (colors[3] << 4) | colors[3];
  5480. colors[1] = (colors[0] * 5 + colors[3] * 3) / 8;
  5481. colors[2] = (colors[3] * 5 + colors[0] * 3) / 8;
  5482. uint64_t total_err = 0;
  5483. for (uint32_t s = low_selector; s <= high_selector; s++)
  5484. {
  5485. int err = block_colors[s].g - colors[g_etc1s_to_atc_selector_mappings[m][s]];
  5486. int err_scale = 1;
  5487. // Special case when the intensity table is 7, low_selector is 0, and high_selector is 3. In this extreme case, it's likely the encoder is trying to strongly favor
  5488. // the low/high selectors which are clamping to either 0 or 255.
  5489. if (((inten == 7) && (low_selector == 0) && (high_selector == 3)) && ((s == 0) || (s == 3)))
  5490. err_scale = 5;
  5491. total_err += (err * err) * err_scale;
  5492. }
  5493. if (total_err < best_err)
  5494. {
  5495. best_err = total_err;
  5496. best_lo = lo;
  5497. best_hi = hi;
  5498. }
  5499. }
  5500. }
  5501. //assert(best_err <= 0xFFFF);
  5502. best_err = basisu::minimum<uint32_t>(best_err, 0xFFFF);
  5503. fprintf(pFile, "{%u,%u,%u},", best_lo, best_hi, (uint32_t)best_err);
  5504. n++;
  5505. if ((n & 31) == 31)
  5506. fprintf(pFile, "\n");
  5507. } // m
  5508. } // sr
  5509. } // g
  5510. } // inten
  5511. fclose(pFile);
  5512. }
  5513. #endif // BASISD_WRITE_NEW_ATC_TABLES
  5514. #endif // BASISD_SUPPORT_ATC
  5515. #if BASISD_SUPPORT_PVRTC2
  5516. struct pvrtc2_block
  5517. {
  5518. uint8_t m_modulation[4];
  5519. union
  5520. {
  5521. union
  5522. {
  5523. // Opaque mode: RGB colora=554 and colorb=555
  5524. struct
  5525. {
  5526. uint32_t m_mod_flag : 1;
  5527. uint32_t m_blue_a : 4;
  5528. uint32_t m_green_a : 5;
  5529. uint32_t m_red_a : 5;
  5530. uint32_t m_hard_flag : 1;
  5531. uint32_t m_blue_b : 5;
  5532. uint32_t m_green_b : 5;
  5533. uint32_t m_red_b : 5;
  5534. uint32_t m_opaque_flag : 1;
  5535. } m_opaque_color_data;
  5536. // Transparent mode: RGBA colora=4433 and colorb=4443
  5537. struct
  5538. {
  5539. uint32_t m_mod_flag : 1;
  5540. uint32_t m_blue_a : 3;
  5541. uint32_t m_green_a : 4;
  5542. uint32_t m_red_a : 4;
  5543. uint32_t m_alpha_a : 3;
  5544. uint32_t m_hard_flag : 1;
  5545. uint32_t m_blue_b : 4;
  5546. uint32_t m_green_b : 4;
  5547. uint32_t m_red_b : 4;
  5548. uint32_t m_alpha_b : 3;
  5549. uint32_t m_opaque_flag : 1;
  5550. } m_trans_color_data;
  5551. };
  5552. uint32_t m_color_data_bits;
  5553. };
  5554. // 554
  5555. void set_low_color(uint32_t r, uint32_t g, uint32_t b)
  5556. {
  5557. assert((r < 32) && (g < 32) && (b < 16));
  5558. m_opaque_color_data.m_red_a = r;
  5559. m_opaque_color_data.m_green_a = g;
  5560. m_opaque_color_data.m_blue_a = b;
  5561. }
  5562. // 555
  5563. void set_high_color(uint32_t r, uint32_t g, uint32_t b)
  5564. {
  5565. assert((r < 32) && (g < 32) && (b < 32));
  5566. m_opaque_color_data.m_red_b = r;
  5567. m_opaque_color_data.m_green_b = g;
  5568. m_opaque_color_data.m_blue_b = b;
  5569. }
  5570. // 4433
  5571. void set_trans_low_color(uint32_t r, uint32_t g, uint32_t b, uint32_t a)
  5572. {
  5573. assert((r < 16) && (g < 16) && (b < 8) && (a < 8));
  5574. m_trans_color_data.m_red_a = r;
  5575. m_trans_color_data.m_green_a = g;
  5576. m_trans_color_data.m_blue_a = b;
  5577. m_trans_color_data.m_alpha_a = a;
  5578. }
  5579. // 4443
  5580. void set_trans_high_color(uint32_t r, uint32_t g, uint32_t b, uint32_t a)
  5581. {
  5582. assert((r < 16) && (g < 16) && (b < 16) && (a < 8));
  5583. m_trans_color_data.m_red_b = r;
  5584. m_trans_color_data.m_green_b = g;
  5585. m_trans_color_data.m_blue_b = b;
  5586. m_trans_color_data.m_alpha_b = a;
  5587. }
  5588. };
  5589. static struct
  5590. {
  5591. uint8_t m_l, m_h;
  5592. } g_pvrtc2_trans_match34[256];
  5593. static struct
  5594. {
  5595. uint8_t m_l, m_h;
  5596. } g_pvrtc2_trans_match44[256];
  5597. static struct
  5598. {
  5599. uint8_t m_l, m_h;
  5600. } g_pvrtc2_alpha_match33[256];
  5601. static struct
  5602. {
  5603. uint8_t m_l, m_h;
  5604. } g_pvrtc2_alpha_match33_0[256];
  5605. static struct
  5606. {
  5607. uint8_t m_l, m_h;
  5608. } g_pvrtc2_alpha_match33_3[256];
  5609. // PVRTC2 can be forced to look like a slightly weaker variant of ATC/BC1, so that's what we do here for simplicity.
  5610. static void convert_etc1s_to_pvrtc2_rgb(void* pDst, const endpoint* pEndpoints, const selector* pSelector)
  5611. {
  5612. pvrtc2_block* pBlock = static_cast<pvrtc2_block*>(pDst);
  5613. pBlock->m_opaque_color_data.m_hard_flag = 1;
  5614. pBlock->m_opaque_color_data.m_mod_flag = 0;
  5615. pBlock->m_opaque_color_data.m_opaque_flag = 1;
  5616. const uint32_t low_selector = pSelector->m_lo_selector;
  5617. const uint32_t high_selector = pSelector->m_hi_selector;
  5618. const color32& base_color = pEndpoints->m_color5;
  5619. const uint32_t inten_table = pEndpoints->m_inten5;
  5620. if (low_selector == high_selector)
  5621. {
  5622. uint32_t r, g, b;
  5623. decoder_etc_block::get_block_color5(base_color, inten_table, low_selector, r, g, b);
  5624. pBlock->set_low_color(g_atc_match55_equals_1[r].m_lo, g_atc_match55_equals_1[g].m_lo, g_pvrtc2_match45_equals_1[b].m_lo);
  5625. pBlock->set_high_color(g_atc_match55_equals_1[r].m_hi, g_atc_match55_equals_1[g].m_hi, g_pvrtc2_match45_equals_1[b].m_hi);
  5626. pBlock->m_modulation[0] = 0x55;
  5627. pBlock->m_modulation[1] = 0x55;
  5628. pBlock->m_modulation[2] = 0x55;
  5629. pBlock->m_modulation[3] = 0x55;
  5630. return;
  5631. }
  5632. else if ((inten_table >= 7) && (pSelector->m_num_unique_selectors == 2) && (pSelector->m_lo_selector == 0) && (pSelector->m_hi_selector == 3))
  5633. {
  5634. color32 block_colors[4];
  5635. decoder_etc_block::get_block_colors5(block_colors, base_color, inten_table);
  5636. const uint32_t r0 = block_colors[0].r;
  5637. const uint32_t g0 = block_colors[0].g;
  5638. const uint32_t b0 = block_colors[0].b;
  5639. const uint32_t r1 = block_colors[3].r;
  5640. const uint32_t g1 = block_colors[3].g;
  5641. const uint32_t b1 = block_colors[3].b;
  5642. pBlock->set_low_color(g_atc_match5[r0].m_hi, g_atc_match5[g0].m_hi, g_pvrtc2_match4[b0].m_hi);
  5643. pBlock->set_high_color(g_atc_match5[r1].m_hi, g_atc_match5[g1].m_hi, g_atc_match5[b1].m_hi);
  5644. pBlock->m_modulation[0] = pSelector->m_selectors[0];
  5645. pBlock->m_modulation[1] = pSelector->m_selectors[1];
  5646. pBlock->m_modulation[2] = pSelector->m_selectors[2];
  5647. pBlock->m_modulation[3] = pSelector->m_selectors[3];
  5648. return;
  5649. }
  5650. const uint32_t selector_range_table = g_etc1s_to_atc_selector_range_index[low_selector][high_selector];
  5651. //[32][8][RANGES][MAPPING]
  5652. const etc1s_to_atc_solution* pTable_r = &g_etc1s_to_atc_55[(inten_table * 32 + base_color.r) * (NUM_ETC1S_TO_ATC_SELECTOR_RANGES * NUM_ETC1S_TO_ATC_SELECTOR_MAPPINGS) + selector_range_table * NUM_ETC1S_TO_ATC_SELECTOR_MAPPINGS];
  5653. const etc1s_to_atc_solution* pTable_g = &g_etc1s_to_atc_55[(inten_table * 32 + base_color.g) * (NUM_ETC1S_TO_ATC_SELECTOR_RANGES * NUM_ETC1S_TO_ATC_SELECTOR_MAPPINGS) + selector_range_table * NUM_ETC1S_TO_ATC_SELECTOR_MAPPINGS];
  5654. const etc1s_to_atc_solution* pTable_b = &g_etc1s_to_pvrtc2_45[(inten_table * 32 + base_color.b) * (NUM_ETC1S_TO_ATC_SELECTOR_RANGES * NUM_ETC1S_TO_ATC_SELECTOR_MAPPINGS) + selector_range_table * NUM_ETC1S_TO_ATC_SELECTOR_MAPPINGS];
  5655. uint32_t best_err = UINT_MAX;
  5656. uint32_t best_mapping = 0;
  5657. assert(NUM_ETC1S_TO_ATC_SELECTOR_MAPPINGS == 10);
  5658. #define DO_ITER(m) { uint32_t total_err = pTable_r[m].m_err + pTable_g[m].m_err + pTable_b[m].m_err; if (total_err < best_err) { best_err = total_err; best_mapping = m; } }
  5659. DO_ITER(0); DO_ITER(1); DO_ITER(2); DO_ITER(3); DO_ITER(4);
  5660. DO_ITER(5); DO_ITER(6); DO_ITER(7); DO_ITER(8); DO_ITER(9);
  5661. #undef DO_ITER
  5662. pBlock->set_low_color(pTable_r[best_mapping].m_lo, pTable_g[best_mapping].m_lo, pTable_b[best_mapping].m_lo);
  5663. pBlock->set_high_color(pTable_r[best_mapping].m_hi, pTable_g[best_mapping].m_hi, pTable_b[best_mapping].m_hi);
  5664. if (ATC_IDENTITY_SELECTOR_MAPPING_INDEX == best_mapping)
  5665. {
  5666. pBlock->m_modulation[0] = pSelector->m_selectors[0];
  5667. pBlock->m_modulation[1] = pSelector->m_selectors[1];
  5668. pBlock->m_modulation[2] = pSelector->m_selectors[2];
  5669. pBlock->m_modulation[3] = pSelector->m_selectors[3];
  5670. }
  5671. else
  5672. {
  5673. // TODO: We could make this faster using several precomputed 256 entry tables, like ETC1S->BC1 does.
  5674. const uint8_t* pSelectors_xlat = &g_etc1s_to_atc_selector_mappings[best_mapping][0];
  5675. const uint32_t sel_bits0 = pSelector->m_selectors[0];
  5676. const uint32_t sel_bits1 = pSelector->m_selectors[1];
  5677. const uint32_t sel_bits2 = pSelector->m_selectors[2];
  5678. const uint32_t sel_bits3 = pSelector->m_selectors[3];
  5679. uint32_t sels0 = 0, sels1 = 0, sels2 = 0, sels3 = 0;
  5680. #define DO_X(x) { \
  5681. const uint32_t x_shift = (x) * 2; \
  5682. sels0 |= (pSelectors_xlat[(sel_bits0 >> x_shift) & 3] << x_shift); \
  5683. sels1 |= (pSelectors_xlat[(sel_bits1 >> x_shift) & 3] << x_shift); \
  5684. sels2 |= (pSelectors_xlat[(sel_bits2 >> x_shift) & 3] << x_shift); \
  5685. sels3 |= (pSelectors_xlat[(sel_bits3 >> x_shift) & 3] << x_shift); }
  5686. DO_X(0);
  5687. DO_X(1);
  5688. DO_X(2);
  5689. DO_X(3);
  5690. #undef DO_X
  5691. pBlock->m_modulation[0] = (uint8_t)sels0;
  5692. pBlock->m_modulation[1] = (uint8_t)sels1;
  5693. pBlock->m_modulation[2] = (uint8_t)sels2;
  5694. pBlock->m_modulation[3] = (uint8_t)sels3;
  5695. }
  5696. }
  5697. typedef struct { float c[4]; } vec4F;
  5698. static inline vec4F* vec4F_set_scalar(vec4F* pV, float x) { pV->c[0] = x; pV->c[1] = x; pV->c[2] = x; pV->c[3] = x; return pV; }
  5699. static inline vec4F* vec4F_set(vec4F* pV, float x, float y, float z, float w) { pV->c[0] = x; pV->c[1] = y; pV->c[2] = z; pV->c[3] = w; return pV; }
  5700. static inline vec4F* vec4F_saturate_in_place(vec4F* pV) { pV->c[0] = saturate(pV->c[0]); pV->c[1] = saturate(pV->c[1]); pV->c[2] = saturate(pV->c[2]); pV->c[3] = saturate(pV->c[3]); return pV; }
  5701. static inline vec4F vec4F_saturate(const vec4F* pV) { vec4F res; res.c[0] = saturate(pV->c[0]); res.c[1] = saturate(pV->c[1]); res.c[2] = saturate(pV->c[2]); res.c[3] = saturate(pV->c[3]); return res; }
  5702. static inline vec4F vec4F_from_color(const color32* pC) { vec4F res; vec4F_set(&res, pC->c[0], pC->c[1], pC->c[2], pC->c[3]); return res; }
  5703. static inline vec4F vec4F_add(const vec4F* pLHS, const vec4F* pRHS) { vec4F res; vec4F_set(&res, pLHS->c[0] + pRHS->c[0], pLHS->c[1] + pRHS->c[1], pLHS->c[2] + pRHS->c[2], pLHS->c[3] + pRHS->c[3]); return res; }
  5704. static inline vec4F vec4F_sub(const vec4F* pLHS, const vec4F* pRHS) { vec4F res; vec4F_set(&res, pLHS->c[0] - pRHS->c[0], pLHS->c[1] - pRHS->c[1], pLHS->c[2] - pRHS->c[2], pLHS->c[3] - pRHS->c[3]); return res; }
  5705. static inline float vec4F_dot(const vec4F* pLHS, const vec4F* pRHS) { return pLHS->c[0] * pRHS->c[0] + pLHS->c[1] * pRHS->c[1] + pLHS->c[2] * pRHS->c[2] + pLHS->c[3] * pRHS->c[3]; }
  5706. static inline vec4F vec4F_mul(const vec4F* pLHS, float s) { vec4F res; vec4F_set(&res, pLHS->c[0] * s, pLHS->c[1] * s, pLHS->c[2] * s, pLHS->c[3] * s); return res; }
  5707. static inline vec4F* vec4F_normalize_in_place(vec4F* pV) { float s = pV->c[0] * pV->c[0] + pV->c[1] * pV->c[1] + pV->c[2] * pV->c[2] + pV->c[3] * pV->c[3]; if (s != 0.0f) { s = 1.0f / sqrtf(s); pV->c[0] *= s; pV->c[1] *= s; pV->c[2] *= s; pV->c[3] *= s; } return pV; }
  5708. static color32 convert_rgba_5554_to_8888(const color32& col)
  5709. {
  5710. return color32((col[0] << 3) | (col[0] >> 2), (col[1] << 3) | (col[1] >> 2), (col[2] << 3) | (col[2] >> 2), (col[3] << 4) | col[3]);
  5711. }
  5712. static inline int sq(int x) { return x * x; }
  5713. // PVRTC2 is a slightly borked format for alpha: In Non-Interpolated mode, the way AlphaB8 is exanded from 4 to 8 bits means it can never be 0.
  5714. // This is actually very bad, because on 100% transparent blocks which have non-trivial color pixels, part of the color channel will leak into alpha!
  5715. // And there's nothing straightforward we can do because using the other modes is too expensive/complex. I can see why Apple didn't adopt it.
  5716. static void convert_etc1s_to_pvrtc2_rgba(void* pDst, const endpoint* pEndpoints, const selector* pSelector, const endpoint* pEndpoint_codebook, const selector* pSelector_codebook)
  5717. {
  5718. pvrtc2_block* pBlock = static_cast<pvrtc2_block*>(pDst);
  5719. const endpoint& alpha_endpoint = pEndpoint_codebook[((uint16_t*)pBlock)[0]];
  5720. const selector& alpha_selectors = pSelector_codebook[((uint16_t*)pBlock)[1]];
  5721. pBlock->m_opaque_color_data.m_hard_flag = 1;
  5722. pBlock->m_opaque_color_data.m_mod_flag = 0;
  5723. pBlock->m_opaque_color_data.m_opaque_flag = 0;
  5724. const int num_unique_alpha_selectors = alpha_selectors.m_num_unique_selectors;
  5725. const color32& alpha_base_color = alpha_endpoint.m_color5;
  5726. const uint32_t alpha_inten_table = alpha_endpoint.m_inten5;
  5727. int constant_alpha_val = -1;
  5728. int alpha_block_colors[4];
  5729. decoder_etc_block::get_block_colors5_g(alpha_block_colors, alpha_base_color, alpha_inten_table);
  5730. if (num_unique_alpha_selectors == 1)
  5731. {
  5732. constant_alpha_val = alpha_block_colors[alpha_selectors.m_lo_selector];
  5733. }
  5734. else
  5735. {
  5736. constant_alpha_val = alpha_block_colors[alpha_selectors.m_lo_selector];
  5737. for (uint32_t i = alpha_selectors.m_lo_selector + 1; i <= alpha_selectors.m_hi_selector; i++)
  5738. {
  5739. if (constant_alpha_val != alpha_block_colors[i])
  5740. {
  5741. constant_alpha_val = -1;
  5742. break;
  5743. }
  5744. }
  5745. }
  5746. if (constant_alpha_val >= 250)
  5747. {
  5748. // It's opaque enough, so don't bother trying to encode it as an alpha block.
  5749. convert_etc1s_to_pvrtc2_rgb(pDst, pEndpoints, pSelector);
  5750. return;
  5751. }
  5752. const color32& base_color = pEndpoints->m_color5;
  5753. const uint32_t inten_table = pEndpoints->m_inten5;
  5754. const uint32_t low_selector = pSelector->m_lo_selector;
  5755. const uint32_t high_selector = pSelector->m_hi_selector;
  5756. const int num_unique_color_selectors = pSelector->m_num_unique_selectors;
  5757. // We need to reencode the block at the pixel level, unfortunately, from two ETC1S planes.
  5758. // Do 4D incremental PCA, project all pixels to this hyperline, then quantize to packed endpoints and compute the modulation values.
  5759. const int br = (base_color.r << 3) | (base_color.r >> 2);
  5760. const int bg = (base_color.g << 3) | (base_color.g >> 2);
  5761. const int bb = (base_color.b << 3) | (base_color.b >> 2);
  5762. color32 block_cols[4];
  5763. for (uint32_t i = 0; i < 4; i++)
  5764. {
  5765. const int ci = g_etc1_inten_tables[inten_table][i];
  5766. block_cols[i].set_clamped(br + ci, bg + ci, bb + ci, alpha_block_colors[i]);
  5767. }
  5768. bool solid_color_block = true;
  5769. if (num_unique_color_selectors > 1)
  5770. {
  5771. for (uint32_t i = low_selector + 1; i <= high_selector; i++)
  5772. {
  5773. if ((block_cols[low_selector].r != block_cols[i].r) || (block_cols[low_selector].g != block_cols[i].g) || (block_cols[low_selector].b != block_cols[i].b))
  5774. {
  5775. solid_color_block = false;
  5776. break;
  5777. }
  5778. }
  5779. }
  5780. if ((solid_color_block) && (constant_alpha_val >= 0))
  5781. {
  5782. // Constant color/alpha block.
  5783. // This is more complex than it may seem because of the way color and alpha are packed in PVRTC2. We need to evaluate mod0, mod1 and mod3 encodings to find the best one.
  5784. uint32_t r, g, b;
  5785. decoder_etc_block::get_block_color5(base_color, inten_table, low_selector, r, g, b);
  5786. // Mod 0
  5787. uint32_t lr0 = (r * 15 + 128) / 255, lg0 = (g * 15 + 128) / 255, lb0 = (b * 7 + 128) / 255;
  5788. uint32_t la0 = g_pvrtc2_alpha_match33_0[constant_alpha_val].m_l;
  5789. uint32_t cr0 = (lr0 << 1) | (lr0 >> 3);
  5790. uint32_t cg0 = (lg0 << 1) | (lg0 >> 3);
  5791. uint32_t cb0 = (lb0 << 2) | (lb0 >> 1);
  5792. uint32_t ca0 = (la0 << 1);
  5793. cr0 = (cr0 << 3) | (cr0 >> 2);
  5794. cg0 = (cg0 << 3) | (cg0 >> 2);
  5795. cb0 = (cb0 << 3) | (cb0 >> 2);
  5796. ca0 = (ca0 << 4) | ca0;
  5797. uint32_t err0 = sq(cr0 - r) + sq(cg0 - g) + sq(cb0 - b) + sq(ca0 - constant_alpha_val) * 2;
  5798. // If the alpha is < 3 or so we're kinda screwed. It's better to have some RGB error than it is to turn a 100% transparent area slightly opaque.
  5799. if ((err0 == 0) || (constant_alpha_val < 3))
  5800. {
  5801. pBlock->set_trans_low_color(lr0, lg0, lb0, la0);
  5802. pBlock->set_trans_high_color(0, 0, 0, 0);
  5803. pBlock->m_modulation[0] = 0;
  5804. pBlock->m_modulation[1] = 0;
  5805. pBlock->m_modulation[2] = 0;
  5806. pBlock->m_modulation[3] = 0;
  5807. return;
  5808. }
  5809. // Mod 3
  5810. uint32_t lr3 = (r * 15 + 128) / 255, lg3 = (g * 15 + 128) / 255, lb3 = (b * 15 + 128) / 255;
  5811. uint32_t la3 = g_pvrtc2_alpha_match33_3[constant_alpha_val].m_l;
  5812. uint32_t cr3 = (lr3 << 1) | (lr3 >> 3);
  5813. uint32_t cg3 = (lg3 << 1) | (lg3 >> 3);
  5814. uint32_t cb3 = (lb3 << 1) | (lb3 >> 3);
  5815. uint32_t ca3 = (la3 << 1) | 1;
  5816. cr3 = (cr3 << 3) | (cr3 >> 2);
  5817. cg3 = (cg3 << 3) | (cg3 >> 2);
  5818. cb3 = (cb3 << 3) | (cb3 >> 2);
  5819. ca3 = (ca3 << 4) | ca3;
  5820. uint32_t err3 = sq(cr3 - r) + sq(cg3 - g) + sq(cb3 - b) + sq(ca3 - constant_alpha_val) * 2;
  5821. // Mod 1
  5822. uint32_t lr1 = g_pvrtc2_trans_match44[r].m_l, lg1 = g_pvrtc2_trans_match44[g].m_l, lb1 = g_pvrtc2_trans_match34[b].m_l;
  5823. uint32_t hr1 = g_pvrtc2_trans_match44[r].m_h, hg1 = g_pvrtc2_trans_match44[g].m_h, hb1 = g_pvrtc2_trans_match34[b].m_h;
  5824. uint32_t la1 = g_pvrtc2_alpha_match33[constant_alpha_val].m_l, ha1 = g_pvrtc2_alpha_match33[constant_alpha_val].m_h;
  5825. uint32_t clr1 = (lr1 << 1) | (lr1 >> 3);
  5826. uint32_t clg1 = (lg1 << 1) | (lg1 >> 3);
  5827. uint32_t clb1 = (lb1 << 2) | (lb1 >> 1);
  5828. uint32_t cla1 = (la1 << 1);
  5829. clr1 = (clr1 << 3) | (clr1 >> 2);
  5830. clg1 = (clg1 << 3) | (clg1 >> 2);
  5831. clb1 = (clb1 << 3) | (clb1 >> 2);
  5832. cla1 = (cla1 << 4) | cla1;
  5833. uint32_t chr1 = (hr1 << 1) | (hr1 >> 3);
  5834. uint32_t chg1 = (hg1 << 1) | (hg1 >> 3);
  5835. uint32_t chb1 = (hb1 << 1) | (hb1 >> 3);
  5836. uint32_t cha1 = (ha1 << 1) | 1;
  5837. chr1 = (chr1 << 3) | (chr1 >> 2);
  5838. chg1 = (chg1 << 3) | (chg1 >> 2);
  5839. chb1 = (chb1 << 3) | (chb1 >> 2);
  5840. cha1 = (cha1 << 4) | cha1;
  5841. uint32_t r1 = (clr1 * 5 + chr1 * 3) / 8;
  5842. uint32_t g1 = (clg1 * 5 + chg1 * 3) / 8;
  5843. uint32_t b1 = (clb1 * 5 + chb1 * 3) / 8;
  5844. uint32_t a1 = (cla1 * 5 + cha1 * 3) / 8;
  5845. uint32_t err1 = sq(r1 - r) + sq(g1 - g) + sq(b1 - b) + sq(a1 - constant_alpha_val) * 2;
  5846. if ((err1 < err0) && (err1 < err3))
  5847. {
  5848. pBlock->set_trans_low_color(lr1, lg1, lb1, la1);
  5849. pBlock->set_trans_high_color(hr1, hg1, hb1, ha1);
  5850. pBlock->m_modulation[0] = 0x55;
  5851. pBlock->m_modulation[1] = 0x55;
  5852. pBlock->m_modulation[2] = 0x55;
  5853. pBlock->m_modulation[3] = 0x55;
  5854. }
  5855. else if (err0 < err3)
  5856. {
  5857. pBlock->set_trans_low_color(lr0, lg0, lb0, la0);
  5858. pBlock->set_trans_high_color(0, 0, 0, 0);
  5859. pBlock->m_modulation[0] = 0;
  5860. pBlock->m_modulation[1] = 0;
  5861. pBlock->m_modulation[2] = 0;
  5862. pBlock->m_modulation[3] = 0;
  5863. }
  5864. else
  5865. {
  5866. pBlock->set_trans_low_color(0, 0, 0, 0);
  5867. pBlock->set_trans_high_color(lr3, lg3, lb3, la3);
  5868. pBlock->m_modulation[0] = 0xFF;
  5869. pBlock->m_modulation[1] = 0xFF;
  5870. pBlock->m_modulation[2] = 0xFF;
  5871. pBlock->m_modulation[3] = 0xFF;
  5872. }
  5873. return;
  5874. }
  5875. // It's a complex block with non-solid color and/or alpha pixels.
  5876. vec4F minColor, maxColor;
  5877. if (solid_color_block)
  5878. {
  5879. // It's a solid color block.
  5880. uint32_t low_a = block_cols[alpha_selectors.m_lo_selector].a;
  5881. uint32_t high_a = block_cols[alpha_selectors.m_hi_selector].a;
  5882. const float S = 1.0f / 255.0f;
  5883. vec4F_set(&minColor, block_cols[low_selector].r * S, block_cols[low_selector].g * S, block_cols[low_selector].b * S, low_a * S);
  5884. vec4F_set(&maxColor, block_cols[low_selector].r * S, block_cols[low_selector].g * S, block_cols[low_selector].b * S, high_a * S);
  5885. }
  5886. else if (constant_alpha_val >= 0)
  5887. {
  5888. // It's a solid alpha block.
  5889. const float S = 1.0f / 255.0f;
  5890. vec4F_set(&minColor, block_cols[low_selector].r * S, block_cols[low_selector].g * S, block_cols[low_selector].b * S, constant_alpha_val * S);
  5891. vec4F_set(&maxColor, block_cols[high_selector].r * S, block_cols[high_selector].g * S, block_cols[high_selector].b * S, constant_alpha_val * S);
  5892. }
  5893. // See if any of the block colors got clamped - if so the principle axis got distorted (it's no longer just the ETC1S luma axis).
  5894. // To keep quality up we need to use full 4D PCA in this case.
  5895. else if ((block_cols[low_selector].c[0] == 0) || (block_cols[high_selector].c[0] == 255) ||
  5896. (block_cols[low_selector].c[1] == 0) || (block_cols[high_selector].c[1] == 255) ||
  5897. (block_cols[low_selector].c[2] == 0) || (block_cols[high_selector].c[2] == 255) ||
  5898. (block_cols[alpha_selectors.m_lo_selector].c[3] == 0) || (block_cols[alpha_selectors.m_hi_selector].c[3] == 255))
  5899. {
  5900. // Find principle component of RGBA colors treated as 4D vectors.
  5901. color32 pixels[16];
  5902. uint32_t sum_r = 0, sum_g = 0, sum_b = 0, sum_a = 0;
  5903. for (uint32_t i = 0; i < 16; i++)
  5904. {
  5905. color32 rgb(block_cols[pSelector->get_selector(i & 3, i >> 2)]);
  5906. uint32_t a = block_cols[alpha_selectors.get_selector(i & 3, i >> 2)].a;
  5907. pixels[i].set(rgb.r, rgb.g, rgb.b, a);
  5908. sum_r += rgb.r;
  5909. sum_g += rgb.g;
  5910. sum_b += rgb.b;
  5911. sum_a += a;
  5912. }
  5913. vec4F meanColor;
  5914. vec4F_set(&meanColor, (float)sum_r, (float)sum_g, (float)sum_b, (float)sum_a);
  5915. vec4F meanColorScaled = vec4F_mul(&meanColor, 1.0f / 16.0f);
  5916. meanColor = vec4F_mul(&meanColor, 1.0f / (float)(16.0f * 255.0f));
  5917. vec4F_saturate_in_place(&meanColor);
  5918. vec4F axis;
  5919. vec4F_set_scalar(&axis, 0.0f);
  5920. // Why this incremental method? Because it's stable and predictable. Covar+power method can require a lot of iterations to converge in 4D.
  5921. for (uint32_t i = 0; i < 16; i++)
  5922. {
  5923. vec4F color = vec4F_from_color(&pixels[i]);
  5924. color = vec4F_sub(&color, &meanColorScaled);
  5925. vec4F a = vec4F_mul(&color, color.c[0]);
  5926. vec4F b = vec4F_mul(&color, color.c[1]);
  5927. vec4F c = vec4F_mul(&color, color.c[2]);
  5928. vec4F d = vec4F_mul(&color, color.c[3]);
  5929. vec4F n = i ? axis : color;
  5930. vec4F_normalize_in_place(&n);
  5931. axis.c[0] += vec4F_dot(&a, &n);
  5932. axis.c[1] += vec4F_dot(&b, &n);
  5933. axis.c[2] += vec4F_dot(&c, &n);
  5934. axis.c[3] += vec4F_dot(&d, &n);
  5935. }
  5936. vec4F_normalize_in_place(&axis);
  5937. if (vec4F_dot(&axis, &axis) < .5f)
  5938. vec4F_set_scalar(&axis, .5f);
  5939. float l = 1e+9f, h = -1e+9f;
  5940. for (uint32_t i = 0; i < 16; i++)
  5941. {
  5942. vec4F color = vec4F_from_color(&pixels[i]);
  5943. vec4F q = vec4F_sub(&color, &meanColorScaled);
  5944. float d = vec4F_dot(&q, &axis);
  5945. l = basisu::minimum(l, d);
  5946. h = basisu::maximum(h, d);
  5947. }
  5948. l *= (1.0f / 255.0f);
  5949. h *= (1.0f / 255.0f);
  5950. vec4F b0 = vec4F_mul(&axis, l);
  5951. vec4F b1 = vec4F_mul(&axis, h);
  5952. vec4F c0 = vec4F_add(&meanColor, &b0);
  5953. vec4F c1 = vec4F_add(&meanColor, &b1);
  5954. minColor = vec4F_saturate(&c0);
  5955. maxColor = vec4F_saturate(&c1);
  5956. if (minColor.c[3] > maxColor.c[3])
  5957. {
  5958. // VS 2019 release Code Generator issue
  5959. //std::swap(minColor, maxColor);
  5960. float a = minColor.c[0], b = minColor.c[1], c = minColor.c[2], d = minColor.c[3];
  5961. minColor.c[0] = maxColor.c[0]; minColor.c[1] = maxColor.c[1]; minColor.c[2] = maxColor.c[2]; minColor.c[3] = maxColor.c[3];
  5962. minColor.c[0] = maxColor.c[0]; minColor.c[1] = maxColor.c[1]; minColor.c[2] = maxColor.c[2]; minColor.c[3] = maxColor.c[3];
  5963. maxColor.c[0] = a; maxColor.c[1] = b; maxColor.c[2] = c; maxColor.c[3] = d;
  5964. }
  5965. }
  5966. else
  5967. {
  5968. // We know the RGB axis is luma, because it's an ETC1S block and none of the block colors got clamped. So we only need to use 2D PCA.
  5969. // We project each LA vector onto two 2D lines with axes (1,1) and (1,-1) and find the largest projection to determine if axis A is flipped relative to L.
  5970. uint32_t block_cols_l[4], block_cols_a[4];
  5971. for (uint32_t i = 0; i < 4; i++)
  5972. {
  5973. block_cols_l[i] = block_cols[i].r + block_cols[i].g + block_cols[i].b;
  5974. block_cols_a[i] = block_cols[i].a * 3;
  5975. }
  5976. int p0_min = INT_MAX, p0_max = INT_MIN;
  5977. int p1_min = INT_MAX, p1_max = INT_MIN;
  5978. for (uint32_t y = 0; y < 4; y++)
  5979. {
  5980. const uint32_t cs = pSelector->m_selectors[y];
  5981. const uint32_t as = alpha_selectors.m_selectors[y];
  5982. {
  5983. const int l = block_cols_l[cs & 3];
  5984. const int a = block_cols_a[as & 3];
  5985. const int p0 = l + a; p0_min = basisu::minimum(p0_min, p0); p0_max = basisu::maximum(p0_max, p0);
  5986. const int p1 = l - a; p1_min = basisu::minimum(p1_min, p1); p1_max = basisu::maximum(p1_max, p1);
  5987. }
  5988. {
  5989. const int l = block_cols_l[(cs >> 2) & 3];
  5990. const int a = block_cols_a[(as >> 2) & 3];
  5991. const int p0 = l + a; p0_min = basisu::minimum(p0_min, p0); p0_max = basisu::maximum(p0_max, p0);
  5992. const int p1 = l - a; p1_min = basisu::minimum(p1_min, p1); p1_max = basisu::maximum(p1_max, p1);
  5993. }
  5994. {
  5995. const int l = block_cols_l[(cs >> 4) & 3];
  5996. const int a = block_cols_a[(as >> 4) & 3];
  5997. const int p0 = l + a; p0_min = basisu::minimum(p0_min, p0); p0_max = basisu::maximum(p0_max, p0);
  5998. const int p1 = l - a; p1_min = basisu::minimum(p1_min, p1); p1_max = basisu::maximum(p1_max, p1);
  5999. }
  6000. {
  6001. const int l = block_cols_l[cs >> 6];
  6002. const int a = block_cols_a[as >> 6];
  6003. const int p0 = l + a; p0_min = basisu::minimum(p0_min, p0); p0_max = basisu::maximum(p0_max, p0);
  6004. const int p1 = l - a; p1_min = basisu::minimum(p1_min, p1); p1_max = basisu::maximum(p1_max, p1);
  6005. }
  6006. }
  6007. int dist0 = p0_max - p0_min;
  6008. int dist1 = p1_max - p1_min;
  6009. const float S = 1.0f / 255.0f;
  6010. vec4F_set(&minColor, block_cols[low_selector].r * S, block_cols[low_selector].g * S, block_cols[low_selector].b * S, block_cols[alpha_selectors.m_lo_selector].a * S);
  6011. vec4F_set(&maxColor, block_cols[high_selector].r * S, block_cols[high_selector].g * S, block_cols[high_selector].b * S, block_cols[alpha_selectors.m_hi_selector].a * S);
  6012. // See if the A component of the principle axis is flipped relative to L. If so, we need to flip either RGB or A bounds.
  6013. if (dist1 > dist0)
  6014. {
  6015. std::swap(minColor.c[0], maxColor.c[0]);
  6016. std::swap(minColor.c[1], maxColor.c[1]);
  6017. std::swap(minColor.c[2], maxColor.c[2]);
  6018. }
  6019. }
  6020. // 4433 4443
  6021. color32 trialMinColor, trialMaxColor;
  6022. trialMinColor.set_clamped((int)(minColor.c[0] * 15.0f + .5f), (int)(minColor.c[1] * 15.0f + .5f), (int)(minColor.c[2] * 7.0f + .5f), (int)(minColor.c[3] * 7.0f + .5f));
  6023. trialMaxColor.set_clamped((int)(maxColor.c[0] * 15.0f + .5f), (int)(maxColor.c[1] * 15.0f + .5f), (int)(maxColor.c[2] * 15.0f + .5f), (int)(maxColor.c[3] * 7.0f + .5f));
  6024. pBlock->set_trans_low_color(trialMinColor.r, trialMinColor.g, trialMinColor.b, trialMinColor.a);
  6025. pBlock->set_trans_high_color(trialMaxColor.r, trialMaxColor.g, trialMaxColor.b, trialMaxColor.a);
  6026. color32 color_a((trialMinColor.r << 1) | (trialMinColor.r >> 3), (trialMinColor.g << 1) | (trialMinColor.g >> 3), (trialMinColor.b << 2) | (trialMinColor.b >> 1), trialMinColor.a << 1);
  6027. color32 color_b((trialMaxColor.r << 1) | (trialMaxColor.r >> 3), (trialMaxColor.g << 1) | (trialMaxColor.g >> 3), (trialMaxColor.b << 1) | (trialMaxColor.b >> 3), (trialMaxColor.a << 1) | 1);
  6028. color32 color0(convert_rgba_5554_to_8888(color_a));
  6029. color32 color3(convert_rgba_5554_to_8888(color_b));
  6030. const int lr = color0.r;
  6031. const int lg = color0.g;
  6032. const int lb = color0.b;
  6033. const int la = color0.a;
  6034. const int axis_r = color3.r - lr;
  6035. const int axis_g = color3.g - lg;
  6036. const int axis_b = color3.b - lb;
  6037. const int axis_a = color3.a - la;
  6038. const int len_a = (axis_r * axis_r) + (axis_g * axis_g) + (axis_b * axis_b) + (axis_a * axis_a);
  6039. const int thresh01 = (len_a * 3) / 16;
  6040. const int thresh12 = len_a >> 1;
  6041. const int thresh23 = (len_a * 13) / 16;
  6042. if ((axis_r | axis_g | axis_b) == 0)
  6043. {
  6044. int ca_sel[4];
  6045. for (uint32_t i = 0; i < 4; i++)
  6046. {
  6047. int ca = (block_cols[i].a - la) * axis_a;
  6048. ca_sel[i] = (ca >= thresh23) + (ca >= thresh12) + (ca >= thresh01);
  6049. }
  6050. for (uint32_t y = 0; y < 4; y++)
  6051. {
  6052. const uint32_t a_sels = alpha_selectors.m_selectors[y];
  6053. uint32_t sel = ca_sel[a_sels & 3] | (ca_sel[(a_sels >> 2) & 3] << 2) | (ca_sel[(a_sels >> 4) & 3] << 4) | (ca_sel[a_sels >> 6] << 6);
  6054. pBlock->m_modulation[y] = (uint8_t)sel;
  6055. }
  6056. }
  6057. else
  6058. {
  6059. int cy[4], ca[4];
  6060. for (uint32_t i = 0; i < 4; i++)
  6061. {
  6062. cy[i] = (block_cols[i].r - lr) * axis_r + (block_cols[i].g - lg) * axis_g + (block_cols[i].b - lb) * axis_b;
  6063. ca[i] = (block_cols[i].a - la) * axis_a;
  6064. }
  6065. for (uint32_t y = 0; y < 4; y++)
  6066. {
  6067. const uint32_t c_sels = pSelector->m_selectors[y];
  6068. const uint32_t a_sels = alpha_selectors.m_selectors[y];
  6069. const int d0 = cy[c_sels & 3] + ca[a_sels & 3];
  6070. const int d1 = cy[(c_sels >> 2) & 3] + ca[(a_sels >> 2) & 3];
  6071. const int d2 = cy[(c_sels >> 4) & 3] + ca[(a_sels >> 4) & 3];
  6072. const int d3 = cy[c_sels >> 6] + ca[a_sels >> 6];
  6073. uint32_t sel = ((d0 >= thresh23) + (d0 >= thresh12) + (d0 >= thresh01)) |
  6074. (((d1 >= thresh23) + (d1 >= thresh12) + (d1 >= thresh01)) << 2) |
  6075. (((d2 >= thresh23) + (d2 >= thresh12) + (d2 >= thresh01)) << 4) |
  6076. (((d3 >= thresh23) + (d3 >= thresh12) + (d3 >= thresh01)) << 6);
  6077. pBlock->m_modulation[y] = (uint8_t)sel;
  6078. }
  6079. }
  6080. }
  6081. static void transcoder_init_pvrtc2()
  6082. {
  6083. for (uint32_t v = 0; v < 256; v++)
  6084. {
  6085. int best_l = 0, best_h = 0, lowest_err = INT_MAX;
  6086. for (uint32_t l = 0; l < 8; l++)
  6087. {
  6088. uint32_t le = (l << 1);
  6089. le = (le << 4) | le;
  6090. for (uint32_t h = 0; h < 8; h++)
  6091. {
  6092. uint32_t he = (h << 1) | 1;
  6093. he = (he << 4) | he;
  6094. uint32_t m = (le * 5 + he * 3) / 8;
  6095. int err = (int)labs((int)v - (int)m);
  6096. if (err < lowest_err)
  6097. {
  6098. lowest_err = err;
  6099. best_l = l;
  6100. best_h = h;
  6101. }
  6102. }
  6103. }
  6104. g_pvrtc2_alpha_match33[v].m_l = (uint8_t)best_l;
  6105. g_pvrtc2_alpha_match33[v].m_h = (uint8_t)best_h;
  6106. }
  6107. for (uint32_t v = 0; v < 256; v++)
  6108. {
  6109. int best_l = 0, best_h = 0, lowest_err = INT_MAX;
  6110. for (uint32_t l = 0; l < 8; l++)
  6111. {
  6112. uint32_t le = (l << 1);
  6113. le = (le << 4) | le;
  6114. int err = (int)labs((int)v - (int)le);
  6115. if (err < lowest_err)
  6116. {
  6117. lowest_err = err;
  6118. best_l = l;
  6119. best_h = l;
  6120. }
  6121. }
  6122. g_pvrtc2_alpha_match33_0[v].m_l = (uint8_t)best_l;
  6123. g_pvrtc2_alpha_match33_0[v].m_h = (uint8_t)best_h;
  6124. }
  6125. for (uint32_t v = 0; v < 256; v++)
  6126. {
  6127. int best_l = 0, best_h = 0, lowest_err = INT_MAX;
  6128. for (uint32_t h = 0; h < 8; h++)
  6129. {
  6130. uint32_t he = (h << 1) | 1;
  6131. he = (he << 4) | he;
  6132. int err = (int)labs((int)v - (int)he);
  6133. if (err < lowest_err)
  6134. {
  6135. lowest_err = err;
  6136. best_l = h;
  6137. best_h = h;
  6138. }
  6139. }
  6140. g_pvrtc2_alpha_match33_3[v].m_l = (uint8_t)best_l;
  6141. g_pvrtc2_alpha_match33_3[v].m_h = (uint8_t)best_h;
  6142. }
  6143. for (uint32_t v = 0; v < 256; v++)
  6144. {
  6145. int best_l = 0, best_h = 0, lowest_err = INT_MAX;
  6146. for (uint32_t l = 0; l < 8; l++)
  6147. {
  6148. uint32_t le = (l << 2) | (l >> 1);
  6149. le = (le << 3) | (le >> 2);
  6150. for (uint32_t h = 0; h < 16; h++)
  6151. {
  6152. uint32_t he = (h << 1) | (h >> 3);
  6153. he = (he << 3) | (he >> 2);
  6154. uint32_t m = (le * 5 + he * 3) / 8;
  6155. int err = (int)labs((int)v - (int)m);
  6156. if (err < lowest_err)
  6157. {
  6158. lowest_err = err;
  6159. best_l = l;
  6160. best_h = h;
  6161. }
  6162. }
  6163. }
  6164. g_pvrtc2_trans_match34[v].m_l = (uint8_t)best_l;
  6165. g_pvrtc2_trans_match34[v].m_h = (uint8_t)best_h;
  6166. }
  6167. for (uint32_t v = 0; v < 256; v++)
  6168. {
  6169. int best_l = 0, best_h = 0, lowest_err = INT_MAX;
  6170. for (uint32_t l = 0; l < 16; l++)
  6171. {
  6172. uint32_t le = (l << 1) | (l >> 3);
  6173. le = (le << 3) | (le >> 2);
  6174. for (uint32_t h = 0; h < 16; h++)
  6175. {
  6176. uint32_t he = (h << 1) | (h >> 3);
  6177. he = (he << 3) | (he >> 2);
  6178. uint32_t m = (le * 5 + he * 3) / 8;
  6179. int err = (int)labs((int)v - (int)m);
  6180. if (err < lowest_err)
  6181. {
  6182. lowest_err = err;
  6183. best_l = l;
  6184. best_h = h;
  6185. }
  6186. }
  6187. }
  6188. g_pvrtc2_trans_match44[v].m_l = (uint8_t)best_l;
  6189. g_pvrtc2_trans_match44[v].m_h = (uint8_t)best_h;
  6190. }
  6191. }
  6192. #endif // BASISD_SUPPORT_PVRTC2
  6193. basisu_lowlevel_etc1s_transcoder::basisu_lowlevel_etc1s_transcoder(const etc1_global_selector_codebook* pGlobal_sel_codebook) :
  6194. m_pGlobal_codebook(nullptr),
  6195. m_pGlobal_sel_codebook(pGlobal_sel_codebook),
  6196. m_selector_history_buf_size(0)
  6197. {
  6198. }
  6199. bool basisu_lowlevel_etc1s_transcoder::decode_palettes(
  6200. uint32_t num_endpoints, const uint8_t* pEndpoints_data, uint32_t endpoints_data_size,
  6201. uint32_t num_selectors, const uint8_t* pSelectors_data, uint32_t selectors_data_size)
  6202. {
  6203. if (m_pGlobal_codebook)
  6204. {
  6205. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::decode_palettes: fail 11\n");
  6206. return false;
  6207. }
  6208. bitwise_decoder sym_codec;
  6209. huffman_decoding_table color5_delta_model0, color5_delta_model1, color5_delta_model2, inten_delta_model;
  6210. if (!sym_codec.init(pEndpoints_data, endpoints_data_size))
  6211. {
  6212. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::decode_palettes: fail 0\n");
  6213. return false;
  6214. }
  6215. if (!sym_codec.read_huffman_table(color5_delta_model0))
  6216. {
  6217. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::decode_palettes: fail 1\n");
  6218. return false;
  6219. }
  6220. if (!sym_codec.read_huffman_table(color5_delta_model1))
  6221. {
  6222. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::decode_palettes: fail 1a\n");
  6223. return false;
  6224. }
  6225. if (!sym_codec.read_huffman_table(color5_delta_model2))
  6226. {
  6227. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::decode_palettes: fail 2a\n");
  6228. return false;
  6229. }
  6230. if (!sym_codec.read_huffman_table(inten_delta_model))
  6231. {
  6232. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::decode_palettes: fail 2b\n");
  6233. return false;
  6234. }
  6235. if (!color5_delta_model0.is_valid() || !color5_delta_model1.is_valid() || !color5_delta_model2.is_valid() || !inten_delta_model.is_valid())
  6236. {
  6237. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::decode_palettes: fail 2b\n");
  6238. return false;
  6239. }
  6240. const bool endpoints_are_grayscale = sym_codec.get_bits(1) != 0;
  6241. m_local_endpoints.resize(num_endpoints);
  6242. color32 prev_color5(16, 16, 16, 0);
  6243. uint32_t prev_inten = 0;
  6244. for (uint32_t i = 0; i < num_endpoints; i++)
  6245. {
  6246. uint32_t inten_delta = sym_codec.decode_huffman(inten_delta_model);
  6247. m_local_endpoints[i].m_inten5 = static_cast<uint8_t>((inten_delta + prev_inten) & 7);
  6248. prev_inten = m_local_endpoints[i].m_inten5;
  6249. for (uint32_t c = 0; c < (endpoints_are_grayscale ? 1U : 3U); c++)
  6250. {
  6251. int delta;
  6252. if (prev_color5[c] <= basist::COLOR5_PAL0_PREV_HI)
  6253. delta = sym_codec.decode_huffman(color5_delta_model0);
  6254. else if (prev_color5[c] <= basist::COLOR5_PAL1_PREV_HI)
  6255. delta = sym_codec.decode_huffman(color5_delta_model1);
  6256. else
  6257. delta = sym_codec.decode_huffman(color5_delta_model2);
  6258. int v = (prev_color5[c] + delta) & 31;
  6259. m_local_endpoints[i].m_color5[c] = static_cast<uint8_t>(v);
  6260. prev_color5[c] = static_cast<uint8_t>(v);
  6261. }
  6262. if (endpoints_are_grayscale)
  6263. {
  6264. m_local_endpoints[i].m_color5[1] = m_local_endpoints[i].m_color5[0];
  6265. m_local_endpoints[i].m_color5[2] = m_local_endpoints[i].m_color5[0];
  6266. }
  6267. }
  6268. sym_codec.stop();
  6269. m_local_selectors.resize(num_selectors);
  6270. if (!sym_codec.init(pSelectors_data, selectors_data_size))
  6271. {
  6272. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::decode_palettes: fail 5\n");
  6273. return false;
  6274. }
  6275. basist::huffman_decoding_table delta_selector_pal_model;
  6276. const bool used_global_selector_cb = (sym_codec.get_bits(1) == 1);
  6277. if (used_global_selector_cb)
  6278. {
  6279. // global selector palette
  6280. uint32_t pal_bits = sym_codec.get_bits(4);
  6281. uint32_t mod_bits = sym_codec.get_bits(4);
  6282. basist::huffman_decoding_table mod_model;
  6283. if (mod_bits)
  6284. {
  6285. if (!sym_codec.read_huffman_table(mod_model))
  6286. {
  6287. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::decode_palettes: fail 6\n");
  6288. return false;
  6289. }
  6290. if (!mod_model.is_valid())
  6291. {
  6292. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::decode_palettes: fail 6a\n");
  6293. return false;
  6294. }
  6295. }
  6296. for (uint32_t i = 0; i < num_selectors; i++)
  6297. {
  6298. uint32_t pal_index = 0;
  6299. if (pal_bits)
  6300. pal_index = sym_codec.get_bits(pal_bits);
  6301. uint32_t mod_index = 0;
  6302. if (mod_bits)
  6303. mod_index = sym_codec.decode_huffman(mod_model);
  6304. if (pal_index >= m_pGlobal_sel_codebook->size())
  6305. {
  6306. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::decode_palettes: fail 7z\n");
  6307. return false;
  6308. }
  6309. const etc1_selector_palette_entry e(m_pGlobal_sel_codebook->get_entry(pal_index, etc1_global_palette_entry_modifier(mod_index)));
  6310. // TODO: Optimize this
  6311. for (uint32_t y = 0; y < 4; y++)
  6312. for (uint32_t x = 0; x < 4; x++)
  6313. m_local_selectors[i].set_selector(x, y, e[x + y * 4]);
  6314. m_local_selectors[i].init_flags();
  6315. }
  6316. }
  6317. else
  6318. {
  6319. const bool used_hybrid_selector_cb = (sym_codec.get_bits(1) == 1);
  6320. if (used_hybrid_selector_cb)
  6321. {
  6322. const uint32_t pal_bits = sym_codec.get_bits(4);
  6323. const uint32_t mod_bits = sym_codec.get_bits(4);
  6324. basist::huffman_decoding_table uses_global_cb_bitflags_model;
  6325. if (!sym_codec.read_huffman_table(uses_global_cb_bitflags_model))
  6326. {
  6327. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::decode_palettes: fail 7\n");
  6328. return false;
  6329. }
  6330. if (!uses_global_cb_bitflags_model.is_valid())
  6331. {
  6332. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::decode_palettes: fail 7a\n");
  6333. return false;
  6334. }
  6335. basist::huffman_decoding_table global_mod_indices_model;
  6336. if (mod_bits)
  6337. {
  6338. if (!sym_codec.read_huffman_table(global_mod_indices_model))
  6339. {
  6340. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::decode_palettes: fail 8\n");
  6341. return false;
  6342. }
  6343. if (!global_mod_indices_model.is_valid())
  6344. {
  6345. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::decode_palettes: fail 8a\n");
  6346. return false;
  6347. }
  6348. }
  6349. uint32_t cur_uses_global_cb_bitflags = 0;
  6350. uint32_t uses_global_cb_bitflags_remaining = 0;
  6351. for (uint32_t q = 0; q < num_selectors; q++)
  6352. {
  6353. if (!uses_global_cb_bitflags_remaining)
  6354. {
  6355. cur_uses_global_cb_bitflags = sym_codec.decode_huffman(uses_global_cb_bitflags_model);
  6356. uses_global_cb_bitflags_remaining = 8;
  6357. }
  6358. uses_global_cb_bitflags_remaining--;
  6359. const bool used_global_cb_flag = (cur_uses_global_cb_bitflags & 1) != 0;
  6360. cur_uses_global_cb_bitflags >>= 1;
  6361. if (used_global_cb_flag)
  6362. {
  6363. const uint32_t pal_index = pal_bits ? sym_codec.get_bits(pal_bits) : 0;
  6364. const uint32_t mod_index = mod_bits ? sym_codec.decode_huffman(global_mod_indices_model) : 0;
  6365. if (pal_index >= m_pGlobal_sel_codebook->size())
  6366. {
  6367. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::decode_palettes: fail 8b\n");
  6368. return false;
  6369. }
  6370. const etc1_selector_palette_entry e(m_pGlobal_sel_codebook->get_entry(pal_index, etc1_global_palette_entry_modifier(mod_index)));
  6371. for (uint32_t y = 0; y < 4; y++)
  6372. for (uint32_t x = 0; x < 4; x++)
  6373. m_local_selectors[q].set_selector(x, y, e[x + y * 4]);
  6374. }
  6375. else
  6376. {
  6377. for (uint32_t j = 0; j < 4; j++)
  6378. {
  6379. uint32_t cur_byte = sym_codec.get_bits(8);
  6380. for (uint32_t k = 0; k < 4; k++)
  6381. m_local_selectors[q].set_selector(k, j, (cur_byte >> (k * 2)) & 3);
  6382. }
  6383. }
  6384. m_local_selectors[q].init_flags();
  6385. }
  6386. }
  6387. else
  6388. {
  6389. const bool used_raw_encoding = (sym_codec.get_bits(1) == 1);
  6390. if (used_raw_encoding)
  6391. {
  6392. for (uint32_t i = 0; i < num_selectors; i++)
  6393. {
  6394. for (uint32_t j = 0; j < 4; j++)
  6395. {
  6396. uint32_t cur_byte = sym_codec.get_bits(8);
  6397. for (uint32_t k = 0; k < 4; k++)
  6398. m_local_selectors[i].set_selector(k, j, (cur_byte >> (k * 2)) & 3);
  6399. }
  6400. m_local_selectors[i].init_flags();
  6401. }
  6402. }
  6403. else
  6404. {
  6405. if (!sym_codec.read_huffman_table(delta_selector_pal_model))
  6406. {
  6407. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::decode_palettes: fail 10\n");
  6408. return false;
  6409. }
  6410. if ((num_selectors > 1) && (!delta_selector_pal_model.is_valid()))
  6411. {
  6412. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::decode_palettes: fail 10a\n");
  6413. return false;
  6414. }
  6415. uint8_t prev_bytes[4] = { 0, 0, 0, 0 };
  6416. for (uint32_t i = 0; i < num_selectors; i++)
  6417. {
  6418. if (!i)
  6419. {
  6420. for (uint32_t j = 0; j < 4; j++)
  6421. {
  6422. uint32_t cur_byte = sym_codec.get_bits(8);
  6423. prev_bytes[j] = static_cast<uint8_t>(cur_byte);
  6424. for (uint32_t k = 0; k < 4; k++)
  6425. m_local_selectors[i].set_selector(k, j, (cur_byte >> (k * 2)) & 3);
  6426. }
  6427. m_local_selectors[i].init_flags();
  6428. continue;
  6429. }
  6430. for (uint32_t j = 0; j < 4; j++)
  6431. {
  6432. int delta_byte = sym_codec.decode_huffman(delta_selector_pal_model);
  6433. uint32_t cur_byte = delta_byte ^ prev_bytes[j];
  6434. prev_bytes[j] = static_cast<uint8_t>(cur_byte);
  6435. for (uint32_t k = 0; k < 4; k++)
  6436. m_local_selectors[i].set_selector(k, j, (cur_byte >> (k * 2)) & 3);
  6437. }
  6438. m_local_selectors[i].init_flags();
  6439. }
  6440. }
  6441. }
  6442. }
  6443. sym_codec.stop();
  6444. return true;
  6445. }
  6446. bool basisu_lowlevel_etc1s_transcoder::decode_tables(const uint8_t* pTable_data, uint32_t table_data_size)
  6447. {
  6448. basist::bitwise_decoder sym_codec;
  6449. if (!sym_codec.init(pTable_data, table_data_size))
  6450. {
  6451. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::decode_tables: fail 0\n");
  6452. return false;
  6453. }
  6454. if (!sym_codec.read_huffman_table(m_endpoint_pred_model))
  6455. {
  6456. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::decode_tables: fail 1\n");
  6457. return false;
  6458. }
  6459. if (m_endpoint_pred_model.get_code_sizes().size() == 0)
  6460. {
  6461. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::decode_tables: fail 1a\n");
  6462. return false;
  6463. }
  6464. if (!sym_codec.read_huffman_table(m_delta_endpoint_model))
  6465. {
  6466. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::decode_tables: fail 2\n");
  6467. return false;
  6468. }
  6469. if (m_delta_endpoint_model.get_code_sizes().size() == 0)
  6470. {
  6471. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::decode_tables: fail 2a\n");
  6472. return false;
  6473. }
  6474. if (!sym_codec.read_huffman_table(m_selector_model))
  6475. {
  6476. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::decode_tables: fail 3\n");
  6477. return false;
  6478. }
  6479. if (m_selector_model.get_code_sizes().size() == 0)
  6480. {
  6481. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::decode_tables: fail 3a\n");
  6482. return false;
  6483. }
  6484. if (!sym_codec.read_huffman_table(m_selector_history_buf_rle_model))
  6485. {
  6486. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::decode_tables: fail 4\n");
  6487. return false;
  6488. }
  6489. if (m_selector_history_buf_rle_model.get_code_sizes().size() == 0)
  6490. {
  6491. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::decode_tables: fail 4a\n");
  6492. return false;
  6493. }
  6494. m_selector_history_buf_size = sym_codec.get_bits(13);
  6495. sym_codec.stop();
  6496. return true;
  6497. }
  6498. bool basisu_lowlevel_etc1s_transcoder::transcode_slice(void* pDst_blocks, uint32_t num_blocks_x, uint32_t num_blocks_y, const uint8_t* pImage_data, uint32_t image_data_size, block_format fmt,
  6499. uint32_t output_block_or_pixel_stride_in_bytes, bool bc1_allow_threecolor_blocks, const bool is_video, const bool is_alpha_slice, const uint32_t level_index, const uint32_t orig_width, const uint32_t orig_height, uint32_t output_row_pitch_in_blocks_or_pixels,
  6500. basisu_transcoder_state* pState, bool transcode_alpha, void *pAlpha_blocks, uint32_t output_rows_in_pixels)
  6501. {
  6502. // 'pDst_blocks' unused when disabling *all* hardware transcode options
  6503. // (and 'bc1_allow_threecolor_blocks' when disabling DXT)
  6504. BASISU_NOTE_UNUSED(pDst_blocks);
  6505. BASISU_NOTE_UNUSED(bc1_allow_threecolor_blocks);
  6506. BASISU_NOTE_UNUSED(transcode_alpha);
  6507. BASISU_NOTE_UNUSED(pAlpha_blocks);
  6508. assert(g_transcoder_initialized);
  6509. if (!g_transcoder_initialized)
  6510. {
  6511. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_slice: Transcoder not globally initialized.\n");
  6512. return false;
  6513. }
  6514. if (!pState)
  6515. pState = &m_def_state;
  6516. const uint32_t total_blocks = num_blocks_x * num_blocks_y;
  6517. if (!output_row_pitch_in_blocks_or_pixels)
  6518. {
  6519. if (basis_block_format_is_uncompressed(fmt))
  6520. output_row_pitch_in_blocks_or_pixels = orig_width;
  6521. else
  6522. {
  6523. if (fmt == block_format::cFXT1_RGB)
  6524. output_row_pitch_in_blocks_or_pixels = (orig_width + 7) / 8;
  6525. else
  6526. output_row_pitch_in_blocks_or_pixels = num_blocks_x;
  6527. }
  6528. }
  6529. if (basis_block_format_is_uncompressed(fmt))
  6530. {
  6531. if (!output_rows_in_pixels)
  6532. output_rows_in_pixels = orig_height;
  6533. }
  6534. basisu::vector<uint32_t>* pPrev_frame_indices = nullptr;
  6535. if (is_video)
  6536. {
  6537. // TODO: Add check to make sure the caller hasn't tried skipping past p-frames
  6538. //const bool alpha_flag = (slice_desc.m_flags & cSliceDescFlagsHasAlpha) != 0;
  6539. //const uint32_t level_index = slice_desc.m_level_index;
  6540. if (level_index >= basisu_transcoder_state::cMaxPrevFrameLevels)
  6541. {
  6542. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_slice: unsupported level_index\n");
  6543. return false;
  6544. }
  6545. pPrev_frame_indices = &pState->m_prev_frame_indices[is_alpha_slice][level_index];
  6546. if (pPrev_frame_indices->size() < total_blocks)
  6547. pPrev_frame_indices->resize(total_blocks);
  6548. }
  6549. basist::bitwise_decoder sym_codec;
  6550. if (!sym_codec.init(pImage_data, image_data_size))
  6551. {
  6552. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_slice: sym_codec.init failed\n");
  6553. return false;
  6554. }
  6555. approx_move_to_front selector_history_buf(m_selector_history_buf_size);
  6556. uint32_t cur_selector_rle_count = 0;
  6557. decoder_etc_block block;
  6558. memset(&block, 0, sizeof(block));
  6559. block.set_flip_bit(true);
  6560. block.set_diff_bit(true);
  6561. void* pPVRTC_work_mem = nullptr;
  6562. uint32_t* pPVRTC_endpoints = nullptr;
  6563. if ((fmt == block_format::cPVRTC1_4_RGB) || (fmt == block_format::cPVRTC1_4_RGBA))
  6564. {
  6565. pPVRTC_work_mem = malloc(num_blocks_x * num_blocks_y * (sizeof(decoder_etc_block) + sizeof(uint32_t)));
  6566. if (!pPVRTC_work_mem)
  6567. {
  6568. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_slice: malloc failed\n");
  6569. return false;
  6570. }
  6571. pPVRTC_endpoints = (uint32_t*) & ((decoder_etc_block*)pPVRTC_work_mem)[num_blocks_x * num_blocks_y];
  6572. }
  6573. if (pState->m_block_endpoint_preds[0].size() < num_blocks_x)
  6574. {
  6575. pState->m_block_endpoint_preds[0].resize(num_blocks_x);
  6576. pState->m_block_endpoint_preds[1].resize(num_blocks_x);
  6577. }
  6578. uint32_t cur_pred_bits = 0;
  6579. int prev_endpoint_pred_sym = 0;
  6580. int endpoint_pred_repeat_count = 0;
  6581. uint32_t prev_endpoint_index = 0;
  6582. const endpoint_vec& endpoints = m_pGlobal_codebook ? m_pGlobal_codebook->m_local_endpoints : m_local_endpoints;
  6583. const selector_vec& selectors = m_pGlobal_codebook ? m_pGlobal_codebook->m_local_selectors : m_local_selectors;
  6584. if (!endpoints.size() || !selectors.size())
  6585. {
  6586. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_slice: global codebooks must be unpacked first\n");
  6587. return false;
  6588. }
  6589. const uint32_t SELECTOR_HISTORY_BUF_FIRST_SYMBOL_INDEX = (uint32_t)selectors.size();
  6590. const uint32_t SELECTOR_HISTORY_BUF_RLE_SYMBOL_INDEX = m_selector_history_buf_size + SELECTOR_HISTORY_BUF_FIRST_SYMBOL_INDEX;
  6591. for (uint32_t block_y = 0; block_y < num_blocks_y; block_y++)
  6592. {
  6593. const uint32_t cur_block_endpoint_pred_array = block_y & 1;
  6594. for (uint32_t block_x = 0; block_x < num_blocks_x; block_x++)
  6595. {
  6596. // Decode endpoint index predictor symbols
  6597. if ((block_x & 1) == 0)
  6598. {
  6599. if ((block_y & 1) == 0)
  6600. {
  6601. if (endpoint_pred_repeat_count)
  6602. {
  6603. endpoint_pred_repeat_count--;
  6604. cur_pred_bits = prev_endpoint_pred_sym;
  6605. }
  6606. else
  6607. {
  6608. cur_pred_bits = sym_codec.decode_huffman(m_endpoint_pred_model);
  6609. if (cur_pred_bits == ENDPOINT_PRED_REPEAT_LAST_SYMBOL)
  6610. {
  6611. endpoint_pred_repeat_count = sym_codec.decode_vlc(ENDPOINT_PRED_COUNT_VLC_BITS) + ENDPOINT_PRED_MIN_REPEAT_COUNT - 1;
  6612. cur_pred_bits = prev_endpoint_pred_sym;
  6613. }
  6614. else
  6615. {
  6616. prev_endpoint_pred_sym = cur_pred_bits;
  6617. }
  6618. }
  6619. pState->m_block_endpoint_preds[cur_block_endpoint_pred_array ^ 1][block_x].m_pred_bits = (uint8_t)(cur_pred_bits >> 4);
  6620. }
  6621. else
  6622. {
  6623. cur_pred_bits = pState->m_block_endpoint_preds[cur_block_endpoint_pred_array][block_x].m_pred_bits;
  6624. }
  6625. }
  6626. // Decode endpoint index
  6627. uint32_t endpoint_index, selector_index = 0;
  6628. const uint32_t pred = cur_pred_bits & 3;
  6629. cur_pred_bits >>= 2;
  6630. if (pred == 0)
  6631. {
  6632. // Left
  6633. if (!block_x)
  6634. {
  6635. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_slice: invalid datastream (0)\n");
  6636. if (pPVRTC_work_mem)
  6637. free(pPVRTC_work_mem);
  6638. return false;
  6639. }
  6640. endpoint_index = prev_endpoint_index;
  6641. }
  6642. else if (pred == 1)
  6643. {
  6644. // Upper
  6645. if (!block_y)
  6646. {
  6647. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_slice: invalid datastream (1)\n");
  6648. if (pPVRTC_work_mem)
  6649. free(pPVRTC_work_mem);
  6650. return false;
  6651. }
  6652. endpoint_index = pState->m_block_endpoint_preds[cur_block_endpoint_pred_array ^ 1][block_x].m_endpoint_index;
  6653. }
  6654. else if (pred == 2)
  6655. {
  6656. if (is_video)
  6657. {
  6658. assert(pred == CR_ENDPOINT_PRED_INDEX);
  6659. endpoint_index = (*pPrev_frame_indices)[block_x + block_y * num_blocks_x];
  6660. selector_index = endpoint_index >> 16;
  6661. endpoint_index &= 0xFFFFU;
  6662. }
  6663. else
  6664. {
  6665. // Upper left
  6666. if ((!block_x) || (!block_y))
  6667. {
  6668. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_slice: invalid datastream (2)\n");
  6669. if (pPVRTC_work_mem)
  6670. free(pPVRTC_work_mem);
  6671. return false;
  6672. }
  6673. endpoint_index = pState->m_block_endpoint_preds[cur_block_endpoint_pred_array ^ 1][block_x - 1].m_endpoint_index;
  6674. }
  6675. }
  6676. else
  6677. {
  6678. // Decode and apply delta
  6679. const uint32_t delta_sym = sym_codec.decode_huffman(m_delta_endpoint_model);
  6680. endpoint_index = delta_sym + prev_endpoint_index;
  6681. if (endpoint_index >= endpoints.size())
  6682. endpoint_index -= (int)endpoints.size();
  6683. }
  6684. pState->m_block_endpoint_preds[cur_block_endpoint_pred_array][block_x].m_endpoint_index = (uint16_t)endpoint_index;
  6685. prev_endpoint_index = endpoint_index;
  6686. // Decode selector index
  6687. if ((!is_video) || (pred != CR_ENDPOINT_PRED_INDEX))
  6688. {
  6689. int selector_sym;
  6690. if (cur_selector_rle_count > 0)
  6691. {
  6692. cur_selector_rle_count--;
  6693. selector_sym = (int)selectors.size();
  6694. }
  6695. else
  6696. {
  6697. selector_sym = sym_codec.decode_huffman(m_selector_model);
  6698. if (selector_sym == static_cast<int>(SELECTOR_HISTORY_BUF_RLE_SYMBOL_INDEX))
  6699. {
  6700. int run_sym = sym_codec.decode_huffman(m_selector_history_buf_rle_model);
  6701. if (run_sym == (SELECTOR_HISTORY_BUF_RLE_COUNT_TOTAL - 1))
  6702. cur_selector_rle_count = sym_codec.decode_vlc(7) + SELECTOR_HISTORY_BUF_RLE_COUNT_THRESH;
  6703. else
  6704. cur_selector_rle_count = run_sym + SELECTOR_HISTORY_BUF_RLE_COUNT_THRESH;
  6705. if (cur_selector_rle_count > total_blocks)
  6706. {
  6707. // The file is corrupted or we've got a bug.
  6708. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_slice: invalid datastream (3)\n");
  6709. if (pPVRTC_work_mem)
  6710. free(pPVRTC_work_mem);
  6711. return false;
  6712. }
  6713. selector_sym = (int)selectors.size();
  6714. cur_selector_rle_count--;
  6715. }
  6716. }
  6717. if (selector_sym >= (int)selectors.size())
  6718. {
  6719. assert(m_selector_history_buf_size > 0);
  6720. int history_buf_index = selector_sym - (int)selectors.size();
  6721. if (history_buf_index >= (int)selector_history_buf.size())
  6722. {
  6723. // The file is corrupted or we've got a bug.
  6724. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_slice: invalid datastream (4)\n");
  6725. if (pPVRTC_work_mem)
  6726. free(pPVRTC_work_mem);
  6727. return false;
  6728. }
  6729. selector_index = selector_history_buf[history_buf_index];
  6730. if (history_buf_index != 0)
  6731. selector_history_buf.use(history_buf_index);
  6732. }
  6733. else
  6734. {
  6735. selector_index = selector_sym;
  6736. if (m_selector_history_buf_size)
  6737. selector_history_buf.add(selector_index);
  6738. }
  6739. }
  6740. if ((endpoint_index >= endpoints.size()) || (selector_index >= selectors.size()))
  6741. {
  6742. // The file is corrupted or we've got a bug.
  6743. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_slice: invalid datastream (5)\n");
  6744. if (pPVRTC_work_mem)
  6745. free(pPVRTC_work_mem);
  6746. return false;
  6747. }
  6748. if (is_video)
  6749. (*pPrev_frame_indices)[block_x + block_y * num_blocks_x] = endpoint_index | (selector_index << 16);
  6750. #if BASISD_ENABLE_DEBUG_FLAGS
  6751. if ((g_debug_flags & cDebugFlagVisCRs) && ((fmt == block_format::cETC1) || (fmt == block_format::cBC1)))
  6752. {
  6753. if ((is_video) && (pred == 2))
  6754. {
  6755. decoder_etc_block* pDst_block = reinterpret_cast<decoder_etc_block*>(static_cast<uint8_t*>(pDst_blocks) + (block_x + block_y * output_row_pitch_in_blocks_or_pixels) * output_block_or_pixel_stride_in_bytes);
  6756. memset(pDst_block, 0xFF, 8);
  6757. continue;
  6758. }
  6759. }
  6760. #endif
  6761. const endpoint* pEndpoints = &endpoints[endpoint_index];
  6762. const selector* pSelector = &selectors[selector_index];
  6763. switch (fmt)
  6764. {
  6765. case block_format::cETC1:
  6766. {
  6767. decoder_etc_block* pDst_block = reinterpret_cast<decoder_etc_block*>(static_cast<uint8_t*>(pDst_blocks) + (block_x + block_y * output_row_pitch_in_blocks_or_pixels) * output_block_or_pixel_stride_in_bytes);
  6768. block.set_base5_color(decoder_etc_block::pack_color5(pEndpoints->m_color5, false));
  6769. block.set_inten_table(0, pEndpoints->m_inten5);
  6770. block.set_inten_table(1, pEndpoints->m_inten5);
  6771. pDst_block->m_uint32[0] = block.m_uint32[0];
  6772. pDst_block->set_raw_selector_bits(pSelector->m_bytes[0], pSelector->m_bytes[1], pSelector->m_bytes[2], pSelector->m_bytes[3]);
  6773. break;
  6774. }
  6775. case block_format::cBC1:
  6776. {
  6777. #if BASISD_SUPPORT_DXT1
  6778. void* pDst_block = static_cast<uint8_t*>(pDst_blocks) + (block_x + block_y * output_row_pitch_in_blocks_or_pixels) * output_block_or_pixel_stride_in_bytes;
  6779. #if BASISD_ENABLE_DEBUG_FLAGS
  6780. if (g_debug_flags & (cDebugFlagVisBC1Sels | cDebugFlagVisBC1Endpoints))
  6781. convert_etc1s_to_dxt1_vis(static_cast<dxt1_block*>(pDst_block), pEndpoints, pSelector, bc1_allow_threecolor_blocks);
  6782. else
  6783. #endif
  6784. convert_etc1s_to_dxt1(static_cast<dxt1_block*>(pDst_block), pEndpoints, pSelector, bc1_allow_threecolor_blocks);
  6785. #else
  6786. assert(0);
  6787. #endif
  6788. break;
  6789. }
  6790. case block_format::cBC4:
  6791. {
  6792. #if BASISD_SUPPORT_DXT5A
  6793. void* pDst_block = static_cast<uint8_t*>(pDst_blocks) + (block_x + block_y * output_row_pitch_in_blocks_or_pixels) * output_block_or_pixel_stride_in_bytes;
  6794. convert_etc1s_to_dxt5a(static_cast<dxt5a_block*>(pDst_block), pEndpoints, pSelector);
  6795. #else
  6796. assert(0);
  6797. #endif
  6798. break;
  6799. }
  6800. case block_format::cPVRTC1_4_RGB:
  6801. {
  6802. #if BASISD_SUPPORT_PVRTC1
  6803. block.set_base5_color(decoder_etc_block::pack_color5(pEndpoints->m_color5, false));
  6804. block.set_inten_table(0, pEndpoints->m_inten5);
  6805. block.set_inten_table(1, pEndpoints->m_inten5);
  6806. block.set_raw_selector_bits(pSelector->m_bytes[0], pSelector->m_bytes[1], pSelector->m_bytes[2], pSelector->m_bytes[3]);
  6807. ((decoder_etc_block*)pPVRTC_work_mem)[block_x + block_y * num_blocks_x] = block;
  6808. const color32& base_color = pEndpoints->m_color5;
  6809. const uint32_t inten_table = pEndpoints->m_inten5;
  6810. const uint32_t low_selector = pSelector->m_lo_selector;
  6811. const uint32_t high_selector = pSelector->m_hi_selector;
  6812. // Get block's RGB bounding box
  6813. color32 block_colors[2];
  6814. decoder_etc_block::get_block_colors5_bounds(block_colors, base_color, inten_table, low_selector, high_selector);
  6815. assert(block_colors[0][0] <= block_colors[1][0]);
  6816. assert(block_colors[0][1] <= block_colors[1][1]);
  6817. assert(block_colors[0][2] <= block_colors[1][2]);
  6818. // Set PVRTC1 endpoints to floor/ceil of bounding box's coordinates.
  6819. pvrtc4_block temp;
  6820. temp.set_opaque_endpoint_floor(0, block_colors[0]);
  6821. temp.set_opaque_endpoint_ceil(1, block_colors[1]);
  6822. pPVRTC_endpoints[block_x + block_y * num_blocks_x] = temp.m_endpoints;
  6823. #else
  6824. assert(0);
  6825. #endif
  6826. break;
  6827. }
  6828. case block_format::cPVRTC1_4_RGBA:
  6829. {
  6830. #if BASISD_SUPPORT_PVRTC1
  6831. assert(pAlpha_blocks);
  6832. block.set_base5_color(decoder_etc_block::pack_color5(pEndpoints->m_color5, false));
  6833. block.set_inten_table(0, pEndpoints->m_inten5);
  6834. block.set_inten_table(1, pEndpoints->m_inten5);
  6835. block.set_raw_selector_bits(pSelector->m_selectors[0], pSelector->m_selectors[1], pSelector->m_selectors[2], pSelector->m_selectors[3]);
  6836. ((decoder_etc_block*)pPVRTC_work_mem)[block_x + block_y * num_blocks_x] = block;
  6837. // Get block's RGBA bounding box
  6838. const color32& base_color = pEndpoints->m_color5;
  6839. const uint32_t inten_table = pEndpoints->m_inten5;
  6840. const uint32_t low_selector = pSelector->m_lo_selector;
  6841. const uint32_t high_selector = pSelector->m_hi_selector;
  6842. color32 block_colors[2];
  6843. decoder_etc_block::get_block_colors5_bounds(block_colors, base_color, inten_table, low_selector, high_selector);
  6844. assert(block_colors[0][0] <= block_colors[1][0]);
  6845. assert(block_colors[0][1] <= block_colors[1][1]);
  6846. assert(block_colors[0][2] <= block_colors[1][2]);
  6847. const uint16_t* pAlpha_block = reinterpret_cast<uint16_t*>(static_cast<uint8_t*>(pAlpha_blocks) + (block_x + block_y * num_blocks_x) * sizeof(uint32_t));
  6848. const endpoint* pAlpha_endpoints = &endpoints[pAlpha_block[0]];
  6849. const selector* pAlpha_selector = &selectors[pAlpha_block[1]];
  6850. const color32& alpha_base_color = pAlpha_endpoints->m_color5;
  6851. const uint32_t alpha_inten_table = pAlpha_endpoints->m_inten5;
  6852. const uint32_t alpha_low_selector = pAlpha_selector->m_lo_selector;
  6853. const uint32_t alpha_high_selector = pAlpha_selector->m_hi_selector;
  6854. uint32_t alpha_block_colors[2];
  6855. decoder_etc_block::get_block_colors5_bounds_g(alpha_block_colors, alpha_base_color, alpha_inten_table, alpha_low_selector, alpha_high_selector);
  6856. assert(alpha_block_colors[0] <= alpha_block_colors[1]);
  6857. block_colors[0].a = (uint8_t)alpha_block_colors[0];
  6858. block_colors[1].a = (uint8_t)alpha_block_colors[1];
  6859. // Set PVRTC1 endpoints to floor/ceil of bounding box's coordinates.
  6860. pvrtc4_block temp;
  6861. temp.set_endpoint_floor(0, block_colors[0]);
  6862. temp.set_endpoint_ceil(1, block_colors[1]);
  6863. pPVRTC_endpoints[block_x + block_y * num_blocks_x] = temp.m_endpoints;
  6864. #else
  6865. assert(0);
  6866. #endif
  6867. break;
  6868. }
  6869. case block_format::cBC7: // for more consistency with UASTC
  6870. case block_format::cBC7_M5_COLOR:
  6871. {
  6872. #if BASISD_SUPPORT_BC7_MODE5
  6873. void* pDst_block = static_cast<uint8_t*>(pDst_blocks) + (block_x + block_y * output_row_pitch_in_blocks_or_pixels) * output_block_or_pixel_stride_in_bytes;
  6874. convert_etc1s_to_bc7_m5_color(pDst_block, pEndpoints, pSelector);
  6875. #else
  6876. assert(0);
  6877. #endif
  6878. break;
  6879. }
  6880. case block_format::cBC7_M5_ALPHA:
  6881. {
  6882. #if BASISD_SUPPORT_BC7_MODE5
  6883. void* pDst_block = static_cast<uint8_t*>(pDst_blocks) + (block_x + block_y * output_row_pitch_in_blocks_or_pixels) * output_block_or_pixel_stride_in_bytes;
  6884. convert_etc1s_to_bc7_m5_alpha(pDst_block, pEndpoints, pSelector);
  6885. #else
  6886. assert(0);
  6887. #endif
  6888. break;
  6889. }
  6890. case block_format::cETC2_EAC_A8:
  6891. {
  6892. #if BASISD_SUPPORT_ETC2_EAC_A8
  6893. void* pDst_block = static_cast<uint8_t*>(pDst_blocks) + (block_x + block_y * output_row_pitch_in_blocks_or_pixels) * output_block_or_pixel_stride_in_bytes;
  6894. convert_etc1s_to_etc2_eac_a8(static_cast<eac_block*>(pDst_block), pEndpoints, pSelector);
  6895. #else
  6896. assert(0);
  6897. #endif
  6898. break;
  6899. }
  6900. case block_format::cASTC_4x4:
  6901. {
  6902. #if BASISD_SUPPORT_ASTC
  6903. void* pDst_block = static_cast<uint8_t*>(pDst_blocks) + (block_x + block_y * output_row_pitch_in_blocks_or_pixels) * output_block_or_pixel_stride_in_bytes;
  6904. convert_etc1s_to_astc_4x4(pDst_block, pEndpoints, pSelector, transcode_alpha, &endpoints[0], &selectors[0]);
  6905. #else
  6906. assert(0);
  6907. #endif
  6908. break;
  6909. }
  6910. case block_format::cATC_RGB:
  6911. {
  6912. #if BASISD_SUPPORT_ATC
  6913. void* pDst_block = static_cast<uint8_t*>(pDst_blocks) + (block_x + block_y * output_row_pitch_in_blocks_or_pixels) * output_block_or_pixel_stride_in_bytes;
  6914. convert_etc1s_to_atc(pDst_block, pEndpoints, pSelector);
  6915. #else
  6916. assert(0);
  6917. #endif
  6918. break;
  6919. }
  6920. case block_format::cFXT1_RGB:
  6921. {
  6922. #if BASISD_SUPPORT_FXT1
  6923. const uint32_t fxt1_block_x = block_x >> 1;
  6924. const uint32_t fxt1_block_y = block_y;
  6925. const uint32_t fxt1_subblock = block_x & 1;
  6926. void* pDst_block = static_cast<uint8_t*>(pDst_blocks) + (fxt1_block_x + fxt1_block_y * output_row_pitch_in_blocks_or_pixels) * output_block_or_pixel_stride_in_bytes;
  6927. convert_etc1s_to_fxt1(pDst_block, pEndpoints, pSelector, fxt1_subblock);
  6928. #else
  6929. assert(0);
  6930. #endif
  6931. break;
  6932. }
  6933. case block_format::cPVRTC2_4_RGB:
  6934. {
  6935. #if BASISD_SUPPORT_PVRTC2
  6936. void* pDst_block = static_cast<uint8_t*>(pDst_blocks) + (block_x + block_y * output_row_pitch_in_blocks_or_pixels) * output_block_or_pixel_stride_in_bytes;
  6937. convert_etc1s_to_pvrtc2_rgb(pDst_block, pEndpoints, pSelector);
  6938. #endif
  6939. break;
  6940. }
  6941. case block_format::cPVRTC2_4_RGBA:
  6942. {
  6943. #if BASISD_SUPPORT_PVRTC2
  6944. assert(transcode_alpha);
  6945. void* pDst_block = static_cast<uint8_t*>(pDst_blocks) + (block_x + block_y * output_row_pitch_in_blocks_or_pixels) * output_block_or_pixel_stride_in_bytes;
  6946. convert_etc1s_to_pvrtc2_rgba(pDst_block, pEndpoints, pSelector, &endpoints[0], &selectors[0]);
  6947. #endif
  6948. break;
  6949. }
  6950. case block_format::cIndices:
  6951. {
  6952. uint16_t* pDst_block = reinterpret_cast<uint16_t *>(static_cast<uint8_t*>(pDst_blocks) + (block_x + block_y * output_row_pitch_in_blocks_or_pixels) * output_block_or_pixel_stride_in_bytes);
  6953. pDst_block[0] = static_cast<uint16_t>(endpoint_index);
  6954. pDst_block[1] = static_cast<uint16_t>(selector_index);
  6955. break;
  6956. }
  6957. case block_format::cA32:
  6958. {
  6959. assert(sizeof(uint32_t) == output_block_or_pixel_stride_in_bytes);
  6960. uint8_t* pDst_pixels = static_cast<uint8_t*>(pDst_blocks) + (block_x * 4 + block_y * 4 * output_row_pitch_in_blocks_or_pixels) * sizeof(uint32_t);
  6961. const uint32_t max_x = basisu::minimum<int>(4, (int)output_row_pitch_in_blocks_or_pixels - (int)block_x * 4);
  6962. const uint32_t max_y = basisu::minimum<int>(4, (int)output_rows_in_pixels - (int)block_y * 4);
  6963. int colors[4];
  6964. decoder_etc_block::get_block_colors5_g(colors, pEndpoints->m_color5, pEndpoints->m_inten5);
  6965. if (max_x == 4)
  6966. {
  6967. for (uint32_t y = 0; y < max_y; y++)
  6968. {
  6969. const uint32_t s = pSelector->m_selectors[y];
  6970. pDst_pixels[3] = static_cast<uint8_t>(colors[s & 3]);
  6971. pDst_pixels[3+4] = static_cast<uint8_t>(colors[(s >> 2) & 3]);
  6972. pDst_pixels[3+8] = static_cast<uint8_t>(colors[(s >> 4) & 3]);
  6973. pDst_pixels[3+12] = static_cast<uint8_t>(colors[(s >> 6) & 3]);
  6974. pDst_pixels += output_row_pitch_in_blocks_or_pixels * sizeof(uint32_t);
  6975. }
  6976. }
  6977. else
  6978. {
  6979. for (uint32_t y = 0; y < max_y; y++)
  6980. {
  6981. const uint32_t s = pSelector->m_selectors[y];
  6982. for (uint32_t x = 0; x < max_x; x++)
  6983. pDst_pixels[3 + 4 * x] = static_cast<uint8_t>(colors[(s >> (x * 2)) & 3]);
  6984. pDst_pixels += output_row_pitch_in_blocks_or_pixels * sizeof(uint32_t);
  6985. }
  6986. }
  6987. break;
  6988. }
  6989. case block_format::cRGB32:
  6990. {
  6991. assert(sizeof(uint32_t) == output_block_or_pixel_stride_in_bytes);
  6992. uint8_t* pDst_pixels = static_cast<uint8_t*>(pDst_blocks) + (block_x * 4 + block_y * 4 * output_row_pitch_in_blocks_or_pixels) * sizeof(uint32_t);
  6993. const uint32_t max_x = basisu::minimum<int>(4, (int)output_row_pitch_in_blocks_or_pixels - (int)block_x * 4);
  6994. const uint32_t max_y = basisu::minimum<int>(4, (int)output_rows_in_pixels - (int)block_y * 4);
  6995. color32 colors[4];
  6996. decoder_etc_block::get_block_colors5(colors, pEndpoints->m_color5, pEndpoints->m_inten5);
  6997. for (uint32_t y = 0; y < max_y; y++)
  6998. {
  6999. const uint32_t s = pSelector->m_selectors[y];
  7000. for (uint32_t x = 0; x < max_x; x++)
  7001. {
  7002. const color32& c = colors[(s >> (x * 2)) & 3];
  7003. pDst_pixels[0 + 4 * x] = c.r;
  7004. pDst_pixels[1 + 4 * x] = c.g;
  7005. pDst_pixels[2 + 4 * x] = c.b;
  7006. }
  7007. pDst_pixels += output_row_pitch_in_blocks_or_pixels * sizeof(uint32_t);
  7008. }
  7009. break;
  7010. }
  7011. case block_format::cRGBA32:
  7012. {
  7013. assert(sizeof(uint32_t) == output_block_or_pixel_stride_in_bytes);
  7014. uint8_t* pDst_pixels = static_cast<uint8_t*>(pDst_blocks) + (block_x * 4 + block_y * 4 * output_row_pitch_in_blocks_or_pixels) * sizeof(uint32_t);
  7015. const uint32_t max_x = basisu::minimum<int>(4, (int)output_row_pitch_in_blocks_or_pixels - (int)block_x * 4);
  7016. const uint32_t max_y = basisu::minimum<int>(4, (int)output_rows_in_pixels - (int)block_y * 4);
  7017. color32 colors[4];
  7018. decoder_etc_block::get_block_colors5(colors, pEndpoints->m_color5, pEndpoints->m_inten5);
  7019. for (uint32_t y = 0; y < max_y; y++)
  7020. {
  7021. const uint32_t s = pSelector->m_selectors[y];
  7022. for (uint32_t x = 0; x < max_x; x++)
  7023. {
  7024. const color32& c = colors[(s >> (x * 2)) & 3];
  7025. pDst_pixels[0 + 4 * x] = c.r;
  7026. pDst_pixels[1 + 4 * x] = c.g;
  7027. pDst_pixels[2 + 4 * x] = c.b;
  7028. pDst_pixels[3 + 4 * x] = 255;
  7029. }
  7030. pDst_pixels += output_row_pitch_in_blocks_or_pixels * sizeof(uint32_t);
  7031. }
  7032. break;
  7033. }
  7034. case block_format::cRGB565:
  7035. case block_format::cBGR565:
  7036. {
  7037. assert(sizeof(uint16_t) == output_block_or_pixel_stride_in_bytes);
  7038. uint8_t* pDst_pixels = static_cast<uint8_t*>(pDst_blocks) + (block_x * 4 + block_y * 4 * output_row_pitch_in_blocks_or_pixels) * sizeof(uint16_t);
  7039. const uint32_t max_x = basisu::minimum<int>(4, (int)output_row_pitch_in_blocks_or_pixels - (int)block_x * 4);
  7040. const uint32_t max_y = basisu::minimum<int>(4, (int)output_rows_in_pixels - (int)block_y * 4);
  7041. color32 colors[4];
  7042. decoder_etc_block::get_block_colors5(colors, pEndpoints->m_color5, pEndpoints->m_inten5);
  7043. uint16_t packed_colors[4];
  7044. if (fmt == block_format::cRGB565)
  7045. {
  7046. for (uint32_t i = 0; i < 4; i++)
  7047. {
  7048. packed_colors[i] = static_cast<uint16_t>((mul_8(colors[i].r, 31) << 11) | (mul_8(colors[i].g, 63) << 5) | mul_8(colors[i].b, 31));
  7049. if (BASISD_IS_BIG_ENDIAN)
  7050. packed_colors[i] = byteswap_uint16(packed_colors[i]);
  7051. }
  7052. }
  7053. else
  7054. {
  7055. for (uint32_t i = 0; i < 4; i++)
  7056. {
  7057. packed_colors[i] = static_cast<uint16_t>((mul_8(colors[i].b, 31) << 11) | (mul_8(colors[i].g, 63) << 5) | mul_8(colors[i].r, 31));
  7058. if (BASISD_IS_BIG_ENDIAN)
  7059. packed_colors[i] = byteswap_uint16(packed_colors[i]);
  7060. }
  7061. }
  7062. for (uint32_t y = 0; y < max_y; y++)
  7063. {
  7064. const uint32_t s = pSelector->m_selectors[y];
  7065. for (uint32_t x = 0; x < max_x; x++)
  7066. reinterpret_cast<uint16_t *>(pDst_pixels)[x] = packed_colors[(s >> (x * 2)) & 3];
  7067. pDst_pixels += output_row_pitch_in_blocks_or_pixels * sizeof(uint16_t);
  7068. }
  7069. break;
  7070. }
  7071. case block_format::cRGBA4444_COLOR:
  7072. {
  7073. assert(sizeof(uint16_t) == output_block_or_pixel_stride_in_bytes);
  7074. uint8_t* pDst_pixels = static_cast<uint8_t*>(pDst_blocks) + (block_x * 4 + block_y * 4 * output_row_pitch_in_blocks_or_pixels) * sizeof(uint16_t);
  7075. const uint32_t max_x = basisu::minimum<int>(4, (int)output_row_pitch_in_blocks_or_pixels - (int)block_x * 4);
  7076. const uint32_t max_y = basisu::minimum<int>(4, (int)output_rows_in_pixels - (int)block_y * 4);
  7077. color32 colors[4];
  7078. decoder_etc_block::get_block_colors5(colors, pEndpoints->m_color5, pEndpoints->m_inten5);
  7079. uint16_t packed_colors[4];
  7080. for (uint32_t i = 0; i < 4; i++)
  7081. {
  7082. packed_colors[i] = static_cast<uint16_t>((mul_8(colors[i].r, 15) << 12) | (mul_8(colors[i].g, 15) << 8) | (mul_8(colors[i].b, 15) << 4));
  7083. }
  7084. for (uint32_t y = 0; y < max_y; y++)
  7085. {
  7086. const uint32_t s = pSelector->m_selectors[y];
  7087. for (uint32_t x = 0; x < max_x; x++)
  7088. {
  7089. uint16_t cur = reinterpret_cast<uint16_t*>(pDst_pixels)[x];
  7090. if (BASISD_IS_BIG_ENDIAN)
  7091. cur = byteswap_uint16(cur);
  7092. cur = (cur & 0xF) | packed_colors[(s >> (x * 2)) & 3];
  7093. if (BASISD_IS_BIG_ENDIAN)
  7094. cur = byteswap_uint16(cur);
  7095. reinterpret_cast<uint16_t*>(pDst_pixels)[x] = cur;
  7096. }
  7097. pDst_pixels += output_row_pitch_in_blocks_or_pixels * sizeof(uint16_t);
  7098. }
  7099. break;
  7100. }
  7101. case block_format::cRGBA4444_COLOR_OPAQUE:
  7102. {
  7103. assert(sizeof(uint16_t) == output_block_or_pixel_stride_in_bytes);
  7104. uint8_t* pDst_pixels = static_cast<uint8_t*>(pDst_blocks) + (block_x * 4 + block_y * 4 * output_row_pitch_in_blocks_or_pixels) * sizeof(uint16_t);
  7105. const uint32_t max_x = basisu::minimum<int>(4, (int)output_row_pitch_in_blocks_or_pixels - (int)block_x * 4);
  7106. const uint32_t max_y = basisu::minimum<int>(4, (int)output_rows_in_pixels - (int)block_y * 4);
  7107. color32 colors[4];
  7108. decoder_etc_block::get_block_colors5(colors, pEndpoints->m_color5, pEndpoints->m_inten5);
  7109. uint16_t packed_colors[4];
  7110. for (uint32_t i = 0; i < 4; i++)
  7111. {
  7112. packed_colors[i] = static_cast<uint16_t>((mul_8(colors[i].r, 15) << 12) | (mul_8(colors[i].g, 15) << 8) | (mul_8(colors[i].b, 15) << 4) | 0xF);
  7113. if (BASISD_IS_BIG_ENDIAN)
  7114. packed_colors[i] = byteswap_uint16(packed_colors[i]);
  7115. }
  7116. for (uint32_t y = 0; y < max_y; y++)
  7117. {
  7118. const uint32_t s = pSelector->m_selectors[y];
  7119. for (uint32_t x = 0; x < max_x; x++)
  7120. reinterpret_cast<uint16_t*>(pDst_pixels)[x] = packed_colors[(s >> (x * 2)) & 3];
  7121. pDst_pixels += output_row_pitch_in_blocks_or_pixels * sizeof(uint16_t);
  7122. }
  7123. break;
  7124. }
  7125. case block_format::cRGBA4444_ALPHA:
  7126. {
  7127. assert(sizeof(uint16_t) == output_block_or_pixel_stride_in_bytes);
  7128. uint8_t* pDst_pixels = static_cast<uint8_t*>(pDst_blocks) + (block_x * 4 + block_y * 4 * output_row_pitch_in_blocks_or_pixels) * sizeof(uint16_t);
  7129. const uint32_t max_x = basisu::minimum<int>(4, (int)output_row_pitch_in_blocks_or_pixels - (int)block_x * 4);
  7130. const uint32_t max_y = basisu::minimum<int>(4, (int)output_rows_in_pixels - (int)block_y * 4);
  7131. color32 colors[4];
  7132. decoder_etc_block::get_block_colors5(colors, pEndpoints->m_color5, pEndpoints->m_inten5);
  7133. uint16_t packed_colors[4];
  7134. for (uint32_t i = 0; i < 4; i++)
  7135. {
  7136. packed_colors[i] = mul_8(colors[i].g, 15);
  7137. if (BASISD_IS_BIG_ENDIAN)
  7138. packed_colors[i] = byteswap_uint16(packed_colors[i]);
  7139. }
  7140. for (uint32_t y = 0; y < max_y; y++)
  7141. {
  7142. const uint32_t s = pSelector->m_selectors[y];
  7143. for (uint32_t x = 0; x < max_x; x++)
  7144. {
  7145. reinterpret_cast<uint16_t*>(pDst_pixels)[x] = packed_colors[(s >> (x * 2)) & 3];
  7146. }
  7147. pDst_pixels += output_row_pitch_in_blocks_or_pixels * sizeof(uint16_t);
  7148. }
  7149. break;
  7150. }
  7151. case block_format::cETC2_EAC_R11:
  7152. {
  7153. #if BASISD_SUPPORT_ETC2_EAC_RG11
  7154. void* pDst_block = static_cast<uint8_t*>(pDst_blocks) + (block_x + block_y * output_row_pitch_in_blocks_or_pixels) * output_block_or_pixel_stride_in_bytes;
  7155. convert_etc1s_to_etc2_eac_r11(static_cast<eac_block*>(pDst_block), pEndpoints, pSelector);
  7156. #else
  7157. assert(0);
  7158. #endif
  7159. break;
  7160. }
  7161. default:
  7162. {
  7163. assert(0);
  7164. break;
  7165. }
  7166. }
  7167. } // block_x
  7168. } // block-y
  7169. if (endpoint_pred_repeat_count != 0)
  7170. {
  7171. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_slice: endpoint_pred_repeat_count != 0. The file is corrupted or this is a bug\n");
  7172. return false;
  7173. }
  7174. //assert(endpoint_pred_repeat_count == 0);
  7175. #if BASISD_SUPPORT_PVRTC1
  7176. // PVRTC post process - create per-pixel modulation values.
  7177. if (fmt == block_format::cPVRTC1_4_RGB)
  7178. fixup_pvrtc1_4_modulation_rgb((decoder_etc_block*)pPVRTC_work_mem, pPVRTC_endpoints, pDst_blocks, num_blocks_x, num_blocks_y);
  7179. else if (fmt == block_format::cPVRTC1_4_RGBA)
  7180. fixup_pvrtc1_4_modulation_rgba((decoder_etc_block*)pPVRTC_work_mem, pPVRTC_endpoints, pDst_blocks, num_blocks_x, num_blocks_y, pAlpha_blocks, &endpoints[0], &selectors[0]);
  7181. #endif // BASISD_SUPPORT_PVRTC1
  7182. if (pPVRTC_work_mem)
  7183. free(pPVRTC_work_mem);
  7184. return true;
  7185. }
  7186. bool basis_validate_output_buffer_size(transcoder_texture_format target_format,
  7187. uint32_t output_blocks_buf_size_in_blocks_or_pixels,
  7188. uint32_t orig_width, uint32_t orig_height,
  7189. uint32_t output_row_pitch_in_blocks_or_pixels,
  7190. uint32_t output_rows_in_pixels,
  7191. uint32_t total_slice_blocks)
  7192. {
  7193. if (basis_transcoder_format_is_uncompressed(target_format))
  7194. {
  7195. // Assume the output buffer is orig_width by orig_height
  7196. if (!output_row_pitch_in_blocks_or_pixels)
  7197. output_row_pitch_in_blocks_or_pixels = orig_width;
  7198. if (!output_rows_in_pixels)
  7199. output_rows_in_pixels = orig_height;
  7200. // Now make sure the output buffer is large enough, or we'll overwrite memory.
  7201. if (output_blocks_buf_size_in_blocks_or_pixels < (output_rows_in_pixels * output_row_pitch_in_blocks_or_pixels))
  7202. {
  7203. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_image: output_blocks_buf_size_in_blocks_or_pixels < (output_rows_in_pixels * output_row_pitch_in_blocks_or_pixels)\n");
  7204. return false;
  7205. }
  7206. }
  7207. else if (target_format == transcoder_texture_format::cTFFXT1_RGB)
  7208. {
  7209. const uint32_t num_blocks_fxt1_x = (orig_width + 7) / 8;
  7210. const uint32_t num_blocks_fxt1_y = (orig_height + 3) / 4;
  7211. const uint32_t total_blocks_fxt1 = num_blocks_fxt1_x * num_blocks_fxt1_y;
  7212. if (output_blocks_buf_size_in_blocks_or_pixels < total_blocks_fxt1)
  7213. {
  7214. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_image: output_blocks_buf_size_in_blocks_or_pixels < total_blocks_fxt1\n");
  7215. return false;
  7216. }
  7217. }
  7218. else
  7219. {
  7220. if (output_blocks_buf_size_in_blocks_or_pixels < total_slice_blocks)
  7221. {
  7222. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_image: output_blocks_buf_size_in_blocks_or_pixels < transcode_image\n");
  7223. return false;
  7224. }
  7225. }
  7226. return true;
  7227. }
  7228. bool basisu_lowlevel_etc1s_transcoder::transcode_image(
  7229. transcoder_texture_format target_format,
  7230. void* pOutput_blocks, uint32_t output_blocks_buf_size_in_blocks_or_pixels,
  7231. const uint8_t* pCompressed_data, uint32_t compressed_data_length,
  7232. uint32_t num_blocks_x, uint32_t num_blocks_y, uint32_t orig_width, uint32_t orig_height, uint32_t level_index,
  7233. uint32_t rgb_offset, uint32_t rgb_length, uint32_t alpha_offset, uint32_t alpha_length,
  7234. uint32_t decode_flags,
  7235. bool basis_file_has_alpha_slices,
  7236. bool is_video,
  7237. uint32_t output_row_pitch_in_blocks_or_pixels,
  7238. basisu_transcoder_state* pState,
  7239. uint32_t output_rows_in_pixels)
  7240. {
  7241. if (((uint64_t)rgb_offset + rgb_length) > (uint64_t)compressed_data_length)
  7242. {
  7243. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_image: source data buffer too small (color)\n");
  7244. return false;
  7245. }
  7246. if (alpha_length)
  7247. {
  7248. if (((uint64_t)alpha_offset + alpha_length) > (uint64_t)compressed_data_length)
  7249. {
  7250. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_image: source data buffer too small (alpha)\n");
  7251. return false;
  7252. }
  7253. }
  7254. else
  7255. {
  7256. assert(!basis_file_has_alpha_slices);
  7257. }
  7258. if ((target_format == transcoder_texture_format::cTFPVRTC1_4_RGB) || (target_format == transcoder_texture_format::cTFPVRTC1_4_RGBA))
  7259. {
  7260. if ((!basisu::is_pow2(num_blocks_x * 4)) || (!basisu::is_pow2(num_blocks_y * 4)))
  7261. {
  7262. // PVRTC1 only supports power of 2 dimensions
  7263. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_image: PVRTC1 only supports power of 2 dimensions\n");
  7264. return false;
  7265. }
  7266. }
  7267. if ((target_format == transcoder_texture_format::cTFPVRTC1_4_RGBA) && (!basis_file_has_alpha_slices))
  7268. {
  7269. // Switch to PVRTC1 RGB if the input doesn't have alpha.
  7270. target_format = transcoder_texture_format::cTFPVRTC1_4_RGB;
  7271. }
  7272. const bool transcode_alpha_data_to_opaque_formats = (decode_flags & cDecodeFlagsTranscodeAlphaDataToOpaqueFormats) != 0;
  7273. const uint32_t bytes_per_block_or_pixel = basis_get_bytes_per_block_or_pixel(target_format);
  7274. const uint32_t total_slice_blocks = num_blocks_x * num_blocks_y;
  7275. if (!basis_validate_output_buffer_size(target_format, output_blocks_buf_size_in_blocks_or_pixels, orig_width, orig_height, output_row_pitch_in_blocks_or_pixels, output_rows_in_pixels, total_slice_blocks))
  7276. {
  7277. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_image: output buffer size too small\n");
  7278. return false;
  7279. }
  7280. bool status = false;
  7281. const uint8_t* pData = pCompressed_data + rgb_offset;
  7282. uint32_t data_len = rgb_length;
  7283. bool is_alpha_slice = false;
  7284. // If the caller wants us to transcode the mip level's alpha data, then use the next slice.
  7285. if ((basis_file_has_alpha_slices) && (transcode_alpha_data_to_opaque_formats))
  7286. {
  7287. pData = pCompressed_data + alpha_offset;
  7288. data_len = alpha_length;
  7289. is_alpha_slice = true;
  7290. }
  7291. switch (target_format)
  7292. {
  7293. case transcoder_texture_format::cTFETC1_RGB:
  7294. {
  7295. //status = transcode_slice(pData, data_size, slice_index_to_decode, pOutput_blocks, output_blocks_buf_size_in_blocks_or_pixels, block_format::cETC1, bytes_per_block_or_pixel, decode_flags, output_row_pitch_in_blocks_or_pixels, pState);
  7296. status = transcode_slice(pOutput_blocks, num_blocks_x, num_blocks_y, pData, data_len, block_format::cETC1, bytes_per_block_or_pixel, false, is_video, is_alpha_slice, level_index, orig_width, orig_height, output_row_pitch_in_blocks_or_pixels, pState, false, nullptr, output_rows_in_pixels);
  7297. if (!status)
  7298. {
  7299. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_image: transcode_slice() to ETC1 failed\n");
  7300. }
  7301. break;
  7302. }
  7303. case transcoder_texture_format::cTFBC1_RGB:
  7304. {
  7305. #if !BASISD_SUPPORT_DXT1
  7306. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_image: BC1/DXT1 unsupported\n");
  7307. return false;
  7308. #else
  7309. // status = transcode_slice(pData, data_size, slice_index_to_decode, pOutput_blocks, output_blocks_buf_size_in_blocks_or_pixels, block_format::cBC1, bytes_per_block_or_pixel, decode_flags, output_row_pitch_in_blocks_or_pixels, pState);
  7310. status = transcode_slice(pOutput_blocks, num_blocks_x, num_blocks_y, pData, data_len, block_format::cBC1, bytes_per_block_or_pixel, true, is_video, is_alpha_slice, level_index, orig_width, orig_height, output_row_pitch_in_blocks_or_pixels, pState, false, nullptr, output_rows_in_pixels);
  7311. if (!status)
  7312. {
  7313. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_image: transcode_slice() to BC1 failed\n");
  7314. }
  7315. break;
  7316. #endif
  7317. }
  7318. case transcoder_texture_format::cTFBC4_R:
  7319. {
  7320. #if !BASISD_SUPPORT_DXT5A
  7321. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_image: BC4/DXT5A unsupported\n");
  7322. return false;
  7323. #else
  7324. //status = transcode_slice(pData, data_size, slice_index_to_decode, pOutput_blocks, output_blocks_buf_size_in_blocks_or_pixels, block_format::cBC4, bytes_per_block_or_pixel, decode_flags, output_row_pitch_in_blocks_or_pixels, pState);
  7325. status = transcode_slice(pOutput_blocks, num_blocks_x, num_blocks_y, pData, data_len, block_format::cBC4, bytes_per_block_or_pixel, false, is_video, is_alpha_slice, level_index, orig_width, orig_height, output_row_pitch_in_blocks_or_pixels, pState, false, nullptr, output_rows_in_pixels);
  7326. if (!status)
  7327. {
  7328. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_image: transcode_slice() to BC4 failed\n");
  7329. }
  7330. break;
  7331. #endif
  7332. }
  7333. case transcoder_texture_format::cTFPVRTC1_4_RGB:
  7334. {
  7335. #if !BASISD_SUPPORT_PVRTC1
  7336. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_image: PVRTC1 4 unsupported\n");
  7337. return false;
  7338. #else
  7339. // output_row_pitch_in_blocks_or_pixels is actually ignored because we're transcoding to PVRTC1. (Print a dev warning if it's != 0?)
  7340. //status = transcode_slice(pData, data_size, slice_index_to_decode, pOutput_blocks, output_blocks_buf_size_in_blocks_or_pixels, block_format::cPVRTC1_4_RGB, bytes_per_block_or_pixel, decode_flags, output_row_pitch_in_blocks_or_pixels, pState);
  7341. status = transcode_slice(pOutput_blocks, num_blocks_x, num_blocks_y, pData, data_len, block_format::cPVRTC1_4_RGB, bytes_per_block_or_pixel, false, is_video, is_alpha_slice, level_index, orig_width, orig_height, output_row_pitch_in_blocks_or_pixels, pState, false, nullptr, output_rows_in_pixels);
  7342. if (!status)
  7343. {
  7344. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_image: transcode_slice() to PVRTC1 4 RGB failed\n");
  7345. }
  7346. break;
  7347. #endif
  7348. }
  7349. case transcoder_texture_format::cTFPVRTC1_4_RGBA:
  7350. {
  7351. #if !BASISD_SUPPORT_PVRTC1
  7352. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_image: PVRTC1 4 unsupported\n");
  7353. return false;
  7354. #else
  7355. assert(basis_file_has_alpha_slices);
  7356. assert(alpha_length);
  7357. // Temp buffer to hold alpha block endpoint/selector indices
  7358. basisu::vector<uint32_t> temp_block_indices(total_slice_blocks);
  7359. // First transcode alpha data to temp buffer
  7360. //status = transcode_slice(pData, data_size, slice_index + 1, &temp_block_indices[0], total_slice_blocks, block_format::cIndices, sizeof(uint32_t), decode_flags, pSlice_descs[slice_index].m_num_blocks_x, pState);
  7361. status = transcode_slice(&temp_block_indices[0], num_blocks_x, num_blocks_y, pCompressed_data + alpha_offset, alpha_length, block_format::cIndices, sizeof(uint32_t), false, is_video, true, level_index, orig_width, orig_height, num_blocks_x, pState, false, nullptr, 0);
  7362. if (!status)
  7363. {
  7364. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_image: transcode_slice() to PVRTC1 4 RGBA failed (0)\n");
  7365. }
  7366. else
  7367. {
  7368. // output_row_pitch_in_blocks_or_pixels is actually ignored because we're transcoding to PVRTC1. (Print a dev warning if it's != 0?)
  7369. //status = transcode_slice(pData, data_size, slice_index, pOutput_blocks, output_blocks_buf_size_in_blocks_or_pixels, block_format::cPVRTC1_4_RGBA, bytes_per_block_or_pixel, decode_flags, output_row_pitch_in_blocks_or_pixels, pState, &temp_block_indices[0]);
  7370. status = transcode_slice(pOutput_blocks, num_blocks_x, num_blocks_y, pCompressed_data + rgb_offset, rgb_length, block_format::cPVRTC1_4_RGBA, bytes_per_block_or_pixel, false, is_video, false, level_index, orig_width, orig_height, output_row_pitch_in_blocks_or_pixels, pState, false, &temp_block_indices[0], 0);
  7371. if (!status)
  7372. {
  7373. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_image: transcode_slice() to PVRTC1 4 RGBA failed (1)\n");
  7374. }
  7375. }
  7376. break;
  7377. #endif
  7378. }
  7379. case transcoder_texture_format::cTFBC7_RGBA:
  7380. case transcoder_texture_format::cTFBC7_ALT:
  7381. {
  7382. #if !BASISD_SUPPORT_BC7_MODE5
  7383. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_image: BC7 unsupported\n");
  7384. return false;
  7385. #else
  7386. assert(bytes_per_block_or_pixel == 16);
  7387. // We used to support transcoding just alpha to BC7 - but is that useful at all?
  7388. // First transcode the color slice. The cBC7_M5_COLOR transcoder will output opaque mode 5 blocks.
  7389. //status = transcode_slice(pData, data_size, slice_index, pOutput_blocks, output_blocks_buf_size_in_blocks_or_pixels, block_format::cBC7_M5_COLOR, 16, decode_flags, output_row_pitch_in_blocks_or_pixels, pState);
  7390. status = transcode_slice(pOutput_blocks, num_blocks_x, num_blocks_y, pCompressed_data + rgb_offset, rgb_length, block_format::cBC7_M5_COLOR, bytes_per_block_or_pixel, false, is_video, false, level_index, orig_width, orig_height, output_row_pitch_in_blocks_or_pixels, pState, false, nullptr, output_rows_in_pixels);
  7391. if ((status) && (basis_file_has_alpha_slices))
  7392. {
  7393. // Now transcode the alpha slice. The cBC7_M5_ALPHA transcoder will now change the opaque mode 5 blocks to blocks with alpha.
  7394. //status = transcode_slice(pData, data_size, slice_index + 1, pOutput_blocks, output_blocks_buf_size_in_blocks_or_pixels, block_format::cBC7_M5_ALPHA, 16, decode_flags, output_row_pitch_in_blocks_or_pixels, pState);
  7395. status = transcode_slice(pOutput_blocks, num_blocks_x, num_blocks_y, pCompressed_data + alpha_offset, alpha_length, block_format::cBC7_M5_ALPHA, bytes_per_block_or_pixel, false, is_video, true, level_index, orig_width, orig_height, output_row_pitch_in_blocks_or_pixels, pState, false, nullptr, output_rows_in_pixels);
  7396. }
  7397. if (!status)
  7398. {
  7399. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_image: transcode_slice() to BC7 failed (0)\n");
  7400. }
  7401. break;
  7402. #endif
  7403. }
  7404. case transcoder_texture_format::cTFETC2_RGBA:
  7405. {
  7406. #if !BASISD_SUPPORT_ETC2_EAC_A8
  7407. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_image: ETC2 EAC A8 unsupported\n");
  7408. return false;
  7409. #else
  7410. assert(bytes_per_block_or_pixel == 16);
  7411. if (basis_file_has_alpha_slices)
  7412. {
  7413. // First decode the alpha data
  7414. //status = transcode_slice(pData, data_size, slice_index + 1, pOutput_blocks, output_blocks_buf_size_in_blocks_or_pixels, block_format::cETC2_EAC_A8, 16, decode_flags, output_row_pitch_in_blocks_or_pixels, pState);
  7415. status = transcode_slice(pOutput_blocks, num_blocks_x, num_blocks_y, pCompressed_data + alpha_offset, alpha_length, block_format::cETC2_EAC_A8, bytes_per_block_or_pixel, false, is_video, true, level_index, orig_width, orig_height, output_row_pitch_in_blocks_or_pixels, pState, false, nullptr, output_rows_in_pixels);
  7416. }
  7417. else
  7418. {
  7419. //write_opaque_alpha_blocks(pSlice_descs[slice_index].m_num_blocks_x, pSlice_descs[slice_index].m_num_blocks_y, pOutput_blocks, output_blocks_buf_size_in_blocks_or_pixels, block_format::cETC2_EAC_A8, 16, output_row_pitch_in_blocks_or_pixels);
  7420. basisu_transcoder::write_opaque_alpha_blocks(num_blocks_x, num_blocks_y, pOutput_blocks, block_format::cETC2_EAC_A8, 16, output_row_pitch_in_blocks_or_pixels);
  7421. status = true;
  7422. }
  7423. if (status)
  7424. {
  7425. // Now decode the color data
  7426. //status = transcode_slice(pData, data_size, slice_index, (uint8_t*)pOutput_blocks + 8, output_blocks_buf_size_in_blocks_or_pixels, block_format::cETC1, 16, decode_flags, output_row_pitch_in_blocks_or_pixels, pState);
  7427. status = transcode_slice((uint8_t *)pOutput_blocks + 8, num_blocks_x, num_blocks_y, pCompressed_data + rgb_offset, rgb_length, block_format::cETC1, bytes_per_block_or_pixel, false, is_video, false, level_index, orig_width, orig_height, output_row_pitch_in_blocks_or_pixels, pState, false, nullptr, output_rows_in_pixels);
  7428. if (!status)
  7429. {
  7430. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_image: transcode_slice() to ETC2 RGB failed\n");
  7431. }
  7432. }
  7433. else
  7434. {
  7435. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_image: transcode_slice() to ETC2 A failed\n");
  7436. }
  7437. break;
  7438. #endif
  7439. }
  7440. case transcoder_texture_format::cTFBC3_RGBA:
  7441. {
  7442. #if !BASISD_SUPPORT_DXT1
  7443. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_image: DXT1 unsupported\n");
  7444. return false;
  7445. #elif !BASISD_SUPPORT_DXT5A
  7446. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_image: DXT5A unsupported\n");
  7447. return false;
  7448. #else
  7449. assert(bytes_per_block_or_pixel == 16);
  7450. // First decode the alpha data
  7451. if (basis_file_has_alpha_slices)
  7452. {
  7453. //status = transcode_slice(pData, data_size, slice_index + 1, pOutput_blocks, output_blocks_buf_size_in_blocks_or_pixels, block_format::cBC4, 16, decode_flags, output_row_pitch_in_blocks_or_pixels, pState);
  7454. status = transcode_slice(pOutput_blocks, num_blocks_x, num_blocks_y, pCompressed_data + alpha_offset, alpha_length, block_format::cBC4, bytes_per_block_or_pixel, false, is_video, true, level_index, orig_width, orig_height, output_row_pitch_in_blocks_or_pixels, pState, false, nullptr, output_rows_in_pixels);
  7455. }
  7456. else
  7457. {
  7458. basisu_transcoder::write_opaque_alpha_blocks(num_blocks_x, num_blocks_y, pOutput_blocks, block_format::cBC4, 16, output_row_pitch_in_blocks_or_pixels);
  7459. status = true;
  7460. }
  7461. if (status)
  7462. {
  7463. // Now decode the color data. Forbid 3 color blocks, which aren't allowed in BC3.
  7464. //status = transcode_slice(pData, data_size, slice_index, (uint8_t*)pOutput_blocks + 8, output_blocks_buf_size_in_blocks_or_pixels, block_format::cBC1, 16, decode_flags | cDecodeFlagsBC1ForbidThreeColorBlocks, output_row_pitch_in_blocks_or_pixels, pState);
  7465. status = transcode_slice((uint8_t *)pOutput_blocks + 8, num_blocks_x, num_blocks_y, pCompressed_data + rgb_offset, rgb_length, block_format::cBC1, bytes_per_block_or_pixel, false, is_video, false, level_index, orig_width, orig_height, output_row_pitch_in_blocks_or_pixels, pState, false, nullptr, output_rows_in_pixels);
  7466. if (!status)
  7467. {
  7468. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_image: transcode_slice() to BC3 RGB failed\n");
  7469. }
  7470. }
  7471. else
  7472. {
  7473. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_image: transcode_slice() to BC3 A failed\n");
  7474. }
  7475. break;
  7476. #endif
  7477. }
  7478. case transcoder_texture_format::cTFBC5_RG:
  7479. {
  7480. #if !BASISD_SUPPORT_DXT5A
  7481. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_image: DXT5A unsupported\n");
  7482. return false;
  7483. #else
  7484. assert(bytes_per_block_or_pixel == 16);
  7485. //bool transcode_slice(void* pDst_blocks, uint32_t num_blocks_x, uint32_t num_blocks_y, const uint8_t* pImage_data, uint32_t image_data_size, block_format fmt,
  7486. // uint32_t output_block_or_pixel_stride_in_bytes, bool bc1_allow_threecolor_blocks, const bool is_video, const bool is_alpha_slice, const uint32_t level_index, const uint32_t orig_width, const uint32_t orig_height, uint32_t output_row_pitch_in_blocks_or_pixels = 0,
  7487. // basisu_transcoder_state* pState = nullptr, bool astc_transcode_alpha = false, void* pAlpha_blocks = nullptr, uint32_t output_rows_in_pixels = 0);
  7488. // Decode the R data (actually the green channel of the color data slice in the basis file)
  7489. //status = transcode_slice(pData, data_size, slice_index, pOutput_blocks, output_blocks_buf_size_in_blocks_or_pixels, block_format::cBC4, 16, decode_flags, output_row_pitch_in_blocks_or_pixels, pState);
  7490. status = transcode_slice(pOutput_blocks, num_blocks_x, num_blocks_y, pCompressed_data + rgb_offset, rgb_length, block_format::cBC4, bytes_per_block_or_pixel, false, is_video, false, level_index, orig_width, orig_height, output_row_pitch_in_blocks_or_pixels, pState, false, nullptr, output_rows_in_pixels);
  7491. if (status)
  7492. {
  7493. if (basis_file_has_alpha_slices)
  7494. {
  7495. // Decode the G data (actually the green channel of the alpha data slice in the basis file)
  7496. //status = transcode_slice(pData, data_size, slice_index + 1, (uint8_t*)pOutput_blocks + 8, output_blocks_buf_size_in_blocks_or_pixels, block_format::cBC4, 16, decode_flags, output_row_pitch_in_blocks_or_pixels, pState);
  7497. status = transcode_slice((uint8_t *)pOutput_blocks + 8, num_blocks_x, num_blocks_y, pCompressed_data + alpha_offset, alpha_length, block_format::cBC4, bytes_per_block_or_pixel, false, is_video, true, level_index, orig_width, orig_height, output_row_pitch_in_blocks_or_pixels, pState, false, nullptr, output_rows_in_pixels);
  7498. if (!status)
  7499. {
  7500. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_image: transcode_slice() to BC5 1 failed\n");
  7501. }
  7502. }
  7503. else
  7504. {
  7505. basisu_transcoder::write_opaque_alpha_blocks(num_blocks_x, num_blocks_y, (uint8_t*)pOutput_blocks + 8, block_format::cBC4, 16, output_row_pitch_in_blocks_or_pixels);
  7506. status = true;
  7507. }
  7508. }
  7509. else
  7510. {
  7511. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_image: transcode_slice() to BC5 channel 0 failed\n");
  7512. }
  7513. break;
  7514. #endif
  7515. }
  7516. case transcoder_texture_format::cTFASTC_4x4_RGBA:
  7517. {
  7518. #if !BASISD_SUPPORT_ASTC
  7519. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_image: ASTC unsupported\n");
  7520. return false;
  7521. #else
  7522. assert(bytes_per_block_or_pixel == 16);
  7523. if (basis_file_has_alpha_slices)
  7524. {
  7525. // First decode the alpha data to the output (we're using the output texture as a temp buffer here).
  7526. //status = transcode_slice(pData, data_size, slice_index + 1, (uint8_t*)pOutput_blocks, output_blocks_buf_size_in_blocks_or_pixels, block_format::cIndices, 16, decode_flags, output_row_pitch_in_blocks_or_pixels, pState);
  7527. status = transcode_slice(pOutput_blocks, num_blocks_x, num_blocks_y, pCompressed_data + alpha_offset, alpha_length, block_format::cIndices, bytes_per_block_or_pixel, false, is_video, true, level_index, orig_width, orig_height, output_row_pitch_in_blocks_or_pixels, pState, false, nullptr, output_rows_in_pixels);
  7528. if (status)
  7529. {
  7530. // Now decode the color data and transcode to ASTC. The transcoder function will read the alpha selector data from the output texture as it converts and
  7531. // transcode both the alpha and color data at the same time to ASTC.
  7532. //status = transcode_slice(pData, data_size, slice_index, pOutput_blocks, output_blocks_buf_size_in_blocks_or_pixels, block_format::cASTC_4x4, 16, decode_flags | cDecodeFlagsOutputHasAlphaIndices, output_row_pitch_in_blocks_or_pixels, pState);
  7533. status = transcode_slice(pOutput_blocks, num_blocks_x, num_blocks_y, pCompressed_data + rgb_offset, rgb_length, block_format::cASTC_4x4, bytes_per_block_or_pixel, false, is_video, false, level_index, orig_width, orig_height, output_row_pitch_in_blocks_or_pixels, pState, true, nullptr, output_rows_in_pixels);
  7534. }
  7535. }
  7536. else
  7537. //status = transcode_slice(pData, data_size, slice_index, pOutput_blocks, output_blocks_buf_size_in_blocks_or_pixels, block_format::cASTC_4x4, 16, decode_flags, output_row_pitch_in_blocks_or_pixels, pState);
  7538. status = transcode_slice(pOutput_blocks, num_blocks_x, num_blocks_y, pCompressed_data + rgb_offset, rgb_length, block_format::cASTC_4x4, bytes_per_block_or_pixel, false, is_video, false, level_index, orig_width, orig_height, output_row_pitch_in_blocks_or_pixels, pState, false, nullptr, output_rows_in_pixels);
  7539. if (!status)
  7540. {
  7541. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_image: transcode_slice() to ASTC failed (0)\n");
  7542. }
  7543. break;
  7544. #endif
  7545. }
  7546. case transcoder_texture_format::cTFATC_RGB:
  7547. {
  7548. #if !BASISD_SUPPORT_ATC
  7549. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_image: ATC unsupported\n");
  7550. return false;
  7551. #else
  7552. //status = transcode_slice(pData, data_size, slice_index_to_decode, pOutput_blocks, output_blocks_buf_size_in_blocks_or_pixels, block_format::cATC_RGB, bytes_per_block_or_pixel, decode_flags, output_row_pitch_in_blocks_or_pixels, pState);
  7553. status = transcode_slice(pOutput_blocks, num_blocks_x, num_blocks_y, pData, data_len, block_format::cATC_RGB, bytes_per_block_or_pixel, false, is_video, is_alpha_slice, level_index, orig_width, orig_height, output_row_pitch_in_blocks_or_pixels, pState, false, nullptr, output_rows_in_pixels);
  7554. if (!status)
  7555. {
  7556. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_image: transcode_slice() to ATC_RGB failed\n");
  7557. }
  7558. break;
  7559. #endif
  7560. }
  7561. case transcoder_texture_format::cTFATC_RGBA:
  7562. {
  7563. #if !BASISD_SUPPORT_ATC
  7564. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_image: ATC unsupported\n");
  7565. return false;
  7566. #elif !BASISD_SUPPORT_DXT5A
  7567. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_image: DXT5A unsupported\n");
  7568. return false;
  7569. #else
  7570. assert(bytes_per_block_or_pixel == 16);
  7571. // First decode the alpha data
  7572. if (basis_file_has_alpha_slices)
  7573. {
  7574. //status = transcode_slice(pData, data_size, slice_index + 1, pOutput_blocks, output_blocks_buf_size_in_blocks_or_pixels, block_format::cBC4, 16, decode_flags, output_row_pitch_in_blocks_or_pixels, pState);
  7575. status = transcode_slice(pOutput_blocks, num_blocks_x, num_blocks_y, pCompressed_data + alpha_offset, alpha_length, block_format::cBC4, bytes_per_block_or_pixel, false, is_video, true, level_index, orig_width, orig_height, output_row_pitch_in_blocks_or_pixels, pState, false, nullptr, output_rows_in_pixels);
  7576. }
  7577. else
  7578. {
  7579. basisu_transcoder::write_opaque_alpha_blocks(num_blocks_x, num_blocks_y, pOutput_blocks, block_format::cBC4, 16, output_row_pitch_in_blocks_or_pixels);
  7580. status = true;
  7581. }
  7582. if (status)
  7583. {
  7584. //status = transcode_slice(pData, data_size, slice_index, (uint8_t*)pOutput_blocks + 8, output_blocks_buf_size_in_blocks_or_pixels, block_format::cATC_RGB, 16, decode_flags, output_row_pitch_in_blocks_or_pixels, pState);
  7585. status = transcode_slice((uint8_t *)pOutput_blocks + 8, num_blocks_x, num_blocks_y, pCompressed_data + rgb_offset, rgb_length, block_format::cATC_RGB, bytes_per_block_or_pixel, false, is_video, false, level_index, orig_width, orig_height, output_row_pitch_in_blocks_or_pixels, pState, false, nullptr, output_rows_in_pixels);
  7586. if (!status)
  7587. {
  7588. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_image: transcode_slice() to ATC RGB failed\n");
  7589. }
  7590. }
  7591. else
  7592. {
  7593. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_image: transcode_slice() to ATC A failed\n");
  7594. }
  7595. break;
  7596. #endif
  7597. }
  7598. case transcoder_texture_format::cTFPVRTC2_4_RGB:
  7599. {
  7600. #if !BASISD_SUPPORT_PVRTC2
  7601. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_image: PVRTC2 unsupported\n");
  7602. return false;
  7603. #else
  7604. //status = transcode_slice(pData, data_size, slice_index_to_decode, pOutput_blocks, output_blocks_buf_size_in_blocks_or_pixels, block_format::cPVRTC2_4_RGB, bytes_per_block_or_pixel, decode_flags, output_row_pitch_in_blocks_or_pixels, pState);
  7605. status = transcode_slice(pOutput_blocks, num_blocks_x, num_blocks_y, pData, data_len, block_format::cPVRTC2_4_RGB, bytes_per_block_or_pixel, false, is_video, is_alpha_slice, level_index, orig_width, orig_height, output_row_pitch_in_blocks_or_pixels, pState, false, nullptr, output_rows_in_pixels);
  7606. if (!status)
  7607. {
  7608. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_image: transcode_slice() to cPVRTC2_4_RGB failed\n");
  7609. }
  7610. break;
  7611. #endif
  7612. }
  7613. case transcoder_texture_format::cTFPVRTC2_4_RGBA:
  7614. {
  7615. #if !BASISD_SUPPORT_PVRTC2
  7616. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_image: PVRTC2 unsupported\n");
  7617. return false;
  7618. #else
  7619. if (basis_file_has_alpha_slices)
  7620. {
  7621. // First decode the alpha data to the output (we're using the output texture as a temp buffer here).
  7622. //status = transcode_slice(pData, data_size, slice_index + 1, (uint8_t*)pOutput_blocks, output_blocks_buf_size_in_blocks_or_pixels, block_format::cIndices, bytes_per_block_or_pixel, decode_flags, output_row_pitch_in_blocks_or_pixels, pState);
  7623. status = transcode_slice(pOutput_blocks, num_blocks_x, num_blocks_y, pCompressed_data + alpha_offset, alpha_length, block_format::cIndices, bytes_per_block_or_pixel, false, is_video, true, level_index, orig_width, orig_height, output_row_pitch_in_blocks_or_pixels, pState, false, nullptr, output_rows_in_pixels);
  7624. if (!status)
  7625. {
  7626. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_image: transcode_slice() to failed\n");
  7627. }
  7628. else
  7629. {
  7630. // Now decode the color data and transcode to PVRTC2 RGBA.
  7631. //status = transcode_slice(pData, data_size, slice_index, pOutput_blocks, output_blocks_buf_size_in_blocks_or_pixels, block_format::cPVRTC2_4_RGBA, bytes_per_block_or_pixel, decode_flags | cDecodeFlagsOutputHasAlphaIndices, output_row_pitch_in_blocks_or_pixels, pState);
  7632. status = transcode_slice(pOutput_blocks, num_blocks_x, num_blocks_y, pCompressed_data + rgb_offset, rgb_length, block_format::cPVRTC2_4_RGBA, bytes_per_block_or_pixel, false, is_video, false, level_index, orig_width, orig_height, output_row_pitch_in_blocks_or_pixels, pState, true, nullptr, output_rows_in_pixels);
  7633. }
  7634. }
  7635. else
  7636. //status = transcode_slice(pData, data_size, slice_index, pOutput_blocks, output_blocks_buf_size_in_blocks_or_pixels, block_format::cPVRTC2_4_RGB, bytes_per_block_or_pixel, decode_flags, output_row_pitch_in_blocks_or_pixels, pState);
  7637. status = transcode_slice(pOutput_blocks, num_blocks_x, num_blocks_y, pCompressed_data + rgb_offset, rgb_length, block_format::cPVRTC2_4_RGB, bytes_per_block_or_pixel, false, is_video, false, level_index, orig_width, orig_height, output_row_pitch_in_blocks_or_pixels, pState, false, nullptr, output_rows_in_pixels);
  7638. if (!status)
  7639. {
  7640. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_image: transcode_slice() to cPVRTC2_4_RGBA failed\n");
  7641. }
  7642. break;
  7643. #endif
  7644. }
  7645. case transcoder_texture_format::cTFRGBA32:
  7646. {
  7647. // Raw 32bpp pixels, decoded in the usual raster order (NOT block order) into an image in memory.
  7648. // First decode the alpha data
  7649. if (basis_file_has_alpha_slices)
  7650. //status = transcode_slice(pData, data_size, slice_index + 1, pOutput_blocks, output_blocks_buf_size_in_blocks_or_pixels, block_format::cA32, sizeof(uint32_t), decode_flags, output_row_pitch_in_blocks_or_pixels, pState, nullptr, output_rows_in_pixels);
  7651. status = transcode_slice(pOutput_blocks, num_blocks_x, num_blocks_y, pCompressed_data + alpha_offset, alpha_length, block_format::cA32, sizeof(uint32_t), false, is_video, true, level_index, orig_width, orig_height, output_row_pitch_in_blocks_or_pixels, pState, false, nullptr, output_rows_in_pixels);
  7652. else
  7653. status = true;
  7654. if (status)
  7655. {
  7656. //status = transcode_slice(pData, data_size, slice_index, pOutput_blocks, output_blocks_buf_size_in_blocks_or_pixels, basis_file_has_alpha_slices ? block_format::cRGB32 : block_format::cRGBA32, sizeof(uint32_t), decode_flags, output_row_pitch_in_blocks_or_pixels, pState, nullptr, output_rows_in_pixels);
  7657. status = transcode_slice(pOutput_blocks, num_blocks_x, num_blocks_y, pCompressed_data + rgb_offset, rgb_length, basis_file_has_alpha_slices ? block_format::cRGB32 : block_format::cRGBA32, sizeof(uint32_t), false, is_video, false, level_index, orig_width, orig_height, output_row_pitch_in_blocks_or_pixels, pState, false, nullptr, output_rows_in_pixels);
  7658. if (!status)
  7659. {
  7660. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_image: transcode_slice() to RGBA32 RGB failed\n");
  7661. }
  7662. }
  7663. else
  7664. {
  7665. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_image: transcode_slice() to RGBA32 A failed\n");
  7666. }
  7667. break;
  7668. }
  7669. case transcoder_texture_format::cTFRGB565:
  7670. case transcoder_texture_format::cTFBGR565:
  7671. {
  7672. // Raw 16bpp pixels, decoded in the usual raster order (NOT block order) into an image in memory.
  7673. //status = transcode_slice(pData, data_size, slice_index_to_decode, pOutput_blocks, output_blocks_buf_size_in_blocks_or_pixels, (fmt == transcoder_texture_format::cTFRGB565) ? block_format::cRGB565 : block_format::cBGR565, sizeof(uint16_t), decode_flags, output_row_pitch_in_blocks_or_pixels, pState, nullptr, output_rows_in_pixels);
  7674. status = transcode_slice(pOutput_blocks, num_blocks_x, num_blocks_y, pData, data_len, (target_format == transcoder_texture_format::cTFRGB565) ? block_format::cRGB565 : block_format::cBGR565, sizeof(uint16_t), false, is_video, is_alpha_slice, level_index, orig_width, orig_height, output_row_pitch_in_blocks_or_pixels, pState, false, nullptr, output_rows_in_pixels);
  7675. if (!status)
  7676. {
  7677. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_image: transcode_slice() to RGB565 RGB failed\n");
  7678. }
  7679. break;
  7680. }
  7681. case transcoder_texture_format::cTFRGBA4444:
  7682. {
  7683. // Raw 16bpp pixels, decoded in the usual raster order (NOT block order) into an image in memory.
  7684. // First decode the alpha data
  7685. if (basis_file_has_alpha_slices)
  7686. //status = transcode_slice(pData, data_size, slice_index + 1, pOutput_blocks, output_blocks_buf_size_in_blocks_or_pixels, block_format::cRGBA4444_ALPHA, sizeof(uint16_t), decode_flags, output_row_pitch_in_blocks_or_pixels, pState, nullptr, output_rows_in_pixels);
  7687. status = transcode_slice(pOutput_blocks, num_blocks_x, num_blocks_y, pCompressed_data + alpha_offset, alpha_length, block_format::cRGBA4444_ALPHA, sizeof(uint16_t), false, is_video, true, level_index, orig_width, orig_height, output_row_pitch_in_blocks_or_pixels, pState, false, nullptr, output_rows_in_pixels);
  7688. else
  7689. status = true;
  7690. if (status)
  7691. {
  7692. //status = transcode_slice(pData, data_size, slice_index, pOutput_blocks, output_blocks_buf_size_in_blocks_or_pixels, basis_file_has_alpha_slices ? block_format::cRGBA4444_COLOR : block_format::cRGBA4444_COLOR_OPAQUE, sizeof(uint16_t), decode_flags, output_row_pitch_in_blocks_or_pixels, pState, nullptr, output_rows_in_pixels);
  7693. status = transcode_slice(pOutput_blocks, num_blocks_x, num_blocks_y, pCompressed_data + rgb_offset, rgb_length, basis_file_has_alpha_slices ? block_format::cRGBA4444_COLOR : block_format::cRGBA4444_COLOR_OPAQUE, sizeof(uint16_t), false, is_video, false, level_index, orig_width, orig_height, output_row_pitch_in_blocks_or_pixels, pState, false, nullptr, output_rows_in_pixels);
  7694. if (!status)
  7695. {
  7696. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_image: transcode_slice() to RGBA4444 RGB failed\n");
  7697. }
  7698. }
  7699. else
  7700. {
  7701. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_image: transcode_slice() to RGBA4444 A failed\n");
  7702. }
  7703. break;
  7704. }
  7705. case transcoder_texture_format::cTFFXT1_RGB:
  7706. {
  7707. #if !BASISD_SUPPORT_FXT1
  7708. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_image: FXT1 unsupported\n");
  7709. return false;
  7710. #else
  7711. //status = transcode_slice(pData, data_size, slice_index_to_decode, pOutput_blocks, output_blocks_buf_size_in_blocks_or_pixels, block_format::cFXT1_RGB, bytes_per_block_or_pixel, decode_flags, output_row_pitch_in_blocks_or_pixels, pState);
  7712. status = transcode_slice(pOutput_blocks, num_blocks_x, num_blocks_y, pData, data_len, block_format::cFXT1_RGB, bytes_per_block_or_pixel, false, is_video, is_alpha_slice, level_index, orig_width, orig_height, output_row_pitch_in_blocks_or_pixels, pState, false, nullptr, output_rows_in_pixels);
  7713. if (!status)
  7714. {
  7715. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_image: transcode_slice() to FXT1_RGB failed\n");
  7716. }
  7717. break;
  7718. #endif
  7719. }
  7720. case transcoder_texture_format::cTFETC2_EAC_R11:
  7721. {
  7722. #if !BASISD_SUPPORT_ETC2_EAC_RG11
  7723. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_image: EAC_RG11 unsupported\n");
  7724. return false;
  7725. #else
  7726. //status = transcode_slice(pData, data_size, slice_index_to_decode, pOutput_blocks, output_blocks_buf_size_in_blocks_or_pixels, block_format::cETC2_EAC_R11, bytes_per_block_or_pixel, decode_flags, output_row_pitch_in_blocks_or_pixels, pState);
  7727. status = transcode_slice(pOutput_blocks, num_blocks_x, num_blocks_y, pData, data_len, block_format::cETC2_EAC_R11, bytes_per_block_or_pixel, false, is_video, is_alpha_slice, level_index, orig_width, orig_height, output_row_pitch_in_blocks_or_pixels, pState, false, nullptr, output_rows_in_pixels);
  7728. if (!status)
  7729. {
  7730. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_image: transcode_slice() to ETC2_EAC_R11 failed\n");
  7731. }
  7732. break;
  7733. #endif
  7734. }
  7735. case transcoder_texture_format::cTFETC2_EAC_RG11:
  7736. {
  7737. #if !BASISD_SUPPORT_ETC2_EAC_RG11
  7738. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_image: EAC_RG11 unsupported\n");
  7739. return false;
  7740. #else
  7741. assert(bytes_per_block_or_pixel == 16);
  7742. if (basis_file_has_alpha_slices)
  7743. {
  7744. // First decode the alpha data to G
  7745. //status = transcode_slice(pData, data_size, slice_index + 1, (uint8_t*)pOutput_blocks + 8, output_blocks_buf_size_in_blocks_or_pixels, block_format::cETC2_EAC_R11, 16, decode_flags, output_row_pitch_in_blocks_or_pixels, pState);
  7746. status = transcode_slice((uint8_t *)pOutput_blocks + 8, num_blocks_x, num_blocks_y, pCompressed_data + alpha_offset, alpha_length, block_format::cETC2_EAC_R11, bytes_per_block_or_pixel, false, is_video, true, level_index, orig_width, orig_height, output_row_pitch_in_blocks_or_pixels, pState, false, nullptr, output_rows_in_pixels);
  7747. }
  7748. else
  7749. {
  7750. basisu_transcoder::write_opaque_alpha_blocks(num_blocks_x, num_blocks_y, (uint8_t*)pOutput_blocks + 8, block_format::cETC2_EAC_R11, 16, output_row_pitch_in_blocks_or_pixels);
  7751. status = true;
  7752. }
  7753. if (status)
  7754. {
  7755. // Now decode the color data to R
  7756. //status = transcode_slice(pData, data_size, slice_index, pOutput_blocks, output_blocks_buf_size_in_blocks_or_pixels, block_format::cETC2_EAC_R11, 16, decode_flags, output_row_pitch_in_blocks_or_pixels, pState);
  7757. status = transcode_slice(pOutput_blocks, num_blocks_x, num_blocks_y, pCompressed_data + rgb_offset, rgb_length, block_format::cETC2_EAC_R11, bytes_per_block_or_pixel, false, is_video, false, level_index, orig_width, orig_height, output_row_pitch_in_blocks_or_pixels, pState, false, nullptr, output_rows_in_pixels);
  7758. if (!status)
  7759. {
  7760. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_image: transcode_slice() to ETC2_EAC_R11 R failed\n");
  7761. }
  7762. }
  7763. else
  7764. {
  7765. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_image: transcode_slice() to ETC2_EAC_R11 G failed\n");
  7766. }
  7767. break;
  7768. #endif
  7769. }
  7770. default:
  7771. {
  7772. assert(0);
  7773. BASISU_DEVEL_ERROR("basisu_lowlevel_etc1s_transcoder::transcode_image: Invalid fmt\n");
  7774. break;
  7775. }
  7776. }
  7777. return status;
  7778. }
  7779. basisu_lowlevel_uastc_transcoder::basisu_lowlevel_uastc_transcoder()
  7780. {
  7781. }
  7782. bool basisu_lowlevel_uastc_transcoder::transcode_slice(void* pDst_blocks, uint32_t num_blocks_x, uint32_t num_blocks_y, const uint8_t* pImage_data, uint32_t image_data_size, block_format fmt,
  7783. uint32_t output_block_or_pixel_stride_in_bytes, bool bc1_allow_threecolor_blocks, bool has_alpha, const uint32_t orig_width, const uint32_t orig_height, uint32_t output_row_pitch_in_blocks_or_pixels,
  7784. basisu_transcoder_state* pState, uint32_t output_rows_in_pixels, int channel0, int channel1, uint32_t decode_flags)
  7785. {
  7786. BASISU_NOTE_UNUSED(pState);
  7787. BASISU_NOTE_UNUSED(bc1_allow_threecolor_blocks);
  7788. assert(g_transcoder_initialized);
  7789. if (!g_transcoder_initialized)
  7790. {
  7791. BASISU_DEVEL_ERROR("basisu_lowlevel_uastc_transcoder::transcode_slice: Transcoder not globally initialized.\n");
  7792. return false;
  7793. }
  7794. #if BASISD_SUPPORT_UASTC
  7795. const uint32_t total_blocks = num_blocks_x * num_blocks_y;
  7796. if (!output_row_pitch_in_blocks_or_pixels)
  7797. {
  7798. if (basis_block_format_is_uncompressed(fmt))
  7799. output_row_pitch_in_blocks_or_pixels = orig_width;
  7800. else
  7801. {
  7802. if (fmt == block_format::cFXT1_RGB)
  7803. output_row_pitch_in_blocks_or_pixels = (orig_width + 7) / 8;
  7804. else
  7805. output_row_pitch_in_blocks_or_pixels = num_blocks_x;
  7806. }
  7807. }
  7808. if (basis_block_format_is_uncompressed(fmt))
  7809. {
  7810. if (!output_rows_in_pixels)
  7811. output_rows_in_pixels = orig_height;
  7812. }
  7813. uint32_t total_expected_block_bytes = sizeof(uastc_block) * total_blocks;
  7814. if (image_data_size < total_expected_block_bytes)
  7815. {
  7816. BASISU_DEVEL_ERROR("basisu_lowlevel_uastc_transcoder::transcode_slice: image_data_size < total_expected_block_bytes The file is corrupted or this is a bug.\n");
  7817. return false;
  7818. }
  7819. const uastc_block* pSource_block = reinterpret_cast<const uastc_block *>(pImage_data);
  7820. const bool high_quality = (decode_flags & cDecodeFlagsHighQuality) != 0;
  7821. const bool from_alpha = has_alpha && (decode_flags & cDecodeFlagsTranscodeAlphaDataToOpaqueFormats) != 0;
  7822. bool status = false;
  7823. if ((fmt == block_format::cPVRTC1_4_RGB) || (fmt == block_format::cPVRTC1_4_RGBA))
  7824. {
  7825. if (fmt == block_format::cPVRTC1_4_RGBA)
  7826. transcode_uastc_to_pvrtc1_4_rgba((const uastc_block*)pImage_data, pDst_blocks, num_blocks_x, num_blocks_y, high_quality);
  7827. else
  7828. transcode_uastc_to_pvrtc1_4_rgb((const uastc_block *)pImage_data, pDst_blocks, num_blocks_x, num_blocks_y, high_quality, from_alpha);
  7829. }
  7830. else
  7831. {
  7832. for (uint32_t block_y = 0; block_y < num_blocks_y; ++block_y)
  7833. {
  7834. void* pDst_block = (uint8_t*)pDst_blocks + block_y * output_row_pitch_in_blocks_or_pixels * output_block_or_pixel_stride_in_bytes;
  7835. for (uint32_t block_x = 0; block_x < num_blocks_x; ++block_x, ++pSource_block, pDst_block = (uint8_t *)pDst_block + output_block_or_pixel_stride_in_bytes)
  7836. {
  7837. switch (fmt)
  7838. {
  7839. case block_format::cETC1:
  7840. {
  7841. if (from_alpha)
  7842. status = transcode_uastc_to_etc1(*pSource_block, pDst_block, 3);
  7843. else
  7844. status = transcode_uastc_to_etc1(*pSource_block, pDst_block);
  7845. break;
  7846. }
  7847. case block_format::cETC2_RGBA:
  7848. {
  7849. status = transcode_uastc_to_etc2_rgba(*pSource_block, pDst_block);
  7850. break;
  7851. }
  7852. case block_format::cBC1:
  7853. {
  7854. status = transcode_uastc_to_bc1(*pSource_block, pDst_block, high_quality);
  7855. break;
  7856. }
  7857. case block_format::cBC3:
  7858. {
  7859. status = transcode_uastc_to_bc3(*pSource_block, pDst_block, high_quality);
  7860. break;
  7861. }
  7862. case block_format::cBC4:
  7863. {
  7864. if (channel0 < 0)
  7865. channel0 = 0;
  7866. status = transcode_uastc_to_bc4(*pSource_block, pDst_block, high_quality, channel0);
  7867. break;
  7868. }
  7869. case block_format::cBC5:
  7870. {
  7871. if (channel0 < 0)
  7872. channel0 = 0;
  7873. if (channel1 < 0)
  7874. channel1 = 3;
  7875. status = transcode_uastc_to_bc5(*pSource_block, pDst_block, high_quality, channel0, channel1);
  7876. break;
  7877. }
  7878. case block_format::cBC7:
  7879. case block_format::cBC7_M5_COLOR: // for consistently with ETC1S
  7880. {
  7881. status = transcode_uastc_to_bc7(*pSource_block, pDst_block);
  7882. break;
  7883. }
  7884. case block_format::cASTC_4x4:
  7885. {
  7886. status = transcode_uastc_to_astc(*pSource_block, pDst_block);
  7887. break;
  7888. }
  7889. case block_format::cETC2_EAC_R11:
  7890. {
  7891. if (channel0 < 0)
  7892. channel0 = 0;
  7893. status = transcode_uastc_to_etc2_eac_r11(*pSource_block, pDst_block, high_quality, channel0);
  7894. break;
  7895. }
  7896. case block_format::cETC2_EAC_RG11:
  7897. {
  7898. if (channel0 < 0)
  7899. channel0 = 0;
  7900. if (channel1 < 0)
  7901. channel1 = 3;
  7902. status = transcode_uastc_to_etc2_eac_rg11(*pSource_block, pDst_block, high_quality, channel0, channel1);
  7903. break;
  7904. }
  7905. case block_format::cRGBA32:
  7906. {
  7907. color32 block_pixels[4][4];
  7908. status = unpack_uastc(*pSource_block, (color32 *)block_pixels, false);
  7909. assert(sizeof(uint32_t) == output_block_or_pixel_stride_in_bytes);
  7910. uint8_t* pDst_pixels = static_cast<uint8_t*>(pDst_blocks) + (block_x * 4 + block_y * 4 * output_row_pitch_in_blocks_or_pixels) * sizeof(uint32_t);
  7911. const uint32_t max_x = basisu::minimum<int>(4, (int)output_row_pitch_in_blocks_or_pixels - (int)block_x * 4);
  7912. const uint32_t max_y = basisu::minimum<int>(4, (int)output_rows_in_pixels - (int)block_y * 4);
  7913. for (uint32_t y = 0; y < max_y; y++)
  7914. {
  7915. for (uint32_t x = 0; x < max_x; x++)
  7916. {
  7917. const color32& c = block_pixels[y][x];
  7918. pDst_pixels[0 + 4 * x] = c.r;
  7919. pDst_pixels[1 + 4 * x] = c.g;
  7920. pDst_pixels[2 + 4 * x] = c.b;
  7921. pDst_pixels[3 + 4 * x] = c.a;
  7922. }
  7923. pDst_pixels += output_row_pitch_in_blocks_or_pixels * sizeof(uint32_t);
  7924. }
  7925. break;
  7926. }
  7927. case block_format::cRGB565:
  7928. case block_format::cBGR565:
  7929. {
  7930. color32 block_pixels[4][4];
  7931. status = unpack_uastc(*pSource_block, (color32*)block_pixels, false);
  7932. assert(sizeof(uint16_t) == output_block_or_pixel_stride_in_bytes);
  7933. uint8_t* pDst_pixels = static_cast<uint8_t*>(pDst_blocks) + (block_x * 4 + block_y * 4 * output_row_pitch_in_blocks_or_pixels) * sizeof(uint16_t);
  7934. const uint32_t max_x = basisu::minimum<int>(4, (int)output_row_pitch_in_blocks_or_pixels - (int)block_x * 4);
  7935. const uint32_t max_y = basisu::minimum<int>(4, (int)output_rows_in_pixels - (int)block_y * 4);
  7936. for (uint32_t y = 0; y < max_y; y++)
  7937. {
  7938. for (uint32_t x = 0; x < max_x; x++)
  7939. {
  7940. const color32& c = block_pixels[y][x];
  7941. const uint16_t packed = (fmt == block_format::cRGB565) ? static_cast<uint16_t>((mul_8(c.r, 31) << 11) | (mul_8(c.g, 63) << 5) | mul_8(c.b, 31)) :
  7942. static_cast<uint16_t>((mul_8(c.b, 31) << 11) | (mul_8(c.g, 63) << 5) | mul_8(c.r, 31));
  7943. pDst_pixels[x * 2 + 0] = (uint8_t)(packed & 0xFF);
  7944. pDst_pixels[x * 2 + 1] = (uint8_t)((packed >> 8) & 0xFF);
  7945. }
  7946. pDst_pixels += output_row_pitch_in_blocks_or_pixels * sizeof(uint16_t);
  7947. }
  7948. break;
  7949. }
  7950. case block_format::cRGBA4444:
  7951. {
  7952. color32 block_pixels[4][4];
  7953. status = unpack_uastc(*pSource_block, (color32*)block_pixels, false);
  7954. assert(sizeof(uint16_t) == output_block_or_pixel_stride_in_bytes);
  7955. uint8_t* pDst_pixels = static_cast<uint8_t*>(pDst_blocks) + (block_x * 4 + block_y * 4 * output_row_pitch_in_blocks_or_pixels) * sizeof(uint16_t);
  7956. const uint32_t max_x = basisu::minimum<int>(4, (int)output_row_pitch_in_blocks_or_pixels - (int)block_x * 4);
  7957. const uint32_t max_y = basisu::minimum<int>(4, (int)output_rows_in_pixels - (int)block_y * 4);
  7958. for (uint32_t y = 0; y < max_y; y++)
  7959. {
  7960. for (uint32_t x = 0; x < max_x; x++)
  7961. {
  7962. const color32& c = block_pixels[y][x];
  7963. const uint16_t packed = static_cast<uint16_t>((mul_8(c.r, 15) << 12) | (mul_8(c.g, 15) << 8) | (mul_8(c.b, 15) << 4) | mul_8(c.a, 15));
  7964. pDst_pixels[x * 2 + 0] = (uint8_t)(packed & 0xFF);
  7965. pDst_pixels[x * 2 + 1] = (uint8_t)((packed >> 8) & 0xFF);
  7966. }
  7967. pDst_pixels += output_row_pitch_in_blocks_or_pixels * sizeof(uint16_t);
  7968. }
  7969. break;
  7970. }
  7971. default:
  7972. assert(0);
  7973. break;
  7974. }
  7975. if (!status)
  7976. {
  7977. BASISU_DEVEL_ERROR("basisu_lowlevel_uastc_transcoder::transcode_slice: Transcoder failed to unpack a UASTC block - this is a bug, or the data was corrupted\n");
  7978. return false;
  7979. }
  7980. } // block_x
  7981. } // block_y
  7982. }
  7983. return true;
  7984. #else
  7985. BASISU_DEVEL_ERROR("basisu_lowlevel_uastc_transcoder::transcode_slice: UASTC is unsupported\n");
  7986. BASISU_NOTE_UNUSED(decode_flags);
  7987. BASISU_NOTE_UNUSED(channel0);
  7988. BASISU_NOTE_UNUSED(channel1);
  7989. BASISU_NOTE_UNUSED(output_rows_in_pixels);
  7990. BASISU_NOTE_UNUSED(output_row_pitch_in_blocks_or_pixels);
  7991. BASISU_NOTE_UNUSED(output_block_or_pixel_stride_in_bytes);
  7992. BASISU_NOTE_UNUSED(fmt);
  7993. BASISU_NOTE_UNUSED(image_data_size);
  7994. BASISU_NOTE_UNUSED(pImage_data);
  7995. BASISU_NOTE_UNUSED(num_blocks_x);
  7996. BASISU_NOTE_UNUSED(num_blocks_y);
  7997. BASISU_NOTE_UNUSED(pDst_blocks);
  7998. return false;
  7999. #endif
  8000. }
  8001. bool basisu_lowlevel_uastc_transcoder::transcode_image(
  8002. transcoder_texture_format target_format,
  8003. void* pOutput_blocks, uint32_t output_blocks_buf_size_in_blocks_or_pixels,
  8004. const uint8_t* pCompressed_data, uint32_t compressed_data_length,
  8005. uint32_t num_blocks_x, uint32_t num_blocks_y, uint32_t orig_width, uint32_t orig_height, uint32_t level_index,
  8006. uint32_t slice_offset, uint32_t slice_length,
  8007. uint32_t decode_flags,
  8008. bool has_alpha,
  8009. bool is_video,
  8010. uint32_t output_row_pitch_in_blocks_or_pixels,
  8011. basisu_transcoder_state* pState,
  8012. uint32_t output_rows_in_pixels,
  8013. int channel0, int channel1)
  8014. {
  8015. BASISU_NOTE_UNUSED(is_video);
  8016. BASISU_NOTE_UNUSED(level_index);
  8017. if (((uint64_t)slice_offset + slice_length) > (uint64_t)compressed_data_length)
  8018. {
  8019. BASISU_DEVEL_ERROR("basisu_lowlevel_uastc_transcoder::transcode_image: source data buffer too small\n");
  8020. return false;
  8021. }
  8022. if ((target_format == transcoder_texture_format::cTFPVRTC1_4_RGB) || (target_format == transcoder_texture_format::cTFPVRTC1_4_RGBA))
  8023. {
  8024. if ((!basisu::is_pow2(num_blocks_x * 4)) || (!basisu::is_pow2(num_blocks_y * 4)))
  8025. {
  8026. // PVRTC1 only supports power of 2 dimensions
  8027. BASISU_DEVEL_ERROR("basisu_lowlevel_uastc_transcoder::transcode_image: PVRTC1 only supports power of 2 dimensions\n");
  8028. return false;
  8029. }
  8030. }
  8031. if ((target_format == transcoder_texture_format::cTFPVRTC1_4_RGBA) && (!has_alpha))
  8032. {
  8033. // Switch to PVRTC1 RGB if the input doesn't have alpha.
  8034. target_format = transcoder_texture_format::cTFPVRTC1_4_RGB;
  8035. }
  8036. const bool transcode_alpha_data_to_opaque_formats = (decode_flags & cDecodeFlagsTranscodeAlphaDataToOpaqueFormats) != 0;
  8037. const uint32_t bytes_per_block_or_pixel = basis_get_bytes_per_block_or_pixel(target_format);
  8038. const uint32_t total_slice_blocks = num_blocks_x * num_blocks_y;
  8039. if (!basis_validate_output_buffer_size(target_format, output_blocks_buf_size_in_blocks_or_pixels, orig_width, orig_height, output_row_pitch_in_blocks_or_pixels, output_rows_in_pixels, total_slice_blocks))
  8040. {
  8041. BASISU_DEVEL_ERROR("basisu_lowlevel_uastc_transcoder::transcode_image: output buffer size too small\n");
  8042. return false;
  8043. }
  8044. bool status = false;
  8045. // UASTC4x4
  8046. switch (target_format)
  8047. {
  8048. case transcoder_texture_format::cTFETC1_RGB:
  8049. {
  8050. //status = transcode_slice(pData, data_size, slice_index, pOutput_blocks, output_blocks_buf_size_in_blocks_or_pixels, block_format::cETC1, bytes_per_block_or_pixel, decode_flags, output_row_pitch_in_blocks_or_pixels, pState);
  8051. status = transcode_slice(pOutput_blocks, num_blocks_x, num_blocks_y, pCompressed_data + slice_offset, slice_length, block_format::cETC1,
  8052. bytes_per_block_or_pixel, false, has_alpha, orig_width, orig_height, output_row_pitch_in_blocks_or_pixels, pState, output_rows_in_pixels, channel0, channel1);
  8053. if (!status)
  8054. {
  8055. BASISU_DEVEL_ERROR("basisu_lowlevel_uastc_transcoder::transcode_image: transcode_slice() to ETC1 failed\n");
  8056. }
  8057. break;
  8058. }
  8059. case transcoder_texture_format::cTFETC2_RGBA:
  8060. {
  8061. //status = transcode_slice(pData, data_size, slice_index, pOutput_blocks, output_blocks_buf_size_in_blocks_or_pixels, block_format::cETC2_RGBA, bytes_per_block_or_pixel, decode_flags, output_row_pitch_in_blocks_or_pixels, pState);
  8062. status = transcode_slice(pOutput_blocks, num_blocks_x, num_blocks_y, pCompressed_data + slice_offset, slice_length, block_format::cETC2_RGBA,
  8063. bytes_per_block_or_pixel, false, has_alpha, orig_width, orig_height, output_row_pitch_in_blocks_or_pixels, pState, output_rows_in_pixels, channel0, channel1);
  8064. if (!status)
  8065. {
  8066. BASISU_DEVEL_ERROR("basisu_lowlevel_uastc_transcoder::transcode_image: transcode_slice() to ETC2 failed\n");
  8067. }
  8068. break;
  8069. }
  8070. case transcoder_texture_format::cTFBC1_RGB:
  8071. {
  8072. // TODO: ETC1S allows BC1 from alpha channel. That doesn't seem actually useful, though.
  8073. //status = transcode_slice(pData, data_size, slice_index, pOutput_blocks, output_blocks_buf_size_in_blocks_or_pixels, block_format::cBC1, bytes_per_block_or_pixel, decode_flags, output_row_pitch_in_blocks_or_pixels, pState);
  8074. status = transcode_slice(pOutput_blocks, num_blocks_x, num_blocks_y, pCompressed_data + slice_offset, slice_length, block_format::cBC1,
  8075. bytes_per_block_or_pixel, true, has_alpha, orig_width, orig_height, output_row_pitch_in_blocks_or_pixels, pState, output_rows_in_pixels, channel0, channel1);
  8076. if (!status)
  8077. {
  8078. BASISU_DEVEL_ERROR("basisu_lowlevel_uastc_transcoder::transcode_image: transcode_slice() to BC1 failed\n");
  8079. }
  8080. break;
  8081. }
  8082. case transcoder_texture_format::cTFBC3_RGBA:
  8083. {
  8084. //status = transcode_slice(pData, data_size, slice_index, pOutput_blocks, output_blocks_buf_size_in_blocks_or_pixels, block_format::cBC3, bytes_per_block_or_pixel, decode_flags, output_row_pitch_in_blocks_or_pixels, pState);
  8085. status = transcode_slice(pOutput_blocks, num_blocks_x, num_blocks_y, pCompressed_data + slice_offset, slice_length, block_format::cBC3,
  8086. bytes_per_block_or_pixel, false, has_alpha, orig_width, orig_height, output_row_pitch_in_blocks_or_pixels, pState, output_rows_in_pixels, channel0, channel1);
  8087. if (!status)
  8088. {
  8089. BASISU_DEVEL_ERROR("basisu_lowlevel_uastc_transcoder::transcode_image: transcode_slice() to BC3 failed\n");
  8090. }
  8091. break;
  8092. }
  8093. case transcoder_texture_format::cTFBC4_R:
  8094. {
  8095. //status = transcode_slice(pData, data_size, slice_index, pOutput_blocks, output_blocks_buf_size_in_blocks_or_pixels, block_format::cBC4, bytes_per_block_or_pixel, decode_flags, output_row_pitch_in_blocks_or_pixels, pState,
  8096. // nullptr, 0,
  8097. // ((has_alpha) && (transcode_alpha_data_to_opaque_formats)) ? 3 : 0);
  8098. status = transcode_slice(pOutput_blocks, num_blocks_x, num_blocks_y, pCompressed_data + slice_offset, slice_length, block_format::cBC4,
  8099. bytes_per_block_or_pixel, false, has_alpha, orig_width, orig_height, output_row_pitch_in_blocks_or_pixels, pState, output_rows_in_pixels,
  8100. ((has_alpha) && (transcode_alpha_data_to_opaque_formats)) ? 3 : 0);
  8101. if (!status)
  8102. {
  8103. BASISU_DEVEL_ERROR("basisu_lowlevel_uastc_transcoder::transcode_image: transcode_slice() to BC4 failed\n");
  8104. }
  8105. break;
  8106. }
  8107. case transcoder_texture_format::cTFBC5_RG:
  8108. {
  8109. //status = transcode_slice(pData, data_size, slice_index, pOutput_blocks, output_blocks_buf_size_in_blocks_or_pixels, block_format::cBC5, bytes_per_block_or_pixel, decode_flags, output_row_pitch_in_blocks_or_pixels, pState,
  8110. // nullptr, 0,
  8111. // 0, 3);
  8112. status = transcode_slice(pOutput_blocks, num_blocks_x, num_blocks_y, pCompressed_data + slice_offset, slice_length, block_format::cBC5,
  8113. bytes_per_block_or_pixel, false, has_alpha, orig_width, orig_height, output_row_pitch_in_blocks_or_pixels, pState, output_rows_in_pixels,
  8114. 0, 3);
  8115. if (!status)
  8116. {
  8117. BASISU_DEVEL_ERROR("basisu_lowlevel_uastc_transcoder::transcode_image: transcode_slice() to BC5 failed\n");
  8118. }
  8119. break;
  8120. }
  8121. case transcoder_texture_format::cTFBC7_RGBA:
  8122. case transcoder_texture_format::cTFBC7_ALT:
  8123. {
  8124. //status = transcode_slice(pData, data_size, slice_index, pOutput_blocks, output_blocks_buf_size_in_blocks_or_pixels, block_format::cBC7, bytes_per_block_or_pixel, decode_flags, output_row_pitch_in_blocks_or_pixels, pState);
  8125. status = transcode_slice(pOutput_blocks, num_blocks_x, num_blocks_y, pCompressed_data + slice_offset, slice_length, block_format::cBC7,
  8126. bytes_per_block_or_pixel, false, has_alpha, orig_width, orig_height, output_row_pitch_in_blocks_or_pixels, pState, output_rows_in_pixels);
  8127. if (!status)
  8128. {
  8129. BASISU_DEVEL_ERROR("basisu_lowlevel_uastc_transcoder::transcode_image: transcode_slice() to BC7 failed\n");
  8130. }
  8131. break;
  8132. }
  8133. case transcoder_texture_format::cTFPVRTC1_4_RGB:
  8134. {
  8135. //status = transcode_slice(pData, data_size, slice_index, pOutput_blocks, output_blocks_buf_size_in_blocks_or_pixels, block_format::cPVRTC1_4_RGB, bytes_per_block_or_pixel, decode_flags, output_row_pitch_in_blocks_or_pixels, pState);
  8136. status = transcode_slice(pOutput_blocks, num_blocks_x, num_blocks_y, pCompressed_data + slice_offset, slice_length, block_format::cPVRTC1_4_RGB,
  8137. bytes_per_block_or_pixel, false, has_alpha, orig_width, orig_height, output_row_pitch_in_blocks_or_pixels, pState, output_rows_in_pixels);
  8138. if (!status)
  8139. {
  8140. BASISU_DEVEL_ERROR("basisu_lowlevel_uastc_transcoder::transcode_image: transcode_slice() to PVRTC1 RGB 4bpp failed\n");
  8141. }
  8142. break;
  8143. }
  8144. case transcoder_texture_format::cTFPVRTC1_4_RGBA:
  8145. {
  8146. //status = transcode_slice(pData, data_size, slice_index, pOutput_blocks, output_blocks_buf_size_in_blocks_or_pixels, block_format::cPVRTC1_4_RGBA, bytes_per_block_or_pixel, decode_flags, output_row_pitch_in_blocks_or_pixels, pState);
  8147. status = transcode_slice(pOutput_blocks, num_blocks_x, num_blocks_y, pCompressed_data + slice_offset, slice_length, block_format::cPVRTC1_4_RGBA,
  8148. bytes_per_block_or_pixel, false, has_alpha, orig_width, orig_height, output_row_pitch_in_blocks_or_pixels, pState, output_rows_in_pixels);
  8149. if (!status)
  8150. {
  8151. BASISU_DEVEL_ERROR("basisu_lowlevel_uastc_transcoder::transcode_image: transcode_slice() to PVRTC1 RGBA 4bpp failed\n");
  8152. }
  8153. break;
  8154. }
  8155. case transcoder_texture_format::cTFASTC_4x4_RGBA:
  8156. {
  8157. //status = transcode_slice(pData, data_size, slice_index, pOutput_blocks, output_blocks_buf_size_in_blocks_or_pixels, block_format::cASTC_4x4, bytes_per_block_or_pixel, decode_flags, output_row_pitch_in_blocks_or_pixels, pState);
  8158. status = transcode_slice(pOutput_blocks, num_blocks_x, num_blocks_y, pCompressed_data + slice_offset, slice_length, block_format::cASTC_4x4,
  8159. bytes_per_block_or_pixel, false, has_alpha, orig_width, orig_height, output_row_pitch_in_blocks_or_pixels, pState, output_rows_in_pixels);
  8160. if (!status)
  8161. {
  8162. BASISU_DEVEL_ERROR("basisu_lowlevel_uastc_transcoder::transcode_image: transcode_slice() to ASTC 4x4 failed\n");
  8163. }
  8164. break;
  8165. }
  8166. case transcoder_texture_format::cTFATC_RGB:
  8167. case transcoder_texture_format::cTFATC_RGBA:
  8168. {
  8169. BASISU_DEVEL_ERROR("basisu_lowlevel_uastc_transcoder::transcode_image: UASTC->ATC currently unsupported\n");
  8170. return false;
  8171. }
  8172. case transcoder_texture_format::cTFFXT1_RGB:
  8173. {
  8174. BASISU_DEVEL_ERROR("basisu_lowlevel_uastc_transcoder::transcode_image: UASTC->FXT1 currently unsupported\n");
  8175. return false;
  8176. }
  8177. case transcoder_texture_format::cTFPVRTC2_4_RGB:
  8178. {
  8179. BASISU_DEVEL_ERROR("basisu_lowlevel_uastc_transcoder::transcode_image: UASTC->PVRTC2 currently unsupported\n");
  8180. return false;
  8181. }
  8182. case transcoder_texture_format::cTFPVRTC2_4_RGBA:
  8183. {
  8184. BASISU_DEVEL_ERROR("basisu_lowlevel_uastc_transcoder::transcode_image: UASTC->PVRTC2 currently unsupported\n");
  8185. return false;
  8186. }
  8187. case transcoder_texture_format::cTFETC2_EAC_R11:
  8188. {
  8189. //status = transcode_slice(pData, data_size, slice_index, pOutput_blocks, output_blocks_buf_size_in_blocks_or_pixels, block_format::cETC2_EAC_R11, bytes_per_block_or_pixel, decode_flags, output_row_pitch_in_blocks_or_pixels, pState,
  8190. // nullptr, 0,
  8191. // ((has_alpha) && (transcode_alpha_data_to_opaque_formats)) ? 3 : 0);
  8192. status = transcode_slice(pOutput_blocks, num_blocks_x, num_blocks_y, pCompressed_data + slice_offset, slice_length, block_format::cETC2_EAC_R11,
  8193. bytes_per_block_or_pixel, false, has_alpha, orig_width, orig_height, output_row_pitch_in_blocks_or_pixels, pState, output_rows_in_pixels,
  8194. ((has_alpha) && (transcode_alpha_data_to_opaque_formats)) ? 3 : 0);
  8195. if (!status)
  8196. {
  8197. BASISU_DEVEL_ERROR("basisu_lowlevel_uastc_transcoder::transcode_image: transcode_slice() to EAC R11 failed\n");
  8198. }
  8199. break;
  8200. }
  8201. case transcoder_texture_format::cTFETC2_EAC_RG11:
  8202. {
  8203. //status = transcode_slice(pData, data_size, slice_index, pOutput_blocks, output_blocks_buf_size_in_blocks_or_pixels, block_format::cETC2_EAC_RG11, bytes_per_block_or_pixel, decode_flags, output_row_pitch_in_blocks_or_pixels, pState,
  8204. // nullptr, 0,
  8205. // 0, 3);
  8206. status = transcode_slice(pOutput_blocks, num_blocks_x, num_blocks_y, pCompressed_data + slice_offset, slice_length, block_format::cETC2_EAC_RG11,
  8207. bytes_per_block_or_pixel, false, has_alpha, orig_width, orig_height, output_row_pitch_in_blocks_or_pixels, pState, output_rows_in_pixels,
  8208. 0, 3);
  8209. if (!status)
  8210. {
  8211. BASISU_DEVEL_ERROR("basisu_basisu_lowlevel_uastc_transcodertranscoder::transcode_image: transcode_slice() to EAC RG11 failed\n");
  8212. }
  8213. break;
  8214. }
  8215. case transcoder_texture_format::cTFRGBA32:
  8216. {
  8217. //status = transcode_slice(pData, data_size, slice_index, pOutput_blocks, output_blocks_buf_size_in_blocks_or_pixels, block_format::cRGBA32, bytes_per_block_or_pixel, decode_flags, output_row_pitch_in_blocks_or_pixels, pState);
  8218. status = transcode_slice(pOutput_blocks, num_blocks_x, num_blocks_y, pCompressed_data + slice_offset, slice_length, block_format::cRGBA32,
  8219. bytes_per_block_or_pixel, false, has_alpha, orig_width, orig_height, output_row_pitch_in_blocks_or_pixels, pState, output_rows_in_pixels);
  8220. if (!status)
  8221. {
  8222. BASISU_DEVEL_ERROR("basisu_lowlevel_uastc_transcoder::transcode_image: transcode_slice() to RGBA32 failed\n");
  8223. }
  8224. break;
  8225. }
  8226. case transcoder_texture_format::cTFRGB565:
  8227. {
  8228. //status = transcode_slice(pData, data_size, slice_index, pOutput_blocks, output_blocks_buf_size_in_blocks_or_pixels, block_format::cRGB565, bytes_per_block_or_pixel, decode_flags, output_row_pitch_in_blocks_or_pixels, pState);
  8229. status = transcode_slice(pOutput_blocks, num_blocks_x, num_blocks_y, pCompressed_data + slice_offset, slice_length, block_format::cRGB565,
  8230. bytes_per_block_or_pixel, false, has_alpha, orig_width, orig_height, output_row_pitch_in_blocks_or_pixels, pState, output_rows_in_pixels);
  8231. if (!status)
  8232. {
  8233. BASISU_DEVEL_ERROR("basisu_lowlevel_uastc_transcoder::transcode_image: transcode_slice() to RGB565 failed\n");
  8234. }
  8235. break;
  8236. }
  8237. case transcoder_texture_format::cTFBGR565:
  8238. {
  8239. //status = transcode_slice(pData, data_size, slice_index, pOutput_blocks, output_blocks_buf_size_in_blocks_or_pixels, block_format::cBGR565, bytes_per_block_or_pixel, decode_flags, output_row_pitch_in_blocks_or_pixels, pState);
  8240. status = transcode_slice(pOutput_blocks, num_blocks_x, num_blocks_y, pCompressed_data + slice_offset, slice_length, block_format::cBGR565,
  8241. bytes_per_block_or_pixel, false, has_alpha, orig_width, orig_height, output_row_pitch_in_blocks_or_pixels, pState, output_rows_in_pixels);
  8242. if (!status)
  8243. {
  8244. BASISU_DEVEL_ERROR("basisu_lowlevel_uastc_transcoder::transcode_image: transcode_slice() to RGB565 failed\n");
  8245. }
  8246. break;
  8247. }
  8248. case transcoder_texture_format::cTFRGBA4444:
  8249. {
  8250. //status = transcode_slice(pData, data_size, slice_index, pOutput_blocks, output_blocks_buf_size_in_blocks_or_pixels, block_format::cRGBA4444, bytes_per_block_or_pixel, decode_flags, output_row_pitch_in_blocks_or_pixels, pState);
  8251. status = transcode_slice(pOutput_blocks, num_blocks_x, num_blocks_y, pCompressed_data + slice_offset, slice_length, block_format::cRGBA4444,
  8252. bytes_per_block_or_pixel, false, has_alpha, orig_width, orig_height, output_row_pitch_in_blocks_or_pixels, pState, output_rows_in_pixels);
  8253. if (!status)
  8254. {
  8255. BASISU_DEVEL_ERROR("basisu_lowlevel_uastc_transcoder::transcode_image: transcode_slice() to RGBA4444 failed\n");
  8256. }
  8257. break;
  8258. }
  8259. default:
  8260. {
  8261. assert(0);
  8262. BASISU_DEVEL_ERROR("basisu_lowlevel_uastc_transcoder::transcode_image: Invalid format\n");
  8263. break;
  8264. }
  8265. }
  8266. return status;
  8267. }
  8268. basisu_transcoder::basisu_transcoder(const etc1_global_selector_codebook* pGlobal_sel_codebook) :
  8269. m_lowlevel_etc1s_decoder(pGlobal_sel_codebook),
  8270. m_ready_to_transcode(false)
  8271. {
  8272. }
  8273. bool basisu_transcoder::validate_file_checksums(const void* pData, uint32_t data_size, bool full_validation) const
  8274. {
  8275. if (!validate_header(pData, data_size))
  8276. return false;
  8277. const basis_file_header* pHeader = reinterpret_cast<const basis_file_header*>(pData);
  8278. #if !BASISU_NO_HEADER_OR_DATA_CRC16_CHECKS
  8279. if (crc16(&pHeader->m_data_size, sizeof(basis_file_header) - BASISU_OFFSETOF(basis_file_header, m_data_size), 0) != pHeader->m_header_crc16)
  8280. {
  8281. BASISU_DEVEL_ERROR("basisu_transcoder::get_total_images: header CRC check failed\n");
  8282. return false;
  8283. }
  8284. if (full_validation)
  8285. {
  8286. if (crc16(reinterpret_cast<const uint8_t*>(pData) + sizeof(basis_file_header), pHeader->m_data_size, 0) != pHeader->m_data_crc16)
  8287. {
  8288. BASISU_DEVEL_ERROR("basisu_transcoder::get_total_images: data CRC check failed\n");
  8289. return false;
  8290. }
  8291. }
  8292. #endif
  8293. return true;
  8294. }
  8295. bool basisu_transcoder::validate_header_quick(const void* pData, uint32_t data_size) const
  8296. {
  8297. if (data_size <= sizeof(basis_file_header))
  8298. return false;
  8299. const basis_file_header* pHeader = reinterpret_cast<const basis_file_header*>(pData);
  8300. if ((pHeader->m_sig != basis_file_header::cBASISSigValue) || (pHeader->m_ver != BASISD_SUPPORTED_BASIS_VERSION) || (pHeader->m_header_size != sizeof(basis_file_header)))
  8301. {
  8302. BASISU_DEVEL_ERROR("basisu_transcoder::get_total_images: header has an invalid signature, or file version is unsupported\n");
  8303. return false;
  8304. }
  8305. uint32_t expected_file_size = sizeof(basis_file_header) + pHeader->m_data_size;
  8306. if (data_size < expected_file_size)
  8307. {
  8308. BASISU_DEVEL_ERROR("basisu_transcoder::get_total_images: source buffer is too small\n");
  8309. return false;
  8310. }
  8311. if ((!pHeader->m_total_slices) || (!pHeader->m_total_images))
  8312. {
  8313. BASISU_DEVEL_ERROR("basisu_transcoder::validate_header_quick: header is invalid\n");
  8314. return false;
  8315. }
  8316. if ((pHeader->m_slice_desc_file_ofs >= data_size) ||
  8317. ((data_size - pHeader->m_slice_desc_file_ofs) < (sizeof(basis_slice_desc) * pHeader->m_total_slices))
  8318. )
  8319. {
  8320. BASISU_DEVEL_ERROR("basisu_transcoder::validate_header_quick: passed in buffer is too small or data is corrupted\n");
  8321. return false;
  8322. }
  8323. return true;
  8324. }
  8325. bool basisu_transcoder::validate_header(const void* pData, uint32_t data_size) const
  8326. {
  8327. if (data_size <= sizeof(basis_file_header))
  8328. {
  8329. BASISU_DEVEL_ERROR("basisu_transcoder::get_total_images: input source buffer is too small\n");
  8330. return false;
  8331. }
  8332. const basis_file_header* pHeader = reinterpret_cast<const basis_file_header*>(pData);
  8333. if ((pHeader->m_sig != basis_file_header::cBASISSigValue) || (pHeader->m_ver != BASISD_SUPPORTED_BASIS_VERSION) || (pHeader->m_header_size != sizeof(basis_file_header)))
  8334. {
  8335. BASISU_DEVEL_ERROR("basisu_transcoder::get_total_images: header has an invalid signature, or file version is unsupported\n");
  8336. return false;
  8337. }
  8338. uint32_t expected_file_size = sizeof(basis_file_header) + pHeader->m_data_size;
  8339. if (data_size < expected_file_size)
  8340. {
  8341. BASISU_DEVEL_ERROR("basisu_transcoder::get_total_images: input source buffer is too small, or header is corrupted\n");
  8342. return false;
  8343. }
  8344. if ((!pHeader->m_total_images) || (!pHeader->m_total_slices))
  8345. {
  8346. BASISU_DEVEL_ERROR("basisu_transcoder::get_total_images: invalid basis file (total images or slices are 0)\n");
  8347. return false;
  8348. }
  8349. if (pHeader->m_total_images > pHeader->m_total_slices)
  8350. {
  8351. BASISU_DEVEL_ERROR("basisu_transcoder::get_total_images: invalid basis file (too many images)\n");
  8352. return false;
  8353. }
  8354. if (pHeader->m_tex_format == (int)basis_tex_format::cETC1S)
  8355. {
  8356. if (pHeader->m_flags & cBASISHeaderFlagHasAlphaSlices)
  8357. {
  8358. if (pHeader->m_total_slices & 1)
  8359. {
  8360. BASISU_DEVEL_ERROR("basisu_transcoder::get_total_images: invalid alpha .basis file\n");
  8361. return false;
  8362. }
  8363. }
  8364. // This flag dates back to pre-Basis Universal, when .basis supported full ETC1 too.
  8365. if ((pHeader->m_flags & cBASISHeaderFlagETC1S) == 0)
  8366. {
  8367. BASISU_DEVEL_ERROR("basisu_transcoder::get_total_images: Invalid .basis file (ETC1S check)\n");
  8368. return false;
  8369. }
  8370. }
  8371. else
  8372. {
  8373. if ((pHeader->m_flags & cBASISHeaderFlagETC1S) != 0)
  8374. {
  8375. BASISU_DEVEL_ERROR("basisu_transcoder::get_total_images: Invalid .basis file (ETC1S check)\n");
  8376. return false;
  8377. }
  8378. }
  8379. if ((pHeader->m_slice_desc_file_ofs >= data_size) ||
  8380. ((data_size - pHeader->m_slice_desc_file_ofs) < (sizeof(basis_slice_desc) * pHeader->m_total_slices))
  8381. )
  8382. {
  8383. BASISU_DEVEL_ERROR("basisu_transcoder::validate_header_quick: passed in buffer is too small or data is corrupted\n");
  8384. return false;
  8385. }
  8386. return true;
  8387. }
  8388. basis_texture_type basisu_transcoder::get_texture_type(const void* pData, uint32_t data_size) const
  8389. {
  8390. if (!validate_header_quick(pData, data_size))
  8391. {
  8392. BASISU_DEVEL_ERROR("basisu_transcoder::get_texture_type: header validation failed\n");
  8393. return cBASISTexType2DArray;
  8394. }
  8395. const basis_file_header* pHeader = static_cast<const basis_file_header*>(pData);
  8396. basis_texture_type btt = static_cast<basis_texture_type>(static_cast<uint8_t>(pHeader->m_tex_type));
  8397. if (btt >= cBASISTexTypeTotal)
  8398. {
  8399. BASISU_DEVEL_ERROR("basisu_transcoder::validate_header_quick: header's texture type field is invalid\n");
  8400. return cBASISTexType2DArray;
  8401. }
  8402. return btt;
  8403. }
  8404. bool basisu_transcoder::get_userdata(const void* pData, uint32_t data_size, uint32_t& userdata0, uint32_t& userdata1) const
  8405. {
  8406. if (!validate_header_quick(pData, data_size))
  8407. {
  8408. BASISU_DEVEL_ERROR("basisu_transcoder::get_userdata: header validation failed\n");
  8409. return false;
  8410. }
  8411. const basis_file_header* pHeader = static_cast<const basis_file_header*>(pData);
  8412. userdata0 = pHeader->m_userdata0;
  8413. userdata1 = pHeader->m_userdata1;
  8414. return true;
  8415. }
  8416. uint32_t basisu_transcoder::get_total_images(const void* pData, uint32_t data_size) const
  8417. {
  8418. if (!validate_header_quick(pData, data_size))
  8419. {
  8420. BASISU_DEVEL_ERROR("basisu_transcoder::get_total_images: header validation failed\n");
  8421. return 0;
  8422. }
  8423. const basis_file_header* pHeader = static_cast<const basis_file_header*>(pData);
  8424. return pHeader->m_total_images;
  8425. }
  8426. basis_tex_format basisu_transcoder::get_tex_format(const void* pData, uint32_t data_size) const
  8427. {
  8428. if (!validate_header_quick(pData, data_size))
  8429. {
  8430. BASISU_DEVEL_ERROR("basisu_transcoder::get_total_images: header validation failed\n");
  8431. return basis_tex_format::cETC1S;
  8432. }
  8433. const basis_file_header* pHeader = static_cast<const basis_file_header*>(pData);
  8434. return (basis_tex_format)(uint32_t)pHeader->m_tex_format;
  8435. }
  8436. bool basisu_transcoder::get_image_info(const void* pData, uint32_t data_size, basisu_image_info& image_info, uint32_t image_index) const
  8437. {
  8438. if (!validate_header_quick(pData, data_size))
  8439. {
  8440. BASISU_DEVEL_ERROR("basisu_transcoder::get_image_info: header validation failed\n");
  8441. return false;
  8442. }
  8443. int slice_index = find_first_slice_index(pData, data_size, image_index, 0);
  8444. if (slice_index < 0)
  8445. {
  8446. BASISU_DEVEL_ERROR("basisu_transcoder::get_image_info: invalid slice index\n");
  8447. return false;
  8448. }
  8449. const basis_file_header* pHeader = static_cast<const basis_file_header*>(pData);
  8450. if (image_index >= pHeader->m_total_images)
  8451. {
  8452. BASISU_DEVEL_ERROR("basisu_transcoder::get_image_info: invalid image_index\n");
  8453. return false;
  8454. }
  8455. const basis_slice_desc* pSlice_descs = reinterpret_cast<const basis_slice_desc*>(static_cast<const uint8_t*>(pData) + pHeader->m_slice_desc_file_ofs);
  8456. uint32_t total_levels = 1;
  8457. for (uint32_t i = slice_index + 1; i < pHeader->m_total_slices; i++)
  8458. if (pSlice_descs[i].m_image_index == image_index)
  8459. total_levels = basisu::maximum<uint32_t>(total_levels, pSlice_descs[i].m_level_index + 1);
  8460. else
  8461. break;
  8462. if (total_levels > 16)
  8463. {
  8464. BASISU_DEVEL_ERROR("basisu_transcoder::get_image_info: invalid image_index\n");
  8465. return false;
  8466. }
  8467. const basis_slice_desc& slice_desc = pSlice_descs[slice_index];
  8468. image_info.m_image_index = image_index;
  8469. image_info.m_total_levels = total_levels;
  8470. image_info.m_alpha_flag = false;
  8471. // For ETC1S, if anything has alpha all images have alpha. For UASTC, we only report alpha when the image actually has alpha.
  8472. if (pHeader->m_tex_format == (int)basis_tex_format::cETC1S)
  8473. image_info.m_alpha_flag = (pHeader->m_flags & cBASISHeaderFlagHasAlphaSlices) != 0;
  8474. else
  8475. image_info.m_alpha_flag = (slice_desc.m_flags & cSliceDescFlagsHasAlpha) != 0;
  8476. image_info.m_iframe_flag = (slice_desc.m_flags & cSliceDescFlagsFrameIsIFrame) != 0;
  8477. image_info.m_width = slice_desc.m_num_blocks_x * 4;
  8478. image_info.m_height = slice_desc.m_num_blocks_y * 4;
  8479. image_info.m_orig_width = slice_desc.m_orig_width;
  8480. image_info.m_orig_height = slice_desc.m_orig_height;
  8481. image_info.m_num_blocks_x = slice_desc.m_num_blocks_x;
  8482. image_info.m_num_blocks_y = slice_desc.m_num_blocks_y;
  8483. image_info.m_total_blocks = image_info.m_num_blocks_x * image_info.m_num_blocks_y;
  8484. image_info.m_first_slice_index = slice_index;
  8485. return true;
  8486. }
  8487. uint32_t basisu_transcoder::get_total_image_levels(const void* pData, uint32_t data_size, uint32_t image_index) const
  8488. {
  8489. if (!validate_header_quick(pData, data_size))
  8490. {
  8491. BASISU_DEVEL_ERROR("basisu_transcoder::get_total_image_levels: header validation failed\n");
  8492. return false;
  8493. }
  8494. int slice_index = find_first_slice_index(pData, data_size, image_index, 0);
  8495. if (slice_index < 0)
  8496. {
  8497. BASISU_DEVEL_ERROR("basisu_transcoder::get_total_image_levels: failed finding slice\n");
  8498. return false;
  8499. }
  8500. const basis_file_header* pHeader = static_cast<const basis_file_header*>(pData);
  8501. if (image_index >= pHeader->m_total_images)
  8502. {
  8503. BASISU_DEVEL_ERROR("basisu_transcoder::get_total_image_levels: invalid image_index\n");
  8504. return false;
  8505. }
  8506. const basis_slice_desc* pSlice_descs = reinterpret_cast<const basis_slice_desc*>(static_cast<const uint8_t*>(pData) + pHeader->m_slice_desc_file_ofs);
  8507. uint32_t total_levels = 1;
  8508. for (uint32_t i = slice_index + 1; i < pHeader->m_total_slices; i++)
  8509. if (pSlice_descs[i].m_image_index == image_index)
  8510. total_levels = basisu::maximum<uint32_t>(total_levels, pSlice_descs[i].m_level_index + 1);
  8511. else
  8512. break;
  8513. const uint32_t cMaxSupportedLevels = 16;
  8514. if (total_levels > cMaxSupportedLevels)
  8515. {
  8516. BASISU_DEVEL_ERROR("basisu_transcoder::get_total_image_levels: invalid image levels!\n");
  8517. return false;
  8518. }
  8519. return total_levels;
  8520. }
  8521. bool basisu_transcoder::get_image_level_desc(const void* pData, uint32_t data_size, uint32_t image_index, uint32_t level_index, uint32_t& orig_width, uint32_t& orig_height, uint32_t& total_blocks) const
  8522. {
  8523. if (!validate_header_quick(pData, data_size))
  8524. {
  8525. BASISU_DEVEL_ERROR("basisu_transcoder::get_image_level_desc: header validation failed\n");
  8526. return false;
  8527. }
  8528. int slice_index = find_first_slice_index(pData, data_size, image_index, level_index);
  8529. if (slice_index < 0)
  8530. {
  8531. BASISU_DEVEL_ERROR("basisu_transcoder::get_image_level_desc: failed finding slice\n");
  8532. return false;
  8533. }
  8534. const basis_file_header* pHeader = static_cast<const basis_file_header*>(pData);
  8535. if (image_index >= pHeader->m_total_images)
  8536. {
  8537. BASISU_DEVEL_ERROR("basisu_transcoder::get_image_level_desc: invalid image_index\n");
  8538. return false;
  8539. }
  8540. const basis_slice_desc* pSlice_descs = reinterpret_cast<const basis_slice_desc*>(static_cast<const uint8_t*>(pData) + pHeader->m_slice_desc_file_ofs);
  8541. const basis_slice_desc& slice_desc = pSlice_descs[slice_index];
  8542. orig_width = slice_desc.m_orig_width;
  8543. orig_height = slice_desc.m_orig_height;
  8544. total_blocks = slice_desc.m_num_blocks_x * slice_desc.m_num_blocks_y;
  8545. return true;
  8546. }
  8547. bool basisu_transcoder::get_image_level_info(const void* pData, uint32_t data_size, basisu_image_level_info& image_info, uint32_t image_index, uint32_t level_index) const
  8548. {
  8549. if (!validate_header_quick(pData, data_size))
  8550. {
  8551. BASISU_DEVEL_ERROR("basisu_transcoder::get_image_level_info: validate_file_checksums failed\n");
  8552. return false;
  8553. }
  8554. int slice_index = find_first_slice_index(pData, data_size, image_index, level_index);
  8555. if (slice_index < 0)
  8556. {
  8557. BASISU_DEVEL_ERROR("basisu_transcoder::get_image_level_info: failed finding slice\n");
  8558. return false;
  8559. }
  8560. const basis_file_header* pHeader = static_cast<const basis_file_header*>(pData);
  8561. if (image_index >= pHeader->m_total_images)
  8562. {
  8563. BASISU_DEVEL_ERROR("basisu_transcoder::get_image_level_info: invalid image_index\n");
  8564. return false;
  8565. }
  8566. const basis_slice_desc* pSlice_descs = reinterpret_cast<const basis_slice_desc*>(static_cast<const uint8_t*>(pData) + pHeader->m_slice_desc_file_ofs);
  8567. const basis_slice_desc& slice_desc = pSlice_descs[slice_index];
  8568. image_info.m_image_index = image_index;
  8569. image_info.m_level_index = level_index;
  8570. // For ETC1S, if anything has alpha all images have alpha. For UASTC, we only report alpha when the image actually has alpha.
  8571. if (pHeader->m_tex_format == (int)basis_tex_format::cETC1S)
  8572. image_info.m_alpha_flag = (pHeader->m_flags & cBASISHeaderFlagHasAlphaSlices) != 0;
  8573. else
  8574. image_info.m_alpha_flag = (slice_desc.m_flags & cSliceDescFlagsHasAlpha) != 0;
  8575. image_info.m_iframe_flag = (slice_desc.m_flags & cSliceDescFlagsFrameIsIFrame) != 0;
  8576. image_info.m_width = slice_desc.m_num_blocks_x * 4;
  8577. image_info.m_height = slice_desc.m_num_blocks_y * 4;
  8578. image_info.m_orig_width = slice_desc.m_orig_width;
  8579. image_info.m_orig_height = slice_desc.m_orig_height;
  8580. image_info.m_num_blocks_x = slice_desc.m_num_blocks_x;
  8581. image_info.m_num_blocks_y = slice_desc.m_num_blocks_y;
  8582. image_info.m_total_blocks = image_info.m_num_blocks_x * image_info.m_num_blocks_y;
  8583. image_info.m_first_slice_index = slice_index;
  8584. image_info.m_rgb_file_ofs = slice_desc.m_file_ofs;
  8585. image_info.m_rgb_file_len = slice_desc.m_file_size;
  8586. image_info.m_alpha_file_ofs = 0;
  8587. image_info.m_alpha_file_len = 0;
  8588. if (pHeader->m_tex_format == (int)basis_tex_format::cETC1S)
  8589. {
  8590. if (pHeader->m_flags & cBASISHeaderFlagHasAlphaSlices)
  8591. {
  8592. assert((slice_index + 1) < (int)pHeader->m_total_slices);
  8593. image_info.m_alpha_file_ofs = pSlice_descs[slice_index + 1].m_file_ofs;
  8594. image_info.m_alpha_file_len = pSlice_descs[slice_index + 1].m_file_size;
  8595. }
  8596. }
  8597. return true;
  8598. }
  8599. bool basisu_transcoder::get_file_info(const void* pData, uint32_t data_size, basisu_file_info& file_info) const
  8600. {
  8601. if (!validate_file_checksums(pData, data_size, false))
  8602. {
  8603. BASISU_DEVEL_ERROR("basisu_transcoder::get_file_info: validate_file_checksums failed\n");
  8604. return false;
  8605. }
  8606. const basis_file_header* pHeader = static_cast<const basis_file_header*>(pData);
  8607. const basis_slice_desc* pSlice_descs = reinterpret_cast<const basis_slice_desc*>(static_cast<const uint8_t*>(pData) + pHeader->m_slice_desc_file_ofs);
  8608. file_info.m_version = pHeader->m_ver;
  8609. file_info.m_total_header_size = sizeof(basis_file_header) + pHeader->m_total_slices * sizeof(basis_slice_desc);
  8610. file_info.m_total_selectors = pHeader->m_total_selectors;
  8611. file_info.m_selector_codebook_ofs = pHeader->m_selector_cb_file_ofs;
  8612. file_info.m_selector_codebook_size = pHeader->m_selector_cb_file_size;
  8613. file_info.m_total_endpoints = pHeader->m_total_endpoints;
  8614. file_info.m_endpoint_codebook_ofs = pHeader->m_endpoint_cb_file_ofs;
  8615. file_info.m_endpoint_codebook_size = pHeader->m_endpoint_cb_file_size;
  8616. file_info.m_tables_ofs = pHeader->m_tables_file_ofs;
  8617. file_info.m_tables_size = pHeader->m_tables_file_size;
  8618. file_info.m_tex_format = static_cast<basis_tex_format>(static_cast<int>(pHeader->m_tex_format));
  8619. file_info.m_etc1s = (pHeader->m_tex_format == (int)basis_tex_format::cETC1S);
  8620. file_info.m_y_flipped = (pHeader->m_flags & cBASISHeaderFlagYFlipped) != 0;
  8621. file_info.m_has_alpha_slices = (pHeader->m_flags & cBASISHeaderFlagHasAlphaSlices) != 0;
  8622. const uint32_t total_slices = pHeader->m_total_slices;
  8623. file_info.m_slice_info.resize(total_slices);
  8624. file_info.m_slices_size = 0;
  8625. file_info.m_tex_type = static_cast<basis_texture_type>(static_cast<uint8_t>(pHeader->m_tex_type));
  8626. if (file_info.m_tex_type > cBASISTexTypeTotal)
  8627. {
  8628. BASISU_DEVEL_ERROR("basisu_transcoder::get_file_info: invalid texture type, file is corrupted\n");
  8629. return false;
  8630. }
  8631. file_info.m_us_per_frame = pHeader->m_us_per_frame;
  8632. file_info.m_userdata0 = pHeader->m_userdata0;
  8633. file_info.m_userdata1 = pHeader->m_userdata1;
  8634. file_info.m_image_mipmap_levels.resize(0);
  8635. file_info.m_image_mipmap_levels.resize(pHeader->m_total_images);
  8636. file_info.m_total_images = pHeader->m_total_images;
  8637. for (uint32_t i = 0; i < total_slices; i++)
  8638. {
  8639. file_info.m_slices_size += pSlice_descs[i].m_file_size;
  8640. basisu_slice_info& slice_info = file_info.m_slice_info[i];
  8641. slice_info.m_orig_width = pSlice_descs[i].m_orig_width;
  8642. slice_info.m_orig_height = pSlice_descs[i].m_orig_height;
  8643. slice_info.m_width = pSlice_descs[i].m_num_blocks_x * 4;
  8644. slice_info.m_height = pSlice_descs[i].m_num_blocks_y * 4;
  8645. slice_info.m_num_blocks_x = pSlice_descs[i].m_num_blocks_x;
  8646. slice_info.m_num_blocks_y = pSlice_descs[i].m_num_blocks_y;
  8647. slice_info.m_total_blocks = slice_info.m_num_blocks_x * slice_info.m_num_blocks_y;
  8648. slice_info.m_compressed_size = pSlice_descs[i].m_file_size;
  8649. slice_info.m_slice_index = i;
  8650. slice_info.m_image_index = pSlice_descs[i].m_image_index;
  8651. slice_info.m_level_index = pSlice_descs[i].m_level_index;
  8652. slice_info.m_unpacked_slice_crc16 = pSlice_descs[i].m_slice_data_crc16;
  8653. slice_info.m_alpha_flag = (pSlice_descs[i].m_flags & cSliceDescFlagsHasAlpha) != 0;
  8654. slice_info.m_iframe_flag = (pSlice_descs[i].m_flags & cSliceDescFlagsFrameIsIFrame) != 0;
  8655. if (pSlice_descs[i].m_image_index >= pHeader->m_total_images)
  8656. {
  8657. BASISU_DEVEL_ERROR("basisu_transcoder::get_file_info: slice desc's image index is invalid\n");
  8658. return false;
  8659. }
  8660. file_info.m_image_mipmap_levels[pSlice_descs[i].m_image_index] = basisu::maximum<uint32_t>(file_info.m_image_mipmap_levels[pSlice_descs[i].m_image_index], pSlice_descs[i].m_level_index + 1);
  8661. if (file_info.m_image_mipmap_levels[pSlice_descs[i].m_image_index] > 16)
  8662. {
  8663. BASISU_DEVEL_ERROR("basisu_transcoder::get_file_info: slice mipmap level is invalid\n");
  8664. return false;
  8665. }
  8666. }
  8667. return true;
  8668. }
  8669. bool basisu_transcoder::start_transcoding(const void* pData, uint32_t data_size)
  8670. {
  8671. if (!validate_header_quick(pData, data_size))
  8672. {
  8673. BASISU_DEVEL_ERROR("basisu_transcoder::start_transcoding: header validation failed\n");
  8674. return false;
  8675. }
  8676. const basis_file_header* pHeader = reinterpret_cast<const basis_file_header*>(pData);
  8677. const uint8_t* pDataU8 = static_cast<const uint8_t*>(pData);
  8678. if (pHeader->m_tex_format == (int)basis_tex_format::cETC1S)
  8679. {
  8680. if (m_lowlevel_etc1s_decoder.m_local_endpoints.size())
  8681. {
  8682. m_lowlevel_etc1s_decoder.clear();
  8683. }
  8684. if (pHeader->m_flags & cBASISHeaderFlagUsesGlobalCodebook)
  8685. {
  8686. if (!m_lowlevel_etc1s_decoder.get_global_codebooks())
  8687. {
  8688. BASISU_DEVEL_ERROR("basisu_transcoder::start_transcoding: File uses global codebooks, but set_global_codebooks() has not been called\n");
  8689. return false;
  8690. }
  8691. if (!m_lowlevel_etc1s_decoder.get_global_codebooks()->get_endpoints().size())
  8692. {
  8693. BASISU_DEVEL_ERROR("basisu_transcoder::start_transcoding: Global codebooks must be unpacked first by calling start_transcoding()\n");
  8694. return false;
  8695. }
  8696. if ((m_lowlevel_etc1s_decoder.get_global_codebooks()->get_endpoints().size() != pHeader->m_total_endpoints) ||
  8697. (m_lowlevel_etc1s_decoder.get_global_codebooks()->get_selectors().size() != pHeader->m_total_selectors))
  8698. {
  8699. BASISU_DEVEL_ERROR("basisu_transcoder::start_transcoding: Global codebook size mismatch (wrong codebooks for file).\n");
  8700. return false;
  8701. }
  8702. if (!pHeader->m_tables_file_size)
  8703. {
  8704. BASISU_DEVEL_ERROR("basisu_transcoder::start_transcoding: file is corrupted (2)\n");
  8705. return false;
  8706. }
  8707. if (pHeader->m_tables_file_ofs > data_size)
  8708. {
  8709. BASISU_DEVEL_ERROR("basisu_transcoder::start_transcoding: file is corrupted or passed in buffer too small (4)\n");
  8710. return false;
  8711. }
  8712. if (pHeader->m_tables_file_size > (data_size - pHeader->m_tables_file_ofs))
  8713. {
  8714. BASISU_DEVEL_ERROR("basisu_transcoder::start_transcoding: file is corrupted or passed in buffer too small (5)\n");
  8715. return false;
  8716. }
  8717. }
  8718. else
  8719. {
  8720. if (!pHeader->m_endpoint_cb_file_size || !pHeader->m_selector_cb_file_size || !pHeader->m_tables_file_size)
  8721. {
  8722. BASISU_DEVEL_ERROR("basisu_transcoder::start_transcoding: file is corrupted (0)\n");
  8723. return false;
  8724. }
  8725. if ((pHeader->m_endpoint_cb_file_ofs > data_size) || (pHeader->m_selector_cb_file_ofs > data_size) || (pHeader->m_tables_file_ofs > data_size))
  8726. {
  8727. BASISU_DEVEL_ERROR("basisu_transcoder::start_transcoding: file is corrupted or passed in buffer too small (1)\n");
  8728. return false;
  8729. }
  8730. if (pHeader->m_endpoint_cb_file_size > (data_size - pHeader->m_endpoint_cb_file_ofs))
  8731. {
  8732. BASISU_DEVEL_ERROR("basisu_transcoder::start_transcoding: file is corrupted or passed in buffer too small (2)\n");
  8733. return false;
  8734. }
  8735. if (pHeader->m_selector_cb_file_size > (data_size - pHeader->m_selector_cb_file_ofs))
  8736. {
  8737. BASISU_DEVEL_ERROR("basisu_transcoder::start_transcoding: file is corrupted or passed in buffer too small (3)\n");
  8738. return false;
  8739. }
  8740. if (pHeader->m_tables_file_size > (data_size - pHeader->m_tables_file_ofs))
  8741. {
  8742. BASISU_DEVEL_ERROR("basisu_transcoder::start_transcoding: file is corrupted or passed in buffer too small (3)\n");
  8743. return false;
  8744. }
  8745. if (!m_lowlevel_etc1s_decoder.decode_palettes(
  8746. pHeader->m_total_endpoints, pDataU8 + pHeader->m_endpoint_cb_file_ofs, pHeader->m_endpoint_cb_file_size,
  8747. pHeader->m_total_selectors, pDataU8 + pHeader->m_selector_cb_file_ofs, pHeader->m_selector_cb_file_size))
  8748. {
  8749. BASISU_DEVEL_ERROR("basisu_transcoder::start_transcoding: decode_palettes failed\n");
  8750. return false;
  8751. }
  8752. }
  8753. if (!m_lowlevel_etc1s_decoder.decode_tables(pDataU8 + pHeader->m_tables_file_ofs, pHeader->m_tables_file_size))
  8754. {
  8755. BASISU_DEVEL_ERROR("basisu_transcoder::start_transcoding: decode_tables failed\n");
  8756. return false;
  8757. }
  8758. }
  8759. else
  8760. {
  8761. // Nothing special to do for UASTC.
  8762. if (m_lowlevel_etc1s_decoder.m_local_endpoints.size())
  8763. {
  8764. m_lowlevel_etc1s_decoder.clear();
  8765. }
  8766. }
  8767. m_ready_to_transcode = true;
  8768. return true;
  8769. }
  8770. bool basisu_transcoder::stop_transcoding()
  8771. {
  8772. m_lowlevel_etc1s_decoder.clear();
  8773. m_ready_to_transcode = false;
  8774. return true;
  8775. }
  8776. bool basisu_transcoder::transcode_slice(const void* pData, uint32_t data_size, uint32_t slice_index, void* pOutput_blocks, uint32_t output_blocks_buf_size_in_blocks_or_pixels, block_format fmt,
  8777. uint32_t output_block_or_pixel_stride_in_bytes, uint32_t decode_flags, uint32_t output_row_pitch_in_blocks_or_pixels, basisu_transcoder_state* pState, void *pAlpha_blocks, uint32_t output_rows_in_pixels, int channel0, int channel1) const
  8778. {
  8779. if (!m_ready_to_transcode)
  8780. {
  8781. BASISU_DEVEL_ERROR("basisu_transcoder::transcode_slice: must call start_transcoding first\n");
  8782. return false;
  8783. }
  8784. if (decode_flags & cDecodeFlagsPVRTCDecodeToNextPow2)
  8785. {
  8786. // TODO: Not yet supported
  8787. BASISU_DEVEL_ERROR("basisu_transcoder::transcode_slice: cDecodeFlagsPVRTCDecodeToNextPow2 currently unsupported\n");
  8788. return false;
  8789. }
  8790. if (!validate_header_quick(pData, data_size))
  8791. {
  8792. BASISU_DEVEL_ERROR("basisu_transcoder::transcode_slice: header validation failed\n");
  8793. return false;
  8794. }
  8795. const basis_file_header* pHeader = reinterpret_cast<const basis_file_header*>(pData);
  8796. const uint8_t* pDataU8 = static_cast<const uint8_t*>(pData);
  8797. if (slice_index >= pHeader->m_total_slices)
  8798. {
  8799. BASISU_DEVEL_ERROR("basisu_transcoder::transcode_slice: slice_index >= pHeader->m_total_slices\n");
  8800. return false;
  8801. }
  8802. const basis_slice_desc& slice_desc = reinterpret_cast<const basis_slice_desc*>(pDataU8 + pHeader->m_slice_desc_file_ofs)[slice_index];
  8803. uint32_t total_4x4_blocks = slice_desc.m_num_blocks_x * slice_desc.m_num_blocks_y;
  8804. if (basis_block_format_is_uncompressed(fmt))
  8805. {
  8806. // Assume the output buffer is orig_width by orig_height
  8807. if (!output_row_pitch_in_blocks_or_pixels)
  8808. output_row_pitch_in_blocks_or_pixels = slice_desc.m_orig_width;
  8809. if (!output_rows_in_pixels)
  8810. output_rows_in_pixels = slice_desc.m_orig_height;
  8811. // Now make sure the output buffer is large enough, or we'll overwrite memory.
  8812. if (output_blocks_buf_size_in_blocks_or_pixels < (output_rows_in_pixels * output_row_pitch_in_blocks_or_pixels))
  8813. {
  8814. BASISU_DEVEL_ERROR("basisu_transcoder::transcode_slice: output_blocks_buf_size_in_blocks_or_pixels < (output_rows_in_pixels * output_row_pitch_in_blocks_or_pixels)\n");
  8815. return false;
  8816. }
  8817. }
  8818. else if (fmt == block_format::cFXT1_RGB)
  8819. {
  8820. const uint32_t num_blocks_fxt1_x = (slice_desc.m_orig_width + 7) / 8;
  8821. const uint32_t num_blocks_fxt1_y = (slice_desc.m_orig_height + 3) / 4;
  8822. const uint32_t total_blocks_fxt1 = num_blocks_fxt1_x * num_blocks_fxt1_y;
  8823. if (output_blocks_buf_size_in_blocks_or_pixels < total_blocks_fxt1)
  8824. {
  8825. BASISU_DEVEL_ERROR("basisu_transcoder::transcode_slice: output_blocks_buf_size_in_blocks_or_pixels < total_blocks_fxt1\n");
  8826. return false;
  8827. }
  8828. }
  8829. else
  8830. {
  8831. if (output_blocks_buf_size_in_blocks_or_pixels < total_4x4_blocks)
  8832. {
  8833. BASISU_DEVEL_ERROR("basisu_transcoder::transcode_slice: output_blocks_buf_size_in_blocks_or_pixels < total_blocks\n");
  8834. return false;
  8835. }
  8836. }
  8837. if (fmt != block_format::cETC1)
  8838. {
  8839. if ((fmt == block_format::cPVRTC1_4_RGB) || (fmt == block_format::cPVRTC1_4_RGBA))
  8840. {
  8841. if ((!basisu::is_pow2(slice_desc.m_num_blocks_x * 4)) || (!basisu::is_pow2(slice_desc.m_num_blocks_y * 4)))
  8842. {
  8843. // PVRTC1 only supports power of 2 dimensions
  8844. BASISU_DEVEL_ERROR("basisu_transcoder::transcode_slice: PVRTC1 only supports power of 2 dimensions\n");
  8845. return false;
  8846. }
  8847. }
  8848. }
  8849. if (slice_desc.m_file_ofs > data_size)
  8850. {
  8851. BASISU_DEVEL_ERROR("basisu_transcoder::transcode_slice: invalid slice_desc.m_file_ofs, or passed in buffer too small\n");
  8852. return false;
  8853. }
  8854. const uint32_t data_size_left = data_size - slice_desc.m_file_ofs;
  8855. if (data_size_left < slice_desc.m_file_size)
  8856. {
  8857. BASISU_DEVEL_ERROR("basisu_transcoder::transcode_slice: invalid slice_desc.m_file_size, or passed in buffer too small\n");
  8858. return false;
  8859. }
  8860. if (pHeader->m_tex_format == (int)basis_tex_format::cUASTC4x4)
  8861. {
  8862. return m_lowlevel_uastc_decoder.transcode_slice(pOutput_blocks, slice_desc.m_num_blocks_x, slice_desc.m_num_blocks_y,
  8863. pDataU8 + slice_desc.m_file_ofs, slice_desc.m_file_size,
  8864. fmt, output_block_or_pixel_stride_in_bytes, (decode_flags & cDecodeFlagsBC1ForbidThreeColorBlocks) == 0, *pHeader, slice_desc, output_row_pitch_in_blocks_or_pixels, pState,
  8865. output_rows_in_pixels, channel0, channel1, decode_flags);
  8866. }
  8867. else
  8868. {
  8869. return m_lowlevel_etc1s_decoder.transcode_slice(pOutput_blocks, slice_desc.m_num_blocks_x, slice_desc.m_num_blocks_y,
  8870. pDataU8 + slice_desc.m_file_ofs, slice_desc.m_file_size,
  8871. fmt, output_block_or_pixel_stride_in_bytes, (decode_flags & cDecodeFlagsBC1ForbidThreeColorBlocks) == 0, *pHeader, slice_desc, output_row_pitch_in_blocks_or_pixels, pState,
  8872. (decode_flags & cDecodeFlagsOutputHasAlphaIndices) != 0, pAlpha_blocks, output_rows_in_pixels);
  8873. }
  8874. }
  8875. int basisu_transcoder::find_first_slice_index(const void* pData, uint32_t data_size, uint32_t image_index, uint32_t level_index) const
  8876. {
  8877. BASISU_NOTE_UNUSED(data_size);
  8878. const basis_file_header* pHeader = reinterpret_cast<const basis_file_header*>(pData);
  8879. const uint8_t* pDataU8 = static_cast<const uint8_t*>(pData);
  8880. // For very large basis files this search could be painful
  8881. // TODO: Binary search this
  8882. for (uint32_t slice_iter = 0; slice_iter < pHeader->m_total_slices; slice_iter++)
  8883. {
  8884. const basis_slice_desc& slice_desc = reinterpret_cast<const basis_slice_desc*>(pDataU8 + pHeader->m_slice_desc_file_ofs)[slice_iter];
  8885. if ((slice_desc.m_image_index == image_index) && (slice_desc.m_level_index == level_index))
  8886. return slice_iter;
  8887. }
  8888. BASISU_DEVEL_ERROR("basisu_transcoder::find_first_slice_index: didn't find slice\n");
  8889. return -1;
  8890. }
  8891. int basisu_transcoder::find_slice(const void* pData, uint32_t data_size, uint32_t image_index, uint32_t level_index, bool alpha_data) const
  8892. {
  8893. if (!validate_header_quick(pData, data_size))
  8894. {
  8895. BASISU_DEVEL_ERROR("basisu_transcoder::find_slice: header validation failed\n");
  8896. return false;
  8897. }
  8898. const basis_file_header* pHeader = reinterpret_cast<const basis_file_header*>(pData);
  8899. const uint8_t* pDataU8 = static_cast<const uint8_t*>(pData);
  8900. const basis_slice_desc* pSlice_descs = reinterpret_cast<const basis_slice_desc*>(pDataU8 + pHeader->m_slice_desc_file_ofs);
  8901. // For very large basis files this search could be painful
  8902. // TODO: Binary search this
  8903. for (uint32_t slice_iter = 0; slice_iter < pHeader->m_total_slices; slice_iter++)
  8904. {
  8905. const basis_slice_desc& slice_desc = pSlice_descs[slice_iter];
  8906. if ((slice_desc.m_image_index == image_index) && (slice_desc.m_level_index == level_index))
  8907. {
  8908. if (pHeader->m_tex_format == (int)basis_tex_format::cETC1S)
  8909. {
  8910. const bool slice_alpha = (slice_desc.m_flags & cSliceDescFlagsHasAlpha) != 0;
  8911. if (slice_alpha == alpha_data)
  8912. return slice_iter;
  8913. }
  8914. else
  8915. {
  8916. return slice_iter;
  8917. }
  8918. }
  8919. }
  8920. BASISU_DEVEL_ERROR("basisu_transcoder::find_slice: didn't find slice\n");
  8921. return -1;
  8922. }
  8923. void basisu_transcoder::write_opaque_alpha_blocks(
  8924. uint32_t num_blocks_x, uint32_t num_blocks_y,
  8925. void* pOutput_blocks, block_format fmt,
  8926. uint32_t block_stride_in_bytes, uint32_t output_row_pitch_in_blocks_or_pixels)
  8927. {
  8928. // 'num_blocks_y', 'pOutput_blocks' & 'block_stride_in_bytes' unused
  8929. // when disabling BASISD_SUPPORT_ETC2_EAC_A8 *and* BASISD_SUPPORT_DXT5A
  8930. BASISU_NOTE_UNUSED(num_blocks_y);
  8931. BASISU_NOTE_UNUSED(pOutput_blocks);
  8932. BASISU_NOTE_UNUSED(block_stride_in_bytes);
  8933. if (!output_row_pitch_in_blocks_or_pixels)
  8934. output_row_pitch_in_blocks_or_pixels = num_blocks_x;
  8935. if ((fmt == block_format::cETC2_EAC_A8) || (fmt == block_format::cETC2_EAC_R11))
  8936. {
  8937. #if BASISD_SUPPORT_ETC2_EAC_A8
  8938. eac_block blk;
  8939. blk.m_base = 255;
  8940. blk.m_multiplier = 1;
  8941. blk.m_table = 13;
  8942. // Selectors are all 4's
  8943. memcpy(&blk.m_selectors, g_etc2_eac_a8_sel4, sizeof(g_etc2_eac_a8_sel4));
  8944. for (uint32_t y = 0; y < num_blocks_y; y++)
  8945. {
  8946. uint32_t dst_ofs = y * output_row_pitch_in_blocks_or_pixels * block_stride_in_bytes;
  8947. for (uint32_t x = 0; x < num_blocks_x; x++)
  8948. {
  8949. memcpy((uint8_t*)pOutput_blocks + dst_ofs, &blk, sizeof(blk));
  8950. dst_ofs += block_stride_in_bytes;
  8951. }
  8952. }
  8953. #endif
  8954. }
  8955. else if (fmt == block_format::cBC4)
  8956. {
  8957. #if BASISD_SUPPORT_DXT5A
  8958. dxt5a_block blk;
  8959. blk.m_endpoints[0] = 255;
  8960. blk.m_endpoints[1] = 255;
  8961. memset(blk.m_selectors, 0, sizeof(blk.m_selectors));
  8962. for (uint32_t y = 0; y < num_blocks_y; y++)
  8963. {
  8964. uint32_t dst_ofs = y * output_row_pitch_in_blocks_or_pixels * block_stride_in_bytes;
  8965. for (uint32_t x = 0; x < num_blocks_x; x++)
  8966. {
  8967. memcpy((uint8_t*)pOutput_blocks + dst_ofs, &blk, sizeof(blk));
  8968. dst_ofs += block_stride_in_bytes;
  8969. }
  8970. }
  8971. #endif
  8972. }
  8973. }
  8974. bool basisu_transcoder::transcode_image_level(
  8975. const void* pData, uint32_t data_size,
  8976. uint32_t image_index, uint32_t level_index,
  8977. void* pOutput_blocks, uint32_t output_blocks_buf_size_in_blocks_or_pixels,
  8978. transcoder_texture_format fmt,
  8979. uint32_t decode_flags, uint32_t output_row_pitch_in_blocks_or_pixels, basisu_transcoder_state *pState, uint32_t output_rows_in_pixels) const
  8980. {
  8981. const uint32_t bytes_per_block_or_pixel = basis_get_bytes_per_block_or_pixel(fmt);
  8982. if (!m_ready_to_transcode)
  8983. {
  8984. BASISU_DEVEL_ERROR("basisu_transcoder::transcode_image_level: must call start_transcoding() first\n");
  8985. return false;
  8986. }
  8987. if (decode_flags & cDecodeFlagsPVRTCDecodeToNextPow2)
  8988. {
  8989. BASISU_DEVEL_ERROR("basisu_transcoder::transcode_image_level: cDecodeFlagsPVRTCDecodeToNextPow2 currently unsupported\n");
  8990. // TODO: Not yet supported
  8991. return false;
  8992. }
  8993. if (!validate_header_quick(pData, data_size))
  8994. {
  8995. BASISU_DEVEL_ERROR("basisu_transcoder::transcode_image_level: header validation failed\n");
  8996. return false;
  8997. }
  8998. const basis_file_header* pHeader = reinterpret_cast<const basis_file_header*>(pData);
  8999. const uint8_t* pDataU8 = static_cast<const uint8_t*>(pData);
  9000. const basis_slice_desc* pSlice_descs = reinterpret_cast<const basis_slice_desc*>(pDataU8 + pHeader->m_slice_desc_file_ofs);
  9001. const bool basis_file_has_alpha_slices = (pHeader->m_flags & cBASISHeaderFlagHasAlphaSlices) != 0;
  9002. int slice_index = find_first_slice_index(pData, data_size, image_index, level_index);
  9003. if (slice_index < 0)
  9004. {
  9005. BASISU_DEVEL_ERROR("basisu_transcoder::transcode_image_level: failed finding slice index\n");
  9006. // Unable to find the requested image/level
  9007. return false;
  9008. }
  9009. if ((fmt == transcoder_texture_format::cTFPVRTC1_4_RGBA) && (!basis_file_has_alpha_slices))
  9010. {
  9011. // Switch to PVRTC1 RGB if the input doesn't have alpha.
  9012. fmt = transcoder_texture_format::cTFPVRTC1_4_RGB;
  9013. }
  9014. if (pHeader->m_tex_format == (int)basis_tex_format::cETC1S)
  9015. {
  9016. if (pSlice_descs[slice_index].m_flags & cSliceDescFlagsHasAlpha)
  9017. {
  9018. BASISU_DEVEL_ERROR("basisu_transcoder::transcode_image_level: alpha basis file has out of order alpha slice\n");
  9019. // The first slice shouldn't have alpha data in a properly formed basis file
  9020. return false;
  9021. }
  9022. if (basis_file_has_alpha_slices)
  9023. {
  9024. // The alpha data should immediately follow the color data, and have the same resolution.
  9025. if ((slice_index + 1U) >= pHeader->m_total_slices)
  9026. {
  9027. BASISU_DEVEL_ERROR("basisu_transcoder::transcode_image_level: alpha basis file has missing alpha slice\n");
  9028. // basis file is missing the alpha slice
  9029. return false;
  9030. }
  9031. // Basic sanity checks
  9032. if ((pSlice_descs[slice_index + 1].m_flags & cSliceDescFlagsHasAlpha) == 0)
  9033. {
  9034. BASISU_DEVEL_ERROR("basisu_transcoder::transcode_image_level: alpha basis file has missing alpha slice (flag check)\n");
  9035. // This slice should have alpha data
  9036. return false;
  9037. }
  9038. if ((pSlice_descs[slice_index].m_num_blocks_x != pSlice_descs[slice_index + 1].m_num_blocks_x) || (pSlice_descs[slice_index].m_num_blocks_y != pSlice_descs[slice_index + 1].m_num_blocks_y))
  9039. {
  9040. BASISU_DEVEL_ERROR("basisu_transcoder::transcode_image_level: alpha basis file slice dimensions bad\n");
  9041. // Alpha slice should have been the same res as the color slice
  9042. return false;
  9043. }
  9044. }
  9045. }
  9046. bool status = false;
  9047. const uint32_t total_slice_blocks = pSlice_descs[slice_index].m_num_blocks_x * pSlice_descs[slice_index].m_num_blocks_y;
  9048. if (((fmt == transcoder_texture_format::cTFPVRTC1_4_RGB) || (fmt == transcoder_texture_format::cTFPVRTC1_4_RGBA)) && (output_blocks_buf_size_in_blocks_or_pixels > total_slice_blocks))
  9049. {
  9050. // The transcoder doesn't write beyond total_slice_blocks, so we need to clear the rest ourselves.
  9051. // For GL usage, PVRTC1 4bpp image size is (max(width, 8)* max(height, 8) * 4 + 7) / 8.
  9052. // However, for KTX and internally in Basis this formula isn't used, it's just ((width+3)/4) * ((height+3)/4) * bytes_per_block_or_pixel. This is all the transcoder actually writes to memory.
  9053. memset(static_cast<uint8_t*>(pOutput_blocks) + total_slice_blocks * bytes_per_block_or_pixel, 0, (output_blocks_buf_size_in_blocks_or_pixels - total_slice_blocks) * bytes_per_block_or_pixel);
  9054. }
  9055. if (pHeader->m_tex_format == (int)basis_tex_format::cUASTC4x4)
  9056. {
  9057. const basis_slice_desc* pSlice_desc = &pSlice_descs[slice_index];
  9058. // Use the container independent image transcode method.
  9059. status = m_lowlevel_uastc_decoder.transcode_image(fmt,
  9060. pOutput_blocks, output_blocks_buf_size_in_blocks_or_pixels,
  9061. (const uint8_t*)pData, data_size, pSlice_desc->m_num_blocks_x, pSlice_desc->m_num_blocks_y, pSlice_desc->m_orig_width, pSlice_desc->m_orig_height, pSlice_desc->m_level_index,
  9062. pSlice_desc->m_file_ofs, pSlice_desc->m_file_size,
  9063. decode_flags, basis_file_has_alpha_slices, pHeader->m_tex_type == cBASISTexTypeVideoFrames, output_row_pitch_in_blocks_or_pixels, pState, output_rows_in_pixels);
  9064. }
  9065. else
  9066. {
  9067. // ETC1S
  9068. const basis_slice_desc* pSlice_desc = &pSlice_descs[slice_index];
  9069. const basis_slice_desc* pAlpha_slice_desc = basis_file_has_alpha_slices ? &pSlice_descs[slice_index + 1] : nullptr;
  9070. assert((pSlice_desc->m_flags & cSliceDescFlagsHasAlpha) == 0);
  9071. if (pAlpha_slice_desc)
  9072. {
  9073. // Basic sanity checks
  9074. assert((pAlpha_slice_desc->m_flags & cSliceDescFlagsHasAlpha) != 0);
  9075. assert(pSlice_desc->m_num_blocks_x == pAlpha_slice_desc->m_num_blocks_x);
  9076. assert(pSlice_desc->m_num_blocks_y == pAlpha_slice_desc->m_num_blocks_y);
  9077. assert(pSlice_desc->m_level_index == pAlpha_slice_desc->m_level_index);
  9078. }
  9079. // Use the container independent image transcode method.
  9080. status = m_lowlevel_etc1s_decoder.transcode_image(fmt,
  9081. pOutput_blocks, output_blocks_buf_size_in_blocks_or_pixels,
  9082. (const uint8_t *)pData, data_size, pSlice_desc->m_num_blocks_x, pSlice_desc->m_num_blocks_y, pSlice_desc->m_orig_width, pSlice_desc->m_orig_height, pSlice_desc->m_level_index,
  9083. pSlice_desc->m_file_ofs, pSlice_desc->m_file_size,
  9084. (pAlpha_slice_desc != nullptr) ? (uint32_t)pAlpha_slice_desc->m_file_ofs : 0U, (pAlpha_slice_desc != nullptr) ? (uint32_t)pAlpha_slice_desc->m_file_size : 0U,
  9085. decode_flags, basis_file_has_alpha_slices, pHeader->m_tex_type == cBASISTexTypeVideoFrames, output_row_pitch_in_blocks_or_pixels, pState, output_rows_in_pixels);
  9086. } // if (pHeader->m_tex_format == (int)basis_tex_format::cUASTC4x4)
  9087. if (!status)
  9088. {
  9089. BASISU_DEVEL_ERROR("basisu_transcoder::transcode_image_level: Returning false\n");
  9090. }
  9091. else
  9092. {
  9093. //BASISU_DEVEL_ERROR("basisu_transcoder::transcode_image_level: Returning true\n");
  9094. }
  9095. return status;
  9096. }
  9097. uint32_t basis_get_bytes_per_block_or_pixel(transcoder_texture_format fmt)
  9098. {
  9099. switch (fmt)
  9100. {
  9101. case transcoder_texture_format::cTFETC1_RGB:
  9102. case transcoder_texture_format::cTFBC1_RGB:
  9103. case transcoder_texture_format::cTFBC4_R:
  9104. case transcoder_texture_format::cTFPVRTC1_4_RGB:
  9105. case transcoder_texture_format::cTFPVRTC1_4_RGBA:
  9106. case transcoder_texture_format::cTFATC_RGB:
  9107. case transcoder_texture_format::cTFPVRTC2_4_RGB:
  9108. case transcoder_texture_format::cTFPVRTC2_4_RGBA:
  9109. case transcoder_texture_format::cTFETC2_EAC_R11:
  9110. return 8;
  9111. case transcoder_texture_format::cTFBC7_RGBA:
  9112. case transcoder_texture_format::cTFBC7_ALT:
  9113. case transcoder_texture_format::cTFETC2_RGBA:
  9114. case transcoder_texture_format::cTFBC3_RGBA:
  9115. case transcoder_texture_format::cTFBC5_RG:
  9116. case transcoder_texture_format::cTFASTC_4x4_RGBA:
  9117. case transcoder_texture_format::cTFATC_RGBA:
  9118. case transcoder_texture_format::cTFFXT1_RGB:
  9119. case transcoder_texture_format::cTFETC2_EAC_RG11:
  9120. return 16;
  9121. case transcoder_texture_format::cTFRGBA32:
  9122. return sizeof(uint32_t);
  9123. case transcoder_texture_format::cTFRGB565:
  9124. case transcoder_texture_format::cTFBGR565:
  9125. case transcoder_texture_format::cTFRGBA4444:
  9126. return sizeof(uint16_t);
  9127. default:
  9128. assert(0);
  9129. BASISU_DEVEL_ERROR("basis_get_basisu_texture_format: Invalid fmt\n");
  9130. break;
  9131. }
  9132. return 0;
  9133. }
  9134. const char* basis_get_format_name(transcoder_texture_format fmt)
  9135. {
  9136. switch (fmt)
  9137. {
  9138. case transcoder_texture_format::cTFETC1_RGB: return "ETC1_RGB";
  9139. case transcoder_texture_format::cTFBC1_RGB: return "BC1_RGB";
  9140. case transcoder_texture_format::cTFBC4_R: return "BC4_R";
  9141. case transcoder_texture_format::cTFPVRTC1_4_RGB: return "PVRTC1_4_RGB";
  9142. case transcoder_texture_format::cTFPVRTC1_4_RGBA: return "PVRTC1_4_RGBA";
  9143. case transcoder_texture_format::cTFBC7_RGBA: return "BC7_RGBA";
  9144. case transcoder_texture_format::cTFBC7_ALT: return "BC7_RGBA";
  9145. case transcoder_texture_format::cTFETC2_RGBA: return "ETC2_RGBA";
  9146. case transcoder_texture_format::cTFBC3_RGBA: return "BC3_RGBA";
  9147. case transcoder_texture_format::cTFBC5_RG: return "BC5_RG";
  9148. case transcoder_texture_format::cTFASTC_4x4_RGBA: return "ASTC_RGBA";
  9149. case transcoder_texture_format::cTFATC_RGB: return "ATC_RGB";
  9150. case transcoder_texture_format::cTFATC_RGBA: return "ATC_RGBA";
  9151. case transcoder_texture_format::cTFRGBA32: return "RGBA32";
  9152. case transcoder_texture_format::cTFRGB565: return "RGB565";
  9153. case transcoder_texture_format::cTFBGR565: return "BGR565";
  9154. case transcoder_texture_format::cTFRGBA4444: return "RGBA4444";
  9155. case transcoder_texture_format::cTFFXT1_RGB: return "FXT1_RGB";
  9156. case transcoder_texture_format::cTFPVRTC2_4_RGB: return "PVRTC2_4_RGB";
  9157. case transcoder_texture_format::cTFPVRTC2_4_RGBA: return "PVRTC2_4_RGBA";
  9158. case transcoder_texture_format::cTFETC2_EAC_R11: return "ETC2_EAC_R11";
  9159. case transcoder_texture_format::cTFETC2_EAC_RG11: return "ETC2_EAC_RG11";
  9160. default:
  9161. assert(0);
  9162. BASISU_DEVEL_ERROR("basis_get_basisu_texture_format: Invalid fmt\n");
  9163. break;
  9164. }
  9165. return "";
  9166. }
  9167. const char* basis_get_block_format_name(block_format fmt)
  9168. {
  9169. switch (fmt)
  9170. {
  9171. case block_format::cETC1: return "ETC1";
  9172. case block_format::cBC1: return "BC1";
  9173. case block_format::cPVRTC1_4_RGB: return "PVRTC1_4_RGB";
  9174. case block_format::cPVRTC1_4_RGBA: return "PVRTC1_4_RGBA";
  9175. case block_format::cBC7: return "BC7";
  9176. case block_format::cETC2_RGBA: return "ETC2_RGBA";
  9177. case block_format::cBC3: return "BC3";
  9178. case block_format::cASTC_4x4: return "ASTC_4x4";
  9179. case block_format::cATC_RGB: return "ATC_RGB";
  9180. case block_format::cRGBA32: return "RGBA32";
  9181. case block_format::cRGB565: return "RGB565";
  9182. case block_format::cBGR565: return "BGR565";
  9183. case block_format::cRGBA4444: return "RGBA4444";
  9184. case block_format::cFXT1_RGB: return "FXT1_RGB";
  9185. case block_format::cPVRTC2_4_RGB: return "PVRTC2_4_RGB";
  9186. case block_format::cPVRTC2_4_RGBA: return "PVRTC2_4_RGBA";
  9187. case block_format::cETC2_EAC_R11: return "ETC2_EAC_R11";
  9188. case block_format::cETC2_EAC_RG11: return "ETC2_EAC_RG11";
  9189. default:
  9190. assert(0);
  9191. BASISU_DEVEL_ERROR("basis_get_basisu_texture_format: Invalid fmt\n");
  9192. break;
  9193. }
  9194. return "";
  9195. }
  9196. const char* basis_get_texture_type_name(basis_texture_type tex_type)
  9197. {
  9198. switch (tex_type)
  9199. {
  9200. case cBASISTexType2D: return "2D";
  9201. case cBASISTexType2DArray: return "2D array";
  9202. case cBASISTexTypeCubemapArray: return "cubemap array";
  9203. case cBASISTexTypeVideoFrames: return "video";
  9204. case cBASISTexTypeVolume: return "3D";
  9205. default:
  9206. assert(0);
  9207. BASISU_DEVEL_ERROR("basis_get_texture_type_name: Invalid tex_type\n");
  9208. break;
  9209. }
  9210. return "";
  9211. }
  9212. bool basis_transcoder_format_has_alpha(transcoder_texture_format fmt)
  9213. {
  9214. switch (fmt)
  9215. {
  9216. case transcoder_texture_format::cTFETC2_RGBA:
  9217. case transcoder_texture_format::cTFBC3_RGBA:
  9218. case transcoder_texture_format::cTFASTC_4x4_RGBA:
  9219. case transcoder_texture_format::cTFBC7_RGBA:
  9220. case transcoder_texture_format::cTFBC7_ALT:
  9221. case transcoder_texture_format::cTFPVRTC1_4_RGBA:
  9222. case transcoder_texture_format::cTFPVRTC2_4_RGBA:
  9223. case transcoder_texture_format::cTFATC_RGBA:
  9224. case transcoder_texture_format::cTFRGBA32:
  9225. case transcoder_texture_format::cTFRGBA4444:
  9226. return true;
  9227. default:
  9228. break;
  9229. }
  9230. return false;
  9231. }
  9232. basisu::texture_format basis_get_basisu_texture_format(transcoder_texture_format fmt)
  9233. {
  9234. switch (fmt)
  9235. {
  9236. case transcoder_texture_format::cTFETC1_RGB: return basisu::texture_format::cETC1;
  9237. case transcoder_texture_format::cTFBC1_RGB: return basisu::texture_format::cBC1;
  9238. case transcoder_texture_format::cTFBC4_R: return basisu::texture_format::cBC4;
  9239. case transcoder_texture_format::cTFPVRTC1_4_RGB: return basisu::texture_format::cPVRTC1_4_RGB;
  9240. case transcoder_texture_format::cTFPVRTC1_4_RGBA: return basisu::texture_format::cPVRTC1_4_RGBA;
  9241. case transcoder_texture_format::cTFBC7_RGBA: return basisu::texture_format::cBC7;
  9242. case transcoder_texture_format::cTFBC7_ALT: return basisu::texture_format::cBC7;
  9243. case transcoder_texture_format::cTFETC2_RGBA: return basisu::texture_format::cETC2_RGBA;
  9244. case transcoder_texture_format::cTFBC3_RGBA: return basisu::texture_format::cBC3;
  9245. case transcoder_texture_format::cTFBC5_RG: return basisu::texture_format::cBC5;
  9246. case transcoder_texture_format::cTFASTC_4x4_RGBA: return basisu::texture_format::cASTC4x4;
  9247. case transcoder_texture_format::cTFATC_RGB: return basisu::texture_format::cATC_RGB;
  9248. case transcoder_texture_format::cTFATC_RGBA: return basisu::texture_format::cATC_RGBA_INTERPOLATED_ALPHA;
  9249. case transcoder_texture_format::cTFRGBA32: return basisu::texture_format::cRGBA32;
  9250. case transcoder_texture_format::cTFRGB565: return basisu::texture_format::cRGB565;
  9251. case transcoder_texture_format::cTFBGR565: return basisu::texture_format::cBGR565;
  9252. case transcoder_texture_format::cTFRGBA4444: return basisu::texture_format::cRGBA4444;
  9253. case transcoder_texture_format::cTFFXT1_RGB: return basisu::texture_format::cFXT1_RGB;
  9254. case transcoder_texture_format::cTFPVRTC2_4_RGB: return basisu::texture_format::cPVRTC2_4_RGBA;
  9255. case transcoder_texture_format::cTFPVRTC2_4_RGBA: return basisu::texture_format::cPVRTC2_4_RGBA;
  9256. case transcoder_texture_format::cTFETC2_EAC_R11: return basisu::texture_format::cETC2_R11_EAC;
  9257. case transcoder_texture_format::cTFETC2_EAC_RG11: return basisu::texture_format::cETC2_RG11_EAC;
  9258. default:
  9259. assert(0);
  9260. BASISU_DEVEL_ERROR("basis_get_basisu_texture_format: Invalid fmt\n");
  9261. break;
  9262. }
  9263. return basisu::texture_format::cInvalidTextureFormat;
  9264. }
  9265. bool basis_transcoder_format_is_uncompressed(transcoder_texture_format tex_type)
  9266. {
  9267. switch (tex_type)
  9268. {
  9269. case transcoder_texture_format::cTFRGBA32:
  9270. case transcoder_texture_format::cTFRGB565:
  9271. case transcoder_texture_format::cTFBGR565:
  9272. case transcoder_texture_format::cTFRGBA4444:
  9273. return true;
  9274. default:
  9275. break;
  9276. }
  9277. return false;
  9278. }
  9279. bool basis_block_format_is_uncompressed(block_format blk_fmt)
  9280. {
  9281. switch (blk_fmt)
  9282. {
  9283. case block_format::cRGB32:
  9284. case block_format::cRGBA32:
  9285. case block_format::cA32:
  9286. case block_format::cRGB565:
  9287. case block_format::cBGR565:
  9288. case block_format::cRGBA4444:
  9289. case block_format::cRGBA4444_COLOR:
  9290. case block_format::cRGBA4444_ALPHA:
  9291. case block_format::cRGBA4444_COLOR_OPAQUE:
  9292. return true;
  9293. default:
  9294. break;
  9295. }
  9296. return false;
  9297. }
  9298. uint32_t basis_get_uncompressed_bytes_per_pixel(transcoder_texture_format fmt)
  9299. {
  9300. switch (fmt)
  9301. {
  9302. case transcoder_texture_format::cTFRGBA32:
  9303. return sizeof(uint32_t);
  9304. case transcoder_texture_format::cTFRGB565:
  9305. case transcoder_texture_format::cTFBGR565:
  9306. case transcoder_texture_format::cTFRGBA4444:
  9307. return sizeof(uint16_t);
  9308. default:
  9309. break;
  9310. }
  9311. return 0;
  9312. }
  9313. uint32_t basis_get_block_width(transcoder_texture_format tex_type)
  9314. {
  9315. switch (tex_type)
  9316. {
  9317. case transcoder_texture_format::cTFFXT1_RGB:
  9318. return 8;
  9319. default:
  9320. break;
  9321. }
  9322. return 4;
  9323. }
  9324. uint32_t basis_get_block_height(transcoder_texture_format tex_type)
  9325. {
  9326. BASISU_NOTE_UNUSED(tex_type);
  9327. return 4;
  9328. }
  9329. bool basis_is_format_supported(transcoder_texture_format tex_type, basis_tex_format fmt)
  9330. {
  9331. if (fmt == basis_tex_format::cUASTC4x4)
  9332. {
  9333. #if BASISD_SUPPORT_UASTC
  9334. switch (tex_type)
  9335. {
  9336. // These niche formats aren't currently supported for UASTC - everything else is.
  9337. case transcoder_texture_format::cTFPVRTC2_4_RGB:
  9338. case transcoder_texture_format::cTFPVRTC2_4_RGBA:
  9339. case transcoder_texture_format::cTFATC_RGB:
  9340. case transcoder_texture_format::cTFATC_RGBA:
  9341. case transcoder_texture_format::cTFFXT1_RGB:
  9342. return false;
  9343. default:
  9344. return true;
  9345. }
  9346. #endif
  9347. }
  9348. else
  9349. {
  9350. switch (tex_type)
  9351. {
  9352. // ETC1 and uncompressed are always supported.
  9353. case transcoder_texture_format::cTFETC1_RGB:
  9354. case transcoder_texture_format::cTFRGBA32:
  9355. case transcoder_texture_format::cTFRGB565:
  9356. case transcoder_texture_format::cTFBGR565:
  9357. case transcoder_texture_format::cTFRGBA4444:
  9358. return true;
  9359. #if BASISD_SUPPORT_DXT1
  9360. case transcoder_texture_format::cTFBC1_RGB:
  9361. return true;
  9362. #endif
  9363. #if BASISD_SUPPORT_DXT5A
  9364. case transcoder_texture_format::cTFBC4_R:
  9365. case transcoder_texture_format::cTFBC5_RG:
  9366. return true;
  9367. #endif
  9368. #if BASISD_SUPPORT_DXT1 && BASISD_SUPPORT_DXT5A
  9369. case transcoder_texture_format::cTFBC3_RGBA:
  9370. return true;
  9371. #endif
  9372. #if BASISD_SUPPORT_PVRTC1
  9373. case transcoder_texture_format::cTFPVRTC1_4_RGB:
  9374. case transcoder_texture_format::cTFPVRTC1_4_RGBA:
  9375. return true;
  9376. #endif
  9377. #if BASISD_SUPPORT_BC7_MODE5
  9378. case transcoder_texture_format::cTFBC7_RGBA:
  9379. case transcoder_texture_format::cTFBC7_ALT:
  9380. return true;
  9381. #endif
  9382. #if BASISD_SUPPORT_ETC2_EAC_A8
  9383. case transcoder_texture_format::cTFETC2_RGBA:
  9384. return true;
  9385. #endif
  9386. #if BASISD_SUPPORT_ASTC
  9387. case transcoder_texture_format::cTFASTC_4x4_RGBA:
  9388. return true;
  9389. #endif
  9390. #if BASISD_SUPPORT_ATC
  9391. case transcoder_texture_format::cTFATC_RGB:
  9392. case transcoder_texture_format::cTFATC_RGBA:
  9393. return true;
  9394. #endif
  9395. #if BASISD_SUPPORT_FXT1
  9396. case transcoder_texture_format::cTFFXT1_RGB:
  9397. return true;
  9398. #endif
  9399. #if BASISD_SUPPORT_PVRTC2
  9400. case transcoder_texture_format::cTFPVRTC2_4_RGB:
  9401. case transcoder_texture_format::cTFPVRTC2_4_RGBA:
  9402. return true;
  9403. #endif
  9404. #if BASISD_SUPPORT_ETC2_EAC_RG11
  9405. case transcoder_texture_format::cTFETC2_EAC_R11:
  9406. case transcoder_texture_format::cTFETC2_EAC_RG11:
  9407. return true;
  9408. #endif
  9409. default:
  9410. break;
  9411. }
  9412. }
  9413. return false;
  9414. }
  9415. // ------------------------------------------------------------------------------------------------------
  9416. // UASTC
  9417. // ------------------------------------------------------------------------------------------------------
  9418. #if BASISD_SUPPORT_UASTC
  9419. const astc_bc7_common_partition2_desc g_astc_bc7_common_partitions2[TOTAL_ASTC_BC7_COMMON_PARTITIONS2] =
  9420. {
  9421. { 0, 28, false }, { 1, 20, false }, { 2, 16, true }, { 3, 29, false },
  9422. { 4, 91, true }, { 5, 9, false }, { 6, 107, true }, { 7, 72, true },
  9423. { 8, 149, false }, { 9, 204, true }, { 10, 50, false }, { 11, 114, true },
  9424. { 12, 496, true }, { 13, 17, true }, { 14, 78, false }, { 15, 39, true },
  9425. { 17, 252, true }, { 18, 828, true }, { 19, 43, false }, { 20, 156, false },
  9426. { 21, 116, false }, { 22, 210, true }, { 23, 476, true }, { 24, 273, false },
  9427. { 25, 684, true }, { 26, 359, false }, { 29, 246, true }, { 32, 195, true },
  9428. { 33, 694, true }, { 52, 524, true }
  9429. };
  9430. const bc73_astc2_common_partition_desc g_bc7_3_astc2_common_partitions[TOTAL_BC7_3_ASTC2_COMMON_PARTITIONS] =
  9431. {
  9432. { 10, 36, 4 }, { 11, 48, 4 }, { 0, 61, 3 }, { 2, 137, 4 },
  9433. { 8, 161, 5 }, { 13, 183, 4 }, { 1, 226, 2 }, { 33, 281, 2 },
  9434. { 40, 302, 3 }, { 20, 307, 4 }, { 21, 479, 0 }, { 58, 495, 3 },
  9435. { 3, 593, 0 }, { 32, 594, 2 }, { 59, 605, 1 }, { 34, 799, 3 },
  9436. { 20, 812, 1 }, { 14, 988, 4 }, { 31, 993, 3 }
  9437. };
  9438. const astc_bc7_common_partition3_desc g_astc_bc7_common_partitions3[TOTAL_ASTC_BC7_COMMON_PARTITIONS3] =
  9439. {
  9440. { 4, 260, 0 }, { 8, 74, 5 }, { 9, 32, 5 }, { 10, 156, 2 },
  9441. { 11, 183, 2 }, { 12, 15, 0 }, { 13, 745, 4 }, { 20, 0, 1 },
  9442. { 35, 335, 1 }, { 36, 902, 5 }, { 57, 254, 0 }
  9443. };
  9444. const uint8_t g_astc_to_bc7_partition_index_perm_tables[6][3] = { { 0, 1, 2 }, { 1, 2, 0 }, { 2, 0, 1 }, { 2, 1, 0 }, { 0, 2, 1 }, { 1, 0, 2 } };
  9445. const uint8_t g_bc7_to_astc_partition_index_perm_tables[6][3] = { { 0, 1, 2 }, { 2, 0, 1 }, { 1, 2, 0 }, { 2, 1, 0 }, { 0, 2, 1 }, { 1, 0, 2 } };
  9446. uint32_t bc7_convert_partition_index_3_to_2(uint32_t p, uint32_t k)
  9447. {
  9448. assert(k < 6);
  9449. switch (k >> 1)
  9450. {
  9451. case 0:
  9452. if (p <= 1)
  9453. p = 0;
  9454. else
  9455. p = 1;
  9456. break;
  9457. case 1:
  9458. if (p == 0)
  9459. p = 0;
  9460. else
  9461. p = 1;
  9462. break;
  9463. case 2:
  9464. if ((p == 0) || (p == 2))
  9465. p = 0;
  9466. else
  9467. p = 1;
  9468. break;
  9469. }
  9470. if (k & 1)
  9471. p = 1 - p;
  9472. return p;
  9473. }
  9474. static const uint8_t g_zero_pattern[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  9475. const uint8_t g_astc_bc7_patterns2[TOTAL_ASTC_BC7_COMMON_PARTITIONS2][16] =
  9476. {
  9477. { 0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1 }, { 0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1 }, { 1,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0 }, { 0,0,0,1,0,0,1,1,0,0,1,1,0,1,1,1 },
  9478. { 1,1,1,1,1,1,1,0,1,1,1,0,1,1,0,0 }, { 0,0,1,1,0,1,1,1,0,1,1,1,1,1,1,1 }, { 1,1,1,0,1,1,0,0,1,0,0,0,0,0,0,0 }, { 1,1,1,1,1,1,1,0,1,1,0,0,1,0,0,0 },
  9479. { 0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,1 }, { 1,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0 }, { 0,0,0,0,0,0,0,1,0,1,1,1,1,1,1,1 }, { 1,1,1,1,1,1,1,1,1,1,1,0,1,0,0,0 },
  9480. { 1,1,1,0,1,0,0,0,0,0,0,0,0,0,0,0 }, { 1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0 }, { 0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1 }, { 1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0 },
  9481. { 1,0,0,0,1,1,1,0,1,1,1,1,1,1,1,1 }, { 1,1,1,1,1,1,1,1,0,1,1,1,0,0,0,1 }, { 0,1,1,1,0,0,1,1,0,0,0,1,0,0,0,0 }, { 0,0,1,1,0,0,0,1,0,0,0,0,0,0,0,0 },
  9482. { 0,0,0,0,1,0,0,0,1,1,0,0,1,1,1,0 }, { 1,1,1,1,1,1,1,1,0,1,1,1,0,0,1,1 }, { 1,0,0,0,1,1,0,0,1,1,0,0,1,1,1,0 }, { 0,0,1,1,0,0,0,1,0,0,0,1,0,0,0,0 },
  9483. { 1,1,1,1,0,1,1,1,0,1,1,1,0,0,1,1 }, { 0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0 }, { 1,1,1,1,0,0,0,0,0,0,0,0,1,1,1,1 }, { 1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0 },
  9484. { 1,1,1,1,0,0,0,0,1,1,1,1,0,0,0,0 }, { 1,0,0,1,0,0,1,1,0,1,1,0,1,1,0,0 }
  9485. };
  9486. const uint8_t g_astc_bc7_patterns3[TOTAL_ASTC_BC7_COMMON_PARTITIONS3][16] =
  9487. {
  9488. { 0,0,0,0,0,0,0,0,1,1,2,2,1,1,2,2 }, { 1,1,1,1,1,1,1,1,0,0,0,0,2,2,2,2 }, { 1,1,1,1,0,0,0,0,0,0,0,0,2,2,2,2 }, { 1,1,1,1,2,2,2,2,0,0,0,0,0,0,0,0 },
  9489. { 1,1,2,0,1,1,2,0,1,1,2,0,1,1,2,0 }, { 0,1,1,2,0,1,1,2,0,1,1,2,0,1,1,2 }, { 0,2,1,1,0,2,1,1,0,2,1,1,0,2,1,1 }, { 2,0,0,0,2,0,0,0,2,1,1,1,2,1,1,1 },
  9490. { 2,0,1,2,2,0,1,2,2,0,1,2,2,0,1,2 }, { 1,1,1,1,0,0,0,0,2,2,2,2,1,1,1,1 }, { 0,0,2,2,0,0,1,1,0,0,1,1,0,0,2,2 }
  9491. };
  9492. const uint8_t g_bc7_3_astc2_patterns2[TOTAL_BC7_3_ASTC2_COMMON_PARTITIONS][16] =
  9493. {
  9494. { 0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0 }, { 0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0 }, { 1,1,0,0,1,1,0,0,1,0,0,0,0,0,0,0 }, { 0,0,0,0,0,0,0,1,0,0,1,1,0,0,1,1 },
  9495. { 1,1,1,1,1,1,1,1,0,0,0,0,1,1,1,1 }, { 0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0 }, { 0,0,0,1,0,0,1,1,1,1,1,1,1,1,1,1 }, { 0,1,1,1,0,0,1,1,0,0,1,1,0,0,1,1 },
  9496. { 1,1,0,0,0,0,0,0,0,0,1,1,1,1,0,0 }, { 0,1,1,1,0,1,1,1,0,0,0,0,0,0,0,0 }, { 0,0,0,0,0,0,0,0,1,1,1,0,1,1,1,0 }, { 1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0 },
  9497. { 0,1,1,1,0,0,1,1,0,0,0,0,0,0,0,0 }, { 0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1 }, { 1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,0 }, { 1,1,0,0,1,1,0,0,1,1,0,0,1,0,0,0 },
  9498. { 1,1,1,1,1,1,1,1,1,0,0,0,1,0,0,0 }, { 0,0,1,1,0,1,1,0,1,1,0,0,1,0,0,0 }, { 1,1,1,1,0,1,1,1,0,0,0,0,0,0,0,0 }
  9499. };
  9500. const uint8_t g_astc_bc7_pattern2_anchors[TOTAL_ASTC_BC7_COMMON_PARTITIONS2][3] =
  9501. {
  9502. { 0, 2 }, { 0, 3 }, { 1, 0 }, { 0, 3 }, { 7, 0 }, { 0, 2 }, { 3, 0 }, { 7, 0 },
  9503. { 0, 11 }, { 2, 0 }, { 0, 7 }, { 11, 0 }, { 3, 0 }, { 8, 0 }, { 0, 4 }, { 12, 0 },
  9504. { 1, 0 }, { 8, 0 }, { 0, 1 }, { 0, 2 }, { 0, 4 }, { 8, 0 }, { 1, 0 }, { 0, 2 },
  9505. { 4, 0 }, { 0, 1 }, { 4, 0 }, { 1, 0 }, { 4, 0 }, { 1, 0 }
  9506. };
  9507. const uint8_t g_astc_bc7_pattern3_anchors[TOTAL_ASTC_BC7_COMMON_PARTITIONS3][3] =
  9508. {
  9509. { 0, 8, 10 }, { 8, 0, 12 }, { 4, 0, 12 }, { 8, 0, 4 }, { 3, 0, 2 }, { 0, 1, 3 }, { 0, 2, 1 }, { 1, 9, 0 }, { 1, 2, 0 }, { 4, 0, 8 }, { 0, 6, 2 }
  9510. };
  9511. const uint8_t g_bc7_3_astc2_patterns2_anchors[TOTAL_BC7_3_ASTC2_COMMON_PARTITIONS][3] =
  9512. {
  9513. { 0, 4 }, { 0, 2 }, { 2, 0 }, { 0, 7 }, { 8, 0 }, { 0, 1 }, { 0, 3 }, { 0, 1 }, { 2, 0 }, { 0, 1 }, { 0, 8 }, { 2, 0 }, { 0, 1 }, { 0, 7 }, { 12, 0 }, { 2, 0 }, { 9, 0 }, { 0, 2 }, { 4, 0 }
  9514. };
  9515. const uint32_t g_uastc_mode_huff_codes[TOTAL_UASTC_MODES + 1][2] =
  9516. {
  9517. { 0x1, 4 },
  9518. { 0x35, 6 },
  9519. { 0x1D, 5 },
  9520. { 0x3, 5 },
  9521. { 0x13, 5 },
  9522. { 0xB, 5 },
  9523. { 0x1B, 5 },
  9524. { 0x7, 5 },
  9525. { 0x17, 5 },
  9526. { 0xF, 5 },
  9527. { 0x2, 3 },
  9528. { 0x0, 2 },
  9529. { 0x6, 3 },
  9530. { 0x1F, 5 },
  9531. { 0xD, 5 },
  9532. { 0x5, 7 },
  9533. { 0x15, 6 },
  9534. { 0x25, 6 },
  9535. { 0x9, 4 },
  9536. { 0x45, 7 } // future expansion
  9537. };
  9538. // If g_uastc_mode_huff_codes[] changes this table must be updated!
  9539. static const uint8_t g_uastc_huff_modes[128] =
  9540. {
  9541. 11,0,10,3,11,15,12,7,11,18,10,5,11,14,12,9,11,0,10,4,11,16,12,8,11,18,10,6,11,2,12,13,11,0,10,3,11,17,12,7,11,18,10,5,11,14,12,9,11,0,10,4,11,1,12,8,11,18,10,6,11,2,12,13,11,0,10,3,11,
  9542. 19,12,7,11,18,10,5,11,14,12,9,11,0,10,4,11,16,12,8,11,18,10,6,11,2,12,13,11,0,10,3,11,17,12,7,11,18,10,5,11,14,12,9,11,0,10,4,11,1,12,8,11,18,10,6,11,2,12,13
  9543. };
  9544. const uint8_t g_uastc_mode_weight_bits[TOTAL_UASTC_MODES] = { 4, 2, 3, 2, 2, 3, 2, 2, 0, 2, 4, 2, 3, 1, 2, 4, 2, 2, 5 };
  9545. const uint8_t g_uastc_mode_weight_ranges[TOTAL_UASTC_MODES] = { 8, 2, 5, 2, 2, 5, 2, 2, 0, 2, 8, 2, 5, 0, 2, 8, 2, 2, 11 };
  9546. const uint8_t g_uastc_mode_endpoint_ranges[TOTAL_UASTC_MODES] = { 19, 20, 8, 7, 12, 20, 18, 12, 0, 8, 13, 13, 19, 20, 20, 20, 20, 20, 11 };
  9547. const uint8_t g_uastc_mode_subsets[TOTAL_UASTC_MODES] = { 1, 1, 2, 3, 2, 1, 1, 2, 0, 2, 1, 1, 1, 1, 1, 1, 2, 1, 1 };
  9548. const uint8_t g_uastc_mode_planes[TOTAL_UASTC_MODES] = { 1, 1, 1, 1, 1, 1, 2, 1, 0, 1, 1, 2, 1, 2, 1, 1, 1, 2, 1 };
  9549. const uint8_t g_uastc_mode_comps[TOTAL_UASTC_MODES] = { 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 2, 2, 2, 3 };
  9550. const uint8_t g_uastc_mode_has_etc1_bias[TOTAL_UASTC_MODES] = { 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1 };
  9551. const uint8_t g_uastc_mode_has_bc1_hint0[TOTAL_UASTC_MODES] = { 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
  9552. const uint8_t g_uastc_mode_has_bc1_hint1[TOTAL_UASTC_MODES] = { 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1 };
  9553. const uint8_t g_uastc_mode_cem[TOTAL_UASTC_MODES] = { 8, 8, 8, 8, 8, 8, 8, 8, 0, 12, 12, 12, 12, 12, 12, 4, 4, 4, 8 };
  9554. const uint8_t g_uastc_mode_has_alpha[TOTAL_UASTC_MODES] = { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0 };
  9555. const uint8_t g_uastc_mode_is_la[TOTAL_UASTC_MODES] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0 };
  9556. const uint8_t g_uastc_mode_total_hint_bits[TOTAL_UASTC_MODES] = { 15, 15, 15, 15, 15, 15, 15, 15, 0, 23, 17, 17, 17, 23, 23, 23, 23, 23, 15 };
  9557. // bits, trits, quints
  9558. const int g_astc_bise_range_table[TOTAL_ASTC_RANGES][3] =
  9559. {
  9560. { 1, 0, 0 }, // 0-1 0
  9561. { 0, 1, 0 }, // 0-2 1
  9562. { 2, 0, 0 }, // 0-3 2
  9563. { 0, 0, 1 }, // 0-4 3
  9564. { 1, 1, 0 }, // 0-5 4
  9565. { 3, 0, 0 }, // 0-7 5
  9566. { 1, 0, 1 }, // 0-9 6
  9567. { 2, 1, 0 }, // 0-11 7
  9568. { 4, 0, 0 }, // 0-15 8
  9569. { 2, 0, 1 }, // 0-19 9
  9570. { 3, 1, 0 }, // 0-23 10
  9571. { 5, 0, 0 }, // 0-31 11
  9572. { 3, 0, 1 }, // 0-39 12
  9573. { 4, 1, 0 }, // 0-47 13
  9574. { 6, 0, 0 }, // 0-63 14
  9575. { 4, 0, 1 }, // 0-79 15
  9576. { 5, 1, 0 }, // 0-95 16
  9577. { 7, 0, 0 }, // 0-127 17
  9578. { 5, 0, 1 }, // 0-159 18
  9579. { 6, 1, 0 }, // 0-191 19
  9580. { 8, 0, 0 }, // 0-255 20
  9581. };
  9582. int astc_get_levels(int range)
  9583. {
  9584. assert(range < (int)BC7ENC_TOTAL_ASTC_RANGES);
  9585. return (1 + 2 * g_astc_bise_range_table[range][1] + 4 * g_astc_bise_range_table[range][2]) << g_astc_bise_range_table[range][0];
  9586. }
  9587. // g_astc_unquant[] is the inverse of g_astc_sorted_order_unquant[]
  9588. astc_quant_bin g_astc_unquant[BC7ENC_TOTAL_ASTC_RANGES][256]; // [ASTC encoded endpoint index]
  9589. // Taken right from the ASTC spec.
  9590. static struct
  9591. {
  9592. const char* m_pB_str;
  9593. uint32_t m_c;
  9594. } g_astc_endpoint_unquant_params[BC7ENC_TOTAL_ASTC_RANGES] =
  9595. {
  9596. { "", 0 },
  9597. { "", 0 },
  9598. { "", 0 },
  9599. { "", 0 },
  9600. { "000000000", 204, }, // 0-5
  9601. { "", 0 },
  9602. { "000000000", 113, }, // 0-9
  9603. { "b000b0bb0", 93 }, // 0-11
  9604. { "", 0 },
  9605. { "b0000bb00", 54 }, // 0-19
  9606. { "cb000cbcb", 44 }, // 0-23
  9607. { "", 0 },
  9608. { "cb0000cbc", 26 }, // 0-39
  9609. { "dcb000dcb", 22 }, // 0-47
  9610. { "", 0 },
  9611. { "dcb0000dc", 13 }, // 0-79
  9612. { "edcb000ed", 11 }, // 0-95
  9613. { "", 0 },
  9614. { "edcb0000e", 6 }, // 0-159
  9615. { "fedcb000f", 5 }, // 0-191
  9616. { "", 0 },
  9617. };
  9618. bool astc_is_valid_endpoint_range(uint32_t range)
  9619. {
  9620. if ((g_astc_bise_range_table[range][1] == 0) && (g_astc_bise_range_table[range][2] == 0))
  9621. return true;
  9622. return g_astc_endpoint_unquant_params[range].m_c != 0;
  9623. }
  9624. uint32_t unquant_astc_endpoint(uint32_t packed_bits, uint32_t packed_trits, uint32_t packed_quints, uint32_t range)
  9625. {
  9626. assert(range < BC7ENC_TOTAL_ASTC_RANGES);
  9627. const uint32_t bits = g_astc_bise_range_table[range][0];
  9628. const uint32_t trits = g_astc_bise_range_table[range][1];
  9629. const uint32_t quints = g_astc_bise_range_table[range][2];
  9630. uint32_t val = 0;
  9631. if ((!trits) && (!quints))
  9632. {
  9633. assert(!packed_trits && !packed_quints);
  9634. int bits_left = 8;
  9635. while (bits_left > 0)
  9636. {
  9637. uint32_t v = packed_bits;
  9638. int n = basisu::minimumi(bits_left, bits);
  9639. if (n < (int)bits)
  9640. v >>= (bits - n);
  9641. assert(v < (1U << n));
  9642. val |= (v << (bits_left - n));
  9643. bits_left -= n;
  9644. }
  9645. }
  9646. else
  9647. {
  9648. const uint32_t A = (packed_bits & 1) ? 511 : 0;
  9649. const uint32_t C = g_astc_endpoint_unquant_params[range].m_c;
  9650. const uint32_t D = trits ? packed_trits : packed_quints;
  9651. assert(C);
  9652. uint32_t B = 0;
  9653. for (uint32_t i = 0; i < 9; i++)
  9654. {
  9655. B <<= 1;
  9656. char c = g_astc_endpoint_unquant_params[range].m_pB_str[i];
  9657. if (c != '0')
  9658. {
  9659. c -= 'a';
  9660. B |= ((packed_bits >> c) & 1);
  9661. }
  9662. }
  9663. val = D * C + B;
  9664. val = val ^ A;
  9665. val = (A & 0x80) | (val >> 2);
  9666. }
  9667. return val;
  9668. }
  9669. uint32_t unquant_astc_endpoint_val(uint32_t packed_val, uint32_t range)
  9670. {
  9671. assert(range < BC7ENC_TOTAL_ASTC_RANGES);
  9672. assert(packed_val < (uint32_t)astc_get_levels(range));
  9673. const uint32_t bits = g_astc_bise_range_table[range][0];
  9674. const uint32_t trits = g_astc_bise_range_table[range][1];
  9675. const uint32_t quints = g_astc_bise_range_table[range][2];
  9676. if ((!trits) && (!quints))
  9677. return unquant_astc_endpoint(packed_val, 0, 0, range);
  9678. else if (trits)
  9679. return unquant_astc_endpoint(packed_val & ((1 << bits) - 1), packed_val >> bits, 0, range);
  9680. else
  9681. return unquant_astc_endpoint(packed_val & ((1 << bits) - 1), 0, packed_val >> bits, range);
  9682. }
  9683. // BC7 - Various BC7 tables/helpers
  9684. const uint32_t g_bc7_weights1[2] = { 0, 64 };
  9685. const uint32_t g_bc7_weights2[4] = { 0, 21, 43, 64 };
  9686. const uint32_t g_bc7_weights3[8] = { 0, 9, 18, 27, 37, 46, 55, 64 };
  9687. const uint32_t g_bc7_weights4[16] = { 0, 4, 9, 13, 17, 21, 26, 30, 34, 38, 43, 47, 51, 55, 60, 64 };
  9688. const uint32_t g_astc_weights4[16] = { 0, 4, 8, 12, 17, 21, 25, 29, 35, 39, 43, 47, 52, 56, 60, 64 };
  9689. const uint32_t g_astc_weights5[32] = { 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64 };
  9690. const uint32_t g_astc_weights_3levels[3] = { 0, 32, 64 };
  9691. const uint8_t g_bc7_partition1[16] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
  9692. const uint8_t g_bc7_partition2[64 * 16] =
  9693. {
  9694. 0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1, 0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,1, 0,1,1,1,0,1,1,1,0,1,1,1,0,1,1,1, 0,0,0,1,0,0,1,1,0,0,1,1,0,1,1,1, 0,0,0,0,0,0,0,1,0,0,0,1,0,0,1,1, 0,0,1,1,0,1,1,1,0,1,1,1,1,1,1,1, 0,0,0,1,0,0,1,1,0,1,1,1,1,1,1,1, 0,0,0,0,0,0,0,1,0,0,1,1,0,1,1,1,
  9695. 0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,1, 0,0,1,1,0,1,1,1,1,1,1,1,1,1,1,1, 0,0,0,0,0,0,0,1,0,1,1,1,1,1,1,1, 0,0,0,0,0,0,0,0,0,0,0,1,0,1,1,1, 0,0,0,1,0,1,1,1,1,1,1,1,1,1,1,1, 0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1, 0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1, 0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,
  9696. 0,0,0,0,1,0,0,0,1,1,1,0,1,1,1,1, 0,1,1,1,0,0,0,1,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,1,0,0,0,1,1,1,0, 0,1,1,1,0,0,1,1,0,0,0,1,0,0,0,0, 0,0,1,1,0,0,0,1,0,0,0,0,0,0,0,0, 0,0,0,0,1,0,0,0,1,1,0,0,1,1,1,0, 0,0,0,0,0,0,0,0,1,0,0,0,1,1,0,0, 0,1,1,1,0,0,1,1,0,0,1,1,0,0,0,1,
  9697. 0,0,1,1,0,0,0,1,0,0,0,1,0,0,0,0, 0,0,0,0,1,0,0,0,1,0,0,0,1,1,0,0, 0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0, 0,0,1,1,0,1,1,0,0,1,1,0,1,1,0,0, 0,0,0,1,0,1,1,1,1,1,1,0,1,0,0,0, 0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0, 0,1,1,1,0,0,0,1,1,0,0,0,1,1,1,0, 0,0,1,1,1,0,0,1,1,0,0,1,1,1,0,0,
  9698. 0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1, 0,0,0,0,1,1,1,1,0,0,0,0,1,1,1,1, 0,1,0,1,1,0,1,0,0,1,0,1,1,0,1,0, 0,0,1,1,0,0,1,1,1,1,0,0,1,1,0,0, 0,0,1,1,1,1,0,0,0,0,1,1,1,1,0,0, 0,1,0,1,0,1,0,1,1,0,1,0,1,0,1,0, 0,1,1,0,1,0,0,1,0,1,1,0,1,0,0,1, 0,1,0,1,1,0,1,0,1,0,1,0,0,1,0,1,
  9699. 0,1,1,1,0,0,1,1,1,1,0,0,1,1,1,0, 0,0,0,1,0,0,1,1,1,1,0,0,1,0,0,0, 0,0,1,1,0,0,1,0,0,1,0,0,1,1,0,0, 0,0,1,1,1,0,1,1,1,1,0,1,1,1,0,0, 0,1,1,0,1,0,0,1,1,0,0,1,0,1,1,0, 0,0,1,1,1,1,0,0,1,1,0,0,0,0,1,1, 0,1,1,0,0,1,1,0,1,0,0,1,1,0,0,1, 0,0,0,0,0,1,1,0,0,1,1,0,0,0,0,0,
  9700. 0,1,0,0,1,1,1,0,0,1,0,0,0,0,0,0, 0,0,1,0,0,1,1,1,0,0,1,0,0,0,0,0, 0,0,0,0,0,0,1,0,0,1,1,1,0,0,1,0, 0,0,0,0,0,1,0,0,1,1,1,0,0,1,0,0, 0,1,1,0,1,1,0,0,1,0,0,1,0,0,1,1, 0,0,1,1,0,1,1,0,1,1,0,0,1,0,0,1, 0,1,1,0,0,0,1,1,1,0,0,1,1,1,0,0, 0,0,1,1,1,0,0,1,1,1,0,0,0,1,1,0,
  9701. 0,1,1,0,1,1,0,0,1,1,0,0,1,0,0,1, 0,1,1,0,0,0,1,1,0,0,1,1,1,0,0,1, 0,1,1,1,1,1,1,0,1,0,0,0,0,0,0,1, 0,0,0,1,1,0,0,0,1,1,1,0,0,1,1,1, 0,0,0,0,1,1,1,1,0,0,1,1,0,0,1,1, 0,0,1,1,0,0,1,1,1,1,1,1,0,0,0,0, 0,0,1,0,0,0,1,0,1,1,1,0,1,1,1,0, 0,1,0,0,0,1,0,0,0,1,1,1,0,1,1,1
  9702. };
  9703. const uint8_t g_bc7_partition3[64 * 16] =
  9704. {
  9705. 0,0,1,1,0,0,1,1,0,2,2,1,2,2,2,2, 0,0,0,1,0,0,1,1,2,2,1,1,2,2,2,1, 0,0,0,0,2,0,0,1,2,2,1,1,2,2,1,1, 0,2,2,2,0,0,2,2,0,0,1,1,0,1,1,1, 0,0,0,0,0,0,0,0,1,1,2,2,1,1,2,2, 0,0,1,1,0,0,1,1,0,0,2,2,0,0,2,2, 0,0,2,2,0,0,2,2,1,1,1,1,1,1,1,1, 0,0,1,1,0,0,1,1,2,2,1,1,2,2,1,1,
  9706. 0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2, 0,0,0,0,1,1,1,1,1,1,1,1,2,2,2,2, 0,0,0,0,1,1,1,1,2,2,2,2,2,2,2,2, 0,0,1,2,0,0,1,2,0,0,1,2,0,0,1,2, 0,1,1,2,0,1,1,2,0,1,1,2,0,1,1,2, 0,1,2,2,0,1,2,2,0,1,2,2,0,1,2,2, 0,0,1,1,0,1,1,2,1,1,2,2,1,2,2,2, 0,0,1,1,2,0,0,1,2,2,0,0,2,2,2,0,
  9707. 0,0,0,1,0,0,1,1,0,1,1,2,1,1,2,2, 0,1,1,1,0,0,1,1,2,0,0,1,2,2,0,0, 0,0,0,0,1,1,2,2,1,1,2,2,1,1,2,2, 0,0,2,2,0,0,2,2,0,0,2,2,1,1,1,1, 0,1,1,1,0,1,1,1,0,2,2,2,0,2,2,2, 0,0,0,1,0,0,0,1,2,2,2,1,2,2,2,1, 0,0,0,0,0,0,1,1,0,1,2,2,0,1,2,2, 0,0,0,0,1,1,0,0,2,2,1,0,2,2,1,0,
  9708. 0,1,2,2,0,1,2,2,0,0,1,1,0,0,0,0, 0,0,1,2,0,0,1,2,1,1,2,2,2,2,2,2, 0,1,1,0,1,2,2,1,1,2,2,1,0,1,1,0, 0,0,0,0,0,1,1,0,1,2,2,1,1,2,2,1, 0,0,2,2,1,1,0,2,1,1,0,2,0,0,2,2, 0,1,1,0,0,1,1,0,2,0,0,2,2,2,2,2, 0,0,1,1,0,1,2,2,0,1,2,2,0,0,1,1, 0,0,0,0,2,0,0,0,2,2,1,1,2,2,2,1,
  9709. 0,0,0,0,0,0,0,2,1,1,2,2,1,2,2,2, 0,2,2,2,0,0,2,2,0,0,1,2,0,0,1,1, 0,0,1,1,0,0,1,2,0,0,2,2,0,2,2,2, 0,1,2,0,0,1,2,0,0,1,2,0,0,1,2,0, 0,0,0,0,1,1,1,1,2,2,2,2,0,0,0,0, 0,1,2,0,1,2,0,1,2,0,1,2,0,1,2,0, 0,1,2,0,2,0,1,2,1,2,0,1,0,1,2,0, 0,0,1,1,2,2,0,0,1,1,2,2,0,0,1,1,
  9710. 0,0,1,1,1,1,2,2,2,2,0,0,0,0,1,1, 0,1,0,1,0,1,0,1,2,2,2,2,2,2,2,2, 0,0,0,0,0,0,0,0,2,1,2,1,2,1,2,1, 0,0,2,2,1,1,2,2,0,0,2,2,1,1,2,2, 0,0,2,2,0,0,1,1,0,0,2,2,0,0,1,1, 0,2,2,0,1,2,2,1,0,2,2,0,1,2,2,1, 0,1,0,1,2,2,2,2,2,2,2,2,0,1,0,1, 0,0,0,0,2,1,2,1,2,1,2,1,2,1,2,1,
  9711. 0,1,0,1,0,1,0,1,0,1,0,1,2,2,2,2, 0,2,2,2,0,1,1,1,0,2,2,2,0,1,1,1, 0,0,0,2,1,1,1,2,0,0,0,2,1,1,1,2, 0,0,0,0,2,1,1,2,2,1,1,2,2,1,1,2, 0,2,2,2,0,1,1,1,0,1,1,1,0,2,2,2, 0,0,0,2,1,1,1,2,1,1,1,2,0,0,0,2, 0,1,1,0,0,1,1,0,0,1,1,0,2,2,2,2, 0,0,0,0,0,0,0,0,2,1,1,2,2,1,1,2,
  9712. 0,1,1,0,0,1,1,0,2,2,2,2,2,2,2,2, 0,0,2,2,0,0,1,1,0,0,1,1,0,0,2,2, 0,0,2,2,1,1,2,2,1,1,2,2,0,0,2,2, 0,0,0,0,0,0,0,0,0,0,0,0,2,1,1,2, 0,0,0,2,0,0,0,1,0,0,0,2,0,0,0,1, 0,2,2,2,1,2,2,2,0,2,2,2,1,2,2,2, 0,1,0,1,2,2,2,2,2,2,2,2,2,2,2,2, 0,1,1,1,2,0,1,1,2,2,0,1,2,2,2,0,
  9713. };
  9714. const uint8_t g_bc7_table_anchor_index_second_subset[64] = { 15,15,15,15,15,15,15,15, 15,15,15,15,15,15,15,15, 15, 2, 8, 2, 2, 8, 8,15, 2, 8, 2, 2, 8, 8, 2, 2, 15,15, 6, 8, 2, 8,15,15, 2, 8, 2, 2, 2,15,15, 6, 6, 2, 6, 8,15,15, 2, 2, 15,15,15,15,15, 2, 2,15 };
  9715. const uint8_t g_bc7_table_anchor_index_third_subset_1[64] =
  9716. {
  9717. 3, 3,15,15, 8, 3,15,15, 8, 8, 6, 6, 6, 5, 3, 3, 3, 3, 8,15, 3, 3, 6,10, 5, 8, 8, 6, 8, 5,15,15, 8,15, 3, 5, 6,10, 8,15, 15, 3,15, 5,15,15,15,15, 3,15, 5, 5, 5, 8, 5,10, 5,10, 8,13,15,12, 3, 3
  9718. };
  9719. const uint8_t g_bc7_table_anchor_index_third_subset_2[64] =
  9720. {
  9721. 15, 8, 8, 3,15,15, 3, 8, 15,15,15,15,15,15,15, 8, 15, 8,15, 3,15, 8,15, 8, 3,15, 6,10,15,15,10, 8, 15, 3,15,10,10, 8, 9,10, 6,15, 8,15, 3, 6, 6, 8, 15, 3,15,15,15,15,15,15, 15,15,15,15, 3,15,15, 8
  9722. };
  9723. const uint8_t g_bc7_num_subsets[8] = { 3, 2, 3, 2, 1, 1, 1, 2 };
  9724. const uint8_t g_bc7_partition_bits[8] = { 4, 6, 6, 6, 0, 0, 0, 6 };
  9725. const uint8_t g_bc7_color_index_bitcount[8] = { 3, 3, 2, 2, 2, 2, 4, 2 };
  9726. const uint8_t g_bc7_mode_has_p_bits[8] = { 1, 1, 0, 1, 0, 0, 1, 1 };
  9727. const uint8_t g_bc7_mode_has_shared_p_bits[8] = { 0, 1, 0, 0, 0, 0, 0, 0 };
  9728. const uint8_t g_bc7_color_precision_table[8] = { 4, 6, 5, 7, 5, 7, 7, 5 };
  9729. const int8_t g_bc7_alpha_precision_table[8] = { 0, 0, 0, 0, 6, 8, 7, 5 };
  9730. const uint8_t g_bc7_alpha_index_bitcount[8] = { 0, 0, 0, 0, 3, 2, 4, 2 };
  9731. endpoint_err g_bc7_mode_6_optimal_endpoints[256][2]; // [c][pbit]
  9732. endpoint_err g_bc7_mode_5_optimal_endpoints[256]; // [c]
  9733. static inline void bc7_set_block_bits(uint8_t* pBytes, uint32_t val, uint32_t num_bits, uint32_t* pCur_ofs)
  9734. {
  9735. assert((num_bits <= 32) && (val < (1ULL << num_bits)));
  9736. while (num_bits)
  9737. {
  9738. const uint32_t n = basisu::minimumu(8 - (*pCur_ofs & 7), num_bits);
  9739. pBytes[*pCur_ofs >> 3] |= (uint8_t)(val << (*pCur_ofs & 7));
  9740. val >>= n;
  9741. num_bits -= n;
  9742. *pCur_ofs += n;
  9743. }
  9744. assert(*pCur_ofs <= 128);
  9745. }
  9746. // TODO: Optimize this.
  9747. void encode_bc7_block(void* pBlock, const bc7_optimization_results* pResults)
  9748. {
  9749. const uint32_t best_mode = pResults->m_mode;
  9750. const uint32_t total_subsets = g_bc7_num_subsets[best_mode];
  9751. const uint32_t total_partitions = 1 << g_bc7_partition_bits[best_mode];
  9752. //const uint32_t num_rotations = 1 << g_bc7_rotation_bits[best_mode];
  9753. //const uint32_t num_index_selectors = (best_mode == 4) ? 2 : 1;
  9754. const uint8_t* pPartition;
  9755. if (total_subsets == 1)
  9756. pPartition = &g_bc7_partition1[0];
  9757. else if (total_subsets == 2)
  9758. pPartition = &g_bc7_partition2[pResults->m_partition * 16];
  9759. else
  9760. pPartition = &g_bc7_partition3[pResults->m_partition * 16];
  9761. uint8_t color_selectors[16];
  9762. memcpy(color_selectors, pResults->m_selectors, 16);
  9763. uint8_t alpha_selectors[16];
  9764. memcpy(alpha_selectors, pResults->m_alpha_selectors, 16);
  9765. color_quad_u8 low[3], high[3];
  9766. memcpy(low, pResults->m_low, sizeof(low));
  9767. memcpy(high, pResults->m_high, sizeof(high));
  9768. uint32_t pbits[3][2];
  9769. memcpy(pbits, pResults->m_pbits, sizeof(pbits));
  9770. int anchor[3] = { -1, -1, -1 };
  9771. for (uint32_t k = 0; k < total_subsets; k++)
  9772. {
  9773. uint32_t anchor_index = 0;
  9774. if (k)
  9775. {
  9776. if ((total_subsets == 3) && (k == 1))
  9777. anchor_index = g_bc7_table_anchor_index_third_subset_1[pResults->m_partition];
  9778. else if ((total_subsets == 3) && (k == 2))
  9779. anchor_index = g_bc7_table_anchor_index_third_subset_2[pResults->m_partition];
  9780. else
  9781. anchor_index = g_bc7_table_anchor_index_second_subset[pResults->m_partition];
  9782. }
  9783. anchor[k] = anchor_index;
  9784. const uint32_t color_index_bits = get_bc7_color_index_size(best_mode, pResults->m_index_selector);
  9785. const uint32_t num_color_indices = 1 << color_index_bits;
  9786. if (color_selectors[anchor_index] & (num_color_indices >> 1))
  9787. {
  9788. for (uint32_t i = 0; i < 16; i++)
  9789. if (pPartition[i] == k)
  9790. color_selectors[i] = (uint8_t)((num_color_indices - 1) - color_selectors[i]);
  9791. if (get_bc7_mode_has_seperate_alpha_selectors(best_mode))
  9792. {
  9793. for (uint32_t q = 0; q < 3; q++)
  9794. {
  9795. uint8_t t = low[k].m_c[q];
  9796. low[k].m_c[q] = high[k].m_c[q];
  9797. high[k].m_c[q] = t;
  9798. }
  9799. }
  9800. else
  9801. {
  9802. color_quad_u8 tmp = low[k];
  9803. low[k] = high[k];
  9804. high[k] = tmp;
  9805. }
  9806. if (!g_bc7_mode_has_shared_p_bits[best_mode])
  9807. {
  9808. uint32_t t = pbits[k][0];
  9809. pbits[k][0] = pbits[k][1];
  9810. pbits[k][1] = t;
  9811. }
  9812. }
  9813. if (get_bc7_mode_has_seperate_alpha_selectors(best_mode))
  9814. {
  9815. const uint32_t alpha_index_bits = get_bc7_alpha_index_size(best_mode, pResults->m_index_selector);
  9816. const uint32_t num_alpha_indices = 1 << alpha_index_bits;
  9817. if (alpha_selectors[anchor_index] & (num_alpha_indices >> 1))
  9818. {
  9819. for (uint32_t i = 0; i < 16; i++)
  9820. if (pPartition[i] == k)
  9821. alpha_selectors[i] = (uint8_t)((num_alpha_indices - 1) - alpha_selectors[i]);
  9822. uint8_t t = low[k].m_c[3];
  9823. low[k].m_c[3] = high[k].m_c[3];
  9824. high[k].m_c[3] = t;
  9825. }
  9826. }
  9827. }
  9828. uint8_t* pBlock_bytes = (uint8_t*)(pBlock);
  9829. memset(pBlock_bytes, 0, BC7ENC_BLOCK_SIZE);
  9830. uint32_t cur_bit_ofs = 0;
  9831. bc7_set_block_bits(pBlock_bytes, 1 << best_mode, best_mode + 1, &cur_bit_ofs);
  9832. if ((best_mode == 4) || (best_mode == 5))
  9833. bc7_set_block_bits(pBlock_bytes, pResults->m_rotation, 2, &cur_bit_ofs);
  9834. if (best_mode == 4)
  9835. bc7_set_block_bits(pBlock_bytes, pResults->m_index_selector, 1, &cur_bit_ofs);
  9836. if (total_partitions > 1)
  9837. bc7_set_block_bits(pBlock_bytes, pResults->m_partition, (total_partitions == 64) ? 6 : 4, &cur_bit_ofs);
  9838. const uint32_t total_comps = (best_mode >= 4) ? 4 : 3;
  9839. for (uint32_t comp = 0; comp < total_comps; comp++)
  9840. {
  9841. for (uint32_t subset = 0; subset < total_subsets; subset++)
  9842. {
  9843. bc7_set_block_bits(pBlock_bytes, low[subset].m_c[comp], (comp == 3) ? g_bc7_alpha_precision_table[best_mode] : g_bc7_color_precision_table[best_mode], &cur_bit_ofs);
  9844. bc7_set_block_bits(pBlock_bytes, high[subset].m_c[comp], (comp == 3) ? g_bc7_alpha_precision_table[best_mode] : g_bc7_color_precision_table[best_mode], &cur_bit_ofs);
  9845. }
  9846. }
  9847. if (g_bc7_mode_has_p_bits[best_mode])
  9848. {
  9849. for (uint32_t subset = 0; subset < total_subsets; subset++)
  9850. {
  9851. bc7_set_block_bits(pBlock_bytes, pbits[subset][0], 1, &cur_bit_ofs);
  9852. if (!g_bc7_mode_has_shared_p_bits[best_mode])
  9853. bc7_set_block_bits(pBlock_bytes, pbits[subset][1], 1, &cur_bit_ofs);
  9854. }
  9855. }
  9856. for (uint32_t y = 0; y < 4; y++)
  9857. {
  9858. for (uint32_t x = 0; x < 4; x++)
  9859. {
  9860. int idx = x + y * 4;
  9861. uint32_t n = pResults->m_index_selector ? get_bc7_alpha_index_size(best_mode, pResults->m_index_selector) : get_bc7_color_index_size(best_mode, pResults->m_index_selector);
  9862. if ((idx == anchor[0]) || (idx == anchor[1]) || (idx == anchor[2]))
  9863. n--;
  9864. bc7_set_block_bits(pBlock_bytes, pResults->m_index_selector ? alpha_selectors[idx] : color_selectors[idx], n, &cur_bit_ofs);
  9865. }
  9866. }
  9867. if (get_bc7_mode_has_seperate_alpha_selectors(best_mode))
  9868. {
  9869. for (uint32_t y = 0; y < 4; y++)
  9870. {
  9871. for (uint32_t x = 0; x < 4; x++)
  9872. {
  9873. int idx = x + y * 4;
  9874. uint32_t n = pResults->m_index_selector ? get_bc7_color_index_size(best_mode, pResults->m_index_selector) : get_bc7_alpha_index_size(best_mode, pResults->m_index_selector);
  9875. if ((idx == anchor[0]) || (idx == anchor[1]) || (idx == anchor[2]))
  9876. n--;
  9877. bc7_set_block_bits(pBlock_bytes, pResults->m_index_selector ? color_selectors[idx] : alpha_selectors[idx], n, &cur_bit_ofs);
  9878. }
  9879. }
  9880. }
  9881. assert(cur_bit_ofs == 128);
  9882. }
  9883. // ASTC
  9884. static inline void astc_set_bits_1_to_9(uint32_t* pDst, int& bit_offset, uint32_t code, uint32_t codesize)
  9885. {
  9886. uint8_t* pBuf = reinterpret_cast<uint8_t*>(pDst);
  9887. assert(codesize <= 9);
  9888. if (codesize)
  9889. {
  9890. uint32_t byte_bit_offset = bit_offset & 7;
  9891. uint32_t val = code << byte_bit_offset;
  9892. uint32_t index = bit_offset >> 3;
  9893. pBuf[index] |= (uint8_t)val;
  9894. if (codesize > (8 - byte_bit_offset))
  9895. pBuf[index + 1] |= (uint8_t)(val >> 8);
  9896. bit_offset += codesize;
  9897. }
  9898. }
  9899. void pack_astc_solid_block(void* pDst_block, const color32& color)
  9900. {
  9901. uint32_t r = color[0], g = color[1], b = color[2];
  9902. uint32_t a = color[3];
  9903. uint32_t* pOutput = static_cast<uint32_t*>(pDst_block);
  9904. uint8_t* pBytes = reinterpret_cast<uint8_t*>(pDst_block);
  9905. pBytes[0] = 0xfc; pBytes[1] = 0xfd; pBytes[2] = 0xff; pBytes[3] = 0xff;
  9906. pOutput[1] = 0xffffffff;
  9907. pOutput[2] = 0;
  9908. pOutput[3] = 0;
  9909. int bit_pos = 64;
  9910. astc_set_bits(reinterpret_cast<uint32_t*>(pDst_block), bit_pos, r | (r << 8), 16);
  9911. astc_set_bits(reinterpret_cast<uint32_t*>(pDst_block), bit_pos, g | (g << 8), 16);
  9912. astc_set_bits(reinterpret_cast<uint32_t*>(pDst_block), bit_pos, b | (b << 8), 16);
  9913. astc_set_bits(reinterpret_cast<uint32_t*>(pDst_block), bit_pos, a | (a << 8), 16);
  9914. }
  9915. // See 23.21 https://www.khronos.org/registry/DataFormat/specs/1.3/dataformat.1.3.inline.html#_partition_pattern_generation
  9916. #ifdef _DEBUG
  9917. static inline uint32_t astc_hash52(uint32_t v)
  9918. {
  9919. uint32_t p = v;
  9920. p ^= p >> 15; p -= p << 17; p += p << 7; p += p << 4;
  9921. p ^= p >> 5; p += p << 16; p ^= p >> 7; p ^= p >> 3;
  9922. p ^= p << 6; p ^= p >> 17;
  9923. return p;
  9924. }
  9925. int astc_compute_texel_partition(int seed, int x, int y, int z, int partitioncount, bool small_block)
  9926. {
  9927. if (small_block)
  9928. {
  9929. x <<= 1; y <<= 1; z <<= 1;
  9930. }
  9931. seed += (partitioncount - 1) * 1024;
  9932. uint32_t rnum = astc_hash52(seed);
  9933. uint8_t seed1 = rnum & 0xF;
  9934. uint8_t seed2 = (rnum >> 4) & 0xF;
  9935. uint8_t seed3 = (rnum >> 8) & 0xF;
  9936. uint8_t seed4 = (rnum >> 12) & 0xF;
  9937. uint8_t seed5 = (rnum >> 16) & 0xF;
  9938. uint8_t seed6 = (rnum >> 20) & 0xF;
  9939. uint8_t seed7 = (rnum >> 24) & 0xF;
  9940. uint8_t seed8 = (rnum >> 28) & 0xF;
  9941. uint8_t seed9 = (rnum >> 18) & 0xF;
  9942. uint8_t seed10 = (rnum >> 22) & 0xF;
  9943. uint8_t seed11 = (rnum >> 26) & 0xF;
  9944. uint8_t seed12 = ((rnum >> 30) | (rnum << 2)) & 0xF;
  9945. seed1 *= seed1; seed2 *= seed2;
  9946. seed3 *= seed3; seed4 *= seed4;
  9947. seed5 *= seed5; seed6 *= seed6;
  9948. seed7 *= seed7; seed8 *= seed8;
  9949. seed9 *= seed9; seed10 *= seed10;
  9950. seed11 *= seed11; seed12 *= seed12;
  9951. int sh1, sh2, sh3;
  9952. if (seed & 1)
  9953. {
  9954. sh1 = (seed & 2 ? 4 : 5); sh2 = (partitioncount == 3 ? 6 : 5);
  9955. }
  9956. else
  9957. {
  9958. sh1 = (partitioncount == 3 ? 6 : 5); sh2 = (seed & 2 ? 4 : 5);
  9959. }
  9960. sh3 = (seed & 0x10) ? sh1 : sh2;
  9961. seed1 >>= sh1; seed2 >>= sh2; seed3 >>= sh1; seed4 >>= sh2;
  9962. seed5 >>= sh1; seed6 >>= sh2; seed7 >>= sh1; seed8 >>= sh2;
  9963. seed9 >>= sh3; seed10 >>= sh3; seed11 >>= sh3; seed12 >>= sh3;
  9964. int a = seed1 * x + seed2 * y + seed11 * z + (rnum >> 14);
  9965. int b = seed3 * x + seed4 * y + seed12 * z + (rnum >> 10);
  9966. int c = seed5 * x + seed6 * y + seed9 * z + (rnum >> 6);
  9967. int d = seed7 * x + seed8 * y + seed10 * z + (rnum >> 2);
  9968. a &= 0x3F; b &= 0x3F; c &= 0x3F; d &= 0x3F;
  9969. if (partitioncount < 4) d = 0;
  9970. if (partitioncount < 3) c = 0;
  9971. if (a >= b && a >= c && a >= d)
  9972. return 0;
  9973. else if (b >= c && b >= d)
  9974. return 1;
  9975. else if (c >= d)
  9976. return 2;
  9977. else
  9978. return 3;
  9979. }
  9980. #endif
  9981. static const uint8_t g_astc_quint_encode[125] =
  9982. {
  9983. 0, 1, 2, 3, 4, 8, 9, 10, 11, 12, 16, 17, 18, 19, 20, 24, 25, 26, 27, 28, 5, 13, 21, 29, 6, 32, 33, 34, 35, 36, 40, 41, 42, 43, 44, 48, 49, 50, 51, 52, 56, 57,
  9984. 58, 59, 60, 37, 45, 53, 61, 14, 64, 65, 66, 67, 68, 72, 73, 74, 75, 76, 80, 81, 82, 83, 84, 88, 89, 90, 91, 92, 69, 77, 85, 93, 22, 96, 97, 98, 99, 100, 104,
  9985. 105, 106, 107, 108, 112, 113, 114, 115, 116, 120, 121, 122, 123, 124, 101, 109, 117, 125, 30, 102, 103, 70, 71, 38, 110, 111, 78, 79, 46, 118, 119, 86, 87, 54,
  9986. 126, 127, 94, 95, 62, 39, 47, 55, 63, 31
  9987. };
  9988. // Encodes 3 values to output, usable for any range that uses quints and bits
  9989. static inline void astc_encode_quints(uint32_t* pOutput, const uint8_t* pValues, int& bit_pos, int n)
  9990. {
  9991. // First extract the trits and the bits from the 5 input values
  9992. int quints = 0, bits[3];
  9993. const uint32_t bit_mask = (1 << n) - 1;
  9994. for (int i = 0; i < 3; i++)
  9995. {
  9996. static const int s_muls[3] = { 1, 5, 25 };
  9997. const int t = pValues[i] >> n;
  9998. quints += t * s_muls[i];
  9999. bits[i] = pValues[i] & bit_mask;
  10000. }
  10001. // Encode the quints, by inverting the bit manipulations done by the decoder, converting 3 quints into 7-bits.
  10002. // See https://www.khronos.org/registry/DataFormat/specs/1.2/dataformat.1.2.html#astc-integer-sequence-encoding
  10003. assert(quints < 125);
  10004. const int T = g_astc_quint_encode[quints];
  10005. // Now interleave the 7 encoded quint bits with the bits to form the encoded output. See table 95-96.
  10006. astc_set_bits(pOutput, bit_pos, bits[0] | (astc_extract_bits(T, 0, 2) << n) | (bits[1] << (3 + n)) | (astc_extract_bits(T, 3, 4) << (3 + n * 2)) |
  10007. (bits[2] << (5 + n * 2)) | (astc_extract_bits(T, 5, 6) << (5 + n * 3)), 7 + n * 3);
  10008. }
  10009. // Packs values using ASTC's BISE to output buffer.
  10010. static void astc_pack_bise(uint32_t* pDst, const uint8_t* pSrc_vals, int bit_pos, int num_vals, int range)
  10011. {
  10012. uint32_t temp[5] = { 0, 0, 0, 0, 0 };
  10013. const int num_bits = g_astc_bise_range_table[range][0];
  10014. int group_size = 0;
  10015. if (g_astc_bise_range_table[range][1])
  10016. group_size = 5;
  10017. else if (g_astc_bise_range_table[range][2])
  10018. group_size = 3;
  10019. if (group_size)
  10020. {
  10021. // Range has trits or quints - pack each group of 5 or 3 values
  10022. const int total_groups = (group_size == 5) ? ((num_vals + 4) / 5) : ((num_vals + 2) / 3);
  10023. for (int group_index = 0; group_index < total_groups; group_index++)
  10024. {
  10025. uint8_t vals[5] = { 0, 0, 0, 0, 0 };
  10026. const int limit = basisu::minimum(group_size, num_vals - group_index * group_size);
  10027. for (int i = 0; i < limit; i++)
  10028. vals[i] = pSrc_vals[group_index * group_size + i];
  10029. if (group_size == 5)
  10030. astc_encode_trits(temp, vals, bit_pos, num_bits);
  10031. else
  10032. astc_encode_quints(temp, vals, bit_pos, num_bits);
  10033. }
  10034. }
  10035. else
  10036. {
  10037. for (int i = 0; i < num_vals; i++)
  10038. astc_set_bits_1_to_9(temp, bit_pos, pSrc_vals[i], num_bits);
  10039. }
  10040. pDst[0] |= temp[0]; pDst[1] |= temp[1];
  10041. pDst[2] |= temp[2]; pDst[3] |= temp[3];
  10042. }
  10043. const uint32_t ASTC_BLOCK_MODE_BITS = 11;
  10044. const uint32_t ASTC_PART_BITS = 2;
  10045. const uint32_t ASTC_CEM_BITS = 4;
  10046. const uint32_t ASTC_PARTITION_INDEX_BITS = 10;
  10047. const uint32_t ASTC_CCS_BITS = 2;
  10048. const uint32_t g_uastc_mode_astc_block_mode[TOTAL_UASTC_MODES] = { 0x242, 0x42, 0x53, 0x42, 0x42, 0x53, 0x442, 0x42, 0, 0x42, 0x242, 0x442, 0x53, 0x441, 0x42, 0x242, 0x42, 0x442, 0x253 };
  10049. bool pack_astc_block(uint32_t* pDst, const astc_block_desc* pBlock, uint32_t uastc_mode)
  10050. {
  10051. assert(uastc_mode < TOTAL_UASTC_MODES);
  10052. uint8_t* pDst_bytes = reinterpret_cast<uint8_t*>(pDst);
  10053. const int total_weights = pBlock->m_dual_plane ? 32 : 16;
  10054. // Set mode bits - see Table 146-147
  10055. uint32_t mode = g_uastc_mode_astc_block_mode[uastc_mode];
  10056. pDst_bytes[0] = (uint8_t)mode;
  10057. pDst_bytes[1] = (uint8_t)(mode >> 8);
  10058. memset(pDst_bytes + 2, 0, 16 - 2);
  10059. int bit_pos = ASTC_BLOCK_MODE_BITS;
  10060. // We only support 1-5 bit weight indices
  10061. assert(!g_astc_bise_range_table[pBlock->m_weight_range][1] && !g_astc_bise_range_table[pBlock->m_weight_range][2]);
  10062. const int bits_per_weight = g_astc_bise_range_table[pBlock->m_weight_range][0];
  10063. // See table 143 - PART
  10064. astc_set_bits_1_to_9(pDst, bit_pos, pBlock->m_subsets - 1, ASTC_PART_BITS);
  10065. if (pBlock->m_subsets == 1)
  10066. astc_set_bits_1_to_9(pDst, bit_pos, pBlock->m_cem, ASTC_CEM_BITS);
  10067. else
  10068. {
  10069. // See table 145
  10070. astc_set_bits(pDst, bit_pos, pBlock->m_partition_seed, ASTC_PARTITION_INDEX_BITS);
  10071. // Table 150 - we assume all CEM's are equal, so write 2 0's along with the CEM
  10072. astc_set_bits_1_to_9(pDst, bit_pos, (pBlock->m_cem << 2) & 63, ASTC_CEM_BITS + 2);
  10073. }
  10074. if (pBlock->m_dual_plane)
  10075. {
  10076. const int total_weight_bits = total_weights * bits_per_weight;
  10077. // See Illegal Encodings 23.24
  10078. // https://www.khronos.org/registry/DataFormat/specs/1.3/dataformat.1.3.inline.html#_illegal_encodings
  10079. assert((total_weight_bits >= 24) && (total_weight_bits <= 96));
  10080. int ccs_bit_pos = 128 - total_weight_bits - ASTC_CCS_BITS;
  10081. astc_set_bits_1_to_9(pDst, ccs_bit_pos, pBlock->m_ccs, ASTC_CCS_BITS);
  10082. }
  10083. const int num_cem_pairs = (1 + (pBlock->m_cem >> 2)) * pBlock->m_subsets;
  10084. assert(num_cem_pairs <= 9);
  10085. astc_pack_bise(pDst, pBlock->m_endpoints, bit_pos, num_cem_pairs * 2, g_uastc_mode_endpoint_ranges[uastc_mode]);
  10086. // Write the weight bits in reverse bit order.
  10087. switch (bits_per_weight)
  10088. {
  10089. case 1:
  10090. {
  10091. const uint32_t N = 1;
  10092. for (int i = 0; i < total_weights; i++)
  10093. {
  10094. const uint32_t ofs = 128 - N - i;
  10095. assert((ofs >> 3) < 16);
  10096. pDst_bytes[ofs >> 3] |= (pBlock->m_weights[i] << (ofs & 7));
  10097. }
  10098. break;
  10099. }
  10100. case 2:
  10101. {
  10102. const uint32_t N = 2;
  10103. for (int i = 0; i < total_weights; i++)
  10104. {
  10105. static const uint8_t s_reverse_bits2[4] = { 0, 2, 1, 3 };
  10106. const uint32_t ofs = 128 - N - (i * N);
  10107. assert((ofs >> 3) < 16);
  10108. pDst_bytes[ofs >> 3] |= (s_reverse_bits2[pBlock->m_weights[i]] << (ofs & 7));
  10109. }
  10110. break;
  10111. }
  10112. case 3:
  10113. {
  10114. const uint32_t N = 3;
  10115. for (int i = 0; i < total_weights; i++)
  10116. {
  10117. static const uint8_t s_reverse_bits3[8] = { 0, 4, 2, 6, 1, 5, 3, 7 };
  10118. const uint32_t ofs = 128 - N - (i * N);
  10119. const uint32_t rev = s_reverse_bits3[pBlock->m_weights[i]] << (ofs & 7);
  10120. uint32_t index = ofs >> 3;
  10121. assert(index < 16);
  10122. pDst_bytes[index++] |= rev & 0xFF;
  10123. if (index < 16)
  10124. pDst_bytes[index++] |= (rev >> 8);
  10125. }
  10126. break;
  10127. }
  10128. case 4:
  10129. {
  10130. const uint32_t N = 4;
  10131. for (int i = 0; i < total_weights; i++)
  10132. {
  10133. static const uint8_t s_reverse_bits4[16] = { 0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15 };
  10134. const int ofs = 128 - N - (i * N);
  10135. assert(ofs >= 0 && (ofs >> 3) < 16);
  10136. pDst_bytes[ofs >> 3] |= (s_reverse_bits4[pBlock->m_weights[i]] << (ofs & 7));
  10137. }
  10138. break;
  10139. }
  10140. case 5:
  10141. {
  10142. const uint32_t N = 5;
  10143. for (int i = 0; i < total_weights; i++)
  10144. {
  10145. static const uint8_t s_reverse_bits5[32] = { 0, 16, 8, 24, 4, 20, 12, 28, 2, 18, 10, 26, 6, 22, 14, 30, 1, 17, 9, 25, 5, 21, 13, 29, 3, 19, 11, 27, 7, 23, 15, 31 };
  10146. const uint32_t ofs = 128 - N - (i * N);
  10147. const uint32_t rev = s_reverse_bits5[pBlock->m_weights[i]] << (ofs & 7);
  10148. uint32_t index = ofs >> 3;
  10149. assert(index < 16);
  10150. pDst_bytes[index++] |= rev & 0xFF;
  10151. if (index < 16)
  10152. pDst_bytes[index++] |= (rev >> 8);
  10153. }
  10154. break;
  10155. }
  10156. default:
  10157. assert(0);
  10158. break;
  10159. }
  10160. return true;
  10161. }
  10162. const uint8_t* get_anchor_indices(uint32_t subsets, uint32_t mode, uint32_t common_pattern, const uint8_t*& pPartition_pattern)
  10163. {
  10164. const uint8_t* pSubset_anchor_indices = g_zero_pattern;
  10165. pPartition_pattern = g_zero_pattern;
  10166. if (subsets >= 2)
  10167. {
  10168. if (subsets == 3)
  10169. {
  10170. pPartition_pattern = &g_astc_bc7_patterns3[common_pattern][0];
  10171. pSubset_anchor_indices = &g_astc_bc7_pattern3_anchors[common_pattern][0];
  10172. }
  10173. else if (mode == 7)
  10174. {
  10175. pPartition_pattern = &g_bc7_3_astc2_patterns2[common_pattern][0];
  10176. pSubset_anchor_indices = &g_bc7_3_astc2_patterns2_anchors[common_pattern][0];
  10177. }
  10178. else
  10179. {
  10180. pPartition_pattern = &g_astc_bc7_patterns2[common_pattern][0];
  10181. pSubset_anchor_indices = &g_astc_bc7_pattern2_anchors[common_pattern][0];
  10182. }
  10183. }
  10184. return pSubset_anchor_indices;
  10185. }
  10186. static inline uint32_t read_bit(const uint8_t* pBuf, uint32_t& bit_offset)
  10187. {
  10188. uint32_t byte_bits = pBuf[bit_offset >> 3] >> (bit_offset & 7);
  10189. bit_offset += 1;
  10190. return byte_bits & 1;
  10191. }
  10192. static inline uint32_t read_bits1_to_9(const uint8_t* pBuf, uint32_t& bit_offset, uint32_t codesize)
  10193. {
  10194. assert(codesize <= 9);
  10195. if (!codesize)
  10196. return 0;
  10197. if ((BASISD_IS_BIG_ENDIAN) || (!BASISD_USE_UNALIGNED_WORD_READS) || (bit_offset >= 112))
  10198. {
  10199. const uint8_t* pBytes = &pBuf[bit_offset >> 3U];
  10200. uint32_t byte_bit_offset = bit_offset & 7U;
  10201. uint32_t bits = pBytes[0] >> byte_bit_offset;
  10202. uint32_t bits_read = basisu::minimum<int>(codesize, 8 - byte_bit_offset);
  10203. uint32_t bits_remaining = codesize - bits_read;
  10204. if (bits_remaining)
  10205. bits |= ((uint32_t)pBytes[1]) << bits_read;
  10206. bit_offset += codesize;
  10207. return bits & ((1U << codesize) - 1U);
  10208. }
  10209. uint32_t byte_bit_offset = bit_offset & 7U;
  10210. const uint16_t w = *(const uint16_t *)(&pBuf[bit_offset >> 3U]);
  10211. bit_offset += codesize;
  10212. return (w >> byte_bit_offset) & ((1U << codesize) - 1U);
  10213. }
  10214. inline uint64_t read_bits64(const uint8_t* pBuf, uint32_t& bit_offset, uint32_t codesize)
  10215. {
  10216. assert(codesize <= 64U);
  10217. uint64_t bits = 0;
  10218. uint32_t total_bits = 0;
  10219. while (total_bits < codesize)
  10220. {
  10221. uint32_t byte_bit_offset = bit_offset & 7U;
  10222. uint32_t bits_to_read = basisu::minimum<int>(codesize - total_bits, 8U - byte_bit_offset);
  10223. uint32_t byte_bits = pBuf[bit_offset >> 3U] >> byte_bit_offset;
  10224. byte_bits &= ((1U << bits_to_read) - 1U);
  10225. bits |= ((uint64_t)(byte_bits) << total_bits);
  10226. total_bits += bits_to_read;
  10227. bit_offset += bits_to_read;
  10228. }
  10229. return bits;
  10230. }
  10231. static inline uint32_t read_bits1_to_9_fst(const uint8_t* pBuf, uint32_t& bit_offset, uint32_t codesize)
  10232. {
  10233. assert(codesize <= 9);
  10234. if (!codesize)
  10235. return 0;
  10236. assert(bit_offset < 112);
  10237. if ((BASISD_IS_BIG_ENDIAN) || (!BASISD_USE_UNALIGNED_WORD_READS))
  10238. {
  10239. const uint8_t* pBytes = &pBuf[bit_offset >> 3U];
  10240. uint32_t byte_bit_offset = bit_offset & 7U;
  10241. uint32_t bits = pBytes[0] >> byte_bit_offset;
  10242. uint32_t bits_read = basisu::minimum<int>(codesize, 8 - byte_bit_offset);
  10243. uint32_t bits_remaining = codesize - bits_read;
  10244. if (bits_remaining)
  10245. bits |= ((uint32_t)pBytes[1]) << bits_read;
  10246. bit_offset += codesize;
  10247. return bits & ((1U << codesize) - 1U);
  10248. }
  10249. uint32_t byte_bit_offset = bit_offset & 7U;
  10250. const uint16_t w = *(const uint16_t*)(&pBuf[bit_offset >> 3U]);
  10251. bit_offset += codesize;
  10252. return (w >> byte_bit_offset)& ((1U << codesize) - 1U);
  10253. }
  10254. bool unpack_uastc(const uastc_block& blk, unpacked_uastc_block& unpacked, bool blue_contract_check, bool read_hints)
  10255. {
  10256. //memset(&unpacked, 0, sizeof(unpacked));
  10257. #if 0
  10258. uint8_t table[128];
  10259. memset(table, 0xFF, sizeof(table));
  10260. {
  10261. for (uint32_t mode = 0; mode <= TOTAL_UASTC_MODES; mode++)
  10262. {
  10263. const uint32_t code = g_uastc_mode_huff_codes[mode][0];
  10264. const uint32_t codesize = g_uastc_mode_huff_codes[mode][1];
  10265. table[code] = mode;
  10266. uint32_t bits_left = 7 - codesize;
  10267. for (uint32_t i = 0; i < (1 << bits_left); i++)
  10268. table[code | (i << codesize)] = mode;
  10269. }
  10270. for (uint32_t i = 0; i < 128; i++)
  10271. printf("%u,", table[i]);
  10272. exit(0);
  10273. }
  10274. #endif
  10275. const int mode = g_uastc_huff_modes[blk.m_bytes[0] & 127];
  10276. if (mode >= (int)TOTAL_UASTC_MODES)
  10277. return false;
  10278. unpacked.m_mode = mode;
  10279. uint32_t bit_ofs = g_uastc_mode_huff_codes[mode][1];
  10280. if (mode == UASTC_MODE_INDEX_SOLID_COLOR)
  10281. {
  10282. unpacked.m_solid_color.r = (uint8_t)read_bits1_to_9_fst(blk.m_bytes, bit_ofs, 8);
  10283. unpacked.m_solid_color.g = (uint8_t)read_bits1_to_9_fst(blk.m_bytes, bit_ofs, 8);
  10284. unpacked.m_solid_color.b = (uint8_t)read_bits1_to_9_fst(blk.m_bytes, bit_ofs, 8);
  10285. unpacked.m_solid_color.a = (uint8_t)read_bits1_to_9_fst(blk.m_bytes, bit_ofs, 8);
  10286. if (read_hints)
  10287. {
  10288. unpacked.m_etc1_flip = false;
  10289. unpacked.m_etc1_diff = read_bit(blk.m_bytes, bit_ofs) != 0;
  10290. unpacked.m_etc1_inten0 = (uint32_t)read_bits1_to_9_fst(blk.m_bytes, bit_ofs, 3);
  10291. unpacked.m_etc1_inten1 = 0;
  10292. unpacked.m_etc1_selector = (uint32_t)read_bits1_to_9_fst(blk.m_bytes, bit_ofs, 2);
  10293. unpacked.m_etc1_r = (uint32_t)read_bits1_to_9_fst(blk.m_bytes, bit_ofs, 5);
  10294. unpacked.m_etc1_g = (uint32_t)read_bits1_to_9_fst(blk.m_bytes, bit_ofs, 5);
  10295. unpacked.m_etc1_b = (uint32_t)read_bits1_to_9_fst(blk.m_bytes, bit_ofs, 5);
  10296. unpacked.m_etc1_bias = 0;
  10297. unpacked.m_etc2_hints = 0;
  10298. }
  10299. return true;
  10300. }
  10301. if (read_hints)
  10302. {
  10303. if (g_uastc_mode_has_bc1_hint0[mode])
  10304. unpacked.m_bc1_hint0 = read_bit(blk.m_bytes, bit_ofs) != 0;
  10305. else
  10306. unpacked.m_bc1_hint0 = false;
  10307. if (g_uastc_mode_has_bc1_hint1[mode])
  10308. unpacked.m_bc1_hint1 = read_bit(blk.m_bytes, bit_ofs) != 0;
  10309. else
  10310. unpacked.m_bc1_hint1 = false;
  10311. unpacked.m_etc1_flip = read_bit(blk.m_bytes, bit_ofs) != 0;
  10312. unpacked.m_etc1_diff = read_bit(blk.m_bytes, bit_ofs) != 0;
  10313. unpacked.m_etc1_inten0 = (uint32_t)read_bits1_to_9_fst(blk.m_bytes, bit_ofs, 3);
  10314. unpacked.m_etc1_inten1 = (uint32_t)read_bits1_to_9_fst(blk.m_bytes, bit_ofs, 3);
  10315. if (g_uastc_mode_has_etc1_bias[mode])
  10316. unpacked.m_etc1_bias = (uint32_t)read_bits1_to_9_fst(blk.m_bytes, bit_ofs, 5);
  10317. else
  10318. unpacked.m_etc1_bias = 0;
  10319. if (g_uastc_mode_has_alpha[mode])
  10320. {
  10321. unpacked.m_etc2_hints = (uint32_t)read_bits1_to_9_fst(blk.m_bytes, bit_ofs, 8);
  10322. //assert(unpacked.m_etc2_hints > 0);
  10323. }
  10324. else
  10325. unpacked.m_etc2_hints = 0;
  10326. }
  10327. else
  10328. bit_ofs += g_uastc_mode_total_hint_bits[mode];
  10329. uint32_t subsets = 1;
  10330. switch (mode)
  10331. {
  10332. case 2:
  10333. case 4:
  10334. case 7:
  10335. case 9:
  10336. case 16:
  10337. unpacked.m_common_pattern = (uint32_t)read_bits1_to_9_fst(blk.m_bytes, bit_ofs, 5);
  10338. subsets = 2;
  10339. break;
  10340. case 3:
  10341. unpacked.m_common_pattern = (uint32_t)read_bits1_to_9_fst(blk.m_bytes, bit_ofs, 4);
  10342. subsets = 3;
  10343. break;
  10344. default:
  10345. break;
  10346. }
  10347. uint32_t part_seed = 0;
  10348. switch (mode)
  10349. {
  10350. case 2:
  10351. case 4:
  10352. case 9:
  10353. case 16:
  10354. if (unpacked.m_common_pattern >= TOTAL_ASTC_BC7_COMMON_PARTITIONS2)
  10355. return false;
  10356. part_seed = g_astc_bc7_common_partitions2[unpacked.m_common_pattern].m_astc;
  10357. break;
  10358. case 3:
  10359. if (unpacked.m_common_pattern >= TOTAL_ASTC_BC7_COMMON_PARTITIONS3)
  10360. return false;
  10361. part_seed = g_astc_bc7_common_partitions3[unpacked.m_common_pattern].m_astc;
  10362. break;
  10363. case 7:
  10364. if (unpacked.m_common_pattern >= TOTAL_BC7_3_ASTC2_COMMON_PARTITIONS)
  10365. return false;
  10366. part_seed = g_bc7_3_astc2_common_partitions[unpacked.m_common_pattern].m_astc2;
  10367. break;
  10368. default:
  10369. break;
  10370. }
  10371. uint32_t total_planes = 1;
  10372. switch (mode)
  10373. {
  10374. case 6:
  10375. case 11:
  10376. case 13:
  10377. unpacked.m_astc.m_ccs = (int)read_bits1_to_9_fst(blk.m_bytes, bit_ofs, 2);
  10378. total_planes = 2;
  10379. break;
  10380. case 17:
  10381. unpacked.m_astc.m_ccs = 3;
  10382. total_planes = 2;
  10383. break;
  10384. default:
  10385. break;
  10386. }
  10387. unpacked.m_astc.m_dual_plane = (total_planes == 2);
  10388. unpacked.m_astc.m_subsets = subsets;
  10389. unpacked.m_astc.m_partition_seed = part_seed;
  10390. const uint32_t total_comps = g_uastc_mode_comps[mode];
  10391. const uint32_t weight_bits = g_uastc_mode_weight_bits[mode];
  10392. unpacked.m_astc.m_weight_range = g_uastc_mode_weight_ranges[mode];
  10393. const uint32_t total_values = total_comps * 2 * subsets;
  10394. const uint32_t endpoint_range = g_uastc_mode_endpoint_ranges[mode];
  10395. const uint32_t cem = g_uastc_mode_cem[mode];
  10396. unpacked.m_astc.m_cem = cem;
  10397. const uint32_t ep_bits = g_astc_bise_range_table[endpoint_range][0];
  10398. const uint32_t ep_trits = g_astc_bise_range_table[endpoint_range][1];
  10399. const uint32_t ep_quints = g_astc_bise_range_table[endpoint_range][2];
  10400. uint32_t total_tqs = 0;
  10401. uint32_t bundle_size = 0, mul = 0;
  10402. if (ep_trits)
  10403. {
  10404. total_tqs = (total_values + 4) / 5;
  10405. bundle_size = 5;
  10406. mul = 3;
  10407. }
  10408. else if (ep_quints)
  10409. {
  10410. total_tqs = (total_values + 2) / 3;
  10411. bundle_size = 3;
  10412. mul = 5;
  10413. }
  10414. uint32_t tq_values[8];
  10415. for (uint32_t i = 0; i < total_tqs; i++)
  10416. {
  10417. uint32_t num_bits = ep_trits ? 8 : 7;
  10418. if (i == (total_tqs - 1))
  10419. {
  10420. uint32_t num_remaining = total_values - (total_tqs - 1) * bundle_size;
  10421. if (ep_trits)
  10422. {
  10423. switch (num_remaining)
  10424. {
  10425. case 1: num_bits = 2; break;
  10426. case 2: num_bits = 4; break;
  10427. case 3: num_bits = 5; break;
  10428. case 4: num_bits = 7; break;
  10429. default: break;
  10430. }
  10431. }
  10432. else if (ep_quints)
  10433. {
  10434. switch (num_remaining)
  10435. {
  10436. case 1: num_bits = 3; break;
  10437. case 2: num_bits = 5; break;
  10438. default: break;
  10439. }
  10440. }
  10441. }
  10442. tq_values[i] = (uint32_t)read_bits1_to_9_fst(blk.m_bytes, bit_ofs, num_bits);
  10443. } // i
  10444. uint32_t accum = 0;
  10445. uint32_t accum_remaining = 0;
  10446. uint32_t next_tq_index = 0;
  10447. for (uint32_t i = 0; i < total_values; i++)
  10448. {
  10449. uint32_t value = (uint32_t)read_bits1_to_9_fst(blk.m_bytes, bit_ofs, ep_bits);
  10450. if (total_tqs)
  10451. {
  10452. if (!accum_remaining)
  10453. {
  10454. assert(next_tq_index < total_tqs);
  10455. accum = tq_values[next_tq_index++];
  10456. accum_remaining = bundle_size;
  10457. }
  10458. // TODO: Optimize with tables
  10459. uint32_t v = accum % mul;
  10460. accum /= mul;
  10461. accum_remaining--;
  10462. value |= (v << ep_bits);
  10463. }
  10464. unpacked.m_astc.m_endpoints[i] = (uint8_t)value;
  10465. }
  10466. const uint8_t* pPartition_pattern;
  10467. const uint8_t* pSubset_anchor_indices = get_anchor_indices(subsets, mode, unpacked.m_common_pattern, pPartition_pattern);
  10468. #ifdef _DEBUG
  10469. for (uint32_t i = 0; i < 16; i++)
  10470. assert(pPartition_pattern[i] == astc_compute_texel_partition(part_seed, i & 3, i >> 2, 0, subsets, true));
  10471. for (uint32_t subset_index = 0; subset_index < subsets; subset_index++)
  10472. {
  10473. uint32_t anchor_index = 0;
  10474. for (uint32_t i = 0; i < 16; i++)
  10475. {
  10476. if (pPartition_pattern[i] == subset_index)
  10477. {
  10478. anchor_index = i;
  10479. break;
  10480. }
  10481. }
  10482. assert(pSubset_anchor_indices[subset_index] == anchor_index);
  10483. }
  10484. #endif
  10485. #if 0
  10486. const uint32_t total_planes_shift = total_planes - 1;
  10487. for (uint32_t i = 0; i < 16 * total_planes; i++)
  10488. {
  10489. uint32_t num_bits = weight_bits;
  10490. for (uint32_t s = 0; s < subsets; s++)
  10491. {
  10492. if (pSubset_anchor_indices[s] == (i >> total_planes_shift))
  10493. {
  10494. num_bits--;
  10495. break;
  10496. }
  10497. }
  10498. unpacked.m_astc.m_weights[i] = (uint8_t)read_bits1_to_9(blk.m_bytes, bit_ofs, num_bits);
  10499. }
  10500. #endif
  10501. if (mode == 18)
  10502. {
  10503. // Mode 18 is the only mode with more than 64 weight bits.
  10504. for (uint32_t i = 0; i < 16; i++)
  10505. unpacked.m_astc.m_weights[i] = (uint8_t)read_bits1_to_9(blk.m_bytes, bit_ofs, i ? weight_bits : (weight_bits - 1));
  10506. }
  10507. else
  10508. {
  10509. // All other modes have <= 64 weight bits.
  10510. uint64_t bits;
  10511. // Read the weight bits
  10512. if ((BASISD_IS_BIG_ENDIAN) || (!BASISD_USE_UNALIGNED_WORD_READS))
  10513. bits = read_bits64(blk.m_bytes, bit_ofs, basisu::minimum<int>(64, 128 - (int)bit_ofs));
  10514. else
  10515. {
  10516. #ifdef __EMSCRIPTEN__
  10517. bits = blk.m_dwords[2];
  10518. bits |= (((uint64_t)blk.m_dwords[3]) << 32U);
  10519. #else
  10520. bits = blk.m_qwords[1];
  10521. #endif
  10522. if (bit_ofs >= 64U)
  10523. bits >>= (bit_ofs - 64U);
  10524. else
  10525. {
  10526. assert(bit_ofs >= 56U);
  10527. uint32_t bits_needed = 64U - bit_ofs;
  10528. bits <<= bits_needed;
  10529. bits |= (blk.m_bytes[7] >> (8U - bits_needed));
  10530. }
  10531. }
  10532. bit_ofs = 0;
  10533. const uint32_t mask = (1U << weight_bits) - 1U;
  10534. const uint32_t anchor_mask = (1U << (weight_bits - 1U)) - 1U;
  10535. if (total_planes == 2)
  10536. {
  10537. // Dual plane modes always have a single subset, and the first 2 weights are anchors.
  10538. unpacked.m_astc.m_weights[0] = (uint8_t)((uint32_t)(bits >> bit_ofs) & anchor_mask);
  10539. bit_ofs += (weight_bits - 1);
  10540. unpacked.m_astc.m_weights[1] = (uint8_t)((uint32_t)(bits >> bit_ofs) & anchor_mask);
  10541. bit_ofs += (weight_bits - 1);
  10542. for (uint32_t i = 2; i < 32; i++)
  10543. {
  10544. unpacked.m_astc.m_weights[i] = (uint8_t)((uint32_t)(bits >> bit_ofs) & mask);
  10545. bit_ofs += weight_bits;
  10546. }
  10547. }
  10548. else
  10549. {
  10550. if (subsets == 1)
  10551. {
  10552. // Specialize the single subset case.
  10553. if (weight_bits == 4)
  10554. {
  10555. assert(bit_ofs == 0);
  10556. // Specialize the most common case: 4-bit weights.
  10557. unpacked.m_astc.m_weights[0] = (uint8_t)((uint32_t)(bits) & 7);
  10558. unpacked.m_astc.m_weights[1] = (uint8_t)((uint32_t)(bits >> 3) & 15);
  10559. unpacked.m_astc.m_weights[2] = (uint8_t)((uint32_t)(bits >> (3 + 4 * 1)) & 15);
  10560. unpacked.m_astc.m_weights[3] = (uint8_t)((uint32_t)(bits >> (3 + 4 * 2)) & 15);
  10561. unpacked.m_astc.m_weights[4] = (uint8_t)((uint32_t)(bits >> (3 + 4 * 3)) & 15);
  10562. unpacked.m_astc.m_weights[5] = (uint8_t)((uint32_t)(bits >> (3 + 4 * 4)) & 15);
  10563. unpacked.m_astc.m_weights[6] = (uint8_t)((uint32_t)(bits >> (3 + 4 * 5)) & 15);
  10564. unpacked.m_astc.m_weights[7] = (uint8_t)((uint32_t)(bits >> (3 + 4 * 6)) & 15);
  10565. unpacked.m_astc.m_weights[8] = (uint8_t)((uint32_t)(bits >> (3 + 4 * 7)) & 15);
  10566. unpacked.m_astc.m_weights[9] = (uint8_t)((uint32_t)(bits >> (3 + 4 * 8)) & 15);
  10567. unpacked.m_astc.m_weights[10] = (uint8_t)((uint32_t)(bits >> (3 + 4 * 9)) & 15);
  10568. unpacked.m_astc.m_weights[11] = (uint8_t)((uint32_t)(bits >> (3 + 4 * 10)) & 15);
  10569. unpacked.m_astc.m_weights[12] = (uint8_t)((uint32_t)(bits >> (3 + 4 * 11)) & 15);
  10570. unpacked.m_astc.m_weights[13] = (uint8_t)((uint32_t)(bits >> (3 + 4 * 12)) & 15);
  10571. unpacked.m_astc.m_weights[14] = (uint8_t)((uint32_t)(bits >> (3 + 4 * 13)) & 15);
  10572. unpacked.m_astc.m_weights[15] = (uint8_t)((uint32_t)(bits >> (3 + 4 * 14)) & 15);
  10573. }
  10574. else
  10575. {
  10576. // First weight is always an anchor.
  10577. unpacked.m_astc.m_weights[0] = (uint8_t)((uint32_t)(bits >> bit_ofs) & anchor_mask);
  10578. bit_ofs += (weight_bits - 1);
  10579. for (uint32_t i = 1; i < 16; i++)
  10580. {
  10581. unpacked.m_astc.m_weights[i] = (uint8_t)((uint32_t)(bits >> bit_ofs) & mask);
  10582. bit_ofs += weight_bits;
  10583. }
  10584. }
  10585. }
  10586. else
  10587. {
  10588. const uint32_t a0 = pSubset_anchor_indices[0], a1 = pSubset_anchor_indices[1], a2 = pSubset_anchor_indices[2];
  10589. for (uint32_t i = 0; i < 16; i++)
  10590. {
  10591. if ((i == a0) || (i == a1) || (i == a2))
  10592. {
  10593. unpacked.m_astc.m_weights[i] = (uint8_t)((uint32_t)(bits >> bit_ofs) & anchor_mask);
  10594. bit_ofs += (weight_bits - 1);
  10595. }
  10596. else
  10597. {
  10598. unpacked.m_astc.m_weights[i] = (uint8_t)((uint32_t)(bits >> bit_ofs) & mask);
  10599. bit_ofs += weight_bits;
  10600. }
  10601. }
  10602. }
  10603. }
  10604. }
  10605. if ((blue_contract_check) && (total_comps >= 3))
  10606. {
  10607. // We only need to disable ASTC Blue Contraction when we'll be packing to ASTC. The other transcoders don't care.
  10608. bool invert_subset[3] = { false, false, false };
  10609. bool any_flag = false;
  10610. for (uint32_t subset_index = 0; subset_index < subsets; subset_index++)
  10611. {
  10612. const int s0 = g_astc_unquant[endpoint_range][unpacked.m_astc.m_endpoints[subset_index * total_comps * 2 + 0]].m_unquant +
  10613. g_astc_unquant[endpoint_range][unpacked.m_astc.m_endpoints[subset_index * total_comps * 2 + 2]].m_unquant +
  10614. g_astc_unquant[endpoint_range][unpacked.m_astc.m_endpoints[subset_index * total_comps * 2 + 4]].m_unquant;
  10615. const int s1 = g_astc_unquant[endpoint_range][unpacked.m_astc.m_endpoints[subset_index * total_comps * 2 + 1]].m_unquant +
  10616. g_astc_unquant[endpoint_range][unpacked.m_astc.m_endpoints[subset_index * total_comps * 2 + 3]].m_unquant +
  10617. g_astc_unquant[endpoint_range][unpacked.m_astc.m_endpoints[subset_index * total_comps * 2 + 5]].m_unquant;
  10618. if (s1 < s0)
  10619. {
  10620. for (uint32_t c = 0; c < total_comps; c++)
  10621. std::swap(unpacked.m_astc.m_endpoints[subset_index * total_comps * 2 + c * 2 + 0], unpacked.m_astc.m_endpoints[subset_index * total_comps * 2 + c * 2 + 1]);
  10622. invert_subset[subset_index] = true;
  10623. any_flag = true;
  10624. }
  10625. }
  10626. if (any_flag)
  10627. {
  10628. const uint32_t weight_mask = (1 << weight_bits) - 1;
  10629. for (uint32_t i = 0; i < 16; i++)
  10630. {
  10631. uint32_t subset = pPartition_pattern[i];
  10632. if (invert_subset[subset])
  10633. {
  10634. unpacked.m_astc.m_weights[i * total_planes] = (uint8_t)(weight_mask - unpacked.m_astc.m_weights[i * total_planes]);
  10635. if (total_planes == 2)
  10636. unpacked.m_astc.m_weights[i * total_planes + 1] = (uint8_t)(weight_mask - unpacked.m_astc.m_weights[i * total_planes + 1]);
  10637. }
  10638. }
  10639. }
  10640. }
  10641. return true;
  10642. }
  10643. static const uint32_t* g_astc_weight_tables[6] = { nullptr, g_bc7_weights1, g_bc7_weights2, g_bc7_weights3, g_astc_weights4, g_astc_weights5 };
  10644. bool unpack_uastc(uint32_t mode, uint32_t common_pattern, const color32& solid_color, const astc_block_desc& astc, color32* pPixels, bool srgb)
  10645. {
  10646. if (mode == UASTC_MODE_INDEX_SOLID_COLOR)
  10647. {
  10648. for (uint32_t i = 0; i < 16; i++)
  10649. pPixels[i] = solid_color;
  10650. return true;
  10651. }
  10652. color32 endpoints[3][2];
  10653. const uint32_t total_subsets = g_uastc_mode_subsets[mode];
  10654. const uint32_t total_comps = basisu::minimum<uint32_t>(4U, g_uastc_mode_comps[mode]);
  10655. const uint32_t endpoint_range = g_uastc_mode_endpoint_ranges[mode];
  10656. const uint32_t total_planes = g_uastc_mode_planes[mode];
  10657. const uint32_t weight_bits = g_uastc_mode_weight_bits[mode];
  10658. const uint32_t weight_levels = 1 << weight_bits;
  10659. for (uint32_t subset_index = 0; subset_index < total_subsets; subset_index++)
  10660. {
  10661. if (total_comps == 2)
  10662. {
  10663. const uint32_t ll = g_astc_unquant[endpoint_range][astc.m_endpoints[subset_index * total_comps * 2 + 0 * 2 + 0]].m_unquant;
  10664. const uint32_t lh = g_astc_unquant[endpoint_range][astc.m_endpoints[subset_index * total_comps * 2 + 0 * 2 + 1]].m_unquant;
  10665. const uint32_t al = g_astc_unquant[endpoint_range][astc.m_endpoints[subset_index * total_comps * 2 + 1 * 2 + 0]].m_unquant;
  10666. const uint32_t ah = g_astc_unquant[endpoint_range][astc.m_endpoints[subset_index * total_comps * 2 + 1 * 2 + 1]].m_unquant;
  10667. endpoints[subset_index][0].set_noclamp_rgba(ll, ll, ll, al);
  10668. endpoints[subset_index][1].set_noclamp_rgba(lh, lh, lh, ah);
  10669. }
  10670. else
  10671. {
  10672. for (uint32_t comp_index = 0; comp_index < total_comps; comp_index++)
  10673. {
  10674. endpoints[subset_index][0][comp_index] = g_astc_unquant[endpoint_range][astc.m_endpoints[subset_index * total_comps * 2 + comp_index * 2 + 0]].m_unquant;
  10675. endpoints[subset_index][1][comp_index] = g_astc_unquant[endpoint_range][astc.m_endpoints[subset_index * total_comps * 2 + comp_index * 2 + 1]].m_unquant;
  10676. }
  10677. for (uint32_t comp_index = total_comps; comp_index < 4; comp_index++)
  10678. {
  10679. endpoints[subset_index][0][comp_index] = 255;
  10680. endpoints[subset_index][1][comp_index] = 255;
  10681. }
  10682. }
  10683. }
  10684. color32 block_colors[3][32];
  10685. const uint32_t* pWeights = g_astc_weight_tables[weight_bits];
  10686. for (uint32_t subset_index = 0; subset_index < total_subsets; subset_index++)
  10687. {
  10688. for (uint32_t l = 0; l < weight_levels; l++)
  10689. {
  10690. if (total_comps == 2)
  10691. {
  10692. const uint8_t lc = (uint8_t)astc_interpolate(endpoints[subset_index][0][0], endpoints[subset_index][1][0], pWeights[l], srgb);
  10693. const uint8_t ac = (uint8_t)astc_interpolate(endpoints[subset_index][0][3], endpoints[subset_index][1][3], pWeights[l], srgb);
  10694. block_colors[subset_index][l].set(lc, lc, lc, ac);
  10695. }
  10696. else
  10697. {
  10698. uint32_t comp_index;
  10699. for (comp_index = 0; comp_index < total_comps; comp_index++)
  10700. block_colors[subset_index][l][comp_index] = (uint8_t)astc_interpolate(endpoints[subset_index][0][comp_index], endpoints[subset_index][1][comp_index], pWeights[l], srgb);
  10701. for (; comp_index < 4; comp_index++)
  10702. block_colors[subset_index][l][comp_index] = 255;
  10703. }
  10704. }
  10705. }
  10706. const uint8_t* pPartition_pattern = g_zero_pattern;
  10707. if (total_subsets >= 2)
  10708. {
  10709. if (total_subsets == 3)
  10710. pPartition_pattern = &g_astc_bc7_patterns3[common_pattern][0];
  10711. else if (mode == 7)
  10712. pPartition_pattern = &g_bc7_3_astc2_patterns2[common_pattern][0];
  10713. else
  10714. pPartition_pattern = &g_astc_bc7_patterns2[common_pattern][0];
  10715. #ifdef _DEBUG
  10716. for (uint32_t i = 0; i < 16; i++)
  10717. {
  10718. assert(pPartition_pattern[i] == (uint8_t)astc_compute_texel_partition(astc.m_partition_seed, i & 3, i >> 2, 0, total_subsets, true));
  10719. }
  10720. #endif
  10721. }
  10722. if (total_planes == 1)
  10723. {
  10724. if (total_subsets == 1)
  10725. {
  10726. for (uint32_t i = 0; i < 16; i++)
  10727. {
  10728. assert(astc.m_weights[i] < weight_levels);
  10729. pPixels[i] = block_colors[0][astc.m_weights[i]];
  10730. }
  10731. }
  10732. else
  10733. {
  10734. for (uint32_t i = 0; i < 16; i++)
  10735. {
  10736. assert(astc.m_weights[i] < weight_levels);
  10737. pPixels[i] = block_colors[pPartition_pattern[i]][astc.m_weights[i]];
  10738. }
  10739. }
  10740. }
  10741. else
  10742. {
  10743. assert(total_subsets == 1);
  10744. for (uint32_t i = 0; i < 16; i++)
  10745. {
  10746. const uint32_t subset_index = 0; // pPartition_pattern[i];
  10747. const uint32_t weight_index0 = astc.m_weights[i * 2];
  10748. const uint32_t weight_index1 = astc.m_weights[i * 2 + 1];
  10749. assert(weight_index0 < weight_levels && weight_index1 < weight_levels);
  10750. color32& c = pPixels[i];
  10751. for (uint32_t comp = 0; comp < 4; comp++)
  10752. {
  10753. if ((int)comp == astc.m_ccs)
  10754. c[comp] = block_colors[subset_index][weight_index1][comp];
  10755. else
  10756. c[comp] = block_colors[subset_index][weight_index0][comp];
  10757. }
  10758. }
  10759. }
  10760. return true;
  10761. }
  10762. bool unpack_uastc(const unpacked_uastc_block& unpacked_blk, color32* pPixels, bool srgb)
  10763. {
  10764. return unpack_uastc(unpacked_blk.m_mode, unpacked_blk.m_common_pattern, unpacked_blk.m_solid_color, unpacked_blk.m_astc, pPixels, srgb);
  10765. }
  10766. bool unpack_uastc(const uastc_block& blk, color32* pPixels, bool srgb)
  10767. {
  10768. unpacked_uastc_block unpacked_blk;
  10769. if (!unpack_uastc(blk, unpacked_blk, false, false))
  10770. return false;
  10771. return unpack_uastc(unpacked_blk, pPixels, srgb);
  10772. }
  10773. // Determines the best shared pbits to use to encode xl/xh
  10774. static void determine_shared_pbits(
  10775. uint32_t total_comps, uint32_t comp_bits, float xl[4], float xh[4],
  10776. color_quad_u8& bestMinColor, color_quad_u8& bestMaxColor, uint32_t best_pbits[2])
  10777. {
  10778. const uint32_t total_bits = comp_bits + 1;
  10779. assert(total_bits >= 4 && total_bits <= 8);
  10780. const int iscalep = (1 << total_bits) - 1;
  10781. const float scalep = (float)iscalep;
  10782. float best_err = 1e+9f;
  10783. for (int p = 0; p < 2; p++)
  10784. {
  10785. color_quad_u8 xMinColor, xMaxColor;
  10786. for (uint32_t c = 0; c < 4; c++)
  10787. {
  10788. xMinColor.m_c[c] = (uint8_t)(clampi(((int)((xl[c] * scalep - p) / 2.0f + .5f)) * 2 + p, p, iscalep - 1 + p));
  10789. xMaxColor.m_c[c] = (uint8_t)(clampi(((int)((xh[c] * scalep - p) / 2.0f + .5f)) * 2 + p, p, iscalep - 1 + p));
  10790. }
  10791. color_quad_u8 scaledLow, scaledHigh;
  10792. for (uint32_t i = 0; i < 4; i++)
  10793. {
  10794. scaledLow.m_c[i] = (xMinColor.m_c[i] << (8 - total_bits));
  10795. scaledLow.m_c[i] |= (scaledLow.m_c[i] >> total_bits);
  10796. assert(scaledLow.m_c[i] <= 255);
  10797. scaledHigh.m_c[i] = (xMaxColor.m_c[i] << (8 - total_bits));
  10798. scaledHigh.m_c[i] |= (scaledHigh.m_c[i] >> total_bits);
  10799. assert(scaledHigh.m_c[i] <= 255);
  10800. }
  10801. float err = 0;
  10802. for (uint32_t i = 0; i < total_comps; i++)
  10803. err += basisu::squaref((scaledLow.m_c[i] / 255.0f) - xl[i]) + basisu::squaref((scaledHigh.m_c[i] / 255.0f) - xh[i]);
  10804. if (err < best_err)
  10805. {
  10806. best_err = err;
  10807. best_pbits[0] = p;
  10808. best_pbits[1] = p;
  10809. for (uint32_t j = 0; j < 4; j++)
  10810. {
  10811. bestMinColor.m_c[j] = xMinColor.m_c[j] >> 1;
  10812. bestMaxColor.m_c[j] = xMaxColor.m_c[j] >> 1;
  10813. }
  10814. }
  10815. }
  10816. }
  10817. // Determines the best unique pbits to use to encode xl/xh
  10818. static void determine_unique_pbits(
  10819. uint32_t total_comps, uint32_t comp_bits, float xl[4], float xh[4],
  10820. color_quad_u8& bestMinColor, color_quad_u8& bestMaxColor, uint32_t best_pbits[2])
  10821. {
  10822. const uint32_t total_bits = comp_bits + 1;
  10823. const int iscalep = (1 << total_bits) - 1;
  10824. const float scalep = (float)iscalep;
  10825. float best_err0 = 1e+9f;
  10826. float best_err1 = 1e+9f;
  10827. for (int p = 0; p < 2; p++)
  10828. {
  10829. color_quad_u8 xMinColor, xMaxColor;
  10830. for (uint32_t c = 0; c < 4; c++)
  10831. {
  10832. xMinColor.m_c[c] = (uint8_t)(clampi(((int)((xl[c] * scalep - p) / 2.0f + .5f)) * 2 + p, p, iscalep - 1 + p));
  10833. xMaxColor.m_c[c] = (uint8_t)(clampi(((int)((xh[c] * scalep - p) / 2.0f + .5f)) * 2 + p, p, iscalep - 1 + p));
  10834. }
  10835. color_quad_u8 scaledLow, scaledHigh;
  10836. for (uint32_t i = 0; i < 4; i++)
  10837. {
  10838. scaledLow.m_c[i] = (xMinColor.m_c[i] << (8 - total_bits));
  10839. scaledLow.m_c[i] |= (scaledLow.m_c[i] >> total_bits);
  10840. assert(scaledLow.m_c[i] <= 255);
  10841. scaledHigh.m_c[i] = (xMaxColor.m_c[i] << (8 - total_bits));
  10842. scaledHigh.m_c[i] |= (scaledHigh.m_c[i] >> total_bits);
  10843. assert(scaledHigh.m_c[i] <= 255);
  10844. }
  10845. float err0 = 0, err1 = 0;
  10846. for (uint32_t i = 0; i < total_comps; i++)
  10847. {
  10848. err0 += basisu::squaref(scaledLow.m_c[i] - xl[i] * 255.0f);
  10849. err1 += basisu::squaref(scaledHigh.m_c[i] - xh[i] * 255.0f);
  10850. }
  10851. if (err0 < best_err0)
  10852. {
  10853. best_err0 = err0;
  10854. best_pbits[0] = p;
  10855. bestMinColor.m_c[0] = xMinColor.m_c[0] >> 1;
  10856. bestMinColor.m_c[1] = xMinColor.m_c[1] >> 1;
  10857. bestMinColor.m_c[2] = xMinColor.m_c[2] >> 1;
  10858. bestMinColor.m_c[3] = xMinColor.m_c[3] >> 1;
  10859. }
  10860. if (err1 < best_err1)
  10861. {
  10862. best_err1 = err1;
  10863. best_pbits[1] = p;
  10864. bestMaxColor.m_c[0] = xMaxColor.m_c[0] >> 1;
  10865. bestMaxColor.m_c[1] = xMaxColor.m_c[1] >> 1;
  10866. bestMaxColor.m_c[2] = xMaxColor.m_c[2] >> 1;
  10867. bestMaxColor.m_c[3] = xMaxColor.m_c[3] >> 1;
  10868. }
  10869. }
  10870. }
  10871. bool transcode_uastc_to_astc(const uastc_block& src_blk, void* pDst)
  10872. {
  10873. unpacked_uastc_block unpacked_src_blk;
  10874. if (!unpack_uastc(src_blk, unpacked_src_blk, true, false))
  10875. return false;
  10876. bool success = false;
  10877. if (unpacked_src_blk.m_mode == UASTC_MODE_INDEX_SOLID_COLOR)
  10878. {
  10879. pack_astc_solid_block(pDst, unpacked_src_blk.m_solid_color);
  10880. success = true;
  10881. }
  10882. else
  10883. {
  10884. success = pack_astc_block(static_cast<uint32_t*>(pDst), &unpacked_src_blk.m_astc, unpacked_src_blk.m_mode);
  10885. }
  10886. return success;
  10887. }
  10888. bool transcode_uastc_to_bc7(const unpacked_uastc_block& unpacked_src_blk, bc7_optimization_results& dst_blk)
  10889. {
  10890. memset(&dst_blk, 0, sizeof(dst_blk));
  10891. const uint32_t mode = unpacked_src_blk.m_mode;
  10892. const uint32_t endpoint_range = g_uastc_mode_endpoint_ranges[mode];
  10893. const uint32_t total_comps = g_uastc_mode_comps[mode];
  10894. switch (mode)
  10895. {
  10896. case 0:
  10897. case 5:
  10898. case 10:
  10899. case 12:
  10900. case 14:
  10901. case 15:
  10902. case 18:
  10903. {
  10904. // MODE 0: DualPlane: 0, WeightRange: 8 (16), Subsets: 1, EndpointRange: 19 (192) - BC7 MODE6 RGB
  10905. // MODE 5: DualPlane: 0, WeightRange : 5 (8), Subsets : 1, EndpointRange : 20 (256) - BC7 MODE6 RGB
  10906. // MODE 10 DualPlane: 0, WeightRange: 8 (16), Subsets: 1, EndpointRange: 13 (48) - BC7 MODE6
  10907. // MODE 12: DualPlane: 0, WeightRange : 5 (8), Subsets : 1, EndpointRange : 19 (192) - BC7 MODE6
  10908. // MODE 14: DualPlane: 0, WeightRange : 2 (4), Subsets : 1, EndpointRange : 20 (256) - BC7 MODE6
  10909. // MODE 18: DualPlane: 0, WeightRange : 11 (32), Subsets : 1, CEM : 8, EndpointRange : 11 (32) - BC7 MODE6
  10910. // MODE 15: DualPlane: 0, WeightRange : 8 (16), Subsets : 1, CEM : 4 (LA Direct), EndpointRange : 20 (256) - BC7 MODE6
  10911. dst_blk.m_mode = 6;
  10912. float xl[4], xh[4];
  10913. if (total_comps == 2)
  10914. {
  10915. xl[0] = g_astc_unquant[endpoint_range][unpacked_src_blk.m_astc.m_endpoints[0]].m_unquant / 255.0f;
  10916. xh[0] = g_astc_unquant[endpoint_range][unpacked_src_blk.m_astc.m_endpoints[1]].m_unquant / 255.0f;
  10917. xl[1] = xl[0];
  10918. xh[1] = xh[0];
  10919. xl[2] = xl[0];
  10920. xh[2] = xh[0];
  10921. xl[3] = g_astc_unquant[endpoint_range][unpacked_src_blk.m_astc.m_endpoints[2]].m_unquant / 255.0f;
  10922. xh[3] = g_astc_unquant[endpoint_range][unpacked_src_blk.m_astc.m_endpoints[3]].m_unquant / 255.0f;
  10923. }
  10924. else
  10925. {
  10926. xl[0] = g_astc_unquant[endpoint_range][unpacked_src_blk.m_astc.m_endpoints[0]].m_unquant / 255.0f;
  10927. xl[1] = g_astc_unquant[endpoint_range][unpacked_src_blk.m_astc.m_endpoints[2]].m_unquant / 255.0f;
  10928. xl[2] = g_astc_unquant[endpoint_range][unpacked_src_blk.m_astc.m_endpoints[4]].m_unquant / 255.0f;
  10929. xh[0] = g_astc_unquant[endpoint_range][unpacked_src_blk.m_astc.m_endpoints[1]].m_unquant / 255.0f;
  10930. xh[1] = g_astc_unquant[endpoint_range][unpacked_src_blk.m_astc.m_endpoints[3]].m_unquant / 255.0f;
  10931. xh[2] = g_astc_unquant[endpoint_range][unpacked_src_blk.m_astc.m_endpoints[5]].m_unquant / 255.0f;
  10932. if (total_comps == 4)
  10933. {
  10934. xl[3] = g_astc_unquant[endpoint_range][unpacked_src_blk.m_astc.m_endpoints[6]].m_unquant / 255.0f;
  10935. xh[3] = g_astc_unquant[endpoint_range][unpacked_src_blk.m_astc.m_endpoints[7]].m_unquant / 255.0f;
  10936. }
  10937. else
  10938. {
  10939. xl[3] = 1.0f;
  10940. xh[3] = 1.0f;
  10941. }
  10942. }
  10943. uint32_t best_pbits[2];
  10944. color_quad_u8 bestMinColor, bestMaxColor;
  10945. determine_unique_pbits((total_comps == 2) ? 4 : total_comps, 7, xl, xh, bestMinColor, bestMaxColor, best_pbits);
  10946. dst_blk.m_low[0] = bestMinColor;
  10947. dst_blk.m_high[0] = bestMaxColor;
  10948. if (total_comps == 3)
  10949. {
  10950. dst_blk.m_low[0].m_c[3] = 127;
  10951. dst_blk.m_high[0].m_c[3] = 127;
  10952. }
  10953. dst_blk.m_pbits[0][0] = best_pbits[0];
  10954. dst_blk.m_pbits[0][1] = best_pbits[1];
  10955. if (mode == 18)
  10956. {
  10957. const uint8_t s_bc7_5_to_4[32] = { 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 6, 7, 8, 9, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15 };
  10958. for (uint32_t i = 0; i < 16; i++)
  10959. dst_blk.m_selectors[i] = s_bc7_5_to_4[unpacked_src_blk.m_astc.m_weights[i]];
  10960. }
  10961. else if (mode == 14)
  10962. {
  10963. const uint8_t s_bc7_2_to_4[4] = { 0, 5, 10, 15 };
  10964. for (uint32_t i = 0; i < 16; i++)
  10965. dst_blk.m_selectors[i] = s_bc7_2_to_4[unpacked_src_blk.m_astc.m_weights[i]];
  10966. }
  10967. else if ((mode == 5) || (mode == 12))
  10968. {
  10969. const uint8_t s_bc7_3_to_4[8] = { 0, 2, 4, 6, 9, 11, 13, 15 };
  10970. for (uint32_t i = 0; i < 16; i++)
  10971. dst_blk.m_selectors[i] = s_bc7_3_to_4[unpacked_src_blk.m_astc.m_weights[i]];
  10972. }
  10973. else
  10974. {
  10975. for (uint32_t i = 0; i < 16; i++)
  10976. dst_blk.m_selectors[i] = unpacked_src_blk.m_astc.m_weights[i];
  10977. }
  10978. break;
  10979. }
  10980. case 1:
  10981. {
  10982. // DualPlane: 0, WeightRange : 2 (4), Subsets : 1, EndpointRange : 20 (256) - BC7 MODE3
  10983. // Mode 1 uses endpoint range 20 - no need to use ASTC dequant tables.
  10984. dst_blk.m_mode = 3;
  10985. float xl[4], xh[4];
  10986. xl[0] = unpacked_src_blk.m_astc.m_endpoints[0] / 255.0f;
  10987. xl[1] = unpacked_src_blk.m_astc.m_endpoints[2] / 255.0f;
  10988. xl[2] = unpacked_src_blk.m_astc.m_endpoints[4] / 255.0f;
  10989. xl[3] = 1.0f;
  10990. xh[0] = unpacked_src_blk.m_astc.m_endpoints[1] / 255.0f;
  10991. xh[1] = unpacked_src_blk.m_astc.m_endpoints[3] / 255.0f;
  10992. xh[2] = unpacked_src_blk.m_astc.m_endpoints[5] / 255.0f;
  10993. xh[3] = 1.0f;
  10994. uint32_t best_pbits[2];
  10995. color_quad_u8 bestMinColor, bestMaxColor;
  10996. memset(&bestMinColor, 0, sizeof(bestMinColor));
  10997. memset(&bestMaxColor, 0, sizeof(bestMaxColor));
  10998. determine_unique_pbits(3, 7, xl, xh, bestMinColor, bestMaxColor, best_pbits);
  10999. for (uint32_t i = 0; i < 3; i++)
  11000. {
  11001. dst_blk.m_low[0].m_c[i] = bestMinColor.m_c[i];
  11002. dst_blk.m_high[0].m_c[i] = bestMaxColor.m_c[i];
  11003. dst_blk.m_low[1].m_c[i] = bestMinColor.m_c[i];
  11004. dst_blk.m_high[1].m_c[i] = bestMaxColor.m_c[i];
  11005. }
  11006. dst_blk.m_pbits[0][0] = best_pbits[0];
  11007. dst_blk.m_pbits[0][1] = best_pbits[1];
  11008. dst_blk.m_pbits[1][0] = best_pbits[0];
  11009. dst_blk.m_pbits[1][1] = best_pbits[1];
  11010. for (uint32_t i = 0; i < 16; i++)
  11011. dst_blk.m_selectors[i] = unpacked_src_blk.m_astc.m_weights[i];
  11012. break;
  11013. }
  11014. case 2:
  11015. {
  11016. // 2. DualPlane: 0, WeightRange : 5 (8), Subsets : 2, EndpointRange : 8 (16) - BC7 MODE1
  11017. dst_blk.m_mode = 1;
  11018. dst_blk.m_partition = g_astc_bc7_common_partitions2[unpacked_src_blk.m_common_pattern].m_bc7;
  11019. const bool invert_partition = g_astc_bc7_common_partitions2[unpacked_src_blk.m_common_pattern].m_invert;
  11020. float xl[4], xh[4];
  11021. xl[3] = 1.0f;
  11022. xh[3] = 1.0f;
  11023. for (uint32_t subset = 0; subset < 2; subset++)
  11024. {
  11025. for (uint32_t i = 0; i < 3; i++)
  11026. {
  11027. uint32_t v = unpacked_src_blk.m_astc.m_endpoints[i * 2 + subset * 6];
  11028. v = (v << 4) | v;
  11029. xl[i] = v / 255.0f;
  11030. v = unpacked_src_blk.m_astc.m_endpoints[i * 2 + subset * 6 + 1];
  11031. v = (v << 4) | v;
  11032. xh[i] = v / 255.0f;
  11033. }
  11034. uint32_t best_pbits[2] = { 0, 0 };
  11035. color_quad_u8 bestMinColor, bestMaxColor;
  11036. memset(&bestMinColor, 0, sizeof(bestMinColor));
  11037. memset(&bestMaxColor, 0, sizeof(bestMaxColor));
  11038. determine_shared_pbits(3, 6, xl, xh, bestMinColor, bestMaxColor, best_pbits);
  11039. const uint32_t bc7_subset_index = invert_partition ? (1 - subset) : subset;
  11040. for (uint32_t i = 0; i < 3; i++)
  11041. {
  11042. dst_blk.m_low[bc7_subset_index].m_c[i] = bestMinColor.m_c[i];
  11043. dst_blk.m_high[bc7_subset_index].m_c[i] = bestMaxColor.m_c[i];
  11044. }
  11045. dst_blk.m_pbits[bc7_subset_index][0] = best_pbits[0];
  11046. } // subset
  11047. for (uint32_t i = 0; i < 16; i++)
  11048. dst_blk.m_selectors[i] = unpacked_src_blk.m_astc.m_weights[i];
  11049. break;
  11050. }
  11051. case 3:
  11052. {
  11053. // DualPlane: 0, WeightRange : 2 (4), Subsets : 3, EndpointRange : 7 (12) - BC7 MODE2
  11054. dst_blk.m_mode = 2;
  11055. dst_blk.m_partition = g_astc_bc7_common_partitions3[unpacked_src_blk.m_common_pattern].m_bc7;
  11056. const uint32_t perm = g_astc_bc7_common_partitions3[unpacked_src_blk.m_common_pattern].m_astc_to_bc7_perm;
  11057. for (uint32_t subset = 0; subset < 3; subset++)
  11058. {
  11059. for (uint32_t comp = 0; comp < 3; comp++)
  11060. {
  11061. uint32_t lo = g_astc_unquant[endpoint_range][unpacked_src_blk.m_astc.m_endpoints[comp * 2 + 0 + subset * 6]].m_unquant;
  11062. uint32_t hi = g_astc_unquant[endpoint_range][unpacked_src_blk.m_astc.m_endpoints[comp * 2 + 1 + subset * 6]].m_unquant;
  11063. // TODO: I think this can be improved by using tables like Basis Universal does with ETC1S conversion.
  11064. lo = (lo * 31 + 127) / 255;
  11065. hi = (hi * 31 + 127) / 255;
  11066. const uint32_t bc7_subset_index = g_astc_to_bc7_partition_index_perm_tables[perm][subset];
  11067. dst_blk.m_low[bc7_subset_index].m_c[comp] = (uint8_t)lo;
  11068. dst_blk.m_high[bc7_subset_index].m_c[comp] = (uint8_t)hi;
  11069. }
  11070. }
  11071. for (uint32_t i = 0; i < 16; i++)
  11072. dst_blk.m_selectors[i] = unpacked_src_blk.m_astc.m_weights[i];
  11073. break;
  11074. }
  11075. case 4:
  11076. {
  11077. // 4. DualPlane: 0, WeightRange: 2 (4), Subsets: 2, EndpointRange: 12 (40) - BC7 MODE3
  11078. dst_blk.m_mode = 3;
  11079. dst_blk.m_partition = g_astc_bc7_common_partitions2[unpacked_src_blk.m_common_pattern].m_bc7;
  11080. const bool invert_partition = g_astc_bc7_common_partitions2[unpacked_src_blk.m_common_pattern].m_invert;
  11081. float xl[4], xh[4];
  11082. xl[3] = 1.0f;
  11083. xh[3] = 1.0f;
  11084. for (uint32_t subset = 0; subset < 2; subset++)
  11085. {
  11086. for (uint32_t i = 0; i < 3; i++)
  11087. {
  11088. xl[i] = g_astc_unquant[endpoint_range][unpacked_src_blk.m_astc.m_endpoints[i * 2 + subset * 6]].m_unquant / 255.0f;
  11089. xh[i] = g_astc_unquant[endpoint_range][unpacked_src_blk.m_astc.m_endpoints[i * 2 + subset * 6 + 1]].m_unquant / 255.0f;
  11090. }
  11091. uint32_t best_pbits[2] = { 0, 0 };
  11092. color_quad_u8 bestMinColor, bestMaxColor;
  11093. memset(&bestMinColor, 0, sizeof(bestMinColor));
  11094. memset(&bestMaxColor, 0, sizeof(bestMaxColor));
  11095. determine_unique_pbits(3, 7, xl, xh, bestMinColor, bestMaxColor, best_pbits);
  11096. const uint32_t bc7_subset_index = invert_partition ? (1 - subset) : subset;
  11097. for (uint32_t i = 0; i < 3; i++)
  11098. {
  11099. dst_blk.m_low[bc7_subset_index].m_c[i] = bestMinColor.m_c[i];
  11100. dst_blk.m_high[bc7_subset_index].m_c[i] = bestMaxColor.m_c[i];
  11101. }
  11102. dst_blk.m_low[bc7_subset_index].m_c[3] = 127;
  11103. dst_blk.m_high[bc7_subset_index].m_c[3] = 127;
  11104. dst_blk.m_pbits[bc7_subset_index][0] = best_pbits[0];
  11105. dst_blk.m_pbits[bc7_subset_index][1] = best_pbits[1];
  11106. } // subset
  11107. for (uint32_t i = 0; i < 16; i++)
  11108. dst_blk.m_selectors[i] = unpacked_src_blk.m_astc.m_weights[i];
  11109. break;
  11110. }
  11111. case 6:
  11112. case 11:
  11113. case 13:
  11114. case 17:
  11115. {
  11116. // MODE 6: DualPlane: 1, WeightRange : 2 (4), Subsets : 1, EndpointRange : 18 (160) - BC7 MODE5 RGB
  11117. // MODE 11: DualPlane: 1, WeightRange: 2 (4), Subsets: 1, EndpointRange: 13 (48) - BC7 MODE5
  11118. // MODE 13: DualPlane: 1, WeightRange: 0 (2), Subsets : 1, EndpointRange : 20 (256) - BC7 MODE5
  11119. // MODE 17: DualPlane: 1, WeightRange: 2 (4), Subsets: 1, CEM: 4 (LA Direct), EndpointRange: 20 (256) - BC7 MODE5
  11120. dst_blk.m_mode = 5;
  11121. dst_blk.m_rotation = (unpacked_src_blk.m_astc.m_ccs + 1) & 3;
  11122. if (total_comps == 2)
  11123. {
  11124. assert(unpacked_src_blk.m_astc.m_ccs == 3);
  11125. dst_blk.m_low->m_c[0] = (uint8_t)((g_astc_unquant[endpoint_range][unpacked_src_blk.m_astc.m_endpoints[0]].m_unquant * 127 + 127) / 255);
  11126. dst_blk.m_high->m_c[0] = (uint8_t)((g_astc_unquant[endpoint_range][unpacked_src_blk.m_astc.m_endpoints[1]].m_unquant * 127 + 127) / 255);
  11127. dst_blk.m_low->m_c[1] = dst_blk.m_low->m_c[0];
  11128. dst_blk.m_high->m_c[1] = dst_blk.m_high->m_c[0];
  11129. dst_blk.m_low->m_c[2] = dst_blk.m_low->m_c[0];
  11130. dst_blk.m_high->m_c[2] = dst_blk.m_high->m_c[0];
  11131. dst_blk.m_low->m_c[3] = (uint8_t)(g_astc_unquant[endpoint_range][unpacked_src_blk.m_astc.m_endpoints[2]].m_unquant);
  11132. dst_blk.m_high->m_c[3] = (uint8_t)(g_astc_unquant[endpoint_range][unpacked_src_blk.m_astc.m_endpoints[3]].m_unquant);
  11133. }
  11134. else
  11135. {
  11136. for (uint32_t astc_comp = 0; astc_comp < 4; astc_comp++)
  11137. {
  11138. uint32_t bc7_comp = astc_comp;
  11139. // ASTC and BC7 handle dual plane component rotations differently:
  11140. // ASTC: 2nd plane separately interpolates the CCS channel.
  11141. // BC7: 2nd plane channel is swapped with alpha, 2nd plane controls alpha interpolation, then we swap alpha with the desired channel.
  11142. if (astc_comp == (uint32_t)unpacked_src_blk.m_astc.m_ccs)
  11143. bc7_comp = 3;
  11144. else if (astc_comp == 3)
  11145. bc7_comp = unpacked_src_blk.m_astc.m_ccs;
  11146. uint32_t l = 255, h = 255;
  11147. if (astc_comp < total_comps)
  11148. {
  11149. l = g_astc_unquant[endpoint_range][unpacked_src_blk.m_astc.m_endpoints[astc_comp * 2 + 0]].m_unquant;
  11150. h = g_astc_unquant[endpoint_range][unpacked_src_blk.m_astc.m_endpoints[astc_comp * 2 + 1]].m_unquant;
  11151. }
  11152. if (bc7_comp < 3)
  11153. {
  11154. l = (l * 127 + 127) / 255;
  11155. h = (h * 127 + 127) / 255;
  11156. }
  11157. dst_blk.m_low->m_c[bc7_comp] = (uint8_t)l;
  11158. dst_blk.m_high->m_c[bc7_comp] = (uint8_t)h;
  11159. }
  11160. }
  11161. if (mode == 13)
  11162. {
  11163. for (uint32_t i = 0; i < 16; i++)
  11164. {
  11165. dst_blk.m_selectors[i] = unpacked_src_blk.m_astc.m_weights[i * 2] ? 3 : 0;
  11166. dst_blk.m_alpha_selectors[i] = unpacked_src_blk.m_astc.m_weights[i * 2 + 1] ? 3 : 0;
  11167. }
  11168. }
  11169. else
  11170. {
  11171. for (uint32_t i = 0; i < 16; i++)
  11172. {
  11173. dst_blk.m_selectors[i] = unpacked_src_blk.m_astc.m_weights[i * 2];
  11174. dst_blk.m_alpha_selectors[i] = unpacked_src_blk.m_astc.m_weights[i * 2 + 1];
  11175. }
  11176. }
  11177. break;
  11178. }
  11179. case 7:
  11180. {
  11181. // DualPlane: 0, WeightRange : 2 (4), Subsets : 2, EndpointRange : 12 (40) - BC7 MODE2
  11182. dst_blk.m_mode = 2;
  11183. dst_blk.m_partition = g_bc7_3_astc2_common_partitions[unpacked_src_blk.m_common_pattern].m_bc73;
  11184. const uint32_t common_pattern_k = g_bc7_3_astc2_common_partitions[unpacked_src_blk.m_common_pattern].k;
  11185. for (uint32_t bc7_part = 0; bc7_part < 3; bc7_part++)
  11186. {
  11187. const uint32_t astc_part = bc7_convert_partition_index_3_to_2(bc7_part, common_pattern_k);
  11188. for (uint32_t c = 0; c < 3; c++)
  11189. {
  11190. dst_blk.m_low[bc7_part].m_c[c] = (g_astc_unquant[endpoint_range][unpacked_src_blk.m_astc.m_endpoints[c * 2 + 0 + astc_part * 6]].m_unquant * 31 + 127) / 255;
  11191. dst_blk.m_high[bc7_part].m_c[c] = (g_astc_unquant[endpoint_range][unpacked_src_blk.m_astc.m_endpoints[c * 2 + 1 + astc_part * 6]].m_unquant * 31 + 127) / 255;
  11192. }
  11193. }
  11194. for (uint32_t i = 0; i < 16; i++)
  11195. dst_blk.m_selectors[i] = unpacked_src_blk.m_astc.m_weights[i];
  11196. break;
  11197. }
  11198. case UASTC_MODE_INDEX_SOLID_COLOR:
  11199. {
  11200. // Void-Extent: Solid Color RGBA (BC7 MODE5 or MODE6)
  11201. const color32& solid_color = unpacked_src_blk.m_solid_color;
  11202. uint32_t best_err0 = g_bc7_mode_6_optimal_endpoints[solid_color.r][0].m_error + g_bc7_mode_6_optimal_endpoints[solid_color.g][0].m_error +
  11203. g_bc7_mode_6_optimal_endpoints[solid_color.b][0].m_error + g_bc7_mode_6_optimal_endpoints[solid_color.a][0].m_error;
  11204. uint32_t best_err1 = g_bc7_mode_6_optimal_endpoints[solid_color.r][1].m_error + g_bc7_mode_6_optimal_endpoints[solid_color.g][1].m_error +
  11205. g_bc7_mode_6_optimal_endpoints[solid_color.b][1].m_error + g_bc7_mode_6_optimal_endpoints[solid_color.a][1].m_error;
  11206. if (best_err0 > 0 && best_err1 > 0)
  11207. {
  11208. dst_blk.m_mode = 5;
  11209. for (uint32_t c = 0; c < 3; c++)
  11210. {
  11211. dst_blk.m_low[0].m_c[c] = g_bc7_mode_5_optimal_endpoints[solid_color.c[c]].m_lo;
  11212. dst_blk.m_high[0].m_c[c] = g_bc7_mode_5_optimal_endpoints[solid_color.c[c]].m_hi;
  11213. }
  11214. memset(dst_blk.m_selectors, BC7ENC_MODE_5_OPTIMAL_INDEX, 16);
  11215. dst_blk.m_low[0].m_c[3] = solid_color.c[3];
  11216. dst_blk.m_high[0].m_c[3] = solid_color.c[3];
  11217. //memset(dst_blk.m_alpha_selectors, 0, 16);
  11218. }
  11219. else
  11220. {
  11221. dst_blk.m_mode = 6;
  11222. uint32_t best_p = 0;
  11223. if (best_err1 < best_err0)
  11224. best_p = 1;
  11225. for (uint32_t c = 0; c < 4; c++)
  11226. {
  11227. dst_blk.m_low[0].m_c[c] = g_bc7_mode_6_optimal_endpoints[solid_color.c[c]][best_p].m_lo;
  11228. dst_blk.m_high[0].m_c[c] = g_bc7_mode_6_optimal_endpoints[solid_color.c[c]][best_p].m_hi;
  11229. }
  11230. dst_blk.m_pbits[0][0] = best_p;
  11231. dst_blk.m_pbits[0][1] = best_p;
  11232. memset(dst_blk.m_selectors, BC7ENC_MODE_6_OPTIMAL_INDEX, 16);
  11233. }
  11234. break;
  11235. }
  11236. case 9:
  11237. case 16:
  11238. {
  11239. // 9. DualPlane: 0, WeightRange : 2 (4), Subsets : 2, EndpointRange : 8 (16) - BC7 MODE7
  11240. // 16. DualPlane: 0, WeightRange: 2 (4), Subsets: 2, CEM: 4 (LA Direct), EndpointRange: 20 (256) - BC7 MODE7
  11241. dst_blk.m_mode = 7;
  11242. dst_blk.m_partition = g_astc_bc7_common_partitions2[unpacked_src_blk.m_common_pattern].m_bc7;
  11243. const bool invert_partition = g_astc_bc7_common_partitions2[unpacked_src_blk.m_common_pattern].m_invert;
  11244. for (uint32_t astc_subset = 0; astc_subset < 2; astc_subset++)
  11245. {
  11246. float xl[4], xh[4];
  11247. if (total_comps == 2)
  11248. {
  11249. xl[0] = g_astc_unquant[endpoint_range][unpacked_src_blk.m_astc.m_endpoints[0 + astc_subset * 4]].m_unquant / 255.0f;
  11250. xh[0] = g_astc_unquant[endpoint_range][unpacked_src_blk.m_astc.m_endpoints[1 + astc_subset * 4]].m_unquant / 255.0f;
  11251. xl[1] = xl[0];
  11252. xh[1] = xh[0];
  11253. xl[2] = xl[0];
  11254. xh[2] = xh[0];
  11255. xl[3] = g_astc_unquant[endpoint_range][unpacked_src_blk.m_astc.m_endpoints[2 + astc_subset * 4]].m_unquant / 255.0f;
  11256. xh[3] = g_astc_unquant[endpoint_range][unpacked_src_blk.m_astc.m_endpoints[3 + astc_subset * 4]].m_unquant / 255.0f;
  11257. }
  11258. else
  11259. {
  11260. xl[0] = g_astc_unquant[endpoint_range][unpacked_src_blk.m_astc.m_endpoints[0 + astc_subset * 8]].m_unquant / 255.0f;
  11261. xl[1] = g_astc_unquant[endpoint_range][unpacked_src_blk.m_astc.m_endpoints[2 + astc_subset * 8]].m_unquant / 255.0f;
  11262. xl[2] = g_astc_unquant[endpoint_range][unpacked_src_blk.m_astc.m_endpoints[4 + astc_subset * 8]].m_unquant / 255.0f;
  11263. xl[3] = g_astc_unquant[endpoint_range][unpacked_src_blk.m_astc.m_endpoints[6 + astc_subset * 8]].m_unquant / 255.0f;
  11264. xh[0] = g_astc_unquant[endpoint_range][unpacked_src_blk.m_astc.m_endpoints[1 + astc_subset * 8]].m_unquant / 255.0f;
  11265. xh[1] = g_astc_unquant[endpoint_range][unpacked_src_blk.m_astc.m_endpoints[3 + astc_subset * 8]].m_unquant / 255.0f;
  11266. xh[2] = g_astc_unquant[endpoint_range][unpacked_src_blk.m_astc.m_endpoints[5 + astc_subset * 8]].m_unquant / 255.0f;
  11267. xh[3] = g_astc_unquant[endpoint_range][unpacked_src_blk.m_astc.m_endpoints[7 + astc_subset * 8]].m_unquant / 255.0f;
  11268. }
  11269. uint32_t best_pbits[2] = { 0, 0 };
  11270. color_quad_u8 bestMinColor, bestMaxColor;
  11271. memset(&bestMinColor, 0, sizeof(bestMinColor));
  11272. memset(&bestMaxColor, 0, sizeof(bestMaxColor));
  11273. determine_unique_pbits(4, 5, xl, xh, bestMinColor, bestMaxColor, best_pbits);
  11274. const uint32_t bc7_subset_index = invert_partition ? (1 - astc_subset) : astc_subset;
  11275. dst_blk.m_low[bc7_subset_index] = bestMinColor;
  11276. dst_blk.m_high[bc7_subset_index] = bestMaxColor;
  11277. dst_blk.m_pbits[bc7_subset_index][0] = best_pbits[0];
  11278. dst_blk.m_pbits[bc7_subset_index][1] = best_pbits[1];
  11279. } // astc_subset
  11280. for (uint32_t i = 0; i < 16; i++)
  11281. dst_blk.m_selectors[i] = unpacked_src_blk.m_astc.m_weights[i];
  11282. break;
  11283. }
  11284. default:
  11285. return false;
  11286. }
  11287. return true;
  11288. }
  11289. bool transcode_uastc_to_bc7(const uastc_block& src_blk, bc7_optimization_results& dst_blk)
  11290. {
  11291. unpacked_uastc_block unpacked_src_blk;
  11292. if (!unpack_uastc(src_blk, unpacked_src_blk, false, false))
  11293. return false;
  11294. return transcode_uastc_to_bc7(unpacked_src_blk, dst_blk);
  11295. }
  11296. bool transcode_uastc_to_bc7(const uastc_block& src_blk, void* pDst)
  11297. {
  11298. bc7_optimization_results temp;
  11299. if (!transcode_uastc_to_bc7(src_blk, temp))
  11300. return false;
  11301. encode_bc7_block(pDst, &temp);
  11302. return true;
  11303. }
  11304. color32 apply_etc1_bias(const color32 &block_color, uint32_t bias, uint32_t limit, uint32_t subblock)
  11305. {
  11306. color32 result;
  11307. for (uint32_t c = 0; c < 3; c++)
  11308. {
  11309. static const int s_divs[3] = { 1, 3, 9 };
  11310. int delta = 0;
  11311. switch (bias)
  11312. {
  11313. case 2: delta = subblock ? 0 : ((c == 0) ? -1 : 0); break;
  11314. case 5: delta = subblock ? 0 : ((c == 1) ? -1 : 0); break;
  11315. case 6: delta = subblock ? 0 : ((c == 2) ? -1 : 0); break;
  11316. case 7: delta = subblock ? 0 : ((c == 0) ? 1 : 0); break;
  11317. case 11: delta = subblock ? 0 : ((c == 1) ? 1 : 0); break;
  11318. case 15: delta = subblock ? 0 : ((c == 2) ? 1 : 0); break;
  11319. case 18: delta = subblock ? ((c == 0) ? -1 : 0) : 0; break;
  11320. case 19: delta = subblock ? ((c == 1) ? -1 : 0) : 0; break;
  11321. case 20: delta = subblock ? ((c == 2) ? -1 : 0) : 0; break;
  11322. case 21: delta = subblock ? ((c == 0) ? 1 : 0) : 0; break;
  11323. case 24: delta = subblock ? ((c == 1) ? 1 : 0) : 0; break;
  11324. case 8: delta = subblock ? ((c == 2) ? 1 : 0) : 0; break;
  11325. case 10: delta = -2; break;
  11326. case 27: delta = subblock ? 0 : -1; break;
  11327. case 28: delta = subblock ? -1 : 1; break;
  11328. case 29: delta = subblock ? 1 : 0; break;
  11329. case 30: delta = subblock ? -1 : 0; break;
  11330. case 31: delta = subblock ? 0 : 1; break;
  11331. default:
  11332. delta = ((bias / s_divs[c]) % 3) - 1;
  11333. break;
  11334. }
  11335. int v = block_color[c];
  11336. if (v == 0)
  11337. {
  11338. if (delta == -2)
  11339. v += 3;
  11340. else
  11341. v += delta + 1;
  11342. }
  11343. else if (v == (int)limit)
  11344. {
  11345. v += (delta - 1);
  11346. }
  11347. else
  11348. {
  11349. v += delta;
  11350. if ((v < 0) || (v > (int)limit))
  11351. v = (v - delta) - delta;
  11352. }
  11353. assert(v >= 0);
  11354. assert(v <= (int)limit);
  11355. result[c] = (uint8_t)v;
  11356. }
  11357. return result;
  11358. }
  11359. static void etc1_determine_selectors(decoder_etc_block& dst_blk, const color32* pSource_pixels, uint32_t first_subblock, uint32_t last_subblock)
  11360. {
  11361. static const uint8_t s_tran[4] = { 1, 0, 2, 3 };
  11362. uint16_t l_bitmask = 0;
  11363. uint16_t h_bitmask = 0;
  11364. for (uint32_t subblock = first_subblock; subblock < last_subblock; subblock++)
  11365. {
  11366. color32 block_colors[4];
  11367. dst_blk.get_block_colors(block_colors, subblock);
  11368. uint32_t block_y[4];
  11369. for (uint32_t i = 0; i < 4; i++)
  11370. block_y[i] = block_colors[i][0] * 54 + block_colors[i][1] * 183 + block_colors[i][2] * 19;
  11371. const uint32_t block_y01 = block_y[0] + block_y[1];
  11372. const uint32_t block_y12 = block_y[1] + block_y[2];
  11373. const uint32_t block_y23 = block_y[2] + block_y[3];
  11374. // X0 X0 X0 X0 X1 X1 X1 X1 X2 X2 X2 X2 X3 X3 X3 X3
  11375. // Y0 Y1 Y2 Y3 Y0 Y1 Y2 Y3 Y0 Y1 Y2 Y3 Y0 Y1 Y2 Y3
  11376. if (dst_blk.get_flip_bit())
  11377. {
  11378. uint32_t ofs = subblock * 2;
  11379. for (uint32_t y = 0; y < 2; y++)
  11380. {
  11381. for (uint32_t x = 0; x < 4; x++)
  11382. {
  11383. const color32& c = pSource_pixels[x + (subblock * 2 + y) * 4];
  11384. const uint32_t l = c[0] * 108 + c[1] * 366 + c[2] * 38;
  11385. uint32_t t = s_tran[(l < block_y01) + (l < block_y12) + (l < block_y23)];
  11386. assert(ofs < 16);
  11387. l_bitmask |= ((t & 1) << ofs);
  11388. h_bitmask |= ((t >> 1) << ofs);
  11389. ofs += 4;
  11390. }
  11391. ofs = (int)ofs + 1 - 4 * 4;
  11392. }
  11393. }
  11394. else
  11395. {
  11396. uint32_t ofs = (subblock * 2) * 4;
  11397. for (uint32_t x = 0; x < 2; x++)
  11398. {
  11399. for (uint32_t y = 0; y < 4; y++)
  11400. {
  11401. const color32& c = pSource_pixels[subblock * 2 + x + y * 4];
  11402. const uint32_t l = c[0] * 108 + c[1] * 366 + c[2] * 38;
  11403. uint32_t t = s_tran[(l < block_y01) + (l < block_y12) + (l < block_y23)];
  11404. assert(ofs < 16);
  11405. l_bitmask |= ((t & 1) << ofs);
  11406. h_bitmask |= ((t >> 1) << ofs);
  11407. ++ofs;
  11408. }
  11409. }
  11410. }
  11411. }
  11412. dst_blk.m_bytes[7] = (uint8_t)(l_bitmask);
  11413. dst_blk.m_bytes[6] = (uint8_t)(l_bitmask >> 8);
  11414. dst_blk.m_bytes[5] = (uint8_t)(h_bitmask);
  11415. dst_blk.m_bytes[4] = (uint8_t)(h_bitmask >> 8);
  11416. }
  11417. static const uint8_t s_etc1_solid_selectors[4][4] = { { 255, 255, 255, 255 }, { 255, 255, 0, 0 }, { 0, 0, 0, 0 }, {0, 0, 255, 255 } };
  11418. struct etc_coord2
  11419. {
  11420. uint8_t m_x, m_y;
  11421. };
  11422. // [flip][subblock][pixel_index]
  11423. const etc_coord2 g_etc1_pixel_coords[2][2][8] =
  11424. {
  11425. {
  11426. {
  11427. { 0, 0 }, { 0, 1 }, { 0, 2 }, { 0, 3 },
  11428. { 1, 0 }, { 1, 1 }, { 1, 2 }, { 1, 3 }
  11429. },
  11430. {
  11431. { 2, 0 }, { 2, 1 }, { 2, 2 }, { 2, 3 },
  11432. { 3, 0 }, { 3, 1 }, { 3, 2 }, { 3, 3 }
  11433. }
  11434. },
  11435. {
  11436. {
  11437. { 0, 0 }, { 1, 0 }, { 2, 0 }, { 3, 0 },
  11438. { 0, 1 }, { 1, 1 }, { 2, 1 }, { 3, 1 }
  11439. },
  11440. {
  11441. { 0, 2 }, { 1, 2 }, { 2, 2 }, { 3, 2 },
  11442. { 0, 3 }, { 1, 3 }, { 2, 3 }, { 3, 3 }
  11443. },
  11444. }
  11445. };
  11446. void transcode_uastc_to_etc1(unpacked_uastc_block& unpacked_src_blk, color32 block_pixels[4][4], void* pDst)
  11447. {
  11448. decoder_etc_block& dst_blk = *static_cast<decoder_etc_block*>(pDst);
  11449. if (unpacked_src_blk.m_mode == UASTC_MODE_INDEX_SOLID_COLOR)
  11450. {
  11451. dst_blk.m_bytes[3] = (uint8_t)((unpacked_src_blk.m_etc1_diff << 1) | (unpacked_src_blk.m_etc1_inten0 << 5) | (unpacked_src_blk.m_etc1_inten0 << 2));
  11452. if (unpacked_src_blk.m_etc1_diff)
  11453. {
  11454. dst_blk.m_bytes[0] = (uint8_t)(unpacked_src_blk.m_etc1_r << 3);
  11455. dst_blk.m_bytes[1] = (uint8_t)(unpacked_src_blk.m_etc1_g << 3);
  11456. dst_blk.m_bytes[2] = (uint8_t)(unpacked_src_blk.m_etc1_b << 3);
  11457. }
  11458. else
  11459. {
  11460. dst_blk.m_bytes[0] = (uint8_t)(unpacked_src_blk.m_etc1_r | (unpacked_src_blk.m_etc1_r << 4));
  11461. dst_blk.m_bytes[1] = (uint8_t)(unpacked_src_blk.m_etc1_g | (unpacked_src_blk.m_etc1_g << 4));
  11462. dst_blk.m_bytes[2] = (uint8_t)(unpacked_src_blk.m_etc1_b | (unpacked_src_blk.m_etc1_b << 4));
  11463. }
  11464. memcpy(dst_blk.m_bytes + 4, &s_etc1_solid_selectors[unpacked_src_blk.m_etc1_selector][0], 4);
  11465. return;
  11466. }
  11467. const bool flip = unpacked_src_blk.m_etc1_flip != 0;
  11468. const bool diff = unpacked_src_blk.m_etc1_diff != 0;
  11469. dst_blk.m_bytes[3] = (uint8_t)((int)flip | (diff << 1) | (unpacked_src_blk.m_etc1_inten0 << 5) | (unpacked_src_blk.m_etc1_inten1 << 2));
  11470. const uint32_t limit = diff ? 31 : 15;
  11471. color32 block_colors[2];
  11472. for (uint32_t subset = 0; subset < 2; subset++)
  11473. {
  11474. uint32_t avg_color[3];
  11475. memset(avg_color, 0, sizeof(avg_color));
  11476. for (uint32_t j = 0; j < 8; j++)
  11477. {
  11478. const etc_coord2& c = g_etc1_pixel_coords[flip][subset][j];
  11479. avg_color[0] += block_pixels[c.m_y][c.m_x].r;
  11480. avg_color[1] += block_pixels[c.m_y][c.m_x].g;
  11481. avg_color[2] += block_pixels[c.m_y][c.m_x].b;
  11482. } // j
  11483. block_colors[subset][0] = (uint8_t)((avg_color[0] * limit + 1020) / (8 * 255));
  11484. block_colors[subset][1] = (uint8_t)((avg_color[1] * limit + 1020) / (8 * 255));
  11485. block_colors[subset][2] = (uint8_t)((avg_color[2] * limit + 1020) / (8 * 255));
  11486. block_colors[subset][3] = 0;
  11487. if (g_uastc_mode_has_etc1_bias[unpacked_src_blk.m_mode])
  11488. {
  11489. block_colors[subset] = apply_etc1_bias(block_colors[subset], unpacked_src_blk.m_etc1_bias, limit, subset);
  11490. }
  11491. } // subset
  11492. if (diff)
  11493. {
  11494. int dr = block_colors[1].r - block_colors[0].r;
  11495. int dg = block_colors[1].g - block_colors[0].g;
  11496. int db = block_colors[1].b - block_colors[0].b;
  11497. dr = basisu::clamp<int>(dr, cETC1ColorDeltaMin, cETC1ColorDeltaMax);
  11498. dg = basisu::clamp<int>(dg, cETC1ColorDeltaMin, cETC1ColorDeltaMax);
  11499. db = basisu::clamp<int>(db, cETC1ColorDeltaMin, cETC1ColorDeltaMax);
  11500. if (dr < 0) dr += 8;
  11501. if (dg < 0) dg += 8;
  11502. if (db < 0) db += 8;
  11503. dst_blk.m_bytes[0] = (uint8_t)((block_colors[0].r << 3) | dr);
  11504. dst_blk.m_bytes[1] = (uint8_t)((block_colors[0].g << 3) | dg);
  11505. dst_blk.m_bytes[2] = (uint8_t)((block_colors[0].b << 3) | db);
  11506. }
  11507. else
  11508. {
  11509. dst_blk.m_bytes[0] = (uint8_t)(block_colors[1].r | (block_colors[0].r << 4));
  11510. dst_blk.m_bytes[1] = (uint8_t)(block_colors[1].g | (block_colors[0].g << 4));
  11511. dst_blk.m_bytes[2] = (uint8_t)(block_colors[1].b | (block_colors[0].b << 4));
  11512. }
  11513. etc1_determine_selectors(dst_blk, &block_pixels[0][0], 0, 2);
  11514. }
  11515. bool transcode_uastc_to_etc1(const uastc_block& src_blk, void* pDst)
  11516. {
  11517. unpacked_uastc_block unpacked_src_blk;
  11518. if (!unpack_uastc(src_blk, unpacked_src_blk, false))
  11519. return false;
  11520. color32 block_pixels[4][4];
  11521. if (unpacked_src_blk.m_mode != UASTC_MODE_INDEX_SOLID_COLOR)
  11522. {
  11523. const bool unpack_srgb = false;
  11524. if (!unpack_uastc(unpacked_src_blk, &block_pixels[0][0], unpack_srgb))
  11525. return false;
  11526. }
  11527. transcode_uastc_to_etc1(unpacked_src_blk, block_pixels, pDst);
  11528. return true;
  11529. }
  11530. static inline int gray_distance2(const uint8_t c, int y)
  11531. {
  11532. int gray_dist = (int)c - y;
  11533. return gray_dist * gray_dist;
  11534. }
  11535. static bool pack_etc1_y_estimate_flipped(const uint8_t* pSrc_pixels,
  11536. int& upper_avg, int& lower_avg, int& left_avg, int& right_avg)
  11537. {
  11538. int sums[2][2];
  11539. #define GET_XY(x, y) pSrc_pixels[(x) + ((y) * 4)]
  11540. sums[0][0] = GET_XY(0, 0) + GET_XY(0, 1) + GET_XY(1, 0) + GET_XY(1, 1);
  11541. sums[1][0] = GET_XY(2, 0) + GET_XY(2, 1) + GET_XY(3, 0) + GET_XY(3, 1);
  11542. sums[0][1] = GET_XY(0, 2) + GET_XY(0, 3) + GET_XY(1, 2) + GET_XY(1, 3);
  11543. sums[1][1] = GET_XY(2, 2) + GET_XY(2, 3) + GET_XY(3, 2) + GET_XY(3, 3);
  11544. upper_avg = (sums[0][0] + sums[1][0] + 4) / 8;
  11545. lower_avg = (sums[0][1] + sums[1][1] + 4) / 8;
  11546. left_avg = (sums[0][0] + sums[0][1] + 4) / 8;
  11547. right_avg = (sums[1][0] + sums[1][1] + 4) / 8;
  11548. #undef GET_XY
  11549. #define GET_XY(x, y, a) gray_distance2(pSrc_pixels[(x) + ((y) * 4)], a)
  11550. int upper_gray_dist = 0, lower_gray_dist = 0, left_gray_dist = 0, right_gray_dist = 0;
  11551. for (uint32_t i = 0; i < 4; i++)
  11552. {
  11553. for (uint32_t j = 0; j < 2; j++)
  11554. {
  11555. upper_gray_dist += GET_XY(i, j, upper_avg);
  11556. lower_gray_dist += GET_XY(i, 2 + j, lower_avg);
  11557. left_gray_dist += GET_XY(j, i, left_avg);
  11558. right_gray_dist += GET_XY(2 + j, i, right_avg);
  11559. }
  11560. }
  11561. #undef GET_XY
  11562. int upper_lower_sum = upper_gray_dist + lower_gray_dist;
  11563. int left_right_sum = left_gray_dist + right_gray_dist;
  11564. return upper_lower_sum < left_right_sum;
  11565. }
  11566. // Base Sel Table
  11567. // XXXXX XX XXX
  11568. static const uint16_t g_etc1_y_solid_block_configs[256] =
  11569. {
  11570. 0,781,64,161,260,192,33,131,96,320,65,162,261,193,34,291,97,224,66,163,262,194,35,549,98,4,67,653,164,195,523,36,99,5,578,68,165,353,196,37,135,100,324,69,166,354,197,38,295,101,228,70,167,
  11571. 355,198,39,553,102,8,71,608,168,199,527,40,103,9,582,72,169,357,200,41,139,104,328,73,170,358,201,42,299,105,232,74,171,359,202,43,557,106,12,75,612,172,203,531,44,107,13,586,76,173,361,
  11572. 204,45,143,108,332,77,174,362,205,46,303,109,236,78,175,363,206,47,561,110,16,79,616,176,207,535,48,111,17,590,80,177,365,208,49,147,112,336,81,178,366,209,50,307,113,240,82,179,367,210,
  11573. 51,565,114,20,83,620,180,211,539,52,115,21,594,84,181,369,212,53,151,116,340,85,182,370,213,54,311,117,244,86,183,371,214,55,569,118,24,87,624,184,215,543,56,119,25,598,88,185,373,216,57,
  11574. 155,120,344,89,186,374,217,58,315,121,248,90,187,375,218,59,573,122,28,91,628,188,219,754,60,123,29,602,92,189,377,220,61,159,124,348,93,190,378,221,62,319,125,252,94,191,379,222,63,882,126
  11575. };
  11576. // individual
  11577. // table base sel0 sel1 sel2 sel3
  11578. static const uint16_t g_etc1_y_solid_block_4i_configs[256] =
  11579. {
  11580. 0xA000,0xA800,0x540B,0xAA01,0xAA01,0xFE00,0xFF00,0xFF00,0x8,0x5515,0x5509,0x5509,0xAA03,0x5508,0x5508,0x9508,0xA508,0xA908,0xAA08,0x5513,0xAA09,0xAA09,0xAA05,0xFF08,0xFF08,0x10,0x551D,0x5511,0x5511,
  11581. 0xAA0B,0x5510,0x5510,0x9510,0xA510,0xA910,0xAA10,0x551B,0xAA11,0xAA11,0xAA0D,0xFF10,0xFF10,0x18,0x5525,0x5519,0x5519,0xAA13,0x5518,0x5518,0x9518,0xA518,0xA918,0xAA18,0x5523,0xAA19,0xAA19,0xAA15,
  11582. 0xFF18,0xFF18,0x20,0x552D,0x5521,0x5521,0xAA1B,0x5520,0x5520,0x9520,0xA520,0xA920,0xAA20,0x552B,0xAA21,0xAA21,0xAA1D,0xFF20,0xFF20,0x28,0x5535,0x5529,0x5529,0xAA23,0x5528,0x5528,0x9528,0xA528,0xA928,
  11583. 0xAA28,0x5533,0xAA29,0xAA29,0xAA25,0xFF28,0xFF28,0x30,0x553D,0x5531,0x5531,0xAA2B,0x5530,0x5530,0x9530,0xA530,0xA930,0xAA30,0x553B,0xAA31,0xAA31,0xAA2D,0xFF30,0xFF30,0x38,0x5545,0x5539,0x5539,0xAA33,
  11584. 0x5538,0x5538,0x9538,0xA538,0xA938,0xAA38,0x5543,0xAA39,0xAA39,0xAA35,0xFF38,0xFF38,0x40,0x554D,0x5541,0x5541,0xAA3B,0x5540,0x5540,0x9540,0xA540,0xA940,0xAA40,0x554B,0xAA41,0xAA41,0xAA3D,0xFF40,0xFF40,
  11585. 0x48,0x5555,0x5549,0x5549,0xAA43,0x5548,0x5548,0x9548,0xA548,0xA948,0xAA48,0x5553,0xAA49,0xAA49,0xAA45,0xFF48,0xFF48,0x50,0x555D,0x5551,0x5551,0xAA4B,0x5550,0x5550,0x9550,0xA550,0xA950,0xAA50,0x555B,
  11586. 0xAA51,0xAA51,0xAA4D,0xFF50,0xFF50,0x58,0x5565,0x5559,0x5559,0xAA53,0x5558,0x5558,0x9558,0xA558,0xA958,0xAA58,0x5563,0xAA59,0xAA59,0xAA55,0xFF58,0xFF58,0x60,0x556D,0x5561,0x5561,0xAA5B,0x5560,0x5560,
  11587. 0x9560,0xA560,0xA960,0xAA60,0x556B,0xAA61,0xAA61,0xAA5D,0xFF60,0xFF60,0x68,0x5575,0x5569,0x5569,0xAA63,0x5568,0x5568,0x9568,0xA568,0xA968,0xAA68,0x5573,0xAA69,0xAA69,0xAA65,0xFF68,0xFF68,0x70,0x557D,
  11588. 0x5571,0x5571,0xAA6B,0x5570,0x5570,0x9570,0xA570,0xA970,0xAA70,0x557B,0xAA71,0xAA71,0xAA6D,0xFF70,0xFF70,0x78,0x78,0x5579,0x5579,0xAA73,0x5578,0x9578,0x2578,0xE6E,0x278
  11589. };
  11590. static const uint16_t g_etc1_y_solid_block_2i_configs[256] =
  11591. {
  11592. 0x416,0x800,0xA00,0x50B,0xA01,0xA01,0xF00,0xF00,0xF00,0x8,0x515,0x509,0x509,0xA03,0x508,0x508,0xF01,0xF01,0xA08,0xA08,0x513,0xA09,0xA09,0xA05,0xF08,0xF08,0x10,0x51D,0x511,0x511,0xA0B,0x510,0x510,0xF09,
  11593. 0xF09,0xA10,0xA10,0x51B,0xA11,0xA11,0xA0D,0xF10,0xF10,0x18,0x525,0x519,0x519,0xA13,0x518,0x518,0xF11,0xF11,0xA18,0xA18,0x523,0xA19,0xA19,0xA15,0xF18,0xF18,0x20,0x52D,0x521,0x521,0xA1B,0x520,0x520,0xF19,
  11594. 0xF19,0xA20,0xA20,0x52B,0xA21,0xA21,0xA1D,0xF20,0xF20,0x28,0x535,0x529,0x529,0xA23,0x528,0x528,0xF21,0xF21,0xA28,0xA28,0x533,0xA29,0xA29,0xA25,0xF28,0xF28,0x30,0x53D,0x531,0x531,0xA2B,0x530,0x530,0xF29,
  11595. 0xF29,0xA30,0xA30,0x53B,0xA31,0xA31,0xA2D,0xF30,0xF30,0x38,0x545,0x539,0x539,0xA33,0x538,0x538,0xF31,0xF31,0xA38,0xA38,0x543,0xA39,0xA39,0xA35,0xF38,0xF38,0x40,0x54D,0x541,0x541,0xA3B,0x540,0x540,0xF39,
  11596. 0xF39,0xA40,0xA40,0x54B,0xA41,0xA41,0xA3D,0xF40,0xF40,0x48,0x555,0x549,0x549,0xA43,0x548,0x548,0xF41,0xF41,0xA48,0xA48,0x553,0xA49,0xA49,0xA45,0xF48,0xF48,0x50,0x55D,0x551,0x551,0xA4B,0x550,0x550,0xF49,
  11597. 0xF49,0xA50,0xA50,0x55B,0xA51,0xA51,0xA4D,0xF50,0xF50,0x58,0x565,0x559,0x559,0xA53,0x558,0x558,0xF51,0xF51,0xA58,0xA58,0x563,0xA59,0xA59,0xA55,0xF58,0xF58,0x60,0x56D,0x561,0x561,0xA5B,0x560,0x560,0xF59,
  11598. 0xF59,0xA60,0xA60,0x56B,0xA61,0xA61,0xA5D,0xF60,0xF60,0x68,0x575,0x569,0x569,0xA63,0x568,0x568,0xF61,0xF61,0xA68,0xA68,0x573,0xA69,0xA69,0xA65,0xF68,0xF68,0x70,0x57D,0x571,0x571,0xA6B,0x570,0x570,0xF69,
  11599. 0xF69,0xA70,0xA70,0x57B,0xA71,0xA71,0xA6D,0xF70,0xF70,0x78,0x78,0x579,0x579,0xA73,0x578,0x578,0xE6E,0x278
  11600. };
  11601. static const uint16_t g_etc1_y_solid_block_1i_configs[256] =
  11602. {
  11603. 0x0,0x116,0x200,0x200,0x10B,0x201,0x201,0x300,0x300,0x8,0x115,0x109,0x109,0x203,0x108,0x108,0x114,0x301,0x204,0x208,0x208,0x113,0x209,0x209,0x205,0x308,0x10,0x11D,0x111,0x111,0x20B,0x110,0x110,0x11C,0x309,
  11604. 0x20C,0x210,0x210,0x11B,0x211,0x211,0x20D,0x310,0x18,0x125,0x119,0x119,0x213,0x118,0x118,0x124,0x311,0x214,0x218,0x218,0x123,0x219,0x219,0x215,0x318,0x20,0x12D,0x121,0x121,0x21B,0x120,0x120,0x12C,0x319,0x21C,
  11605. 0x220,0x220,0x12B,0x221,0x221,0x21D,0x320,0x28,0x135,0x129,0x129,0x223,0x128,0x128,0x134,0x321,0x224,0x228,0x228,0x133,0x229,0x229,0x225,0x328,0x30,0x13D,0x131,0x131,0x22B,0x130,0x130,0x13C,0x329,0x22C,0x230,
  11606. 0x230,0x13B,0x231,0x231,0x22D,0x330,0x38,0x145,0x139,0x139,0x233,0x138,0x138,0x144,0x331,0x234,0x238,0x238,0x143,0x239,0x239,0x235,0x338,0x40,0x14D,0x141,0x141,0x23B,0x140,0x140,0x14C,0x339,0x23C,0x240,0x240,
  11607. 0x14B,0x241,0x241,0x23D,0x340,0x48,0x155,0x149,0x149,0x243,0x148,0x148,0x154,0x341,0x244,0x248,0x248,0x153,0x249,0x249,0x245,0x348,0x50,0x15D,0x151,0x151,0x24B,0x150,0x150,0x15C,0x349,0x24C,0x250,0x250,0x15B,
  11608. 0x251,0x251,0x24D,0x350,0x58,0x165,0x159,0x159,0x253,0x158,0x158,0x164,0x351,0x254,0x258,0x258,0x163,0x259,0x259,0x255,0x358,0x60,0x16D,0x161,0x161,0x25B,0x160,0x160,0x16C,0x359,0x25C,0x260,0x260,0x16B,0x261,
  11609. 0x261,0x25D,0x360,0x68,0x175,0x169,0x169,0x263,0x168,0x168,0x174,0x361,0x264,0x268,0x268,0x173,0x269,0x269,0x265,0x368,0x70,0x17D,0x171,0x171,0x26B,0x170,0x170,0x17C,0x369,0x26C,0x270,0x270,0x17B,0x271,0x271,
  11610. 0x26D,0x370,0x78,0x78,0x179,0x179,0x273,0x178,0x178,0x26E,0x278
  11611. };
  11612. // We don't have any useful hints to accelerate single channel ETC1, so we need to real-time encode from scratch.
  11613. bool transcode_uastc_to_etc1(const uastc_block& src_blk, void* pDst, uint32_t channel)
  11614. {
  11615. unpacked_uastc_block unpacked_src_blk;
  11616. if (!unpack_uastc(src_blk, unpacked_src_blk, false))
  11617. return false;
  11618. #if 0
  11619. for (uint32_t individ = 0; individ < 2; individ++)
  11620. {
  11621. uint32_t overall_error = 0;
  11622. for (uint32_t c = 0; c < 256; c++)
  11623. {
  11624. uint32_t best_err = UINT32_MAX;
  11625. uint32_t best_individ = 0;
  11626. uint32_t best_base = 0;
  11627. uint32_t best_sels[4] = { 0,0,0,0 };
  11628. uint32_t best_table = 0;
  11629. const uint32_t limit = individ ? 16 : 32;
  11630. for (uint32_t table = 0; table < 8; table++)
  11631. {
  11632. for (uint32_t base = 0; base < limit; base++)
  11633. {
  11634. uint32_t total_e = 0;
  11635. uint32_t sels[4] = { 0,0,0,0 };
  11636. const uint32_t N = 4;
  11637. for (uint32_t i = 0; i < basisu::minimum<uint32_t>(N, (256 - c)); i++)
  11638. {
  11639. uint32_t best_sel_e = UINT32_MAX;
  11640. uint32_t best_sel = 0;
  11641. for (uint32_t sel = 0; sel < 4; sel++)
  11642. {
  11643. int val = individ ? ((base << 4) | base) : ((base << 3) | (base >> 2));
  11644. val = clamp255(val + g_etc1_inten_tables[table][sel]);
  11645. int e = iabs(val - clamp255(c + i));
  11646. if (e < best_sel_e)
  11647. {
  11648. best_sel_e = e;
  11649. best_sel = sel;
  11650. }
  11651. } // sel
  11652. sels[i] = best_sel;
  11653. total_e += best_sel_e * best_sel_e;
  11654. } // i
  11655. if (total_e < best_err)
  11656. {
  11657. best_err = total_e;
  11658. best_individ = individ;
  11659. best_base = base;
  11660. memcpy(best_sels, sels, sizeof(best_sels));
  11661. best_table = table;
  11662. }
  11663. } // base
  11664. } // table
  11665. //printf("%u: %u,%u,%u,%u,%u,%u,%u,%u\n", c, best_err, best_individ, best_table, best_base, best_sels[0], best_sels[1], best_sels[2], best_sels[3]);
  11666. uint32_t encoded = best_table | (best_base << 3) |
  11667. (best_sels[0] << 8) |
  11668. (best_sels[1] << 10) |
  11669. (best_sels[2] << 12) |
  11670. (best_sels[3] << 14);
  11671. printf("0x%X,", encoded);
  11672. overall_error += best_err;
  11673. } // c
  11674. printf("\n");
  11675. printf("Overall error: %u\n", overall_error);
  11676. } // individ
  11677. exit(0);
  11678. #endif
  11679. #if 0
  11680. for (uint32_t individ = 0; individ < 2; individ++)
  11681. {
  11682. uint32_t overall_error = 0;
  11683. for (uint32_t c = 0; c < 256; c++)
  11684. {
  11685. uint32_t best_err = UINT32_MAX;
  11686. uint32_t best_individ = 0;
  11687. uint32_t best_base = 0;
  11688. uint32_t best_sels[4] = { 0,0,0,0 };
  11689. uint32_t best_table = 0;
  11690. const uint32_t limit = individ ? 16 : 32;
  11691. for (uint32_t table = 0; table < 8; table++)
  11692. {
  11693. for (uint32_t base = 0; base < limit; base++)
  11694. {
  11695. uint32_t total_e = 0;
  11696. uint32_t sels[4] = { 0,0,0,0 };
  11697. const uint32_t N = 1;
  11698. for (uint32_t i = 0; i < basisu::minimum<uint32_t>(N, (256 - c)); i++)
  11699. {
  11700. uint32_t best_sel_e = UINT32_MAX;
  11701. uint32_t best_sel = 0;
  11702. for (uint32_t sel = 0; sel < 4; sel++)
  11703. {
  11704. int val = individ ? ((base << 4) | base) : ((base << 3) | (base >> 2));
  11705. val = clamp255(val + g_etc1_inten_tables[table][sel]);
  11706. int e = iabs(val - clamp255(c + i));
  11707. if (e < best_sel_e)
  11708. {
  11709. best_sel_e = e;
  11710. best_sel = sel;
  11711. }
  11712. } // sel
  11713. sels[i] = best_sel;
  11714. total_e += best_sel_e * best_sel_e;
  11715. } // i
  11716. if (total_e < best_err)
  11717. {
  11718. best_err = total_e;
  11719. best_individ = individ;
  11720. best_base = base;
  11721. memcpy(best_sels, sels, sizeof(best_sels));
  11722. best_table = table;
  11723. }
  11724. } // base
  11725. } // table
  11726. //printf("%u: %u,%u,%u,%u,%u,%u,%u,%u\n", c, best_err, best_individ, best_table, best_base, best_sels[0], best_sels[1], best_sels[2], best_sels[3]);
  11727. uint32_t encoded = best_table | (best_base << 3) |
  11728. (best_sels[0] << 8) |
  11729. (best_sels[1] << 10) |
  11730. (best_sels[2] << 12) |
  11731. (best_sels[3] << 14);
  11732. printf("0x%X,", encoded);
  11733. overall_error += best_err;
  11734. } // c
  11735. printf("\n");
  11736. printf("Overall error: %u\n", overall_error);
  11737. } // individ
  11738. exit(0);
  11739. #endif
  11740. decoder_etc_block& dst_blk = *static_cast<decoder_etc_block*>(pDst);
  11741. if (unpacked_src_blk.m_mode == UASTC_MODE_INDEX_SOLID_COLOR)
  11742. {
  11743. const uint32_t y = unpacked_src_blk.m_solid_color[channel];
  11744. const uint32_t encoded_config = g_etc1_y_solid_block_configs[y];
  11745. const uint32_t base = encoded_config & 31;
  11746. const uint32_t sel = (encoded_config >> 5) & 3;
  11747. const uint32_t table = encoded_config >> 7;
  11748. dst_blk.m_bytes[3] = (uint8_t)(2 | (table << 5) | (table << 2));
  11749. dst_blk.m_bytes[0] = (uint8_t)(base << 3);
  11750. dst_blk.m_bytes[1] = (uint8_t)(base << 3);
  11751. dst_blk.m_bytes[2] = (uint8_t)(base << 3);
  11752. memcpy(dst_blk.m_bytes + 4, &s_etc1_solid_selectors[sel][0], 4);
  11753. return true;
  11754. }
  11755. color32 block_pixels[4][4];
  11756. const bool unpack_srgb = false;
  11757. if (!unpack_uastc(unpacked_src_blk, &block_pixels[0][0], unpack_srgb))
  11758. return false;
  11759. uint8_t block_y[4][4];
  11760. for (uint32_t i = 0; i < 16; i++)
  11761. ((uint8_t*)block_y)[i] = ((color32*)block_pixels)[i][channel];
  11762. int upper_avg, lower_avg, left_avg, right_avg;
  11763. bool flip = pack_etc1_y_estimate_flipped(&block_y[0][0], upper_avg, lower_avg, left_avg, right_avg);
  11764. // non-flipped: | |
  11765. // vs.
  11766. // flipped: --
  11767. // --
  11768. uint32_t low[2] = { 255, 255 }, high[2] = { 0, 0 };
  11769. if (flip)
  11770. {
  11771. for (uint32_t y = 0; y < 2; y++)
  11772. {
  11773. for (uint32_t x = 0; x < 4; x++)
  11774. {
  11775. const uint32_t v = block_y[y][x];
  11776. low[0] = basisu::minimum(low[0], v);
  11777. high[0] = basisu::maximum(high[0], v);
  11778. }
  11779. }
  11780. for (uint32_t y = 2; y < 4; y++)
  11781. {
  11782. for (uint32_t x = 0; x < 4; x++)
  11783. {
  11784. const uint32_t v = block_y[y][x];
  11785. low[1] = basisu::minimum(low[1], v);
  11786. high[1] = basisu::maximum(high[1], v);
  11787. }
  11788. }
  11789. }
  11790. else
  11791. {
  11792. for (uint32_t y = 0; y < 4; y++)
  11793. {
  11794. for (uint32_t x = 0; x < 2; x++)
  11795. {
  11796. const uint32_t v = block_y[y][x];
  11797. low[0] = basisu::minimum(low[0], v);
  11798. high[0] = basisu::maximum(high[0], v);
  11799. }
  11800. }
  11801. for (uint32_t y = 0; y < 4; y++)
  11802. {
  11803. for (uint32_t x = 2; x < 4; x++)
  11804. {
  11805. const uint32_t v = block_y[y][x];
  11806. low[1] = basisu::minimum(low[1], v);
  11807. high[1] = basisu::maximum(high[1], v);
  11808. }
  11809. }
  11810. }
  11811. const uint32_t range[2] = { high[0] - low[0], high[1] - low[1] };
  11812. dst_blk.m_bytes[3] = (uint8_t)((int)flip);
  11813. if ((range[0] <= 3) && (range[1] <= 3))
  11814. {
  11815. // This is primarily for better gradients.
  11816. dst_blk.m_bytes[0] = 0;
  11817. dst_blk.m_bytes[1] = 0;
  11818. dst_blk.m_bytes[2] = 0;
  11819. uint16_t l_bitmask = 0, h_bitmask = 0;
  11820. for (uint32_t subblock = 0; subblock < 2; subblock++)
  11821. {
  11822. const uint32_t encoded = (range[subblock] == 0) ? g_etc1_y_solid_block_1i_configs[low[subblock]] : ((range[subblock] < 2) ? g_etc1_y_solid_block_2i_configs[low[subblock]] : g_etc1_y_solid_block_4i_configs[low[subblock]]);
  11823. const uint32_t table = encoded & 7;
  11824. const uint32_t base = (encoded >> 3) & 31;
  11825. assert(base <= 15);
  11826. const uint32_t sels[4] = { (encoded >> 8) & 3, (encoded >> 10) & 3, (encoded >> 12) & 3, (encoded >> 14) & 3 };
  11827. dst_blk.m_bytes[3] |= (uint8_t)(table << (subblock ? 2 : 5));
  11828. const uint32_t sv = base << (subblock ? 0 : 4);
  11829. dst_blk.m_bytes[0] |= (uint8_t)(sv);
  11830. dst_blk.m_bytes[1] |= (uint8_t)(sv);
  11831. dst_blk.m_bytes[2] |= (uint8_t)(sv);
  11832. if (flip)
  11833. {
  11834. uint32_t ofs = subblock * 2;
  11835. for (uint32_t y = 0; y < 2; y++)
  11836. {
  11837. for (uint32_t x = 0; x < 4; x++)
  11838. {
  11839. uint32_t t = block_y[y + subblock * 2][x];
  11840. assert(t >= low[subblock] && t <= high[subblock]);
  11841. t -= low[subblock];
  11842. assert(t <= 3);
  11843. t = g_selector_index_to_etc1[sels[t]];
  11844. assert(ofs < 16);
  11845. l_bitmask |= ((t & 1) << ofs);
  11846. h_bitmask |= ((t >> 1) << ofs);
  11847. ofs += 4;
  11848. }
  11849. ofs = (int)ofs + 1 - 4 * 4;
  11850. }
  11851. }
  11852. else
  11853. {
  11854. uint32_t ofs = (subblock * 2) * 4;
  11855. for (uint32_t x = 0; x < 2; x++)
  11856. {
  11857. for (uint32_t y = 0; y < 4; y++)
  11858. {
  11859. uint32_t t = block_y[y][x + subblock * 2];
  11860. assert(t >= low[subblock] && t <= high[subblock]);
  11861. t -= low[subblock];
  11862. assert(t <= 3);
  11863. t = g_selector_index_to_etc1[sels[t]];
  11864. assert(ofs < 16);
  11865. l_bitmask |= ((t & 1) << ofs);
  11866. h_bitmask |= ((t >> 1) << ofs);
  11867. ++ofs;
  11868. }
  11869. }
  11870. }
  11871. } // subblock
  11872. dst_blk.m_bytes[7] = (uint8_t)(l_bitmask);
  11873. dst_blk.m_bytes[6] = (uint8_t)(l_bitmask >> 8);
  11874. dst_blk.m_bytes[5] = (uint8_t)(h_bitmask);
  11875. dst_blk.m_bytes[4] = (uint8_t)(h_bitmask >> 8);
  11876. return true;
  11877. }
  11878. uint32_t y0 = ((flip ? upper_avg : left_avg) * 31 + 127) / 255;
  11879. uint32_t y1 = ((flip ? lower_avg : right_avg) * 31 + 127) / 255;
  11880. bool diff = true;
  11881. int dy = y1 - y0;
  11882. if ((dy < cETC1ColorDeltaMin) || (dy > cETC1ColorDeltaMax))
  11883. {
  11884. diff = false;
  11885. y0 = ((flip ? upper_avg : left_avg) * 15 + 127) / 255;
  11886. y1 = ((flip ? lower_avg : right_avg) * 15 + 127) / 255;
  11887. dst_blk.m_bytes[0] = (uint8_t)(y1 | (y0 << 4));
  11888. dst_blk.m_bytes[1] = (uint8_t)(y1 | (y0 << 4));
  11889. dst_blk.m_bytes[2] = (uint8_t)(y1 | (y0 << 4));
  11890. }
  11891. else
  11892. {
  11893. dy = basisu::clamp<int>(dy, cETC1ColorDeltaMin, cETC1ColorDeltaMax);
  11894. y1 = y0 + dy;
  11895. if (dy < 0) dy += 8;
  11896. dst_blk.m_bytes[0] = (uint8_t)((y0 << 3) | dy);
  11897. dst_blk.m_bytes[1] = (uint8_t)((y0 << 3) | dy);
  11898. dst_blk.m_bytes[2] = (uint8_t)((y0 << 3) | dy);
  11899. dst_blk.m_bytes[3] |= 2;
  11900. }
  11901. const uint32_t base_y[2] = { diff ? ((y0 << 3) | (y0 >> 2)) : ((y0 << 4) | y0), diff ? ((y1 << 3) | (y1 >> 2)) : ((y1 << 4) | y1) };
  11902. uint32_t enc_range[2];
  11903. for (uint32_t subset = 0; subset < 2; subset++)
  11904. {
  11905. const int pos = basisu::iabs((int)high[subset] - (int)base_y[subset]);
  11906. const int neg = basisu::iabs((int)base_y[subset] - (int)low[subset]);
  11907. enc_range[subset] = basisu::maximum(pos, neg);
  11908. }
  11909. uint16_t l_bitmask = 0, h_bitmask = 0;
  11910. for (uint32_t subblock = 0; subblock < 2; subblock++)
  11911. {
  11912. if ((!diff) && (range[subblock] <= 3))
  11913. {
  11914. const uint32_t encoded = (range[subblock] == 0) ? g_etc1_y_solid_block_1i_configs[low[subblock]] : ((range[subblock] < 2) ? g_etc1_y_solid_block_2i_configs[low[subblock]] : g_etc1_y_solid_block_4i_configs[low[subblock]]);
  11915. const uint32_t table = encoded & 7;
  11916. const uint32_t base = (encoded >> 3) & 31;
  11917. assert(base <= 15);
  11918. const uint32_t sels[4] = { (encoded >> 8) & 3, (encoded >> 10) & 3, (encoded >> 12) & 3, (encoded >> 14) & 3 };
  11919. dst_blk.m_bytes[3] |= (uint8_t)(table << (subblock ? 2 : 5));
  11920. const uint32_t mask = ~(0xF << (subblock ? 0 : 4));
  11921. dst_blk.m_bytes[0] &= mask;
  11922. dst_blk.m_bytes[1] &= mask;
  11923. dst_blk.m_bytes[2] &= mask;
  11924. const uint32_t sv = base << (subblock ? 0 : 4);
  11925. dst_blk.m_bytes[0] |= (uint8_t)(sv);
  11926. dst_blk.m_bytes[1] |= (uint8_t)(sv);
  11927. dst_blk.m_bytes[2] |= (uint8_t)(sv);
  11928. if (flip)
  11929. {
  11930. uint32_t ofs = subblock * 2;
  11931. for (uint32_t y = 0; y < 2; y++)
  11932. {
  11933. for (uint32_t x = 0; x < 4; x++)
  11934. {
  11935. uint32_t t = block_y[y + subblock * 2][x];
  11936. assert(t >= low[subblock] && t <= high[subblock]);
  11937. t -= low[subblock];
  11938. assert(t <= 3);
  11939. t = g_selector_index_to_etc1[sels[t]];
  11940. assert(ofs < 16);
  11941. l_bitmask |= ((t & 1) << ofs);
  11942. h_bitmask |= ((t >> 1) << ofs);
  11943. ofs += 4;
  11944. }
  11945. ofs = (int)ofs + 1 - 4 * 4;
  11946. }
  11947. }
  11948. else
  11949. {
  11950. uint32_t ofs = (subblock * 2) * 4;
  11951. for (uint32_t x = 0; x < 2; x++)
  11952. {
  11953. for (uint32_t y = 0; y < 4; y++)
  11954. {
  11955. uint32_t t = block_y[y][x + subblock * 2];
  11956. assert(t >= low[subblock] && t <= high[subblock]);
  11957. t -= low[subblock];
  11958. assert(t <= 3);
  11959. t = g_selector_index_to_etc1[sels[t]];
  11960. assert(ofs < 16);
  11961. l_bitmask |= ((t & 1) << ofs);
  11962. h_bitmask |= ((t >> 1) << ofs);
  11963. ++ofs;
  11964. }
  11965. }
  11966. }
  11967. continue;
  11968. } // if
  11969. uint32_t best_err = UINT32_MAX;
  11970. uint8_t best_sels[8];
  11971. uint32_t best_inten = 0;
  11972. const int base = base_y[subblock];
  11973. const int low_limit = -base;
  11974. const int high_limit = 255 - base;
  11975. assert(low_limit <= 0 && high_limit >= 0);
  11976. uint32_t inten_table_mask = 0xFF;
  11977. const uint32_t er = enc_range[subblock];
  11978. // Each one of these tables is expensive to evaluate, so let's only examine the ones we know may be useful.
  11979. if (er <= 51)
  11980. {
  11981. inten_table_mask = 0xF;
  11982. if (er > 22)
  11983. inten_table_mask &= ~(1 << 0);
  11984. if ((er < 4) || (er > 39))
  11985. inten_table_mask &= ~(1 << 1);
  11986. if (er < 9)
  11987. inten_table_mask &= ~(1 << 2);
  11988. if (er < 12)
  11989. inten_table_mask &= ~(1 << 3);
  11990. }
  11991. else
  11992. {
  11993. inten_table_mask &= ~((1 << 0) | (1 << 1));
  11994. if (er > 60)
  11995. inten_table_mask &= ~(1 << 2);
  11996. if (er > 89)
  11997. inten_table_mask &= ~(1 << 3);
  11998. if (er > 120)
  11999. inten_table_mask &= ~(1 << 4);
  12000. if (er > 136)
  12001. inten_table_mask &= ~(1 << 5);
  12002. if (er > 174)
  12003. inten_table_mask &= ~(1 << 6);
  12004. }
  12005. for (uint32_t inten = 0; inten < 8; inten++)
  12006. {
  12007. if ((inten_table_mask & (1 << inten)) == 0)
  12008. continue;
  12009. const int t0 = basisu::maximum(low_limit, g_etc1_inten_tables[inten][0]);
  12010. const int t1 = basisu::maximum(low_limit, g_etc1_inten_tables[inten][1]);
  12011. const int t2 = basisu::minimum(high_limit, g_etc1_inten_tables[inten][2]);
  12012. const int t3 = basisu::minimum(high_limit, g_etc1_inten_tables[inten][3]);
  12013. assert((t0 <= t1) && (t1 <= t2) && (t2 <= t3));
  12014. const int tv[4] = { t2, t3, t1, t0 };
  12015. const int thresh01 = t0 + t1;
  12016. const int thresh12 = t1 + t2;
  12017. const int thresh23 = t2 + t3;
  12018. assert(thresh01 <= thresh12 && thresh12 <= thresh23);
  12019. static const uint8_t s_table[4] = { 1, 0, 2, 3 };
  12020. uint32_t total_err = 0;
  12021. uint8_t sels[8];
  12022. if (flip)
  12023. {
  12024. if (((int)high[subblock] - base) * 2 < thresh01)
  12025. {
  12026. memset(sels, 3, 8);
  12027. for (uint32_t y = 0; y < 2; y++)
  12028. {
  12029. for (uint32_t x = 0; x < 4; x++)
  12030. {
  12031. const int delta = (int)block_y[y + subblock * 2][x] - base;
  12032. const uint32_t c = 3;
  12033. uint32_t e = basisu::iabs(tv[c] - delta);
  12034. total_err += e * e;
  12035. }
  12036. if (total_err >= best_err)
  12037. break;
  12038. }
  12039. }
  12040. else if (((int)low[subblock] - base) * 2 >= thresh23)
  12041. {
  12042. memset(sels, 1, 8);
  12043. for (uint32_t y = 0; y < 2; y++)
  12044. {
  12045. for (uint32_t x = 0; x < 4; x++)
  12046. {
  12047. const int delta = (int)block_y[y + subblock * 2][x] - base;
  12048. const uint32_t c = 1;
  12049. uint32_t e = basisu::iabs(tv[c] - delta);
  12050. total_err += e * e;
  12051. }
  12052. if (total_err >= best_err)
  12053. break;
  12054. }
  12055. }
  12056. else
  12057. {
  12058. for (uint32_t y = 0; y < 2; y++)
  12059. {
  12060. for (uint32_t x = 0; x < 4; x++)
  12061. {
  12062. const int delta = (int)block_y[y + subblock * 2][x] - base;
  12063. const int delta2 = delta * 2;
  12064. uint32_t c = s_table[(delta2 < thresh01) + (delta2 < thresh12) + (delta2 < thresh23)];
  12065. sels[y * 4 + x] = (uint8_t)c;
  12066. uint32_t e = basisu::iabs(tv[c] - delta);
  12067. total_err += e * e;
  12068. }
  12069. if (total_err >= best_err)
  12070. break;
  12071. }
  12072. }
  12073. }
  12074. else
  12075. {
  12076. if (((int)high[subblock] - base) * 2 < thresh01)
  12077. {
  12078. memset(sels, 3, 8);
  12079. for (uint32_t y = 0; y < 4; y++)
  12080. {
  12081. for (uint32_t x = 0; x < 2; x++)
  12082. {
  12083. const int delta = (int)block_y[y][x + subblock * 2] - base;
  12084. const uint32_t c = 3;
  12085. uint32_t e = basisu::iabs(tv[c] - delta);
  12086. total_err += e * e;
  12087. }
  12088. if (total_err >= best_err)
  12089. break;
  12090. }
  12091. }
  12092. else if (((int)low[subblock] - base) * 2 >= thresh23)
  12093. {
  12094. memset(sels, 1, 8);
  12095. for (uint32_t y = 0; y < 4; y++)
  12096. {
  12097. for (uint32_t x = 0; x < 2; x++)
  12098. {
  12099. const int delta = (int)block_y[y][x + subblock * 2] - base;
  12100. const uint32_t c = 1;
  12101. uint32_t e = basisu::iabs(tv[c] - delta);
  12102. total_err += e * e;
  12103. }
  12104. if (total_err >= best_err)
  12105. break;
  12106. }
  12107. }
  12108. else
  12109. {
  12110. for (uint32_t y = 0; y < 4; y++)
  12111. {
  12112. for (uint32_t x = 0; x < 2; x++)
  12113. {
  12114. const int delta = (int)block_y[y][x + subblock * 2] - base;
  12115. const int delta2 = delta * 2;
  12116. uint32_t c = s_table[(delta2 < thresh01) + (delta2 < thresh12) + (delta2 < thresh23)];
  12117. sels[y * 2 + x] = (uint8_t)c;
  12118. uint32_t e = basisu::iabs(tv[c] - delta);
  12119. total_err += e * e;
  12120. }
  12121. if (total_err >= best_err)
  12122. break;
  12123. }
  12124. }
  12125. }
  12126. if (total_err < best_err)
  12127. {
  12128. best_err = total_err;
  12129. best_inten = inten;
  12130. memcpy(best_sels, sels, 8);
  12131. }
  12132. } // inten
  12133. //g_inten_hist[best_inten][enc_range[subblock]]++;
  12134. dst_blk.m_bytes[3] |= (uint8_t)(best_inten << (subblock ? 2 : 5));
  12135. if (flip)
  12136. {
  12137. uint32_t ofs = subblock * 2;
  12138. for (uint32_t y = 0; y < 2; y++)
  12139. {
  12140. for (uint32_t x = 0; x < 4; x++)
  12141. {
  12142. uint32_t t = best_sels[y * 4 + x];
  12143. assert(ofs < 16);
  12144. l_bitmask |= ((t & 1) << ofs);
  12145. h_bitmask |= ((t >> 1) << ofs);
  12146. ofs += 4;
  12147. }
  12148. ofs = (int)ofs + 1 - 4 * 4;
  12149. }
  12150. }
  12151. else
  12152. {
  12153. uint32_t ofs = (subblock * 2) * 4;
  12154. for (uint32_t x = 0; x < 2; x++)
  12155. {
  12156. for (uint32_t y = 0; y < 4; y++)
  12157. {
  12158. uint32_t t = best_sels[y * 2 + x];
  12159. assert(ofs < 16);
  12160. l_bitmask |= ((t & 1) << ofs);
  12161. h_bitmask |= ((t >> 1) << ofs);
  12162. ++ofs;
  12163. }
  12164. }
  12165. }
  12166. } // subblock
  12167. dst_blk.m_bytes[7] = (uint8_t)(l_bitmask);
  12168. dst_blk.m_bytes[6] = (uint8_t)(l_bitmask >> 8);
  12169. dst_blk.m_bytes[5] = (uint8_t)(h_bitmask);
  12170. dst_blk.m_bytes[4] = (uint8_t)(h_bitmask >> 8);
  12171. return true;
  12172. }
  12173. const uint32_t ETC2_EAC_MIN_VALUE_SELECTOR = 3, ETC2_EAC_MAX_VALUE_SELECTOR = 7;
  12174. void transcode_uastc_to_etc2_eac_a8(unpacked_uastc_block& unpacked_src_blk, color32 block_pixels[4][4], void* pDst)
  12175. {
  12176. eac_block& dst = *static_cast<eac_block*>(pDst);
  12177. const color32* pSrc_pixels = &block_pixels[0][0];
  12178. if ((!g_uastc_mode_has_alpha[unpacked_src_blk.m_mode]) || (unpacked_src_blk.m_mode == UASTC_MODE_INDEX_SOLID_COLOR))
  12179. {
  12180. const uint32_t a = (unpacked_src_blk.m_mode == UASTC_MODE_INDEX_SOLID_COLOR) ? unpacked_src_blk.m_solid_color[3] : 255;
  12181. dst.m_base = a;
  12182. dst.m_table = 13;
  12183. dst.m_multiplier = 1;
  12184. memcpy(dst.m_selectors, g_etc2_eac_a8_sel4, sizeof(g_etc2_eac_a8_sel4));
  12185. return;
  12186. }
  12187. uint32_t min_a = 255, max_a = 0;
  12188. for (uint32_t i = 0; i < 16; i++)
  12189. {
  12190. min_a = basisu::minimum<uint32_t>(min_a, pSrc_pixels[i].a);
  12191. max_a = basisu::maximum<uint32_t>(max_a, pSrc_pixels[i].a);
  12192. }
  12193. if (min_a == max_a)
  12194. {
  12195. dst.m_base = min_a;
  12196. dst.m_table = 13;
  12197. dst.m_multiplier = 1;
  12198. memcpy(dst.m_selectors, g_etc2_eac_a8_sel4, sizeof(g_etc2_eac_a8_sel4));
  12199. return;
  12200. }
  12201. const uint32_t table = unpacked_src_blk.m_etc2_hints & 0xF;
  12202. const int multiplier = unpacked_src_blk.m_etc2_hints >> 4;
  12203. assert(multiplier >= 1);
  12204. dst.m_multiplier = multiplier;
  12205. dst.m_table = table;
  12206. const float range = (float)(g_eac_modifier_table[dst.m_table][ETC2_EAC_MAX_VALUE_SELECTOR] - g_eac_modifier_table[dst.m_table][ETC2_EAC_MIN_VALUE_SELECTOR]);
  12207. const int center = (int)roundf(basisu::lerp((float)min_a, (float)max_a, (float)(0 - g_eac_modifier_table[dst.m_table][ETC2_EAC_MIN_VALUE_SELECTOR]) / range));
  12208. dst.m_base = center;
  12209. const int8_t* pTable = &g_eac_modifier_table[dst.m_table][0];
  12210. uint32_t vals[8];
  12211. for (uint32_t j = 0; j < 8; j++)
  12212. vals[j] = clamp255(center + (pTable[j] * multiplier));
  12213. uint64_t sels = 0;
  12214. for (uint32_t i = 0; i < 16; i++)
  12215. {
  12216. const uint32_t a = block_pixels[i & 3][i >> 2].a;
  12217. const uint32_t err0 = (basisu::iabs(vals[0] - a) << 3) | 0;
  12218. const uint32_t err1 = (basisu::iabs(vals[1] - a) << 3) | 1;
  12219. const uint32_t err2 = (basisu::iabs(vals[2] - a) << 3) | 2;
  12220. const uint32_t err3 = (basisu::iabs(vals[3] - a) << 3) | 3;
  12221. const uint32_t err4 = (basisu::iabs(vals[4] - a) << 3) | 4;
  12222. const uint32_t err5 = (basisu::iabs(vals[5] - a) << 3) | 5;
  12223. const uint32_t err6 = (basisu::iabs(vals[6] - a) << 3) | 6;
  12224. const uint32_t err7 = (basisu::iabs(vals[7] - a) << 3) | 7;
  12225. const uint32_t min_err = basisu::minimum(basisu::minimum(basisu::minimum(basisu::minimum(basisu::minimum(basisu::minimum(err0, err1, err2), err3), err4), err5), err6), err7);
  12226. const uint64_t best_index = min_err & 7;
  12227. sels |= (best_index << (45 - i * 3));
  12228. }
  12229. dst.set_selector_bits(sels);
  12230. }
  12231. bool transcode_uastc_to_etc2_rgba(const uastc_block& src_blk, void* pDst)
  12232. {
  12233. eac_block& dst_etc2_eac_a8_blk = *static_cast<eac_block*>(pDst);
  12234. decoder_etc_block& dst_etc1_blk = static_cast<decoder_etc_block*>(pDst)[1];
  12235. unpacked_uastc_block unpacked_src_blk;
  12236. if (!unpack_uastc(src_blk, unpacked_src_blk, false))
  12237. return false;
  12238. color32 block_pixels[4][4];
  12239. if (unpacked_src_blk.m_mode != UASTC_MODE_INDEX_SOLID_COLOR)
  12240. {
  12241. const bool unpack_srgb = false;
  12242. if (!unpack_uastc(unpacked_src_blk, &block_pixels[0][0], unpack_srgb))
  12243. return false;
  12244. }
  12245. transcode_uastc_to_etc2_eac_a8(unpacked_src_blk, block_pixels, &dst_etc2_eac_a8_blk);
  12246. transcode_uastc_to_etc1(unpacked_src_blk, block_pixels, &dst_etc1_blk);
  12247. return true;
  12248. }
  12249. static const uint8_t s_uastc5_to_bc1[32] = { 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 1, 1, 1, 1, 1 };
  12250. static const uint8_t s_uastc4_to_bc1[16] = { 0, 0, 0, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 1, 1, 1 };
  12251. static const uint8_t s_uastc3_to_bc1[8] = { 0, 0, 2, 2, 3, 3, 1, 1 };
  12252. static const uint8_t s_uastc2_to_bc1[4] = { 0, 2, 3, 1 };
  12253. static const uint8_t s_uastc1_to_bc1[2] = { 0, 1 };
  12254. const uint8_t* s_uastc_to_bc1_weights[6] = { nullptr, s_uastc1_to_bc1, s_uastc2_to_bc1, s_uastc3_to_bc1, s_uastc4_to_bc1, s_uastc5_to_bc1 };
  12255. void encode_bc4(void* pDst, const uint8_t* pPixels, uint32_t stride)
  12256. {
  12257. uint32_t min0_v, max0_v, min1_v, max1_v,min2_v, max2_v, min3_v, max3_v;
  12258. {
  12259. min0_v = max0_v = pPixels[0 * stride];
  12260. min1_v = max1_v = pPixels[1 * stride];
  12261. min2_v = max2_v = pPixels[2 * stride];
  12262. min3_v = max3_v = pPixels[3 * stride];
  12263. }
  12264. {
  12265. uint32_t v0 = pPixels[4 * stride]; min0_v = basisu::minimum(min0_v, v0); max0_v = basisu::maximum(max0_v, v0);
  12266. uint32_t v1 = pPixels[5 * stride]; min1_v = basisu::minimum(min1_v, v1); max1_v = basisu::maximum(max1_v, v1);
  12267. uint32_t v2 = pPixels[6 * stride]; min2_v = basisu::minimum(min2_v, v2); max2_v = basisu::maximum(max2_v, v2);
  12268. uint32_t v3 = pPixels[7 * stride]; min3_v = basisu::minimum(min3_v, v3); max3_v = basisu::maximum(max3_v, v3);
  12269. }
  12270. {
  12271. uint32_t v0 = pPixels[8 * stride]; min0_v = basisu::minimum(min0_v, v0); max0_v = basisu::maximum(max0_v, v0);
  12272. uint32_t v1 = pPixels[9 * stride]; min1_v = basisu::minimum(min1_v, v1); max1_v = basisu::maximum(max1_v, v1);
  12273. uint32_t v2 = pPixels[10 * stride]; min2_v = basisu::minimum(min2_v, v2); max2_v = basisu::maximum(max2_v, v2);
  12274. uint32_t v3 = pPixels[11 * stride]; min3_v = basisu::minimum(min3_v, v3); max3_v = basisu::maximum(max3_v, v3);
  12275. }
  12276. {
  12277. uint32_t v0 = pPixels[12 * stride]; min0_v = basisu::minimum(min0_v, v0); max0_v = basisu::maximum(max0_v, v0);
  12278. uint32_t v1 = pPixels[13 * stride]; min1_v = basisu::minimum(min1_v, v1); max1_v = basisu::maximum(max1_v, v1);
  12279. uint32_t v2 = pPixels[14 * stride]; min2_v = basisu::minimum(min2_v, v2); max2_v = basisu::maximum(max2_v, v2);
  12280. uint32_t v3 = pPixels[15 * stride]; min3_v = basisu::minimum(min3_v, v3); max3_v = basisu::maximum(max3_v, v3);
  12281. }
  12282. const uint32_t min_v = basisu::minimum(min0_v, min1_v, min2_v, min3_v);
  12283. const uint32_t max_v = basisu::maximum(max0_v, max1_v, max2_v, max3_v);
  12284. uint8_t* pDst_bytes = static_cast<uint8_t*>(pDst);
  12285. pDst_bytes[0] = (uint8_t)max_v;
  12286. pDst_bytes[1] = (uint8_t)min_v;
  12287. if (max_v == min_v)
  12288. {
  12289. memset(pDst_bytes + 2, 0, 6);
  12290. return;
  12291. }
  12292. const uint32_t delta = max_v - min_v;
  12293. // min_v is now 0. Compute thresholds between values by scaling max_v. It's x14 because we're adding two x7 scale factors.
  12294. const int t0 = delta * 13;
  12295. const int t1 = delta * 11;
  12296. const int t2 = delta * 9;
  12297. const int t3 = delta * 7;
  12298. const int t4 = delta * 5;
  12299. const int t5 = delta * 3;
  12300. const int t6 = delta * 1;
  12301. // BC4 floors in its divisions, which we compensate for with the 4 bias.
  12302. // This function is optimal for all possible inputs (i.e. it outputs the same results as checking all 8 values and choosing the closest one).
  12303. const int bias = 4 - min_v * 14;
  12304. static const uint32_t s_tran0[8] = { 1U , 7U , 6U , 5U , 4U , 3U , 2U , 0U };
  12305. static const uint32_t s_tran1[8] = { 1U << 3U, 7U << 3U, 6U << 3U, 5U << 3U, 4U << 3U, 3U << 3U, 2U << 3U, 0U << 3U };
  12306. static const uint32_t s_tran2[8] = { 1U << 6U, 7U << 6U, 6U << 6U, 5U << 6U, 4U << 6U, 3U << 6U, 2U << 6U, 0U << 6U };
  12307. static const uint32_t s_tran3[8] = { 1U << 9U, 7U << 9U, 6U << 9U, 5U << 9U, 4U << 9U, 3U << 9U, 2U << 9U, 0U << 9U };
  12308. uint64_t a0, a1, a2, a3;
  12309. {
  12310. const int v0 = pPixels[0 * stride] * 14 + bias;
  12311. const int v1 = pPixels[1 * stride] * 14 + bias;
  12312. const int v2 = pPixels[2 * stride] * 14 + bias;
  12313. const int v3 = pPixels[3 * stride] * 14 + bias;
  12314. a0 = s_tran0[(v0 >= t0) + (v0 >= t1) + (v0 >= t2) + (v0 >= t3) + (v0 >= t4) + (v0 >= t5) + (v0 >= t6)];
  12315. a1 = s_tran1[(v1 >= t0) + (v1 >= t1) + (v1 >= t2) + (v1 >= t3) + (v1 >= t4) + (v1 >= t5) + (v1 >= t6)];
  12316. a2 = s_tran2[(v2 >= t0) + (v2 >= t1) + (v2 >= t2) + (v2 >= t3) + (v2 >= t4) + (v2 >= t5) + (v2 >= t6)];
  12317. a3 = s_tran3[(v3 >= t0) + (v3 >= t1) + (v3 >= t2) + (v3 >= t3) + (v3 >= t4) + (v3 >= t5) + (v3 >= t6)];
  12318. }
  12319. {
  12320. const int v0 = pPixels[4 * stride] * 14 + bias;
  12321. const int v1 = pPixels[5 * stride] * 14 + bias;
  12322. const int v2 = pPixels[6 * stride] * 14 + bias;
  12323. const int v3 = pPixels[7 * stride] * 14 + bias;
  12324. a0 |= (s_tran0[(v0 >= t0) + (v0 >= t1) + (v0 >= t2) + (v0 >= t3) + (v0 >= t4) + (v0 >= t5) + (v0 >= t6)] << 12U);
  12325. a1 |= (s_tran1[(v1 >= t0) + (v1 >= t1) + (v1 >= t2) + (v1 >= t3) + (v1 >= t4) + (v1 >= t5) + (v1 >= t6)] << 12U);
  12326. a2 |= (s_tran2[(v2 >= t0) + (v2 >= t1) + (v2 >= t2) + (v2 >= t3) + (v2 >= t4) + (v2 >= t5) + (v2 >= t6)] << 12U);
  12327. a3 |= (s_tran3[(v3 >= t0) + (v3 >= t1) + (v3 >= t2) + (v3 >= t3) + (v3 >= t4) + (v3 >= t5) + (v3 >= t6)] << 12U);
  12328. }
  12329. {
  12330. const int v0 = pPixels[8 * stride] * 14 + bias;
  12331. const int v1 = pPixels[9 * stride] * 14 + bias;
  12332. const int v2 = pPixels[10 * stride] * 14 + bias;
  12333. const int v3 = pPixels[11 * stride] * 14 + bias;
  12334. a0 |= (((uint64_t)s_tran0[(v0 >= t0) + (v0 >= t1) + (v0 >= t2) + (v0 >= t3) + (v0 >= t4) + (v0 >= t5) + (v0 >= t6)]) << 24U);
  12335. a1 |= (((uint64_t)s_tran1[(v1 >= t0) + (v1 >= t1) + (v1 >= t2) + (v1 >= t3) + (v1 >= t4) + (v1 >= t5) + (v1 >= t6)]) << 24U);
  12336. a2 |= (((uint64_t)s_tran2[(v2 >= t0) + (v2 >= t1) + (v2 >= t2) + (v2 >= t3) + (v2 >= t4) + (v2 >= t5) + (v2 >= t6)]) << 24U);
  12337. a3 |= (((uint64_t)s_tran3[(v3 >= t0) + (v3 >= t1) + (v3 >= t2) + (v3 >= t3) + (v3 >= t4) + (v3 >= t5) + (v3 >= t6)]) << 24U);
  12338. }
  12339. {
  12340. const int v0 = pPixels[12 * stride] * 14 + bias;
  12341. const int v1 = pPixels[13 * stride] * 14 + bias;
  12342. const int v2 = pPixels[14 * stride] * 14 + bias;
  12343. const int v3 = pPixels[15 * stride] * 14 + bias;
  12344. a0 |= (((uint64_t)s_tran0[(v0 >= t0) + (v0 >= t1) + (v0 >= t2) + (v0 >= t3) + (v0 >= t4) + (v0 >= t5) + (v0 >= t6)]) << 36U);
  12345. a1 |= (((uint64_t)s_tran1[(v1 >= t0) + (v1 >= t1) + (v1 >= t2) + (v1 >= t3) + (v1 >= t4) + (v1 >= t5) + (v1 >= t6)]) << 36U);
  12346. a2 |= (((uint64_t)s_tran2[(v2 >= t0) + (v2 >= t1) + (v2 >= t2) + (v2 >= t3) + (v2 >= t4) + (v2 >= t5) + (v2 >= t6)]) << 36U);
  12347. a3 |= (((uint64_t)s_tran3[(v3 >= t0) + (v3 >= t1) + (v3 >= t2) + (v3 >= t3) + (v3 >= t4) + (v3 >= t5) + (v3 >= t6)]) << 36U);
  12348. }
  12349. const uint64_t f = a0 | a1 | a2 | a3;
  12350. pDst_bytes[2] = (uint8_t)f;
  12351. pDst_bytes[3] = (uint8_t)(f >> 8U);
  12352. pDst_bytes[4] = (uint8_t)(f >> 16U);
  12353. pDst_bytes[5] = (uint8_t)(f >> 24U);
  12354. pDst_bytes[6] = (uint8_t)(f >> 32U);
  12355. pDst_bytes[7] = (uint8_t)(f >> 40U);
  12356. }
  12357. static void bc1_find_sels(const color32 *pSrc_pixels, uint32_t lr, uint32_t lg, uint32_t lb, uint32_t hr, uint32_t hg, uint32_t hb, uint8_t sels[16])
  12358. {
  12359. uint32_t block_r[4], block_g[4], block_b[4];
  12360. block_r[0] = (lr << 3) | (lr >> 2); block_g[0] = (lg << 2) | (lg >> 4); block_b[0] = (lb << 3) | (lb >> 2);
  12361. block_r[3] = (hr << 3) | (hr >> 2); block_g[3] = (hg << 2) | (hg >> 4); block_b[3] = (hb << 3) | (hb >> 2);
  12362. block_r[1] = (block_r[0] * 2 + block_r[3]) / 3; block_g[1] = (block_g[0] * 2 + block_g[3]) / 3; block_b[1] = (block_b[0] * 2 + block_b[3]) / 3;
  12363. block_r[2] = (block_r[3] * 2 + block_r[0]) / 3; block_g[2] = (block_g[3] * 2 + block_g[0]) / 3; block_b[2] = (block_b[3] * 2 + block_b[0]) / 3;
  12364. int ar = block_r[3] - block_r[0], ag = block_g[3] - block_g[0], ab = block_b[3] - block_b[0];
  12365. int dots[4];
  12366. for (uint32_t i = 0; i < 4; i++)
  12367. dots[i] = (int)block_r[i] * ar + (int)block_g[i] * ag + (int)block_b[i] * ab;
  12368. int t0 = dots[0] + dots[1], t1 = dots[1] + dots[2], t2 = dots[2] + dots[3];
  12369. ar *= 2; ag *= 2; ab *= 2;
  12370. for (uint32_t i = 0; i < 16; i++)
  12371. {
  12372. const int d = pSrc_pixels[i].r * ar + pSrc_pixels[i].g * ag + pSrc_pixels[i].b * ab;
  12373. static const uint8_t s_sels[4] = { 3, 2, 1, 0 };
  12374. // Rounding matters here!
  12375. // d <= t0: <=, not <, to the later LS step "sees" a wider range of selectors. It matters for quality.
  12376. sels[i] = s_sels[(d <= t0) + (d < t1) + (d < t2)];
  12377. }
  12378. }
  12379. static inline void bc1_find_sels_2(const color32* pSrc_pixels, uint32_t lr, uint32_t lg, uint32_t lb, uint32_t hr, uint32_t hg, uint32_t hb, uint8_t sels[16])
  12380. {
  12381. uint32_t block_r[4], block_g[4], block_b[4];
  12382. block_r[0] = (lr << 3) | (lr >> 2); block_g[0] = (lg << 2) | (lg >> 4); block_b[0] = (lb << 3) | (lb >> 2);
  12383. block_r[3] = (hr << 3) | (hr >> 2); block_g[3] = (hg << 2) | (hg >> 4); block_b[3] = (hb << 3) | (hb >> 2);
  12384. block_r[1] = (block_r[0] * 2 + block_r[3]) / 3; block_g[1] = (block_g[0] * 2 + block_g[3]) / 3; block_b[1] = (block_b[0] * 2 + block_b[3]) / 3;
  12385. block_r[2] = (block_r[3] * 2 + block_r[0]) / 3; block_g[2] = (block_g[3] * 2 + block_g[0]) / 3; block_b[2] = (block_b[3] * 2 + block_b[0]) / 3;
  12386. int ar = block_r[3] - block_r[0], ag = block_g[3] - block_g[0], ab = block_b[3] - block_b[0];
  12387. int dots[4];
  12388. for (uint32_t i = 0; i < 4; i++)
  12389. dots[i] = (int)block_r[i] * ar + (int)block_g[i] * ag + (int)block_b[i] * ab;
  12390. int t0 = dots[0] + dots[1], t1 = dots[1] + dots[2], t2 = dots[2] + dots[3];
  12391. ar *= 2; ag *= 2; ab *= 2;
  12392. static const uint8_t s_sels[4] = { 3, 2, 1, 0 };
  12393. for (uint32_t i = 0; i < 16; i += 4)
  12394. {
  12395. const int d0 = pSrc_pixels[i+0].r * ar + pSrc_pixels[i+0].g * ag + pSrc_pixels[i+0].b * ab;
  12396. const int d1 = pSrc_pixels[i+1].r * ar + pSrc_pixels[i+1].g * ag + pSrc_pixels[i+1].b * ab;
  12397. const int d2 = pSrc_pixels[i+2].r * ar + pSrc_pixels[i+2].g * ag + pSrc_pixels[i+2].b * ab;
  12398. const int d3 = pSrc_pixels[i+3].r * ar + pSrc_pixels[i+3].g * ag + pSrc_pixels[i+3].b * ab;
  12399. sels[i+0] = s_sels[(d0 <= t0) + (d0 < t1) + (d0 < t2)];
  12400. sels[i+1] = s_sels[(d1 <= t0) + (d1 < t1) + (d1 < t2)];
  12401. sels[i+2] = s_sels[(d2 <= t0) + (d2 < t1) + (d2 < t2)];
  12402. sels[i+3] = s_sels[(d3 <= t0) + (d3 < t1) + (d3 < t2)];
  12403. }
  12404. }
  12405. struct vec3F { float c[3]; };
  12406. static bool compute_least_squares_endpoints_rgb(const color32* pColors, const uint8_t* pSelectors, vec3F* pXl, vec3F* pXh)
  12407. {
  12408. // Derived from bc7enc16's LS function.
  12409. // Least squares using normal equations: http://www.cs.cornell.edu/~bindel/class/cs3220-s12/notes/lec10.pdf
  12410. // I did this in matrix form first, expanded out all the ops, then optimized it a bit.
  12411. uint32_t uq00_r = 0, uq10_r = 0, ut_r = 0, uq00_g = 0, uq10_g = 0, ut_g = 0, uq00_b = 0, uq10_b = 0, ut_b = 0;
  12412. // This table is: 9 * (w * w), 9 * ((1.0f - w) * w), 9 * ((1.0f - w) * (1.0f - w))
  12413. // where w is [0,1/3,2/3,1]. 9 is the perfect multiplier.
  12414. static const uint32_t s_weight_vals[4] = { 0x000009, 0x010204, 0x040201, 0x090000 };
  12415. uint32_t weight_accum = 0;
  12416. for (uint32_t i = 0; i < 16; i++)
  12417. {
  12418. const uint32_t r = pColors[i].c[0], g = pColors[i].c[1], b = pColors[i].c[2];
  12419. const uint32_t sel = pSelectors[i];
  12420. ut_r += r;
  12421. ut_g += g;
  12422. ut_b += b;
  12423. weight_accum += s_weight_vals[sel];
  12424. uq00_r += sel * r;
  12425. uq00_g += sel * g;
  12426. uq00_b += sel * b;
  12427. }
  12428. float q00_r = (float)uq00_r, q10_r = (float)uq10_r, t_r = (float)ut_r;
  12429. float q00_g = (float)uq00_g, q10_g = (float)uq10_g, t_g = (float)ut_g;
  12430. float q00_b = (float)uq00_b, q10_b = (float)uq10_b, t_b = (float)ut_b;
  12431. q10_r = t_r * 3.0f - q00_r;
  12432. q10_g = t_g * 3.0f - q00_g;
  12433. q10_b = t_b * 3.0f - q00_b;
  12434. float z00 = (float)((weight_accum >> 16) & 0xFF);
  12435. float z10 = (float)((weight_accum >> 8) & 0xFF);
  12436. float z11 = (float)(weight_accum & 0xFF);
  12437. float z01 = z10;
  12438. float det = z00 * z11 - z01 * z10;
  12439. if (fabs(det) < 1e-8f)
  12440. return false;
  12441. det = 3.0f / det;
  12442. float iz00, iz01, iz10, iz11;
  12443. iz00 = z11 * det;
  12444. iz01 = -z01 * det;
  12445. iz10 = -z10 * det;
  12446. iz11 = z00 * det;
  12447. pXl->c[0] = iz00 * q00_r + iz01 * q10_r; pXh->c[0] = iz10 * q00_r + iz11 * q10_r;
  12448. pXl->c[1] = iz00 * q00_g + iz01 * q10_g; pXh->c[1] = iz10 * q00_g + iz11 * q10_g;
  12449. pXl->c[2] = iz00 * q00_b + iz01 * q10_b; pXh->c[2] = iz10 * q00_b + iz11 * q10_b;
  12450. // Check and fix channel singularities - might not be needed, but is in UASTC's encoder.
  12451. for (uint32_t c = 0; c < 3; c++)
  12452. {
  12453. if ((pXl->c[c] < 0.0f) || (pXh->c[c] > 255.0f))
  12454. {
  12455. uint32_t lo_v = UINT32_MAX, hi_v = 0;
  12456. for (uint32_t i = 0; i < 16; i++)
  12457. {
  12458. lo_v = basisu::minimumu(lo_v, pColors[i].c[c]);
  12459. hi_v = basisu::maximumu(hi_v, pColors[i].c[c]);
  12460. }
  12461. if (lo_v == hi_v)
  12462. {
  12463. pXl->c[c] = (float)lo_v;
  12464. pXh->c[c] = (float)hi_v;
  12465. }
  12466. }
  12467. }
  12468. return true;
  12469. }
  12470. void encode_bc1_solid_block(void* pDst, uint32_t fr, uint32_t fg, uint32_t fb)
  12471. {
  12472. dxt1_block* pDst_block = static_cast<dxt1_block*>(pDst);
  12473. uint32_t mask = 0xAA;
  12474. uint32_t max16 = (g_bc1_match5_equals_1[fr].m_hi << 11) | (g_bc1_match6_equals_1[fg].m_hi << 5) | g_bc1_match5_equals_1[fb].m_hi;
  12475. uint32_t min16 = (g_bc1_match5_equals_1[fr].m_lo << 11) | (g_bc1_match6_equals_1[fg].m_lo << 5) | g_bc1_match5_equals_1[fb].m_lo;
  12476. if (min16 == max16)
  12477. {
  12478. // Always forbid 3 color blocks
  12479. // This is to guarantee that BC3 blocks never use punchthrough alpha (3 color) mode, which isn't supported on some (all?) GPU's.
  12480. mask = 0;
  12481. // Make l > h
  12482. if (min16 > 0)
  12483. min16--;
  12484. else
  12485. {
  12486. // l = h = 0
  12487. assert(min16 == max16 && max16 == 0);
  12488. max16 = 1;
  12489. min16 = 0;
  12490. mask = 0x55;
  12491. }
  12492. assert(max16 > min16);
  12493. }
  12494. if (max16 < min16)
  12495. {
  12496. std::swap(max16, min16);
  12497. mask ^= 0x55;
  12498. }
  12499. pDst_block->set_low_color(static_cast<uint16_t>(max16));
  12500. pDst_block->set_high_color(static_cast<uint16_t>(min16));
  12501. pDst_block->m_selectors[0] = static_cast<uint8_t>(mask);
  12502. pDst_block->m_selectors[1] = static_cast<uint8_t>(mask);
  12503. pDst_block->m_selectors[2] = static_cast<uint8_t>(mask);
  12504. pDst_block->m_selectors[3] = static_cast<uint8_t>(mask);
  12505. }
  12506. static inline uint8_t to_5(uint32_t v) { v = v * 31 + 128; return (uint8_t)((v + (v >> 8)) >> 8); }
  12507. static inline uint8_t to_6(uint32_t v) { v = v * 63 + 128; return (uint8_t)((v + (v >> 8)) >> 8); }
  12508. // Good references: squish library, stb_dxt.
  12509. void encode_bc1(void* pDst, const uint8_t* pPixels, uint32_t flags)
  12510. {
  12511. const color32* pSrc_pixels = (const color32*)pPixels;
  12512. dxt1_block* pDst_block = static_cast<dxt1_block*>(pDst);
  12513. int avg_r = -1, avg_g = 0, avg_b = 0;
  12514. int lr = 0, lg = 0, lb = 0, hr = 0, hg = 0, hb = 0;
  12515. uint8_t sels[16];
  12516. const bool use_sels = (flags & cEncodeBC1UseSelectors) != 0;
  12517. if (use_sels)
  12518. {
  12519. // Caller is jamming in their own selectors for us to try.
  12520. const uint32_t s = pDst_block->m_selectors[0] | (pDst_block->m_selectors[1] << 8) | (pDst_block->m_selectors[2] << 16) | (pDst_block->m_selectors[3] << 24);
  12521. static const uint8_t s_sel_tran[4] = { 0, 3, 1, 2 };
  12522. for (uint32_t i = 0; i < 16; i++)
  12523. sels[i] = s_sel_tran[(s >> (i * 2)) & 3];
  12524. }
  12525. else
  12526. {
  12527. const uint32_t fr = pSrc_pixels[0].r, fg = pSrc_pixels[0].g, fb = pSrc_pixels[0].b;
  12528. uint32_t j;
  12529. for (j = 1; j < 16; j++)
  12530. if ((pSrc_pixels[j].r != fr) || (pSrc_pixels[j].g != fg) || (pSrc_pixels[j].b != fb))
  12531. break;
  12532. if (j == 16)
  12533. {
  12534. encode_bc1_solid_block(pDst, fr, fg, fb);
  12535. return;
  12536. }
  12537. // Select 2 colors along the principle axis. (There must be a faster/simpler way.)
  12538. int total_r = fr, total_g = fg, total_b = fb;
  12539. int max_r = fr, max_g = fg, max_b = fb;
  12540. int min_r = fr, min_g = fg, min_b = fb;
  12541. for (uint32_t i = 1; i < 16; i++)
  12542. {
  12543. const int r = pSrc_pixels[i].r, g = pSrc_pixels[i].g, b = pSrc_pixels[i].b;
  12544. max_r = basisu::maximum(max_r, r); max_g = basisu::maximum(max_g, g); max_b = basisu::maximum(max_b, b);
  12545. min_r = basisu::minimum(min_r, r); min_g = basisu::minimum(min_g, g); min_b = basisu::minimum(min_b, b);
  12546. total_r += r; total_g += g; total_b += b;
  12547. }
  12548. avg_r = (total_r + 8) >> 4;
  12549. avg_g = (total_g + 8) >> 4;
  12550. avg_b = (total_b + 8) >> 4;
  12551. int icov[6] = { 0, 0, 0, 0, 0, 0 };
  12552. for (uint32_t i = 0; i < 16; i++)
  12553. {
  12554. int r = (int)pSrc_pixels[i].r - avg_r;
  12555. int g = (int)pSrc_pixels[i].g - avg_g;
  12556. int b = (int)pSrc_pixels[i].b - avg_b;
  12557. icov[0] += r * r;
  12558. icov[1] += r * g;
  12559. icov[2] += r * b;
  12560. icov[3] += g * g;
  12561. icov[4] += g * b;
  12562. icov[5] += b * b;
  12563. }
  12564. float cov[6];
  12565. for (uint32_t i = 0; i < 6; i++)
  12566. cov[i] = static_cast<float>(icov[i])* (1.0f / 255.0f);
  12567. #if 0
  12568. // Seems silly to use full PCA to choose 2 colors. The diff in avg. PSNR between using PCA vs. not is small (~.025 difference).
  12569. // TODO: Try 2 or 3 different normalized diagonal vectors, choose the one that results in the largest dot delta
  12570. int saxis_r = max_r - min_r;
  12571. int saxis_g = max_g - min_g;
  12572. int saxis_b = max_b - min_b;
  12573. #else
  12574. float xr = (float)(max_r - min_r);
  12575. float xg = (float)(max_g - min_g);
  12576. float xb = (float)(max_b - min_b);
  12577. //float xr = (float)(max_r - avg_r); // max-avg is nearly the same, and doesn't require computing min's
  12578. //float xg = (float)(max_g - avg_g);
  12579. //float xb = (float)(max_b - avg_b);
  12580. for (uint32_t power_iter = 0; power_iter < 4; power_iter++)
  12581. {
  12582. float r = xr * cov[0] + xg * cov[1] + xb * cov[2];
  12583. float g = xr * cov[1] + xg * cov[3] + xb * cov[4];
  12584. float b = xr * cov[2] + xg * cov[4] + xb * cov[5];
  12585. xr = r; xg = g; xb = b;
  12586. }
  12587. float k = basisu::maximum(fabsf(xr), fabsf(xg), fabsf(xb));
  12588. int saxis_r = 306, saxis_g = 601, saxis_b = 117;
  12589. if (k >= 2)
  12590. {
  12591. float m = 1024.0f / k;
  12592. saxis_r = (int)(xr * m);
  12593. saxis_g = (int)(xg * m);
  12594. saxis_b = (int)(xb * m);
  12595. }
  12596. #endif
  12597. int low_dot = INT_MAX, high_dot = INT_MIN, low_c = 0, high_c = 0;
  12598. for (uint32_t i = 0; i < 16; i++)
  12599. {
  12600. int dot = pSrc_pixels[i].r * saxis_r + pSrc_pixels[i].g * saxis_g + pSrc_pixels[i].b * saxis_b;
  12601. if (dot < low_dot)
  12602. {
  12603. low_dot = dot;
  12604. low_c = i;
  12605. }
  12606. if (dot > high_dot)
  12607. {
  12608. high_dot = dot;
  12609. high_c = i;
  12610. }
  12611. }
  12612. lr = to_5(pSrc_pixels[low_c].r);
  12613. lg = to_6(pSrc_pixels[low_c].g);
  12614. lb = to_5(pSrc_pixels[low_c].b);
  12615. hr = to_5(pSrc_pixels[high_c].r);
  12616. hg = to_6(pSrc_pixels[high_c].g);
  12617. hb = to_5(pSrc_pixels[high_c].b);
  12618. bc1_find_sels(pSrc_pixels, lr, lg, lb, hr, hg, hb, sels);
  12619. } // if (use_sels)
  12620. const uint32_t total_ls_passes = (flags & cEncodeBC1HigherQuality) ? 3 : (flags & cEncodeBC1HighQuality ? 2 : 1);
  12621. for (uint32_t ls_pass = 0; ls_pass < total_ls_passes; ls_pass++)
  12622. {
  12623. // This is where the real magic happens. We have an array of candidate selectors, so let's use least squares to compute the optimal low/high endpoint colors.
  12624. vec3F xl, xh;
  12625. if (!compute_least_squares_endpoints_rgb(pSrc_pixels, sels, &xl, &xh))
  12626. {
  12627. if (avg_r < 0)
  12628. {
  12629. int total_r = 0, total_g = 0, total_b = 0;
  12630. for (uint32_t i = 0; i < 16; i++)
  12631. {
  12632. total_r += pSrc_pixels[i].r;
  12633. total_g += pSrc_pixels[i].g;
  12634. total_b += pSrc_pixels[i].b;
  12635. }
  12636. avg_r = (total_r + 8) >> 4;
  12637. avg_g = (total_g + 8) >> 4;
  12638. avg_b = (total_b + 8) >> 4;
  12639. }
  12640. // All selectors equal - treat it as a solid block which should always be equal or better.
  12641. lr = g_bc1_match5_equals_1[avg_r].m_hi;
  12642. lg = g_bc1_match6_equals_1[avg_g].m_hi;
  12643. lb = g_bc1_match5_equals_1[avg_b].m_hi;
  12644. hr = g_bc1_match5_equals_1[avg_r].m_lo;
  12645. hg = g_bc1_match6_equals_1[avg_g].m_lo;
  12646. hb = g_bc1_match5_equals_1[avg_b].m_lo;
  12647. // In high/higher quality mode, let it try again in case the optimal tables have caused the sels to diverge.
  12648. }
  12649. else
  12650. {
  12651. lr = basisu::clamp((int)((xl.c[0]) * (31.0f / 255.0f) + .5f), 0, 31);
  12652. lg = basisu::clamp((int)((xl.c[1]) * (63.0f / 255.0f) + .5f), 0, 63);
  12653. lb = basisu::clamp((int)((xl.c[2]) * (31.0f / 255.0f) + .5f), 0, 31);
  12654. hr = basisu::clamp((int)((xh.c[0]) * (31.0f / 255.0f) + .5f), 0, 31);
  12655. hg = basisu::clamp((int)((xh.c[1]) * (63.0f / 255.0f) + .5f), 0, 63);
  12656. hb = basisu::clamp((int)((xh.c[2]) * (31.0f / 255.0f) + .5f), 0, 31);
  12657. }
  12658. bc1_find_sels(pSrc_pixels, lr, lg, lb, hr, hg, hb, sels);
  12659. }
  12660. uint32_t lc16 = dxt1_block::pack_unscaled_color(lr, lg, lb);
  12661. uint32_t hc16 = dxt1_block::pack_unscaled_color(hr, hg, hb);
  12662. // Always forbid 3 color blocks
  12663. if (lc16 == hc16)
  12664. {
  12665. uint8_t mask = 0;
  12666. // Make l > h
  12667. if (hc16 > 0)
  12668. hc16--;
  12669. else
  12670. {
  12671. // lc16 = hc16 = 0
  12672. assert(lc16 == hc16 && hc16 == 0);
  12673. hc16 = 0;
  12674. lc16 = 1;
  12675. mask = 0x55; // select hc16
  12676. }
  12677. assert(lc16 > hc16);
  12678. pDst_block->set_low_color(static_cast<uint16_t>(lc16));
  12679. pDst_block->set_high_color(static_cast<uint16_t>(hc16));
  12680. pDst_block->m_selectors[0] = mask;
  12681. pDst_block->m_selectors[1] = mask;
  12682. pDst_block->m_selectors[2] = mask;
  12683. pDst_block->m_selectors[3] = mask;
  12684. }
  12685. else
  12686. {
  12687. uint8_t invert_mask = 0;
  12688. if (lc16 < hc16)
  12689. {
  12690. std::swap(lc16, hc16);
  12691. invert_mask = 0x55;
  12692. }
  12693. assert(lc16 > hc16);
  12694. pDst_block->set_low_color((uint16_t)lc16);
  12695. pDst_block->set_high_color((uint16_t)hc16);
  12696. uint32_t packed_sels = 0;
  12697. static const uint8_t s_sel_trans[4] = { 0, 2, 3, 1 };
  12698. for (uint32_t i = 0; i < 16; i++)
  12699. packed_sels |= ((uint32_t)s_sel_trans[sels[i]] << (i * 2));
  12700. pDst_block->m_selectors[0] = (uint8_t)packed_sels ^ invert_mask;
  12701. pDst_block->m_selectors[1] = (uint8_t)(packed_sels >> 8) ^ invert_mask;
  12702. pDst_block->m_selectors[2] = (uint8_t)(packed_sels >> 16) ^ invert_mask;
  12703. pDst_block->m_selectors[3] = (uint8_t)(packed_sels >> 24) ^ invert_mask;
  12704. }
  12705. }
  12706. void encode_bc1_alt(void* pDst, const uint8_t* pPixels, uint32_t flags)
  12707. {
  12708. const color32* pSrc_pixels = (const color32*)pPixels;
  12709. dxt1_block* pDst_block = static_cast<dxt1_block*>(pDst);
  12710. int avg_r = -1, avg_g = 0, avg_b = 0;
  12711. int lr = 0, lg = 0, lb = 0, hr = 0, hg = 0, hb = 0;
  12712. uint8_t sels[16];
  12713. const bool use_sels = (flags & cEncodeBC1UseSelectors) != 0;
  12714. if (use_sels)
  12715. {
  12716. // Caller is jamming in their own selectors for us to try.
  12717. const uint32_t s = pDst_block->m_selectors[0] | (pDst_block->m_selectors[1] << 8) | (pDst_block->m_selectors[2] << 16) | (pDst_block->m_selectors[3] << 24);
  12718. static const uint8_t s_sel_tran[4] = { 0, 3, 1, 2 };
  12719. for (uint32_t i = 0; i < 16; i++)
  12720. sels[i] = s_sel_tran[(s >> (i * 2)) & 3];
  12721. }
  12722. else
  12723. {
  12724. const uint32_t fr = pSrc_pixels[0].r, fg = pSrc_pixels[0].g, fb = pSrc_pixels[0].b;
  12725. uint32_t j;
  12726. for (j = 1; j < 16; j++)
  12727. if ((pSrc_pixels[j].r != fr) || (pSrc_pixels[j].g != fg) || (pSrc_pixels[j].b != fb))
  12728. break;
  12729. if (j == 16)
  12730. {
  12731. encode_bc1_solid_block(pDst, fr, fg, fb);
  12732. return;
  12733. }
  12734. // Select 2 colors along the principle axis. (There must be a faster/simpler way.)
  12735. int total_r = fr, total_g = fg, total_b = fb;
  12736. int max_r = fr, max_g = fg, max_b = fb;
  12737. int min_r = fr, min_g = fg, min_b = fb;
  12738. uint32_t grayscale_flag = (fr == fg) && (fr == fb);
  12739. for (uint32_t i = 1; i < 16; i++)
  12740. {
  12741. const int r = pSrc_pixels[i].r, g = pSrc_pixels[i].g, b = pSrc_pixels[i].b;
  12742. grayscale_flag &= ((r == g) && (r == b));
  12743. max_r = basisu::maximum(max_r, r); max_g = basisu::maximum(max_g, g); max_b = basisu::maximum(max_b, b);
  12744. min_r = basisu::minimum(min_r, r); min_g = basisu::minimum(min_g, g); min_b = basisu::minimum(min_b, b);
  12745. total_r += r; total_g += g; total_b += b;
  12746. }
  12747. if (grayscale_flag)
  12748. {
  12749. // Grayscale blocks are a common enough case to specialize.
  12750. if ((max_r - min_r) < 2)
  12751. {
  12752. lr = lb = hr = hb = to_5(fr);
  12753. lg = hg = to_6(fr);
  12754. }
  12755. else
  12756. {
  12757. lr = lb = to_5(min_r);
  12758. lg = to_6(min_r);
  12759. hr = hb = to_5(max_r);
  12760. hg = to_6(max_r);
  12761. }
  12762. }
  12763. else
  12764. {
  12765. avg_r = (total_r + 8) >> 4;
  12766. avg_g = (total_g + 8) >> 4;
  12767. avg_b = (total_b + 8) >> 4;
  12768. // Find the shortest vector from a AABB corner to the block's average color.
  12769. // This is to help avoid outliers.
  12770. uint32_t dist[3][2];
  12771. dist[0][0] = basisu::square(min_r - avg_r) << 3; dist[0][1] = basisu::square(max_r - avg_r) << 3;
  12772. dist[1][0] = basisu::square(min_g - avg_g) << 3; dist[1][1] = basisu::square(max_g - avg_g) << 3;
  12773. dist[2][0] = basisu::square(min_b - avg_b) << 3; dist[2][1] = basisu::square(max_b - avg_b) << 3;
  12774. uint32_t min_d0 = (dist[0][0] + dist[1][0] + dist[2][0]);
  12775. uint32_t d4 = (dist[0][0] + dist[1][0] + dist[2][1]) | 4;
  12776. min_d0 = basisu::minimum(min_d0, d4);
  12777. uint32_t min_d1 = (dist[0][1] + dist[1][0] + dist[2][0]) | 1;
  12778. uint32_t d5 = (dist[0][1] + dist[1][0] + dist[2][1]) | 5;
  12779. min_d1 = basisu::minimum(min_d1, d5);
  12780. uint32_t d2 = (dist[0][0] + dist[1][1] + dist[2][0]) | 2;
  12781. min_d0 = basisu::minimum(min_d0, d2);
  12782. uint32_t d3 = (dist[0][1] + dist[1][1] + dist[2][0]) | 3;
  12783. min_d1 = basisu::minimum(min_d1, d3);
  12784. uint32_t d6 = (dist[0][0] + dist[1][1] + dist[2][1]) | 6;
  12785. min_d0 = basisu::minimum(min_d0, d6);
  12786. uint32_t d7 = (dist[0][1] + dist[1][1] + dist[2][1]) | 7;
  12787. min_d1 = basisu::minimum(min_d1, d7);
  12788. uint32_t min_d = basisu::minimum(min_d0, min_d1);
  12789. uint32_t best_i = min_d & 7;
  12790. int delta_r = (best_i & 1) ? (max_r - avg_r) : (avg_r - min_r);
  12791. int delta_g = (best_i & 2) ? (max_g - avg_g) : (avg_g - min_g);
  12792. int delta_b = (best_i & 4) ? (max_b - avg_b) : (avg_b - min_b);
  12793. // Note: if delta_r/g/b==0, we actually want to choose a single color, so the block average color optimization kicks in.
  12794. uint32_t low_c = 0, high_c = 0;
  12795. if ((delta_r | delta_g | delta_b) != 0)
  12796. {
  12797. // Now we have a smaller AABB going from the block's average color to a cornerpoint of the larger AABB.
  12798. // Project all pixels colors along the 4 vectors going from a smaller AABB cornerpoint to the opposite cornerpoint, find largest projection.
  12799. // One of these vectors will be a decent approximation of the block's PCA.
  12800. const int saxis0_r = delta_r, saxis0_g = delta_g, saxis0_b = delta_b;
  12801. int low_dot0 = INT_MAX, high_dot0 = INT_MIN;
  12802. int low_dot1 = INT_MAX, high_dot1 = INT_MIN;
  12803. int low_dot2 = INT_MAX, high_dot2 = INT_MIN;
  12804. int low_dot3 = INT_MAX, high_dot3 = INT_MIN;
  12805. //int low_c0, low_c1, low_c2, low_c3;
  12806. //int high_c0, high_c1, high_c2, high_c3;
  12807. for (uint32_t i = 0; i < 16; i++)
  12808. {
  12809. const int dotx = pSrc_pixels[i].r * saxis0_r;
  12810. const int doty = pSrc_pixels[i].g * saxis0_g;
  12811. const int dotz = pSrc_pixels[i].b * saxis0_b;
  12812. const int dot0 = ((dotz + dotx + doty) << 4) + i;
  12813. const int dot1 = ((dotz - dotx - doty) << 4) + i;
  12814. const int dot2 = ((dotz - dotx + doty) << 4) + i;
  12815. const int dot3 = ((dotz + dotx - doty) << 4) + i;
  12816. if (dot0 < low_dot0)
  12817. {
  12818. low_dot0 = dot0;
  12819. //low_c0 = i;
  12820. }
  12821. if ((dot0 ^ 15) > high_dot0)
  12822. {
  12823. high_dot0 = dot0 ^ 15;
  12824. //high_c0 = i;
  12825. }
  12826. if (dot1 < low_dot1)
  12827. {
  12828. low_dot1 = dot1;
  12829. //low_c1 = i;
  12830. }
  12831. if ((dot1 ^ 15) > high_dot1)
  12832. {
  12833. high_dot1 = dot1 ^ 15;
  12834. //high_c1 = i;
  12835. }
  12836. if (dot2 < low_dot2)
  12837. {
  12838. low_dot2 = dot2;
  12839. //low_c2 = i;
  12840. }
  12841. if ((dot2 ^ 15) > high_dot2)
  12842. {
  12843. high_dot2 = dot2 ^ 15;
  12844. //high_c2 = i;
  12845. }
  12846. if (dot3 < low_dot3)
  12847. {
  12848. low_dot3 = dot3;
  12849. //low_c3 = i;
  12850. }
  12851. if ((dot3 ^ 15) > high_dot3)
  12852. {
  12853. high_dot3 = dot3 ^ 15;
  12854. //high_c3 = i;
  12855. }
  12856. }
  12857. low_c = low_dot0 & 15;
  12858. high_c = ~high_dot0 & 15;
  12859. uint32_t r = (high_dot0 & ~15) - (low_dot0 & ~15);
  12860. uint32_t tr = (high_dot1 & ~15) - (low_dot1 & ~15);
  12861. if (tr > r) {
  12862. low_c = low_dot1 & 15;
  12863. high_c = ~high_dot1 & 15;
  12864. r = tr;
  12865. }
  12866. tr = (high_dot2 & ~15) - (low_dot2 & ~15);
  12867. if (tr > r) {
  12868. low_c = low_dot2 & 15;
  12869. high_c = ~high_dot2 & 15;
  12870. r = tr;
  12871. }
  12872. tr = (high_dot3 & ~15) - (low_dot3 & ~15);
  12873. if (tr > r) {
  12874. low_c = low_dot3 & 15;
  12875. high_c = ~high_dot3 & 15;
  12876. }
  12877. }
  12878. lr = to_5(pSrc_pixels[low_c].r);
  12879. lg = to_6(pSrc_pixels[low_c].g);
  12880. lb = to_5(pSrc_pixels[low_c].b);
  12881. hr = to_5(pSrc_pixels[high_c].r);
  12882. hg = to_6(pSrc_pixels[high_c].g);
  12883. hb = to_5(pSrc_pixels[high_c].b);
  12884. }
  12885. bc1_find_sels_2(pSrc_pixels, lr, lg, lb, hr, hg, hb, sels);
  12886. } // if (use_sels)
  12887. const uint32_t total_ls_passes = (flags & cEncodeBC1HigherQuality) ? 3 : (flags & cEncodeBC1HighQuality ? 2 : 1);
  12888. for (uint32_t ls_pass = 0; ls_pass < total_ls_passes; ls_pass++)
  12889. {
  12890. int prev_lr = lr, prev_lg = lg, prev_lb = lb, prev_hr = hr, prev_hg = hg, prev_hb = hb;
  12891. // This is where the real magic happens. We have an array of candidate selectors, so let's use least squares to compute the optimal low/high endpoint colors.
  12892. vec3F xl, xh;
  12893. if (!compute_least_squares_endpoints_rgb(pSrc_pixels, sels, &xl, &xh))
  12894. {
  12895. if (avg_r < 0)
  12896. {
  12897. int total_r = 0, total_g = 0, total_b = 0;
  12898. for (uint32_t i = 0; i < 16; i++)
  12899. {
  12900. total_r += pSrc_pixels[i].r;
  12901. total_g += pSrc_pixels[i].g;
  12902. total_b += pSrc_pixels[i].b;
  12903. }
  12904. avg_r = (total_r + 8) >> 4;
  12905. avg_g = (total_g + 8) >> 4;
  12906. avg_b = (total_b + 8) >> 4;
  12907. }
  12908. // All selectors equal - treat it as a solid block which should always be equal or better.
  12909. lr = g_bc1_match5_equals_1[avg_r].m_hi;
  12910. lg = g_bc1_match6_equals_1[avg_g].m_hi;
  12911. lb = g_bc1_match5_equals_1[avg_b].m_hi;
  12912. hr = g_bc1_match5_equals_1[avg_r].m_lo;
  12913. hg = g_bc1_match6_equals_1[avg_g].m_lo;
  12914. hb = g_bc1_match5_equals_1[avg_b].m_lo;
  12915. // In high/higher quality mode, let it try again in case the optimal tables have caused the sels to diverge.
  12916. }
  12917. else
  12918. {
  12919. lr = basisu::clamp((int)((xl.c[0]) * (31.0f / 255.0f) + .5f), 0, 31);
  12920. lg = basisu::clamp((int)((xl.c[1]) * (63.0f / 255.0f) + .5f), 0, 63);
  12921. lb = basisu::clamp((int)((xl.c[2]) * (31.0f / 255.0f) + .5f), 0, 31);
  12922. hr = basisu::clamp((int)((xh.c[0]) * (31.0f / 255.0f) + .5f), 0, 31);
  12923. hg = basisu::clamp((int)((xh.c[1]) * (63.0f / 255.0f) + .5f), 0, 63);
  12924. hb = basisu::clamp((int)((xh.c[2]) * (31.0f / 255.0f) + .5f), 0, 31);
  12925. }
  12926. if ((prev_lr == lr) && (prev_lg == lg) && (prev_lb == lb) && (prev_hr == hr) && (prev_hg == hg) && (prev_hb == hb))
  12927. break;
  12928. bc1_find_sels_2(pSrc_pixels, lr, lg, lb, hr, hg, hb, sels);
  12929. }
  12930. uint32_t lc16 = dxt1_block::pack_unscaled_color(lr, lg, lb);
  12931. uint32_t hc16 = dxt1_block::pack_unscaled_color(hr, hg, hb);
  12932. // Always forbid 3 color blocks
  12933. if (lc16 == hc16)
  12934. {
  12935. uint8_t mask = 0;
  12936. // Make l > h
  12937. if (hc16 > 0)
  12938. hc16--;
  12939. else
  12940. {
  12941. // lc16 = hc16 = 0
  12942. assert(lc16 == hc16 && hc16 == 0);
  12943. hc16 = 0;
  12944. lc16 = 1;
  12945. mask = 0x55; // select hc16
  12946. }
  12947. assert(lc16 > hc16);
  12948. pDst_block->set_low_color(static_cast<uint16_t>(lc16));
  12949. pDst_block->set_high_color(static_cast<uint16_t>(hc16));
  12950. pDst_block->m_selectors[0] = mask;
  12951. pDst_block->m_selectors[1] = mask;
  12952. pDst_block->m_selectors[2] = mask;
  12953. pDst_block->m_selectors[3] = mask;
  12954. }
  12955. else
  12956. {
  12957. uint8_t invert_mask = 0;
  12958. if (lc16 < hc16)
  12959. {
  12960. std::swap(lc16, hc16);
  12961. invert_mask = 0x55;
  12962. }
  12963. assert(lc16 > hc16);
  12964. pDst_block->set_low_color((uint16_t)lc16);
  12965. pDst_block->set_high_color((uint16_t)hc16);
  12966. uint32_t packed_sels = 0;
  12967. static const uint8_t s_sel_trans[4] = { 0, 2, 3, 1 };
  12968. for (uint32_t i = 0; i < 16; i++)
  12969. packed_sels |= ((uint32_t)s_sel_trans[sels[i]] << (i * 2));
  12970. pDst_block->m_selectors[0] = (uint8_t)packed_sels ^ invert_mask;
  12971. pDst_block->m_selectors[1] = (uint8_t)(packed_sels >> 8) ^ invert_mask;
  12972. pDst_block->m_selectors[2] = (uint8_t)(packed_sels >> 16) ^ invert_mask;
  12973. pDst_block->m_selectors[3] = (uint8_t)(packed_sels >> 24) ^ invert_mask;
  12974. }
  12975. }
  12976. // Scale the UASTC first subset endpoints and first plane's weight indices directly to BC1's - fastest.
  12977. void transcode_uastc_to_bc1_hint0(const unpacked_uastc_block& unpacked_src_blk, void* pDst)
  12978. {
  12979. const uint32_t mode = unpacked_src_blk.m_mode;
  12980. const astc_block_desc& astc_blk = unpacked_src_blk.m_astc;
  12981. dxt1_block& b = *static_cast<dxt1_block*>(pDst);
  12982. const uint32_t endpoint_range = g_uastc_mode_endpoint_ranges[mode];
  12983. const uint32_t total_comps = g_uastc_mode_comps[mode];
  12984. if (total_comps == 2)
  12985. {
  12986. const uint32_t l = g_astc_unquant[endpoint_range][astc_blk.m_endpoints[0]].m_unquant;
  12987. const uint32_t h = g_astc_unquant[endpoint_range][astc_blk.m_endpoints[1]].m_unquant;
  12988. b.set_low_color(dxt1_block::pack_color(color32(l, l, l, 255), true, 127));
  12989. b.set_high_color(dxt1_block::pack_color(color32(h, h, h, 255), true, 127));
  12990. }
  12991. else
  12992. {
  12993. b.set_low_color(dxt1_block::pack_color(
  12994. color32(g_astc_unquant[endpoint_range][astc_blk.m_endpoints[0]].m_unquant,
  12995. g_astc_unquant[endpoint_range][astc_blk.m_endpoints[2]].m_unquant,
  12996. g_astc_unquant[endpoint_range][astc_blk.m_endpoints[4]].m_unquant,
  12997. 255), true, 127)
  12998. );
  12999. b.set_high_color(dxt1_block::pack_color(
  13000. color32(g_astc_unquant[endpoint_range][astc_blk.m_endpoints[1]].m_unquant,
  13001. g_astc_unquant[endpoint_range][astc_blk.m_endpoints[3]].m_unquant,
  13002. g_astc_unquant[endpoint_range][astc_blk.m_endpoints[5]].m_unquant,
  13003. 255), true, 127)
  13004. );
  13005. }
  13006. if (b.get_low_color() == b.get_high_color())
  13007. {
  13008. // Always forbid 3 color blocks
  13009. uint16_t lc16 = (uint16_t)b.get_low_color();
  13010. uint16_t hc16 = (uint16_t)b.get_high_color();
  13011. uint8_t mask = 0;
  13012. // Make l > h
  13013. if (hc16 > 0)
  13014. hc16--;
  13015. else
  13016. {
  13017. // lc16 = hc16 = 0
  13018. assert(lc16 == hc16 && hc16 == 0);
  13019. hc16 = 0;
  13020. lc16 = 1;
  13021. mask = 0x55; // select hc16
  13022. }
  13023. assert(lc16 > hc16);
  13024. b.set_low_color(static_cast<uint16_t>(lc16));
  13025. b.set_high_color(static_cast<uint16_t>(hc16));
  13026. b.m_selectors[0] = mask;
  13027. b.m_selectors[1] = mask;
  13028. b.m_selectors[2] = mask;
  13029. b.m_selectors[3] = mask;
  13030. }
  13031. else
  13032. {
  13033. bool invert = false;
  13034. if (b.get_low_color() < b.get_high_color())
  13035. {
  13036. std::swap(b.m_low_color[0], b.m_high_color[0]);
  13037. std::swap(b.m_low_color[1], b.m_high_color[1]);
  13038. invert = true;
  13039. }
  13040. const uint8_t* pTran = s_uastc_to_bc1_weights[g_uastc_mode_weight_bits[mode]];
  13041. const uint32_t plane_shift = g_uastc_mode_planes[mode] - 1;
  13042. uint32_t sels = 0;
  13043. for (int i = 15; i >= 0; --i)
  13044. {
  13045. uint32_t s = pTran[astc_blk.m_weights[i << plane_shift]];
  13046. if (invert)
  13047. s ^= 1;
  13048. sels = (sels << 2) | s;
  13049. }
  13050. b.m_selectors[0] = sels & 0xFF;
  13051. b.m_selectors[1] = (sels >> 8) & 0xFF;
  13052. b.m_selectors[2] = (sels >> 16) & 0xFF;
  13053. b.m_selectors[3] = (sels >> 24) & 0xFF;
  13054. }
  13055. }
  13056. // Scale the UASTC first plane's weight indices to BC1, use 1 or 2 least squares passes to compute endpoints - no PCA needed.
  13057. void transcode_uastc_to_bc1_hint1(const unpacked_uastc_block& unpacked_src_blk, const color32 block_pixels[4][4], void* pDst, bool high_quality)
  13058. {
  13059. const uint32_t mode = unpacked_src_blk.m_mode;
  13060. const astc_block_desc& astc_blk = unpacked_src_blk.m_astc;
  13061. dxt1_block& b = *static_cast<dxt1_block*>(pDst);
  13062. b.set_low_color(1);
  13063. b.set_high_color(0);
  13064. const uint8_t* pTran = s_uastc_to_bc1_weights[g_uastc_mode_weight_bits[mode]];
  13065. const uint32_t plane_shift = g_uastc_mode_planes[mode] - 1;
  13066. uint32_t sels = 0;
  13067. for (int i = 15; i >= 0; --i)
  13068. {
  13069. sels <<= 2;
  13070. sels |= pTran[astc_blk.m_weights[i << plane_shift]];
  13071. }
  13072. b.m_selectors[0] = sels & 0xFF;
  13073. b.m_selectors[1] = (sels >> 8) & 0xFF;
  13074. b.m_selectors[2] = (sels >> 16) & 0xFF;
  13075. b.m_selectors[3] = (sels >> 24) & 0xFF;
  13076. encode_bc1(&b, (const uint8_t*)&block_pixels[0][0].c[0], (high_quality ? cEncodeBC1HighQuality : 0) | cEncodeBC1UseSelectors);
  13077. }
  13078. bool transcode_uastc_to_bc1(const uastc_block& src_blk, void* pDst, bool high_quality)
  13079. {
  13080. unpacked_uastc_block unpacked_src_blk;
  13081. if (!unpack_uastc(src_blk, unpacked_src_blk, false))
  13082. return false;
  13083. const uint32_t mode = unpacked_src_blk.m_mode;
  13084. if (mode == UASTC_MODE_INDEX_SOLID_COLOR)
  13085. {
  13086. encode_bc1_solid_block(pDst, unpacked_src_blk.m_solid_color.r, unpacked_src_blk.m_solid_color.g, unpacked_src_blk.m_solid_color.b);
  13087. return true;
  13088. }
  13089. if ((!high_quality) && (unpacked_src_blk.m_bc1_hint0))
  13090. transcode_uastc_to_bc1_hint0(unpacked_src_blk, pDst);
  13091. else
  13092. {
  13093. color32 block_pixels[4][4];
  13094. const bool unpack_srgb = false;
  13095. if (!unpack_uastc(unpacked_src_blk, &block_pixels[0][0], unpack_srgb))
  13096. return false;
  13097. if (unpacked_src_blk.m_bc1_hint1)
  13098. transcode_uastc_to_bc1_hint1(unpacked_src_blk, block_pixels, pDst, high_quality);
  13099. else
  13100. encode_bc1(pDst, &block_pixels[0][0].r, high_quality ? cEncodeBC1HighQuality : 0);
  13101. }
  13102. return true;
  13103. }
  13104. static void write_bc4_solid_block(uint8_t* pDst, uint32_t a)
  13105. {
  13106. pDst[0] = (uint8_t)a;
  13107. pDst[1] = (uint8_t)a;
  13108. memset(pDst + 2, 0, 6);
  13109. }
  13110. bool transcode_uastc_to_bc3(const uastc_block& src_blk, void* pDst, bool high_quality)
  13111. {
  13112. unpacked_uastc_block unpacked_src_blk;
  13113. if (!unpack_uastc(src_blk, unpacked_src_blk, false))
  13114. return false;
  13115. const uint32_t mode = unpacked_src_blk.m_mode;
  13116. void* pBC4_block = pDst;
  13117. dxt1_block* pBC1_block = &static_cast<dxt1_block*>(pDst)[1];
  13118. if (mode == UASTC_MODE_INDEX_SOLID_COLOR)
  13119. {
  13120. write_bc4_solid_block(static_cast<uint8_t*>(pBC4_block), unpacked_src_blk.m_solid_color.a);
  13121. encode_bc1_solid_block(pBC1_block, unpacked_src_blk.m_solid_color.r, unpacked_src_blk.m_solid_color.g, unpacked_src_blk.m_solid_color.b);
  13122. return true;
  13123. }
  13124. color32 block_pixels[4][4];
  13125. const bool unpack_srgb = false;
  13126. if (!unpack_uastc(unpacked_src_blk, &block_pixels[0][0], unpack_srgb))
  13127. return false;
  13128. basist::encode_bc4(pBC4_block, &block_pixels[0][0].a, sizeof(color32));
  13129. if ((!high_quality) && (unpacked_src_blk.m_bc1_hint0))
  13130. transcode_uastc_to_bc1_hint0(unpacked_src_blk, pBC1_block);
  13131. else
  13132. {
  13133. if (unpacked_src_blk.m_bc1_hint1)
  13134. transcode_uastc_to_bc1_hint1(unpacked_src_blk, block_pixels, pBC1_block, high_quality);
  13135. else
  13136. encode_bc1(pBC1_block, &block_pixels[0][0].r, high_quality ? cEncodeBC1HighQuality : 0);
  13137. }
  13138. return true;
  13139. }
  13140. bool transcode_uastc_to_bc4(const uastc_block& src_blk, void* pDst, bool high_quality, uint32_t chan0)
  13141. {
  13142. BASISU_NOTE_UNUSED(high_quality);
  13143. unpacked_uastc_block unpacked_src_blk;
  13144. if (!unpack_uastc(src_blk, unpacked_src_blk, false))
  13145. return false;
  13146. const uint32_t mode = unpacked_src_blk.m_mode;
  13147. void* pBC4_block = pDst;
  13148. if (mode == UASTC_MODE_INDEX_SOLID_COLOR)
  13149. {
  13150. write_bc4_solid_block(static_cast<uint8_t*>(pBC4_block), unpacked_src_blk.m_solid_color.c[chan0]);
  13151. return true;
  13152. }
  13153. color32 block_pixels[4][4];
  13154. const bool unpack_srgb = false;
  13155. if (!unpack_uastc(unpacked_src_blk, &block_pixels[0][0], unpack_srgb))
  13156. return false;
  13157. basist::encode_bc4(pBC4_block, &block_pixels[0][0].c[chan0], sizeof(color32));
  13158. return true;
  13159. }
  13160. bool transcode_uastc_to_bc5(const uastc_block& src_blk, void* pDst, bool high_quality, uint32_t chan0, uint32_t chan1)
  13161. {
  13162. BASISU_NOTE_UNUSED(high_quality);
  13163. unpacked_uastc_block unpacked_src_blk;
  13164. if (!unpack_uastc(src_blk, unpacked_src_blk, false))
  13165. return false;
  13166. const uint32_t mode = unpacked_src_blk.m_mode;
  13167. void* pBC4_block0 = pDst;
  13168. void* pBC4_block1 = (uint8_t*)pDst + 8;
  13169. if (mode == UASTC_MODE_INDEX_SOLID_COLOR)
  13170. {
  13171. write_bc4_solid_block(static_cast<uint8_t*>(pBC4_block0), unpacked_src_blk.m_solid_color.c[chan0]);
  13172. write_bc4_solid_block(static_cast<uint8_t*>(pBC4_block1), unpacked_src_blk.m_solid_color.c[chan1]);
  13173. return true;
  13174. }
  13175. color32 block_pixels[4][4];
  13176. const bool unpack_srgb = false;
  13177. if (!unpack_uastc(unpacked_src_blk, &block_pixels[0][0], unpack_srgb))
  13178. return false;
  13179. basist::encode_bc4(pBC4_block0, &block_pixels[0][0].c[chan0], sizeof(color32));
  13180. basist::encode_bc4(pBC4_block1, &block_pixels[0][0].c[chan1], sizeof(color32));
  13181. return true;
  13182. }
  13183. static const uint8_t s_etc2_eac_bit_ofs[16] = { 45, 33, 21, 9, 42, 30, 18, 6, 39, 27, 15, 3, 36, 24, 12, 0 };
  13184. static void pack_eac_solid_block(eac_block& blk, uint32_t a)
  13185. {
  13186. blk.m_base = static_cast<uint8_t>(a);
  13187. blk.m_table = 13;
  13188. blk.m_multiplier = 0;
  13189. memcpy(blk.m_selectors, g_etc2_eac_a8_sel4, sizeof(g_etc2_eac_a8_sel4));
  13190. return;
  13191. }
  13192. // Only checks 4 tables.
  13193. static void pack_eac(eac_block& blk, const uint8_t* pPixels, uint32_t stride)
  13194. {
  13195. uint32_t min_alpha = 255, max_alpha = 0;
  13196. for (uint32_t i = 0; i < 16; i++)
  13197. {
  13198. const uint32_t a = pPixels[i * stride];
  13199. if (a < min_alpha) min_alpha = a;
  13200. if (a > max_alpha) max_alpha = a;
  13201. }
  13202. if (min_alpha == max_alpha)
  13203. {
  13204. pack_eac_solid_block(blk, min_alpha);
  13205. return;
  13206. }
  13207. const uint32_t alpha_range = max_alpha - min_alpha;
  13208. const uint32_t SINGLE_TABLE_THRESH = 5;
  13209. if (alpha_range <= SINGLE_TABLE_THRESH)
  13210. {
  13211. // If alpha_range <= 5 table 13 is lossless
  13212. int base = clamp255((int)max_alpha - 2);
  13213. blk.m_base = base;
  13214. blk.m_multiplier = 1;
  13215. blk.m_table = 13;
  13216. base -= 3;
  13217. uint64_t packed_sels = 0;
  13218. for (uint32_t i = 0; i < 16; i++)
  13219. {
  13220. const int a = pPixels[i * stride];
  13221. static const uint8_t s_sels[6] = { 2, 1, 0, 4, 5, 6 };
  13222. int sel = a - base;
  13223. assert(sel >= 0 && sel <= 5);
  13224. packed_sels |= (static_cast<uint64_t>(s_sels[sel]) << s_etc2_eac_bit_ofs[i]);
  13225. }
  13226. blk.set_selector_bits(packed_sels);
  13227. return;
  13228. }
  13229. const uint32_t T0 = 2, T1 = 8, T2 = 11, T3 = 13;
  13230. static const uint8_t s_tables[4] = { T0, T1, T2, T3 };
  13231. int base[4], mul[4];
  13232. uint32_t mul_or = 0;
  13233. for (uint32_t i = 0; i < 4; i++)
  13234. {
  13235. const uint32_t table = s_tables[i];
  13236. const float range = (float)(g_eac_modifier_table[table][ETC2_EAC_MAX_VALUE_SELECTOR] - g_eac_modifier_table[table][ETC2_EAC_MIN_VALUE_SELECTOR]);
  13237. base[i] = clamp255((int)roundf(basisu::lerp((float)min_alpha, (float)max_alpha, (float)(0 - g_eac_modifier_table[table][ETC2_EAC_MIN_VALUE_SELECTOR]) / range)));
  13238. mul[i] = clampi((int)roundf(alpha_range / range), 1, 15);
  13239. mul_or |= mul[i];
  13240. }
  13241. uint32_t total_err[4] = { 0, 0, 0, 0 };
  13242. uint8_t sels[4][16];
  13243. for (uint32_t i = 0; i < 16; i++)
  13244. {
  13245. const int a = pPixels[i * stride];
  13246. uint32_t l0 = UINT32_MAX, l1 = UINT32_MAX, l2 = UINT32_MAX, l3 = UINT32_MAX;
  13247. if ((a < 7) || (a > (255 - 7)))
  13248. {
  13249. for (uint32_t s = 0; s < 8; s++)
  13250. {
  13251. const int v0 = clamp255(mul[0] * g_eac_modifier_table[T0][s] + base[0]);
  13252. const int v1 = clamp255(mul[1] * g_eac_modifier_table[T1][s] + base[1]);
  13253. const int v2 = clamp255(mul[2] * g_eac_modifier_table[T2][s] + base[2]);
  13254. const int v3 = clamp255(mul[3] * g_eac_modifier_table[T3][s] + base[3]);
  13255. l0 = basisu::minimum(l0, (basisu::iabs(v0 - a) << 3) | s);
  13256. l1 = basisu::minimum(l1, (basisu::iabs(v1 - a) << 3) | s);
  13257. l2 = basisu::minimum(l2, (basisu::iabs(v2 - a) << 3) | s);
  13258. l3 = basisu::minimum(l3, (basisu::iabs(v3 - a) << 3) | s);
  13259. }
  13260. }
  13261. else if (mul_or == 1)
  13262. {
  13263. const int a0 = base[0] - a, a1 = base[1] - a, a2 = base[2] - a, a3 = base[3] - a;
  13264. for (uint32_t s = 0; s < 8; s++)
  13265. {
  13266. const int v0 = g_eac_modifier_table[T0][s] + a0;
  13267. const int v1 = g_eac_modifier_table[T1][s] + a1;
  13268. const int v2 = g_eac_modifier_table[T2][s] + a2;
  13269. const int v3 = g_eac_modifier_table[T3][s] + a3;
  13270. l0 = basisu::minimum(l0, (basisu::iabs(v0) << 3) | s);
  13271. l1 = basisu::minimum(l1, (basisu::iabs(v1) << 3) | s);
  13272. l2 = basisu::minimum(l2, (basisu::iabs(v2) << 3) | s);
  13273. l3 = basisu::minimum(l3, (basisu::iabs(v3) << 3) | s);
  13274. }
  13275. }
  13276. else
  13277. {
  13278. const int a0 = base[0] - a, a1 = base[1] - a, a2 = base[2] - a, a3 = base[3] - a;
  13279. for (uint32_t s = 0; s < 8; s++)
  13280. {
  13281. const int v0 = mul[0] * g_eac_modifier_table[T0][s] + a0;
  13282. const int v1 = mul[1] * g_eac_modifier_table[T1][s] + a1;
  13283. const int v2 = mul[2] * g_eac_modifier_table[T2][s] + a2;
  13284. const int v3 = mul[3] * g_eac_modifier_table[T3][s] + a3;
  13285. l0 = basisu::minimum(l0, (basisu::iabs(v0) << 3) | s);
  13286. l1 = basisu::minimum(l1, (basisu::iabs(v1) << 3) | s);
  13287. l2 = basisu::minimum(l2, (basisu::iabs(v2) << 3) | s);
  13288. l3 = basisu::minimum(l3, (basisu::iabs(v3) << 3) | s);
  13289. }
  13290. }
  13291. sels[0][i] = l0 & 7;
  13292. sels[1][i] = l1 & 7;
  13293. sels[2][i] = l2 & 7;
  13294. sels[3][i] = l3 & 7;
  13295. total_err[0] += basisu::square<uint32_t>(l0 >> 3);
  13296. total_err[1] += basisu::square<uint32_t>(l1 >> 3);
  13297. total_err[2] += basisu::square<uint32_t>(l2 >> 3);
  13298. total_err[3] += basisu::square<uint32_t>(l3 >> 3);
  13299. }
  13300. uint32_t min_err = total_err[0], min_index = 0;
  13301. for (uint32_t i = 1; i < 4; i++)
  13302. {
  13303. if (total_err[i] < min_err)
  13304. {
  13305. min_err = total_err[i];
  13306. min_index = i;
  13307. }
  13308. }
  13309. blk.m_base = base[min_index];
  13310. blk.m_multiplier = mul[min_index];
  13311. blk.m_table = s_tables[min_index];
  13312. uint64_t packed_sels = 0;
  13313. const uint8_t* pSels = &sels[min_index][0];
  13314. for (uint32_t i = 0; i < 16; i++)
  13315. packed_sels |= (static_cast<uint64_t>(pSels[i]) << s_etc2_eac_bit_ofs[i]);
  13316. blk.set_selector_bits(packed_sels);
  13317. }
  13318. // Checks all 16 tables. Around ~2 dB better vs. pack_eac(), ~1.2 dB less than near-optimal.
  13319. static void pack_eac_high_quality(eac_block& blk, const uint8_t* pPixels, uint32_t stride)
  13320. {
  13321. uint32_t min_alpha = 255, max_alpha = 0;
  13322. for (uint32_t i = 0; i < 16; i++)
  13323. {
  13324. const uint32_t a = pPixels[i * stride];
  13325. if (a < min_alpha) min_alpha = a;
  13326. if (a > max_alpha) max_alpha = a;
  13327. }
  13328. if (min_alpha == max_alpha)
  13329. {
  13330. pack_eac_solid_block(blk, min_alpha);
  13331. return;
  13332. }
  13333. const uint32_t alpha_range = max_alpha - min_alpha;
  13334. const uint32_t SINGLE_TABLE_THRESH = 5;
  13335. if (alpha_range <= SINGLE_TABLE_THRESH)
  13336. {
  13337. // If alpha_range <= 5 table 13 is lossless
  13338. int base = clamp255((int)max_alpha - 2);
  13339. blk.m_base = base;
  13340. blk.m_multiplier = 1;
  13341. blk.m_table = 13;
  13342. base -= 3;
  13343. uint64_t packed_sels = 0;
  13344. for (uint32_t i = 0; i < 16; i++)
  13345. {
  13346. const int a = pPixels[i * stride];
  13347. static const uint8_t s_sels[6] = { 2, 1, 0, 4, 5, 6 };
  13348. int sel = a - base;
  13349. assert(sel >= 0 && sel <= 5);
  13350. packed_sels |= (static_cast<uint64_t>(s_sels[sel]) << s_etc2_eac_bit_ofs[i]);
  13351. }
  13352. blk.set_selector_bits(packed_sels);
  13353. return;
  13354. }
  13355. int base[16], mul[16];
  13356. for (uint32_t table = 0; table < 16; table++)
  13357. {
  13358. const float range = (float)(g_eac_modifier_table[table][ETC2_EAC_MAX_VALUE_SELECTOR] - g_eac_modifier_table[table][ETC2_EAC_MIN_VALUE_SELECTOR]);
  13359. base[table] = clamp255((int)roundf(basisu::lerp((float)min_alpha, (float)max_alpha, (float)(0 - g_eac_modifier_table[table][ETC2_EAC_MIN_VALUE_SELECTOR]) / range)));
  13360. mul[table] = clampi((int)roundf(alpha_range / range), 1, 15);
  13361. }
  13362. uint32_t total_err[16];
  13363. memset(total_err, 0, sizeof(total_err));
  13364. uint8_t sels[16][16];
  13365. for (uint32_t table = 0; table < 16; table++)
  13366. {
  13367. const int8_t* pTable = &g_eac_modifier_table[table][0];
  13368. const int m = mul[table], b = base[table];
  13369. uint32_t prev_l = 0, prev_a = UINT32_MAX;
  13370. for (uint32_t i = 0; i < 16; i++)
  13371. {
  13372. const int a = pPixels[i * stride];
  13373. if ((uint32_t)a == prev_a)
  13374. {
  13375. sels[table][i] = prev_l & 7;
  13376. total_err[table] += basisu::square<uint32_t>(prev_l >> 3);
  13377. }
  13378. else
  13379. {
  13380. uint32_t l = basisu::iabs(clamp255(m * pTable[0] + b) - a) << 3;
  13381. l = basisu::minimum(l, (basisu::iabs(clamp255(m * pTable[1] + b) - a) << 3) | 1);
  13382. l = basisu::minimum(l, (basisu::iabs(clamp255(m * pTable[2] + b) - a) << 3) | 2);
  13383. l = basisu::minimum(l, (basisu::iabs(clamp255(m * pTable[3] + b) - a) << 3) | 3);
  13384. l = basisu::minimum(l, (basisu::iabs(clamp255(m * pTable[4] + b) - a) << 3) | 4);
  13385. l = basisu::minimum(l, (basisu::iabs(clamp255(m * pTable[5] + b) - a) << 3) | 5);
  13386. l = basisu::minimum(l, (basisu::iabs(clamp255(m * pTable[6] + b) - a) << 3) | 6);
  13387. l = basisu::minimum(l, (basisu::iabs(clamp255(m * pTable[7] + b) - a) << 3) | 7);
  13388. sels[table][i] = l & 7;
  13389. total_err[table] += basisu::square<uint32_t>(l >> 3);
  13390. prev_l = l;
  13391. prev_a = a;
  13392. }
  13393. }
  13394. }
  13395. uint32_t min_err = total_err[0], min_index = 0;
  13396. for (uint32_t i = 1; i < 16; i++)
  13397. {
  13398. if (total_err[i] < min_err)
  13399. {
  13400. min_err = total_err[i];
  13401. min_index = i;
  13402. }
  13403. }
  13404. blk.m_base = base[min_index];
  13405. blk.m_multiplier = mul[min_index];
  13406. blk.m_table = min_index;
  13407. uint64_t packed_sels = 0;
  13408. const uint8_t* pSels = &sels[min_index][0];
  13409. for (uint32_t i = 0; i < 16; i++)
  13410. packed_sels |= (static_cast<uint64_t>(pSels[i]) << s_etc2_eac_bit_ofs[i]);
  13411. blk.set_selector_bits(packed_sels);
  13412. }
  13413. bool transcode_uastc_to_etc2_eac_r11(const uastc_block& src_blk, void* pDst, bool high_quality, uint32_t chan0)
  13414. {
  13415. unpacked_uastc_block unpacked_src_blk;
  13416. if (!unpack_uastc(src_blk, unpacked_src_blk, false))
  13417. return false;
  13418. const uint32_t mode = unpacked_src_blk.m_mode;
  13419. if (mode == UASTC_MODE_INDEX_SOLID_COLOR)
  13420. {
  13421. pack_eac_solid_block(*static_cast<eac_block*>(pDst), unpacked_src_blk.m_solid_color.c[chan0]);
  13422. return true;
  13423. }
  13424. color32 block_pixels[4][4];
  13425. const bool unpack_srgb = false;
  13426. if (!unpack_uastc(unpacked_src_blk, &block_pixels[0][0], unpack_srgb))
  13427. return false;
  13428. if (chan0 == 3)
  13429. transcode_uastc_to_etc2_eac_a8(unpacked_src_blk, block_pixels, pDst);
  13430. else
  13431. (high_quality ? pack_eac_high_quality : pack_eac)(*static_cast<eac_block*>(pDst), &block_pixels[0][0].c[chan0], sizeof(color32));
  13432. return true;
  13433. }
  13434. bool transcode_uastc_to_etc2_eac_rg11(const uastc_block& src_blk, void* pDst, bool high_quality, uint32_t chan0, uint32_t chan1)
  13435. {
  13436. unpacked_uastc_block unpacked_src_blk;
  13437. if (!unpack_uastc(src_blk, unpacked_src_blk, false))
  13438. return false;
  13439. const uint32_t mode = unpacked_src_blk.m_mode;
  13440. if (mode == UASTC_MODE_INDEX_SOLID_COLOR)
  13441. {
  13442. pack_eac_solid_block(static_cast<eac_block*>(pDst)[0], unpacked_src_blk.m_solid_color.c[chan0]);
  13443. pack_eac_solid_block(static_cast<eac_block*>(pDst)[1], unpacked_src_blk.m_solid_color.c[chan1]);
  13444. return true;
  13445. }
  13446. color32 block_pixels[4][4];
  13447. const bool unpack_srgb = false;
  13448. if (!unpack_uastc(unpacked_src_blk, &block_pixels[0][0], unpack_srgb))
  13449. return false;
  13450. if (chan0 == 3)
  13451. transcode_uastc_to_etc2_eac_a8(unpacked_src_blk, block_pixels, &static_cast<eac_block*>(pDst)[0]);
  13452. else
  13453. (high_quality ? pack_eac_high_quality : pack_eac)(static_cast<eac_block*>(pDst)[0], &block_pixels[0][0].c[chan0], sizeof(color32));
  13454. if (chan1 == 3)
  13455. transcode_uastc_to_etc2_eac_a8(unpacked_src_blk, block_pixels, &static_cast<eac_block*>(pDst)[1]);
  13456. else
  13457. (high_quality ? pack_eac_high_quality : pack_eac)(static_cast<eac_block*>(pDst)[1], &block_pixels[0][0].c[chan1], sizeof(color32));
  13458. return true;
  13459. }
  13460. // PVRTC1
  13461. static void fixup_pvrtc1_4_modulation_rgb(
  13462. const uastc_block* pSrc_blocks,
  13463. const uint32_t* pPVRTC_endpoints,
  13464. void* pDst_blocks,
  13465. uint32_t num_blocks_x, uint32_t num_blocks_y, bool from_alpha)
  13466. {
  13467. const uint32_t x_mask = num_blocks_x - 1;
  13468. const uint32_t y_mask = num_blocks_y - 1;
  13469. const uint32_t x_bits = basisu::total_bits(x_mask);
  13470. const uint32_t y_bits = basisu::total_bits(y_mask);
  13471. const uint32_t min_bits = basisu::minimum(x_bits, y_bits);
  13472. //const uint32_t max_bits = basisu::maximum(x_bits, y_bits);
  13473. const uint32_t swizzle_mask = (1 << (min_bits * 2)) - 1;
  13474. uint32_t block_index = 0;
  13475. // really 3x3
  13476. int e0[4][4], e1[4][4];
  13477. for (int y = 0; y < static_cast<int>(num_blocks_y); y++)
  13478. {
  13479. const uint32_t* pE_rows[3];
  13480. for (int ey = 0; ey < 3; ey++)
  13481. {
  13482. int by = y + ey - 1;
  13483. const uint32_t* pE = &pPVRTC_endpoints[(by & y_mask) * num_blocks_x];
  13484. pE_rows[ey] = pE;
  13485. for (int ex = 0; ex < 3; ex++)
  13486. {
  13487. int bx = 0 + ex - 1;
  13488. const uint32_t e = pE[bx & x_mask];
  13489. e0[ex][ey] = (get_opaque_endpoint_l0(e) * 255) / 31;
  13490. e1[ex][ey] = (get_opaque_endpoint_l1(e) * 255) / 31;
  13491. }
  13492. }
  13493. const uint32_t y_swizzle = (g_pvrtc_swizzle_table[y >> 8] << 16) | g_pvrtc_swizzle_table[y & 0xFF];
  13494. for (int x = 0; x < static_cast<int>(num_blocks_x); x++, block_index++)
  13495. {
  13496. const uastc_block& src_block = pSrc_blocks[block_index];
  13497. color32 block_pixels[4][4];
  13498. unpack_uastc(src_block, &block_pixels[0][0], false);
  13499. if (from_alpha)
  13500. {
  13501. // Just set RGB to alpha to avoid adding complexity below.
  13502. for (uint32_t i = 0; i < 16; i++)
  13503. {
  13504. const uint8_t a = ((color32*)block_pixels)[i].a;
  13505. ((color32*)block_pixels)[i].set(a, a, a, 255);
  13506. }
  13507. }
  13508. const uint32_t x_swizzle = (g_pvrtc_swizzle_table[x >> 8] << 17) | (g_pvrtc_swizzle_table[x & 0xFF] << 1);
  13509. uint32_t swizzled = x_swizzle | y_swizzle;
  13510. if (num_blocks_x != num_blocks_y)
  13511. {
  13512. swizzled &= swizzle_mask;
  13513. if (num_blocks_x > num_blocks_y)
  13514. swizzled |= ((x >> min_bits) << (min_bits * 2));
  13515. else
  13516. swizzled |= ((y >> min_bits) << (min_bits * 2));
  13517. }
  13518. pvrtc4_block* pDst_block = static_cast<pvrtc4_block*>(pDst_blocks) + swizzled;
  13519. pDst_block->m_endpoints = pPVRTC_endpoints[block_index];
  13520. {
  13521. const uint32_t ex = 2;
  13522. int bx = x + ex - 1;
  13523. bx &= x_mask;
  13524. #define DO_ROW(ey) \
  13525. { \
  13526. const uint32_t e = pE_rows[ey][bx]; \
  13527. e0[ex][ey] = (get_opaque_endpoint_l0(e) * 255) / 31; \
  13528. e1[ex][ey] = (get_opaque_endpoint_l1(e) * 255) / 31; \
  13529. }
  13530. DO_ROW(0);
  13531. DO_ROW(1);
  13532. DO_ROW(2);
  13533. #undef DO_ROW
  13534. }
  13535. uint32_t mod = 0;
  13536. #define DO_PIX(lx, ly, w0, w1, w2, w3) \
  13537. { \
  13538. int ca_l = a0 * w0 + a1 * w1 + a2 * w2 + a3 * w3; \
  13539. int cb_l = b0 * w0 + b1 * w1 + b2 * w2 + b3 * w3; \
  13540. int cl = (block_pixels[ly][lx].r + block_pixels[ly][lx].g + block_pixels[ly][lx].b) * 16; \
  13541. int dl = cb_l - ca_l; \
  13542. int vl = cl - ca_l; \
  13543. int p = vl * 16; \
  13544. if (ca_l > cb_l) { p = -p; dl = -dl; } \
  13545. uint32_t m = 0; \
  13546. if (p > 3 * dl) m = (uint32_t)(1 << ((ly) * 8 + (lx) * 2)); \
  13547. if (p > 8 * dl) m = (uint32_t)(2 << ((ly) * 8 + (lx) * 2)); \
  13548. if (p > 13 * dl) m = (uint32_t)(3 << ((ly) * 8 + (lx) * 2)); \
  13549. mod |= m; \
  13550. }
  13551. {
  13552. const uint32_t ex = 0, ey = 0;
  13553. const int a0 = e0[ex][ey], a1 = e0[ex + 1][ey], a2 = e0[ex][ey + 1], a3 = e0[ex + 1][ey + 1];
  13554. const int b0 = e1[ex][ey], b1 = e1[ex + 1][ey], b2 = e1[ex][ey + 1], b3 = e1[ex + 1][ey + 1];
  13555. DO_PIX(0, 0, 4, 4, 4, 4);
  13556. DO_PIX(1, 0, 2, 6, 2, 6);
  13557. DO_PIX(0, 1, 2, 2, 6, 6);
  13558. DO_PIX(1, 1, 1, 3, 3, 9);
  13559. }
  13560. {
  13561. const uint32_t ex = 1, ey = 0;
  13562. const int a0 = e0[ex][ey], a1 = e0[ex + 1][ey], a2 = e0[ex][ey + 1], a3 = e0[ex + 1][ey + 1];
  13563. const int b0 = e1[ex][ey], b1 = e1[ex + 1][ey], b2 = e1[ex][ey + 1], b3 = e1[ex + 1][ey + 1];
  13564. DO_PIX(2, 0, 8, 0, 8, 0);
  13565. DO_PIX(3, 0, 6, 2, 6, 2);
  13566. DO_PIX(2, 1, 4, 0, 12, 0);
  13567. DO_PIX(3, 1, 3, 1, 9, 3);
  13568. }
  13569. {
  13570. const uint32_t ex = 0, ey = 1;
  13571. const int a0 = e0[ex][ey], a1 = e0[ex + 1][ey], a2 = e0[ex][ey + 1], a3 = e0[ex + 1][ey + 1];
  13572. const int b0 = e1[ex][ey], b1 = e1[ex + 1][ey], b2 = e1[ex][ey + 1], b3 = e1[ex + 1][ey + 1];
  13573. DO_PIX(0, 2, 8, 8, 0, 0);
  13574. DO_PIX(1, 2, 4, 12, 0, 0);
  13575. DO_PIX(0, 3, 6, 6, 2, 2);
  13576. DO_PIX(1, 3, 3, 9, 1, 3);
  13577. }
  13578. {
  13579. const uint32_t ex = 1, ey = 1;
  13580. const int a0 = e0[ex][ey], a1 = e0[ex + 1][ey], a2 = e0[ex][ey + 1], a3 = e0[ex + 1][ey + 1];
  13581. const int b0 = e1[ex][ey], b1 = e1[ex + 1][ey], b2 = e1[ex][ey + 1], b3 = e1[ex + 1][ey + 1];
  13582. DO_PIX(2, 2, 16, 0, 0, 0);
  13583. DO_PIX(3, 2, 12, 4, 0, 0);
  13584. DO_PIX(2, 3, 12, 0, 4, 0);
  13585. DO_PIX(3, 3, 9, 3, 3, 1);
  13586. }
  13587. #undef DO_PIX
  13588. pDst_block->m_modulation = mod;
  13589. e0[0][0] = e0[1][0]; e0[1][0] = e0[2][0];
  13590. e0[0][1] = e0[1][1]; e0[1][1] = e0[2][1];
  13591. e0[0][2] = e0[1][2]; e0[1][2] = e0[2][2];
  13592. e1[0][0] = e1[1][0]; e1[1][0] = e1[2][0];
  13593. e1[0][1] = e1[1][1]; e1[1][1] = e1[2][1];
  13594. e1[0][2] = e1[1][2]; e1[1][2] = e1[2][2];
  13595. } // x
  13596. } // y
  13597. }
  13598. static void fixup_pvrtc1_4_modulation_rgba(
  13599. const uastc_block* pSrc_blocks,
  13600. const uint32_t* pPVRTC_endpoints,
  13601. void* pDst_blocks, uint32_t num_blocks_x, uint32_t num_blocks_y)
  13602. {
  13603. const uint32_t x_mask = num_blocks_x - 1;
  13604. const uint32_t y_mask = num_blocks_y - 1;
  13605. const uint32_t x_bits = basisu::total_bits(x_mask);
  13606. const uint32_t y_bits = basisu::total_bits(y_mask);
  13607. const uint32_t min_bits = basisu::minimum(x_bits, y_bits);
  13608. //const uint32_t max_bits = basisu::maximum(x_bits, y_bits);
  13609. const uint32_t swizzle_mask = (1 << (min_bits * 2)) - 1;
  13610. uint32_t block_index = 0;
  13611. // really 3x3
  13612. int e0[4][4], e1[4][4];
  13613. for (int y = 0; y < static_cast<int>(num_blocks_y); y++)
  13614. {
  13615. const uint32_t* pE_rows[3];
  13616. for (int ey = 0; ey < 3; ey++)
  13617. {
  13618. int by = y + ey - 1;
  13619. const uint32_t* pE = &pPVRTC_endpoints[(by & y_mask) * num_blocks_x];
  13620. pE_rows[ey] = pE;
  13621. for (int ex = 0; ex < 3; ex++)
  13622. {
  13623. int bx = 0 + ex - 1;
  13624. const uint32_t e = pE[bx & x_mask];
  13625. e0[ex][ey] = get_endpoint_l8(e, 0);
  13626. e1[ex][ey] = get_endpoint_l8(e, 1);
  13627. }
  13628. }
  13629. const uint32_t y_swizzle = (g_pvrtc_swizzle_table[y >> 8] << 16) | g_pvrtc_swizzle_table[y & 0xFF];
  13630. for (int x = 0; x < static_cast<int>(num_blocks_x); x++, block_index++)
  13631. {
  13632. const uastc_block& src_block = pSrc_blocks[block_index];
  13633. color32 block_pixels[4][4];
  13634. unpack_uastc(src_block, &block_pixels[0][0], false);
  13635. const uint32_t x_swizzle = (g_pvrtc_swizzle_table[x >> 8] << 17) | (g_pvrtc_swizzle_table[x & 0xFF] << 1);
  13636. uint32_t swizzled = x_swizzle | y_swizzle;
  13637. if (num_blocks_x != num_blocks_y)
  13638. {
  13639. swizzled &= swizzle_mask;
  13640. if (num_blocks_x > num_blocks_y)
  13641. swizzled |= ((x >> min_bits) << (min_bits * 2));
  13642. else
  13643. swizzled |= ((y >> min_bits) << (min_bits * 2));
  13644. }
  13645. pvrtc4_block* pDst_block = static_cast<pvrtc4_block*>(pDst_blocks) + swizzled;
  13646. pDst_block->m_endpoints = pPVRTC_endpoints[block_index];
  13647. {
  13648. const uint32_t ex = 2;
  13649. int bx = x + ex - 1;
  13650. bx &= x_mask;
  13651. #define DO_ROW(ey) \
  13652. { \
  13653. const uint32_t e = pE_rows[ey][bx]; \
  13654. e0[ex][ey] = get_endpoint_l8(e, 0); \
  13655. e1[ex][ey] = get_endpoint_l8(e, 1); \
  13656. }
  13657. DO_ROW(0);
  13658. DO_ROW(1);
  13659. DO_ROW(2);
  13660. #undef DO_ROW
  13661. }
  13662. uint32_t mod = 0;
  13663. #define DO_PIX(lx, ly, w0, w1, w2, w3) \
  13664. { \
  13665. int ca_l = a0 * w0 + a1 * w1 + a2 * w2 + a3 * w3; \
  13666. int cb_l = b0 * w0 + b1 * w1 + b2 * w2 + b3 * w3; \
  13667. int cl = 16 * (block_pixels[ly][lx].r + block_pixels[ly][lx].g + block_pixels[ly][lx].b + block_pixels[ly][lx].a); \
  13668. int dl = cb_l - ca_l; \
  13669. int vl = cl - ca_l; \
  13670. int p = vl * 16; \
  13671. if (ca_l > cb_l) { p = -p; dl = -dl; } \
  13672. uint32_t m = 0; \
  13673. if (p > 3 * dl) m = (uint32_t)(1 << ((ly) * 8 + (lx) * 2)); \
  13674. if (p > 8 * dl) m = (uint32_t)(2 << ((ly) * 8 + (lx) * 2)); \
  13675. if (p > 13 * dl) m = (uint32_t)(3 << ((ly) * 8 + (lx) * 2)); \
  13676. mod |= m; \
  13677. }
  13678. {
  13679. const uint32_t ex = 0, ey = 0;
  13680. const int a0 = e0[ex][ey], a1 = e0[ex + 1][ey], a2 = e0[ex][ey + 1], a3 = e0[ex + 1][ey + 1];
  13681. const int b0 = e1[ex][ey], b1 = e1[ex + 1][ey], b2 = e1[ex][ey + 1], b3 = e1[ex + 1][ey + 1];
  13682. DO_PIX(0, 0, 4, 4, 4, 4);
  13683. DO_PIX(1, 0, 2, 6, 2, 6);
  13684. DO_PIX(0, 1, 2, 2, 6, 6);
  13685. DO_PIX(1, 1, 1, 3, 3, 9);
  13686. }
  13687. {
  13688. const uint32_t ex = 1, ey = 0;
  13689. const int a0 = e0[ex][ey], a1 = e0[ex + 1][ey], a2 = e0[ex][ey + 1], a3 = e0[ex + 1][ey + 1];
  13690. const int b0 = e1[ex][ey], b1 = e1[ex + 1][ey], b2 = e1[ex][ey + 1], b3 = e1[ex + 1][ey + 1];
  13691. DO_PIX(2, 0, 8, 0, 8, 0);
  13692. DO_PIX(3, 0, 6, 2, 6, 2);
  13693. DO_PIX(2, 1, 4, 0, 12, 0);
  13694. DO_PIX(3, 1, 3, 1, 9, 3);
  13695. }
  13696. {
  13697. const uint32_t ex = 0, ey = 1;
  13698. const int a0 = e0[ex][ey], a1 = e0[ex + 1][ey], a2 = e0[ex][ey + 1], a3 = e0[ex + 1][ey + 1];
  13699. const int b0 = e1[ex][ey], b1 = e1[ex + 1][ey], b2 = e1[ex][ey + 1], b3 = e1[ex + 1][ey + 1];
  13700. DO_PIX(0, 2, 8, 8, 0, 0);
  13701. DO_PIX(1, 2, 4, 12, 0, 0);
  13702. DO_PIX(0, 3, 6, 6, 2, 2);
  13703. DO_PIX(1, 3, 3, 9, 1, 3);
  13704. }
  13705. {
  13706. const uint32_t ex = 1, ey = 1;
  13707. const int a0 = e0[ex][ey], a1 = e0[ex + 1][ey], a2 = e0[ex][ey + 1], a3 = e0[ex + 1][ey + 1];
  13708. const int b0 = e1[ex][ey], b1 = e1[ex + 1][ey], b2 = e1[ex][ey + 1], b3 = e1[ex + 1][ey + 1];
  13709. DO_PIX(2, 2, 16, 0, 0, 0);
  13710. DO_PIX(3, 2, 12, 4, 0, 0);
  13711. DO_PIX(2, 3, 12, 0, 4, 0);
  13712. DO_PIX(3, 3, 9, 3, 3, 1);
  13713. }
  13714. #undef DO_PIX
  13715. pDst_block->m_modulation = mod;
  13716. e0[0][0] = e0[1][0]; e0[1][0] = e0[2][0];
  13717. e0[0][1] = e0[1][1]; e0[1][1] = e0[2][1];
  13718. e0[0][2] = e0[1][2]; e0[1][2] = e0[2][2];
  13719. e1[0][0] = e1[1][0]; e1[1][0] = e1[2][0];
  13720. e1[0][1] = e1[1][1]; e1[1][1] = e1[2][1];
  13721. e1[0][2] = e1[1][2]; e1[1][2] = e1[2][2];
  13722. } // x
  13723. } // y
  13724. }
  13725. bool transcode_uastc_to_pvrtc1_4_rgb(const uastc_block* pSrc_blocks, void* pDst_blocks, uint32_t num_blocks_x, uint32_t num_blocks_y, bool high_quality, bool from_alpha)
  13726. {
  13727. BASISU_NOTE_UNUSED(high_quality);
  13728. if ((!num_blocks_x) || (!num_blocks_y))
  13729. return false;
  13730. const uint32_t width = num_blocks_x * 4;
  13731. const uint32_t height = num_blocks_y * 4;
  13732. if (!basisu::is_pow2(width) || !basisu::is_pow2(height))
  13733. return false;
  13734. basisu::vector<uint32_t> temp_endpoints(num_blocks_x * num_blocks_y);
  13735. for (uint32_t y = 0; y < num_blocks_y; y++)
  13736. {
  13737. for (uint32_t x = 0; x < num_blocks_x; x++)
  13738. {
  13739. color32 block_pixels[16];
  13740. if (!unpack_uastc(pSrc_blocks[x + y * num_blocks_x], block_pixels, false))
  13741. return false;
  13742. // Get block's RGB bounding box
  13743. color32 low_color(255, 255, 255, 255), high_color(0, 0, 0, 0);
  13744. if (from_alpha)
  13745. {
  13746. uint32_t low_a = 255, high_a = 0;
  13747. for (uint32_t i = 0; i < 16; i++)
  13748. {
  13749. low_a = basisu::minimum<uint32_t>(low_a, block_pixels[i].a);
  13750. high_a = basisu::maximum<uint32_t>(high_a, block_pixels[i].a);
  13751. }
  13752. low_color.set(low_a, low_a, low_a, 255);
  13753. high_color.set(high_a, high_a, high_a, 255);
  13754. }
  13755. else
  13756. {
  13757. for (uint32_t i = 0; i < 16; i++)
  13758. {
  13759. low_color = color32::comp_min(low_color, block_pixels[i]);
  13760. high_color = color32::comp_max(high_color, block_pixels[i]);
  13761. }
  13762. }
  13763. // Set PVRTC1 endpoints to floor/ceil of bounding box's coordinates.
  13764. pvrtc4_block temp;
  13765. temp.set_opaque_endpoint_floor(0, low_color);
  13766. temp.set_opaque_endpoint_ceil(1, high_color);
  13767. temp_endpoints[x + y * num_blocks_x] = temp.m_endpoints;
  13768. }
  13769. }
  13770. fixup_pvrtc1_4_modulation_rgb(pSrc_blocks, &temp_endpoints[0], pDst_blocks, num_blocks_x, num_blocks_y, from_alpha);
  13771. return true;
  13772. }
  13773. bool transcode_uastc_to_pvrtc1_4_rgba(const uastc_block* pSrc_blocks, void* pDst_blocks, uint32_t num_blocks_x, uint32_t num_blocks_y, bool high_quality)
  13774. {
  13775. BASISU_NOTE_UNUSED(high_quality);
  13776. if ((!num_blocks_x) || (!num_blocks_y))
  13777. return false;
  13778. const uint32_t width = num_blocks_x * 4;
  13779. const uint32_t height = num_blocks_y * 4;
  13780. if (!basisu::is_pow2(width) || !basisu::is_pow2(height))
  13781. return false;
  13782. basisu::vector<uint32_t> temp_endpoints(num_blocks_x * num_blocks_y);
  13783. for (uint32_t y = 0; y < num_blocks_y; y++)
  13784. {
  13785. for (uint32_t x = 0; x < num_blocks_x; x++)
  13786. {
  13787. color32 block_pixels[16];
  13788. if (!unpack_uastc(pSrc_blocks[x + y * num_blocks_x], block_pixels, false))
  13789. return false;
  13790. // Get block's RGBA bounding box
  13791. color32 low_color(255, 255, 255, 255), high_color(0, 0, 0, 0);
  13792. for (uint32_t i = 0; i < 16; i++)
  13793. {
  13794. low_color = color32::comp_min(low_color, block_pixels[i]);
  13795. high_color = color32::comp_max(high_color, block_pixels[i]);
  13796. }
  13797. // Set PVRTC1 endpoints to floor/ceil of bounding box's coordinates.
  13798. pvrtc4_block temp;
  13799. temp.set_endpoint_floor(0, low_color);
  13800. temp.set_endpoint_ceil(1, high_color);
  13801. temp_endpoints[x + y * num_blocks_x] = temp.m_endpoints;
  13802. }
  13803. }
  13804. fixup_pvrtc1_4_modulation_rgba(pSrc_blocks, &temp_endpoints[0], pDst_blocks, num_blocks_x, num_blocks_y);
  13805. return true;
  13806. }
  13807. void uastc_init()
  13808. {
  13809. for (uint32_t range = 0; range < BC7ENC_TOTAL_ASTC_RANGES; range++)
  13810. {
  13811. if (!astc_is_valid_endpoint_range(range))
  13812. continue;
  13813. const uint32_t levels = astc_get_levels(range);
  13814. uint32_t vals[256];
  13815. for (uint32_t i = 0; i < levels; i++)
  13816. vals[i] = (unquant_astc_endpoint_val(i, range) << 8) | i;
  13817. std::sort(vals, vals + levels);
  13818. for (uint32_t i = 0; i < levels; i++)
  13819. {
  13820. const uint32_t order = vals[i] & 0xFF;
  13821. const uint32_t unq = vals[i] >> 8;
  13822. g_astc_unquant[range][order].m_unquant = (uint8_t)unq;
  13823. g_astc_unquant[range][order].m_index = (uint8_t)i;
  13824. } // i
  13825. }
  13826. // TODO: Precompute?
  13827. // BC7 777.1
  13828. for (int c = 0; c < 256; c++)
  13829. {
  13830. for (uint32_t lp = 0; lp < 2; lp++)
  13831. {
  13832. endpoint_err best;
  13833. best.m_error = (uint16_t)UINT16_MAX;
  13834. for (uint32_t l = 0; l < 128; l++)
  13835. {
  13836. const uint32_t low = (l << 1) | lp;
  13837. for (uint32_t h = 0; h < 128; h++)
  13838. {
  13839. const uint32_t high = (h << 1) | lp;
  13840. const int k = (low * (64 - g_bc7_weights4[BC7ENC_MODE_6_OPTIMAL_INDEX]) + high * g_bc7_weights4[BC7ENC_MODE_6_OPTIMAL_INDEX] + 32) >> 6;
  13841. const int err = (k - c) * (k - c);
  13842. if (err < best.m_error)
  13843. {
  13844. best.m_error = (uint16_t)err;
  13845. best.m_lo = (uint8_t)l;
  13846. best.m_hi = (uint8_t)h;
  13847. }
  13848. } // h
  13849. } // l
  13850. g_bc7_mode_6_optimal_endpoints[c][lp] = best;
  13851. } // lp
  13852. } // c
  13853. // BC7 777
  13854. for (int c = 0; c < 256; c++)
  13855. {
  13856. endpoint_err best;
  13857. best.m_error = (uint16_t)UINT16_MAX;
  13858. for (uint32_t l = 0; l < 128; l++)
  13859. {
  13860. const uint32_t low = (l << 1) | (l >> 6);
  13861. for (uint32_t h = 0; h < 128; h++)
  13862. {
  13863. const uint32_t high = (h << 1) | (h >> 6);
  13864. const int k = (low * (64 - g_bc7_weights2[BC7ENC_MODE_5_OPTIMAL_INDEX]) + high * g_bc7_weights2[BC7ENC_MODE_5_OPTIMAL_INDEX] + 32) >> 6;
  13865. const int err = (k - c) * (k - c);
  13866. if (err < best.m_error)
  13867. {
  13868. best.m_error = (uint16_t)err;
  13869. best.m_lo = (uint8_t)l;
  13870. best.m_hi = (uint8_t)h;
  13871. }
  13872. } // h
  13873. } // l
  13874. g_bc7_mode_5_optimal_endpoints[c] = best;
  13875. } // c
  13876. }
  13877. #endif // #if BASISD_SUPPORT_UASTC
  13878. // ------------------------------------------------------------------------------------------------------
  13879. // KTX2
  13880. // ------------------------------------------------------------------------------------------------------
  13881. #if BASISD_SUPPORT_KTX2
  13882. const uint8_t g_ktx2_file_identifier[12] = { 0xAB, 0x4B, 0x54, 0x58, 0x20, 0x32, 0x30, 0xBB, 0x0D, 0x0A, 0x1A, 0x0A };
  13883. ktx2_transcoder::ktx2_transcoder(basist::etc1_global_selector_codebook* pGlobal_sel_codebook) :
  13884. m_etc1s_transcoder(pGlobal_sel_codebook)
  13885. {
  13886. clear();
  13887. }
  13888. void ktx2_transcoder::clear()
  13889. {
  13890. m_pData = nullptr;
  13891. m_data_size = 0;
  13892. memset(&m_header, 0, sizeof(m_header));
  13893. m_levels.clear();
  13894. m_dfd.clear();
  13895. m_key_values.clear();
  13896. memset(&m_etc1s_header, 0, sizeof(m_etc1s_header));
  13897. m_etc1s_image_descs.clear();
  13898. m_format = basist::basis_tex_format::cETC1S;
  13899. m_dfd_color_model = 0;
  13900. m_dfd_color_prims = KTX2_DF_PRIMARIES_UNSPECIFIED;
  13901. m_dfd_transfer_func = 0;
  13902. m_dfd_flags = 0;
  13903. m_dfd_samples = 0;
  13904. m_dfd_chan0 = KTX2_DF_CHANNEL_UASTC_RGB;
  13905. m_dfd_chan1 = KTX2_DF_CHANNEL_UASTC_RGB;
  13906. m_etc1s_transcoder.clear();
  13907. m_def_transcoder_state.clear();
  13908. m_has_alpha = false;
  13909. m_is_video = false;
  13910. }
  13911. bool ktx2_transcoder::init(const void* pData, uint32_t data_size)
  13912. {
  13913. clear();
  13914. if (!pData)
  13915. {
  13916. BASISU_DEVEL_ERROR("ktx2_transcoder::init: pData is nullptr\n");
  13917. assert(0);
  13918. return false;
  13919. }
  13920. if (data_size <= sizeof(ktx2_header))
  13921. {
  13922. BASISU_DEVEL_ERROR("ktx2_transcoder::init: File is impossibly too small to be a valid KTX2 file\n");
  13923. return false;
  13924. }
  13925. if (memcmp(pData, g_ktx2_file_identifier, sizeof(g_ktx2_file_identifier)) != 0)
  13926. {
  13927. BASISU_DEVEL_ERROR("ktx2_transcoder::init: KTX2 file identifier is not present\n");
  13928. return false;
  13929. }
  13930. m_pData = static_cast<const uint8_t *>(pData);
  13931. m_data_size = data_size;
  13932. memcpy(&m_header, pData, sizeof(m_header));
  13933. // We only support UASTC and ETC1S
  13934. if (m_header.m_vk_format != KTX2_VK_FORMAT_UNDEFINED)
  13935. {
  13936. BASISU_DEVEL_ERROR("ktx2_transcoder::init: KTX2 file must be in ETC1S or UASTC format\n");
  13937. return false;
  13938. }
  13939. // 3.3: "When format is VK_FORMAT_UNDEFINED, typeSize must equal 1."
  13940. if (m_header.m_type_size != 1)
  13941. {
  13942. BASISU_DEVEL_ERROR("ktx2_transcoder::init: Invalid type_size\n");
  13943. return false;
  13944. }
  13945. // We only currently support 2D textures (plain, cubemapped, or texture array), which is by far the most common use case.
  13946. // The BasisU library does not support 1D or 3D textures at all.
  13947. if ((m_header.m_pixel_width < 1) || (m_header.m_pixel_height < 1) || (m_header.m_pixel_depth > 0))
  13948. {
  13949. BASISU_DEVEL_ERROR("ktx2_transcoder::init: Only 2D or cubemap textures are supported\n");
  13950. return false;
  13951. }
  13952. // Face count must be 1 or 6
  13953. if ((m_header.m_face_count != 1) && (m_header.m_face_count != 6))
  13954. {
  13955. BASISU_DEVEL_ERROR("ktx2_transcoder::init: Invalid face count, file is corrupted or invalid\n");
  13956. return false;
  13957. }
  13958. if (m_header.m_face_count > 1)
  13959. {
  13960. // 3.4: Make sure cubemaps are square.
  13961. if (m_header.m_pixel_width != m_header.m_pixel_height)
  13962. {
  13963. BASISU_DEVEL_ERROR("ktx2_transcoder::init: Cubemap is not square\n");
  13964. return false;
  13965. }
  13966. }
  13967. // 3.7 levelCount: "levelCount=0 is allowed, except for block-compressed formats"
  13968. if (m_header.m_level_count < 1)
  13969. {
  13970. BASISU_DEVEL_ERROR("ktx2_transcoder::init: Invalid level count\n");
  13971. return false;
  13972. }
  13973. // Sanity check the level count.
  13974. if (m_header.m_level_count > KTX2_MAX_SUPPORTED_LEVEL_COUNT)
  13975. {
  13976. BASISU_DEVEL_ERROR("ktx2_transcoder::init: Too many levels or file is corrupted or invalid\n");
  13977. return false;
  13978. }
  13979. if (m_header.m_supercompression_scheme > KTX2_SS_ZSTANDARD)
  13980. {
  13981. BASISU_DEVEL_ERROR("ktx2_transcoder::init: Invalid/unsupported supercompression or file is corrupted or invalid\n");
  13982. return false;
  13983. }
  13984. if (m_header.m_supercompression_scheme == KTX2_SS_BASISLZ)
  13985. {
  13986. if (m_header.m_sgd_byte_length <= sizeof(ktx2_etc1s_global_data_header))
  13987. {
  13988. BASISU_DEVEL_ERROR("ktx2_transcoder::init: Supercompression global data is too small\n");
  13989. return false;
  13990. }
  13991. if (m_header.m_sgd_byte_offset < sizeof(ktx2_header))
  13992. {
  13993. BASISU_DEVEL_ERROR("ktx2_transcoder::init: Supercompression global data offset is too low\n");
  13994. return false;
  13995. }
  13996. if (m_header.m_sgd_byte_offset + m_header.m_sgd_byte_length > m_data_size)
  13997. {
  13998. BASISU_DEVEL_ERROR("ktx2_transcoder::init: Supercompression global data offset and/or length is too high\n");
  13999. return false;
  14000. }
  14001. }
  14002. if (!m_levels.try_resize(m_header.m_level_count))
  14003. {
  14004. BASISU_DEVEL_ERROR("ktx2_transcoder::init: Out of memory\n");
  14005. return false;
  14006. }
  14007. const uint32_t level_index_size_in_bytes = basisu::maximum(1U, (uint32_t)m_header.m_level_count) * sizeof(ktx2_level_index);
  14008. if ((sizeof(ktx2_header) + level_index_size_in_bytes) > m_data_size)
  14009. {
  14010. BASISU_DEVEL_ERROR("ktx2_transcoder::init: File is too small (can't read level index array)\n");
  14011. return false;
  14012. }
  14013. memcpy(&m_levels[0], m_pData + sizeof(ktx2_header), level_index_size_in_bytes);
  14014. // Sanity check the level offsets and byte sizes
  14015. for (uint32_t i = 0; i < m_levels.size(); i++)
  14016. {
  14017. if (m_levels[i].m_byte_offset < sizeof(ktx2_header))
  14018. {
  14019. BASISU_DEVEL_ERROR("ktx2_transcoder::init: Invalid level offset (too low)\n");
  14020. return false;
  14021. }
  14022. if (!m_levels[i].m_byte_length)
  14023. {
  14024. BASISU_DEVEL_ERROR("ktx2_transcoder::init: Invalid level byte length\n");
  14025. }
  14026. if ((m_levels[i].m_byte_offset + m_levels[i].m_byte_length) > m_data_size)
  14027. {
  14028. BASISU_DEVEL_ERROR("ktx2_transcoder::init: Invalid level offset and/or length\n");
  14029. return false;
  14030. }
  14031. const uint64_t MAX_SANE_LEVEL_UNCOMP_SIZE = 2048ULL * 1024ULL * 1024ULL;
  14032. if (m_levels[i].m_uncompressed_byte_length >= MAX_SANE_LEVEL_UNCOMP_SIZE)
  14033. {
  14034. BASISU_DEVEL_ERROR("ktx2_transcoder::init: Invalid level offset (too large)\n");
  14035. return false;
  14036. }
  14037. if (m_header.m_supercompression_scheme == KTX2_SS_BASISLZ)
  14038. {
  14039. if (m_levels[i].m_uncompressed_byte_length)
  14040. {
  14041. BASISU_DEVEL_ERROR("ktx2_transcoder::init: Invalid uncompressed length (0)\n");
  14042. return false;
  14043. }
  14044. }
  14045. else if (m_header.m_supercompression_scheme >= KTX2_SS_ZSTANDARD)
  14046. {
  14047. if (!m_levels[i].m_uncompressed_byte_length)
  14048. {
  14049. BASISU_DEVEL_ERROR("ktx2_transcoder::init: Invalid uncompressed length (1)\n");
  14050. return false;
  14051. }
  14052. }
  14053. }
  14054. const uint32_t DFD_MINIMUM_SIZE = 44, DFD_MAXIMUM_SIZE = 60;
  14055. if ((m_header.m_dfd_byte_length != DFD_MINIMUM_SIZE) && (m_header.m_dfd_byte_length != DFD_MAXIMUM_SIZE))
  14056. {
  14057. BASISU_DEVEL_ERROR("ktx2_transcoder::init: Unsupported DFD size\n");
  14058. return false;
  14059. }
  14060. if (((m_header.m_dfd_byte_offset + m_header.m_dfd_byte_length) > m_data_size) || (m_header.m_dfd_byte_offset < sizeof(ktx2_header)))
  14061. {
  14062. BASISU_DEVEL_ERROR("ktx2_transcoder::init: Invalid DFD offset and/or length\n");
  14063. return false;
  14064. }
  14065. const uint8_t* pDFD = m_pData + m_header.m_dfd_byte_offset;
  14066. if (!m_dfd.try_resize(m_header.m_dfd_byte_length))
  14067. {
  14068. BASISU_DEVEL_ERROR("ktx2_transcoder::init: Out of memory\n");
  14069. return false;
  14070. }
  14071. memcpy(m_dfd.data(), pDFD, m_header.m_dfd_byte_length);
  14072. // This is all hard coded for only ETC1S and UASTC.
  14073. uint32_t dfd_total_size = basisu::read_le_dword(pDFD);
  14074. // 3.10.3: Sanity check
  14075. if (dfd_total_size != m_header.m_dfd_byte_length)
  14076. {
  14077. BASISU_DEVEL_ERROR("ktx2_transcoder::init: DFD size validation failed (1)\n");
  14078. return false;
  14079. }
  14080. // 3.10.3: More sanity checking
  14081. if (m_header.m_kvd_byte_length)
  14082. {
  14083. if (dfd_total_size != m_header.m_kvd_byte_offset - m_header.m_dfd_byte_offset)
  14084. {
  14085. BASISU_DEVEL_ERROR("ktx2_transcoder::init: DFD size validation failed (2)\n");
  14086. return false;
  14087. }
  14088. }
  14089. const uint32_t dfd_bits = basisu::read_le_dword(pDFD + 3 * sizeof(uint32_t));
  14090. const uint32_t sample_channel0 = basisu::read_le_dword(pDFD + 7 * sizeof(uint32_t));
  14091. m_dfd_color_model = dfd_bits & 255;
  14092. m_dfd_color_prims = (ktx2_df_color_primaries)((dfd_bits >> 8) & 255);
  14093. m_dfd_transfer_func = (dfd_bits >> 16) & 255;
  14094. m_dfd_flags = (dfd_bits >> 24) & 255;
  14095. // See 3.10.1.Restrictions
  14096. if ((m_dfd_transfer_func != KTX2_KHR_DF_TRANSFER_LINEAR) && (m_dfd_transfer_func != KTX2_KHR_DF_TRANSFER_SRGB))
  14097. {
  14098. BASISU_DEVEL_ERROR("ktx2_transcoder::init: Invalid DFD transfer function\n");
  14099. return false;
  14100. }
  14101. if (m_dfd_color_model == KTX2_KDF_DF_MODEL_ETC1S)
  14102. {
  14103. m_format = basist::basis_tex_format::cETC1S;
  14104. // 3.10.2: "Whether the image has 1 or 2 slices can be determined from the DFD’s sample count."
  14105. // If m_has_alpha is true it may be 2-channel RRRG or 4-channel RGBA, but we let the caller deal with that.
  14106. m_has_alpha = (m_header.m_dfd_byte_length == 60);
  14107. m_dfd_samples = m_has_alpha ? 2 : 1;
  14108. m_dfd_chan0 = (ktx2_df_channel_id)((sample_channel0 >> 24) & 15);
  14109. if (m_has_alpha)
  14110. {
  14111. const uint32_t sample_channel1 = basisu::read_le_dword(pDFD + 11 * sizeof(uint32_t));
  14112. m_dfd_chan1 = (ktx2_df_channel_id)((sample_channel1 >> 24) & 15);
  14113. }
  14114. }
  14115. else if (m_dfd_color_model == KTX2_KDF_DF_MODEL_UASTC)
  14116. {
  14117. m_format = basist::basis_tex_format::cUASTC4x4;
  14118. m_dfd_samples = 1;
  14119. m_dfd_chan0 = (ktx2_df_channel_id)((sample_channel0 >> 24) & 15);
  14120. // We're assuming "DATA" means RGBA so it has alpha.
  14121. m_has_alpha = (m_dfd_chan0 == KTX2_DF_CHANNEL_UASTC_RGBA) || (m_dfd_chan0 == KTX2_DF_CHANNEL_UASTC_RRRG);
  14122. }
  14123. else
  14124. {
  14125. // Unsupported DFD color model.
  14126. BASISU_DEVEL_ERROR("ktx2_transcoder::init: Unsupported DFD color model\n");
  14127. return false;
  14128. }
  14129. if (!read_key_values())
  14130. {
  14131. BASISU_DEVEL_ERROR("ktx2_transcoder::init: read_key_values() failed\n");
  14132. return false;
  14133. }
  14134. // Check for a KTXanimData key
  14135. for (uint32_t i = 0; i < m_key_values.size(); i++)
  14136. {
  14137. if (strcmp(reinterpret_cast<const char*>(m_key_values[i].m_key.data()), "KTXanimData") == 0)
  14138. {
  14139. m_is_video = true;
  14140. break;
  14141. }
  14142. }
  14143. return true;
  14144. }
  14145. uint32_t ktx2_transcoder::get_etc1s_image_descs_image_flags(uint32_t level_index, uint32_t layer_index, uint32_t face_index) const
  14146. {
  14147. const uint32_t etc1s_image_index =
  14148. (level_index * basisu::maximum<uint32_t>(m_header.m_layer_count, 1) * m_header.m_face_count) +
  14149. layer_index * m_header.m_face_count +
  14150. face_index;
  14151. if (etc1s_image_index >= get_etc1s_image_descs().size())
  14152. {
  14153. assert(0);
  14154. return 0;
  14155. }
  14156. return get_etc1s_image_descs()[etc1s_image_index].m_image_flags;
  14157. }
  14158. const basisu::uint8_vec* ktx2_transcoder::find_key(const std::string& key_name) const
  14159. {
  14160. for (uint32_t i = 0; i < m_key_values.size(); i++)
  14161. if (strcmp((const char *)m_key_values[i].m_key.data(), key_name.c_str()) == 0)
  14162. return &m_key_values[i].m_value;
  14163. return nullptr;
  14164. }
  14165. bool ktx2_transcoder::start_transcoding()
  14166. {
  14167. if (!m_pData)
  14168. {
  14169. BASISU_DEVEL_ERROR("ktx2_transcoder::start_transcoding: Must call init() first\n");
  14170. return false;
  14171. }
  14172. if (m_header.m_supercompression_scheme == KTX2_SS_BASISLZ)
  14173. {
  14174. // Check if we've already decompressed the ETC1S global data. If so don't unpack it again.
  14175. if (!m_etc1s_transcoder.get_endpoints().empty())
  14176. return true;
  14177. if (!decompress_etc1s_global_data())
  14178. {
  14179. BASISU_DEVEL_ERROR("ktx2_transcoder::start_transcoding: decompress_etc1s_global_data() failed\n");
  14180. return false;
  14181. }
  14182. if (!m_is_video)
  14183. {
  14184. // See if there are any P-frames. If so it must be a video, even if there wasn't a KTXanimData key.
  14185. // Video cannot be a cubemap, and it must be a texture array.
  14186. if ((m_header.m_face_count == 1) && (m_header.m_layer_count > 1))
  14187. {
  14188. for (uint32_t i = 0; i < m_etc1s_image_descs.size(); i++)
  14189. {
  14190. if (m_etc1s_image_descs[i].m_image_flags & KTX2_IMAGE_IS_P_FRAME)
  14191. {
  14192. m_is_video = true;
  14193. break;
  14194. }
  14195. }
  14196. }
  14197. }
  14198. }
  14199. else if (m_header.m_supercompression_scheme == KTX2_SS_ZSTANDARD)
  14200. {
  14201. #if !BASISD_SUPPORT_KTX2_ZSTD
  14202. BASISU_DEVEL_ERROR("ktx2_transcoder::start_transcoding: File uses zstd supercompression, but zstd support was not enabled at compilation time (BASISD_SUPPORT_KTX2_ZSTD == 0)\n");
  14203. return false;
  14204. #endif
  14205. }
  14206. return true;
  14207. }
  14208. bool ktx2_transcoder::get_image_level_info(ktx2_image_level_info& level_info, uint32_t level_index, uint32_t layer_index, uint32_t face_index) const
  14209. {
  14210. if (level_index >= m_levels.size())
  14211. {
  14212. BASISU_DEVEL_ERROR("ktx2_transcoder::get_image_level_info: level_index >= m_levels.size()\n");
  14213. return false;
  14214. }
  14215. if (m_header.m_face_count > 1)
  14216. {
  14217. if (face_index >= 6)
  14218. {
  14219. BASISU_DEVEL_ERROR("ktx2_transcoder::get_image_level_info: face_index >= 6\n");
  14220. return false;
  14221. }
  14222. }
  14223. else if (face_index != 0)
  14224. {
  14225. BASISU_DEVEL_ERROR("ktx2_transcoder::get_image_level_info: face_index != 0\n");
  14226. return false;
  14227. }
  14228. if (layer_index >= basisu::maximum<uint32_t>(m_header.m_layer_count, 1))
  14229. {
  14230. BASISU_DEVEL_ERROR("ktx2_transcoder::get_image_level_info: layer_index >= maximum<uint32_t>(m_header.m_layer_count, 1)\n");
  14231. return false;
  14232. }
  14233. const uint32_t level_width = basisu::maximum<uint32_t>(m_header.m_pixel_width >> level_index, 1);
  14234. const uint32_t level_height = basisu::maximum<uint32_t>(m_header.m_pixel_height >> level_index, 1);
  14235. const uint32_t num_blocks_x = (level_width + 3) >> 2;
  14236. const uint32_t num_blocks_y = (level_height + 3) >> 2;
  14237. level_info.m_face_index = face_index;
  14238. level_info.m_layer_index = layer_index;
  14239. level_info.m_level_index = level_index;
  14240. level_info.m_orig_width = level_width;
  14241. level_info.m_orig_height = level_height;
  14242. level_info.m_width = num_blocks_x * 4;
  14243. level_info.m_height = num_blocks_y * 4;
  14244. level_info.m_num_blocks_x = num_blocks_x;
  14245. level_info.m_num_blocks_y = num_blocks_y;
  14246. level_info.m_total_blocks = num_blocks_x * num_blocks_y;
  14247. level_info.m_alpha_flag = m_has_alpha;
  14248. level_info.m_iframe_flag = false;
  14249. if (m_etc1s_image_descs.size())
  14250. {
  14251. const uint32_t etc1s_image_index =
  14252. (level_index * basisu::maximum<uint32_t>(m_header.m_layer_count, 1) * m_header.m_face_count) +
  14253. layer_index * m_header.m_face_count +
  14254. face_index;
  14255. level_info.m_iframe_flag = (m_etc1s_image_descs[etc1s_image_index].m_image_flags & KTX2_IMAGE_IS_P_FRAME) == 0;
  14256. }
  14257. return true;
  14258. }
  14259. bool ktx2_transcoder::transcode_image_level(
  14260. uint32_t level_index, uint32_t layer_index, uint32_t face_index,
  14261. void* pOutput_blocks, uint32_t output_blocks_buf_size_in_blocks_or_pixels,
  14262. basist::transcoder_texture_format fmt,
  14263. uint32_t decode_flags, uint32_t output_row_pitch_in_blocks_or_pixels, uint32_t output_rows_in_pixels, int channel0, int channel1,
  14264. ktx2_transcoder_state* pState)
  14265. {
  14266. if (!m_pData)
  14267. {
  14268. BASISU_DEVEL_ERROR("ktx2_transcoder::transcode_image_2D: Must call init() first\n");
  14269. return false;
  14270. }
  14271. if (!pState)
  14272. pState = &m_def_transcoder_state;
  14273. if (level_index >= m_levels.size())
  14274. {
  14275. BASISU_DEVEL_ERROR("ktx2_transcoder::transcode_image_2D: level_index >= m_levels.size()\n");
  14276. return false;
  14277. }
  14278. if (m_header.m_face_count > 1)
  14279. {
  14280. if (face_index >= 6)
  14281. {
  14282. BASISU_DEVEL_ERROR("ktx2_transcoder::transcode_image_2D: face_index >= 6\n");
  14283. return false;
  14284. }
  14285. }
  14286. else if (face_index != 0)
  14287. {
  14288. BASISU_DEVEL_ERROR("ktx2_transcoder::transcode_image_2D: face_index != 0\n");
  14289. return false;
  14290. }
  14291. if (layer_index >= basisu::maximum<uint32_t>(m_header.m_layer_count, 1))
  14292. {
  14293. BASISU_DEVEL_ERROR("ktx2_transcoder::transcode_image_2D: layer_index >= maximum<uint32_t>(m_header.m_layer_count, 1)\n");
  14294. return false;
  14295. }
  14296. const uint8_t* pComp_level_data = m_pData + m_levels[level_index].m_byte_offset;
  14297. uint64_t comp_level_data_size = m_levels[level_index].m_byte_length;
  14298. const uint8_t* pUncomp_level_data = pComp_level_data;
  14299. uint64_t uncomp_level_data_size = comp_level_data_size;
  14300. if (uncomp_level_data_size > UINT32_MAX)
  14301. {
  14302. BASISU_DEVEL_ERROR("ktx2_transcoder::transcode_image_2D: uncomp_level_data_size > UINT32_MAX\n");
  14303. return false;
  14304. }
  14305. if (m_header.m_supercompression_scheme == KTX2_SS_ZSTANDARD)
  14306. {
  14307. // Check if we've already decompressed this level's supercompressed data.
  14308. if ((int)level_index != pState->m_uncomp_data_level_index)
  14309. {
  14310. // Uncompress the entire level's supercompressed data.
  14311. if (!decompress_level_data(level_index, pState->m_level_uncomp_data))
  14312. {
  14313. BASISU_DEVEL_ERROR("ktx2_transcoder::transcode_image_2D: decompress_level_data() failed\n");
  14314. return false;
  14315. }
  14316. pState->m_uncomp_data_level_index = level_index;
  14317. }
  14318. pUncomp_level_data = pState->m_level_uncomp_data.data();
  14319. uncomp_level_data_size = pState->m_level_uncomp_data.size();
  14320. }
  14321. const uint32_t level_width = basisu::maximum<uint32_t>(m_header.m_pixel_width >> level_index, 1);
  14322. const uint32_t level_height = basisu::maximum<uint32_t>(m_header.m_pixel_height >> level_index, 1);
  14323. const uint32_t num_blocks_x = (level_width + 3) >> 2;
  14324. const uint32_t num_blocks_y = (level_height + 3) >> 2;
  14325. if (m_format == basist::basis_tex_format::cETC1S)
  14326. {
  14327. // Ensure start_transcoding() was called.
  14328. if (m_etc1s_transcoder.get_endpoints().empty())
  14329. {
  14330. BASISU_DEVEL_ERROR("ktx2_transcoder::transcode_image_2D: must call start_transcoding() first\n");
  14331. return false;
  14332. }
  14333. const uint32_t etc1s_image_index =
  14334. (level_index * basisu::maximum<uint32_t>(m_header.m_layer_count, 1) * m_header.m_face_count) +
  14335. layer_index * m_header.m_face_count +
  14336. face_index;
  14337. // Sanity check
  14338. if (etc1s_image_index >= m_etc1s_image_descs.size())
  14339. {
  14340. BASISU_DEVEL_ERROR("ktx2_transcoder::transcode_image_2D: etc1s_image_index >= m_etc1s_image_descs.size()\n");
  14341. assert(0);
  14342. return false;
  14343. }
  14344. if (static_cast<uint32_t>(m_data_size) != m_data_size)
  14345. {
  14346. BASISU_DEVEL_ERROR("ktx2_transcoder::transcode_image_2D: File is too large\n");
  14347. return false;
  14348. }
  14349. const ktx2_etc1s_image_desc& image_desc = m_etc1s_image_descs[etc1s_image_index];
  14350. if (!m_etc1s_transcoder.transcode_image(fmt,
  14351. pOutput_blocks, output_blocks_buf_size_in_blocks_or_pixels, m_pData, static_cast<uint32_t>(m_data_size),
  14352. num_blocks_x, num_blocks_y, level_width, level_height,
  14353. level_index,
  14354. m_levels[level_index].m_byte_offset + image_desc.m_rgb_slice_byte_offset, image_desc.m_rgb_slice_byte_length,
  14355. image_desc.m_alpha_slice_byte_length ? (m_levels[level_index].m_byte_offset + image_desc.m_alpha_slice_byte_offset) : 0, image_desc.m_alpha_slice_byte_length,
  14356. decode_flags, m_has_alpha,
  14357. m_is_video, output_row_pitch_in_blocks_or_pixels, &pState->m_transcoder_state, output_rows_in_pixels))
  14358. {
  14359. BASISU_DEVEL_ERROR("ktx2_transcoder::transcode_image_2D: ETC1S transcode_image() failed, this is either a bug or the file is corrupted/invalid\n");
  14360. return false;
  14361. }
  14362. }
  14363. else if (m_format == basist::basis_tex_format::cUASTC4x4)
  14364. {
  14365. // Compute length and offset to uncompressed 2D UASTC texture data, given the face/layer indices.
  14366. assert(uncomp_level_data_size == m_levels[level_index].m_uncompressed_byte_length);
  14367. const uint32_t total_2D_image_size = num_blocks_x * num_blocks_y * KTX2_UASTC_BLOCK_SIZE;
  14368. const uint32_t uncomp_ofs = (layer_index * m_header.m_face_count + face_index) * total_2D_image_size;
  14369. // Sanity checks
  14370. if (uncomp_ofs >= uncomp_level_data_size)
  14371. {
  14372. BASISU_DEVEL_ERROR("ktx2_transcoder::transcode_image_2D: uncomp_ofs >= total_2D_image_size\n");
  14373. return false;
  14374. }
  14375. if ((uncomp_level_data_size - uncomp_ofs) < total_2D_image_size)
  14376. {
  14377. BASISU_DEVEL_ERROR("ktx2_transcoder::transcode_image_2D: (uncomp_level_data_size - uncomp_ofs) < total_2D_image_size\n");
  14378. return false;
  14379. }
  14380. if (!m_uastc_transcoder.transcode_image(fmt,
  14381. pOutput_blocks, output_blocks_buf_size_in_blocks_or_pixels,
  14382. (const uint8_t*)pUncomp_level_data + uncomp_ofs, (uint32_t)total_2D_image_size, num_blocks_x, num_blocks_y, level_width, level_height, level_index,
  14383. 0, (uint32_t)total_2D_image_size,
  14384. decode_flags, m_has_alpha, m_is_video, output_row_pitch_in_blocks_or_pixels, nullptr, output_rows_in_pixels, channel0, channel1))
  14385. {
  14386. BASISU_DEVEL_ERROR("ktx2_transcoder::transcode_image_2D: UASTC transcode_image() failed, this is either a bug or the file is corrupted/invalid\n");
  14387. return false;
  14388. }
  14389. }
  14390. else
  14391. {
  14392. // Shouldn't get here.
  14393. BASISU_DEVEL_ERROR("ktx2_transcoder::transcode_image_2D: Internal error\n");
  14394. assert(0);
  14395. return false;
  14396. }
  14397. return true;
  14398. }
  14399. bool ktx2_transcoder::decompress_level_data(uint32_t level_index, basisu::uint8_vec& uncomp_data)
  14400. {
  14401. const uint64_t comp_size = m_levels[level_index].m_byte_length;
  14402. const uint64_t uncomp_size = m_levels[level_index].m_uncompressed_byte_length;
  14403. if (((size_t)comp_size) != comp_size)
  14404. {
  14405. BASISU_DEVEL_ERROR("ktx2_transcoder::decompress_level_data: Compressed data too large\n");
  14406. return false;
  14407. }
  14408. if (((size_t)uncomp_size) != uncomp_size)
  14409. {
  14410. BASISU_DEVEL_ERROR("ktx2_transcoder::decompress_level_data: Uncompressed data too large\n");
  14411. return false;
  14412. }
  14413. if (!uncomp_data.try_resize(uncomp_size))
  14414. {
  14415. BASISU_DEVEL_ERROR("ktx2_transcoder::decompress_level_data: Out of memory\n");
  14416. return false;
  14417. }
  14418. if (m_header.m_supercompression_scheme == KTX2_SS_ZSTANDARD)
  14419. {
  14420. #if BASISD_SUPPORT_KTX2_ZSTD
  14421. const uint8_t* pComp_data = m_levels[level_index].m_byte_offset + m_pData;
  14422. size_t actualUncompSize = ZSTD_decompress(uncomp_data.data(), (size_t)uncomp_size, pComp_data, (size_t)comp_size);
  14423. if (ZSTD_isError(actualUncompSize))
  14424. {
  14425. BASISU_DEVEL_ERROR("ktx2_transcoder::decompress_level_data: Zstd decompression failed, file is invalid or corrupted\n");
  14426. return false;
  14427. }
  14428. if (actualUncompSize != uncomp_size)
  14429. {
  14430. BASISU_DEVEL_ERROR("ktx2_transcoder::decompress_level_data: Zstd decompression returned too few bytes, file is invalid or corrupted\n");
  14431. return false;
  14432. }
  14433. #else
  14434. BASISU_DEVEL_ERROR("ktx2_transcoder::decompress_level_data: File uses Zstd supercompression, but Zstd support was not enabled at compile time (BASISD_SUPPORT_KTX2_ZSTD is 0)\n");
  14435. return false;
  14436. #endif
  14437. }
  14438. return true;
  14439. }
  14440. bool ktx2_transcoder::decompress_etc1s_global_data()
  14441. {
  14442. // Note: we don't actually support 3D textures in here yet
  14443. //uint32_t layer_pixel_depth = basisu::maximum<uint32_t>(m_header.m_pixel_depth, 1);
  14444. //for (uint32_t i = 1; i < m_header.m_level_count; i++)
  14445. // layer_pixel_depth += basisu::maximum<uint32_t>(m_header.m_pixel_depth >> i, 1);
  14446. const uint32_t image_count = basisu::maximum<uint32_t>(m_header.m_layer_count, 1) * m_header.m_face_count * m_header.m_level_count;
  14447. assert(image_count);
  14448. const uint8_t* pSrc = m_pData + m_header.m_sgd_byte_offset;
  14449. memcpy(&m_etc1s_header, pSrc, sizeof(ktx2_etc1s_global_data_header));
  14450. pSrc += sizeof(ktx2_etc1s_global_data_header);
  14451. if ((!m_etc1s_header.m_endpoints_byte_length) || (!m_etc1s_header.m_selectors_byte_length) || (!m_etc1s_header.m_tables_byte_length))
  14452. {
  14453. BASISU_DEVEL_ERROR("ktx2_transcoder::decompress_etc1s_global_data: Invalid ETC1S global data\n");
  14454. return false;
  14455. }
  14456. if ((!m_etc1s_header.m_endpoint_count) || (!m_etc1s_header.m_selector_count))
  14457. {
  14458. BASISU_DEVEL_ERROR("ktx2_transcoder::decompress_etc1s_global_data: endpoint and/or selector count is 0, file is invalid or corrupted\n");
  14459. return false;
  14460. }
  14461. // Sanity check the ETC1S header.
  14462. if ((sizeof(ktx2_etc1s_global_data_header) +
  14463. sizeof(ktx2_etc1s_image_desc) * image_count +
  14464. m_etc1s_header.m_endpoints_byte_length +
  14465. m_etc1s_header.m_selectors_byte_length +
  14466. m_etc1s_header.m_tables_byte_length +
  14467. m_etc1s_header.m_extended_byte_length) > m_header.m_sgd_byte_length)
  14468. {
  14469. BASISU_DEVEL_ERROR("ktx2_transcoder::decompress_etc1s_global_data: SGD byte length is too small, file is invalid or corrupted\n");
  14470. return false;
  14471. }
  14472. if (!m_etc1s_image_descs.try_resize(image_count))
  14473. {
  14474. BASISU_DEVEL_ERROR("ktx2_transcoder::decompress_etc1s_global_data: Out of memory\n");
  14475. return false;
  14476. }
  14477. memcpy(m_etc1s_image_descs.data(), pSrc, sizeof(ktx2_etc1s_image_desc) * image_count);
  14478. pSrc += sizeof(ktx2_etc1s_image_desc) * image_count;
  14479. // Sanity check the ETC1S image descs
  14480. for (uint32_t i = 0; i < image_count; i++)
  14481. {
  14482. // m_etc1s_transcoder.transcode_image() will validate the slice offsets/lengths before transcoding.
  14483. if (!m_etc1s_image_descs[i].m_rgb_slice_byte_length)
  14484. {
  14485. BASISU_DEVEL_ERROR("ktx2_transcoder::decompress_etc1s_global_data: ETC1S image descs sanity check failed (1)\n");
  14486. return false;
  14487. }
  14488. if (m_has_alpha)
  14489. {
  14490. if (!m_etc1s_image_descs[i].m_alpha_slice_byte_length)
  14491. {
  14492. BASISU_DEVEL_ERROR("ktx2_transcoder::decompress_etc1s_global_data: ETC1S image descs sanity check failed (2)\n");
  14493. return false;
  14494. }
  14495. }
  14496. }
  14497. const uint8_t* pEndpoint_data = pSrc;
  14498. const uint8_t* pSelector_data = pSrc + m_etc1s_header.m_endpoints_byte_length;
  14499. const uint8_t* pTables_data = pSrc + m_etc1s_header.m_endpoints_byte_length + m_etc1s_header.m_selectors_byte_length;
  14500. if (!m_etc1s_transcoder.decode_tables(pTables_data, m_etc1s_header.m_tables_byte_length))
  14501. {
  14502. BASISU_DEVEL_ERROR("ktx2_transcoder::decompress_etc1s_global_data: decode_tables() failed, file is invalid or corrupted\n");
  14503. return false;
  14504. }
  14505. if (!m_etc1s_transcoder.decode_palettes(
  14506. m_etc1s_header.m_endpoint_count, pEndpoint_data, m_etc1s_header.m_endpoints_byte_length,
  14507. m_etc1s_header.m_selector_count, pSelector_data, m_etc1s_header.m_selectors_byte_length))
  14508. {
  14509. BASISU_DEVEL_ERROR("ktx2_transcoder::decompress_etc1s_global_data: decode_palettes() failed, file is likely corrupted\n");
  14510. return false;
  14511. }
  14512. return true;
  14513. }
  14514. bool ktx2_transcoder::read_key_values()
  14515. {
  14516. if (!m_header.m_kvd_byte_length)
  14517. {
  14518. if (m_header.m_kvd_byte_offset)
  14519. {
  14520. BASISU_DEVEL_ERROR("ktx2_transcoder::read_key_values: Invalid KVD byte offset (it should be zero when the length is zero)\n");
  14521. return false;
  14522. }
  14523. return true;
  14524. }
  14525. if (m_header.m_kvd_byte_offset < sizeof(ktx2_header))
  14526. {
  14527. BASISU_DEVEL_ERROR("ktx2_transcoder::read_key_values: Invalid KVD byte offset\n");
  14528. return false;
  14529. }
  14530. if ((m_header.m_kvd_byte_offset + m_header.m_kvd_byte_length) > m_data_size)
  14531. {
  14532. BASISU_DEVEL_ERROR("ktx2_transcoder::read_key_values: Invalid KVD byte offset and/or length\n");
  14533. return false;
  14534. }
  14535. const uint8_t* pSrc = m_pData + m_header.m_kvd_byte_offset;
  14536. uint32_t src_left = m_header.m_kvd_byte_length;
  14537. if (!m_key_values.try_reserve(8))
  14538. {
  14539. BASISU_DEVEL_ERROR("ktx2_transcoder::read_key_values: Out of memory\n");
  14540. return false;
  14541. }
  14542. while (src_left > sizeof(uint32_t))
  14543. {
  14544. uint32_t l = basisu::read_le_dword(pSrc);
  14545. pSrc += sizeof(uint32_t);
  14546. src_left -= sizeof(uint32_t);
  14547. if (l < 2)
  14548. {
  14549. BASISU_DEVEL_ERROR("ktx2_transcoder::read_key_values: Failed reading key value fields (0)\n");
  14550. return false;
  14551. }
  14552. if (src_left < l)
  14553. {
  14554. BASISU_DEVEL_ERROR("ktx2_transcoder::read_key_values: Failed reading key value fields (1)\n");
  14555. return false;
  14556. }
  14557. if (!m_key_values.try_resize(m_key_values.size() + 1))
  14558. {
  14559. BASISU_DEVEL_ERROR("ktx2_transcoder::read_key_values: Out of memory\n");
  14560. return false;
  14561. }
  14562. basisu::uint8_vec& key_data = m_key_values.back().m_key;
  14563. basisu::uint8_vec& value_data = m_key_values.back().m_value;
  14564. do
  14565. {
  14566. if (!l)
  14567. {
  14568. BASISU_DEVEL_ERROR("ktx2_transcoder::read_key_values: Failed reading key value fields (2)\n");
  14569. return false;
  14570. }
  14571. if (!key_data.try_push_back(*pSrc++))
  14572. {
  14573. BASISU_DEVEL_ERROR("ktx2_transcoder::read_key_values: Out of memory\n");
  14574. return false;
  14575. }
  14576. src_left--;
  14577. l--;
  14578. } while (key_data.back());
  14579. if (!value_data.try_resize(l))
  14580. {
  14581. BASISU_DEVEL_ERROR("ktx2_transcoder::read_key_values: Out of memory\n");
  14582. return false;
  14583. }
  14584. if (l)
  14585. {
  14586. memcpy(value_data.data(), pSrc, l);
  14587. pSrc += l;
  14588. src_left -= l;
  14589. }
  14590. uint32_t ofs = (uint32_t)(pSrc - m_pData) & 3;
  14591. uint32_t alignment_bytes = (4 - ofs) & 3;
  14592. if (src_left < alignment_bytes)
  14593. {
  14594. BASISU_DEVEL_ERROR("ktx2_transcoder::read_key_values: Failed reading key value fields (3)\n");
  14595. return false;
  14596. }
  14597. pSrc += alignment_bytes;
  14598. src_left -= alignment_bytes;
  14599. }
  14600. return true;
  14601. }
  14602. #endif // BASISD_SUPPORT_KTX2
  14603. bool basisu_transcoder_supports_ktx2()
  14604. {
  14605. #if BASISD_SUPPORT_KTX2
  14606. return true;
  14607. #else
  14608. return false;
  14609. #endif
  14610. }
  14611. bool basisu_transcoder_supports_ktx2_zstd()
  14612. {
  14613. #if BASISD_SUPPORT_KTX2_ZSTD
  14614. return true;
  14615. #else
  14616. return false;
  14617. #endif
  14618. }
  14619. } // namespace basist