ConvectionKernels.cpp 365 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587
  1. /*
  2. Convection Texture Tools
  3. Copyright (c) 2018 Eric Lasota
  4. Permission is hereby granted, free of charge, to any person obtaining
  5. a copy of this software and associated documentation files (the
  6. "Software"), to deal in the Software without restriction, including
  7. without limitation the rights to use, copy, modify, merge, publish,
  8. distribute, sublicense, and/or sell copies of the Software, and to
  9. permit persons to whom the Software is furnished to do so, subject
  10. to the following conditions:
  11. The above copyright notice and this permission notice shall be included
  12. in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  14. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  16. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  17. CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  18. TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  19. SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. -------------------------------------------------------------------------------------
  21. Portions based on DirectX Texture Library (DirectXTex)
  22. Copyright (c) Microsoft Corporation. All rights reserved.
  23. Licensed under the MIT License.
  24. http://go.microsoft.com/fwlink/?LinkId=248926
  25. */
  26. #include "ConvectionKernels.h"
  27. #include "ConvectionKernels_BC7_SingleColor.h"
  28. #if (defined(_M_IX86_FP) && _M_IX86_FP >= 2) || defined(_M_X64) || defined(__SSE2__)
  29. #define CVTT_USE_SSE2
  30. #endif
  31. #ifdef CVTT_USE_SSE2
  32. #include <emmintrin.h>
  33. #endif
  34. #include <float.h>
  35. #include <assert.h>
  36. #include <string.h>
  37. #include <algorithm>
  38. #include <math.h>
  39. #define UNREFERENCED_PARAMETER(n) ((void)n)
  40. namespace cvtt
  41. {
  42. #ifdef CVTT_USE_SSE2
  43. // SSE2 version
  44. struct ParallelMath
  45. {
  46. typedef uint16_t ScalarUInt16;
  47. typedef int16_t ScalarSInt16;
  48. template<unsigned int TRoundingMode>
  49. struct RoundForScope
  50. {
  51. unsigned int m_oldCSR;
  52. RoundForScope()
  53. {
  54. m_oldCSR = _mm_getcsr();
  55. _mm_setcsr((m_oldCSR & ~_MM_ROUND_MASK) | (TRoundingMode));
  56. }
  57. ~RoundForScope()
  58. {
  59. _mm_setcsr(m_oldCSR);
  60. }
  61. };
  62. struct RoundTowardZeroForScope : RoundForScope<_MM_ROUND_TOWARD_ZERO>
  63. {
  64. };
  65. struct RoundTowardNearestForScope : RoundForScope<_MM_ROUND_NEAREST>
  66. {
  67. };
  68. struct RoundUpForScope : RoundForScope<_MM_ROUND_UP>
  69. {
  70. };
  71. struct RoundDownForScope : RoundForScope<_MM_ROUND_DOWN>
  72. {
  73. };
  74. static const int ParallelSize = 8;
  75. enum Int16Subtype
  76. {
  77. IntSubtype_Signed,
  78. IntSubtype_UnsignedFull,
  79. IntSubtype_UnsignedTruncated,
  80. IntSubtype_Abstract,
  81. };
  82. template<int TSubtype>
  83. struct VInt16
  84. {
  85. __m128i m_value;
  86. inline VInt16 operator+(int16_t other) const
  87. {
  88. VInt16 result;
  89. result.m_value = _mm_add_epi16(m_value, _mm_set1_epi16(static_cast<int16_t>(other)));
  90. return result;
  91. }
  92. inline VInt16 operator+(const VInt16 &other) const
  93. {
  94. VInt16 result;
  95. result.m_value = _mm_add_epi16(m_value, other.m_value);
  96. return result;
  97. }
  98. inline VInt16 operator|(const VInt16 &other) const
  99. {
  100. VInt16 result;
  101. result.m_value = _mm_or_si128(m_value, other.m_value);
  102. return result;
  103. }
  104. inline VInt16 operator&(const VInt16 &other) const
  105. {
  106. VInt16 result;
  107. result.m_value = _mm_and_si128(m_value, other.m_value);
  108. return result;
  109. }
  110. inline VInt16 operator-(const VInt16 &other) const
  111. {
  112. VInt16 result;
  113. result.m_value = _mm_sub_epi16(m_value, other.m_value);
  114. return result;
  115. }
  116. inline VInt16 operator<<(int bits) const
  117. {
  118. VInt16 result;
  119. result.m_value = _mm_slli_epi16(m_value, bits);
  120. return result;
  121. }
  122. };
  123. typedef VInt16<IntSubtype_Signed> SInt16;
  124. typedef VInt16<IntSubtype_UnsignedFull> UInt16;
  125. typedef VInt16<IntSubtype_UnsignedTruncated> UInt15;
  126. typedef VInt16<IntSubtype_Abstract> AInt16;
  127. template<int TSubtype>
  128. struct VInt32
  129. {
  130. __m128i m_values[2];
  131. inline VInt32 operator+(const VInt32& other) const
  132. {
  133. VInt32 result;
  134. result.m_values[0] = _mm_add_epi32(m_values[0], other.m_values[0]);
  135. result.m_values[1] = _mm_add_epi32(m_values[1], other.m_values[1]);
  136. return result;
  137. }
  138. inline VInt32 operator-(const VInt32& other) const
  139. {
  140. VInt32 result;
  141. result.m_values[0] = _mm_sub_epi32(m_values[0], other.m_values[0]);
  142. result.m_values[1] = _mm_sub_epi32(m_values[1], other.m_values[1]);
  143. return result;
  144. }
  145. inline VInt32 operator<<(const int other) const
  146. {
  147. VInt32 result;
  148. result.m_values[0] = _mm_slli_epi32(m_values[0], other);
  149. result.m_values[1] = _mm_slli_epi32(m_values[1], other);
  150. return result;
  151. }
  152. };
  153. typedef VInt32<IntSubtype_Signed> SInt32;
  154. typedef VInt32<IntSubtype_UnsignedTruncated> UInt31;
  155. typedef VInt32<IntSubtype_UnsignedFull> UInt32;
  156. typedef VInt32<IntSubtype_Abstract> AInt32;
  157. template<class TTargetType>
  158. struct LosslessCast
  159. {
  160. #ifdef CVTT_PERMIT_ALIASING
  161. template<int TSrcSubtype>
  162. static const TTargetType& Cast(const VInt32<TSrcSubtype> &src)
  163. {
  164. return reinterpret_cast<VInt32<TSubtype>&>(src);
  165. }
  166. template<int TSrcSubtype>
  167. static const TTargetType& Cast(const VInt16<TSrcSubtype> &src)
  168. {
  169. return reinterpret_cast<VInt16<TSubtype>&>(src);
  170. }
  171. #else
  172. template<int TSrcSubtype>
  173. static TTargetType Cast(const VInt32<TSrcSubtype> &src)
  174. {
  175. TTargetType result;
  176. result.m_values[0] = src.m_values[0];
  177. result.m_values[1] = src.m_values[1];
  178. return result;
  179. }
  180. template<int TSrcSubtype>
  181. static TTargetType Cast(const VInt16<TSrcSubtype> &src)
  182. {
  183. TTargetType result;
  184. result.m_value = src.m_value;
  185. return result;
  186. }
  187. #endif
  188. };
  189. struct Int64
  190. {
  191. __m128i m_values[4];
  192. };
  193. struct Float
  194. {
  195. __m128 m_values[2];
  196. inline Float operator+(const Float &other) const
  197. {
  198. Float result;
  199. result.m_values[0] = _mm_add_ps(m_values[0], other.m_values[0]);
  200. result.m_values[1] = _mm_add_ps(m_values[1], other.m_values[1]);
  201. return result;
  202. }
  203. inline Float operator+(float other) const
  204. {
  205. Float result;
  206. result.m_values[0] = _mm_add_ps(m_values[0], _mm_set1_ps(other));
  207. result.m_values[1] = _mm_add_ps(m_values[1], _mm_set1_ps(other));
  208. return result;
  209. }
  210. inline Float operator-(const Float& other) const
  211. {
  212. Float result;
  213. result.m_values[0] = _mm_sub_ps(m_values[0], other.m_values[0]);
  214. result.m_values[1] = _mm_sub_ps(m_values[1], other.m_values[1]);
  215. return result;
  216. }
  217. inline Float operator-() const
  218. {
  219. Float result;
  220. result.m_values[0] = _mm_sub_ps(_mm_setzero_ps(), m_values[0]);
  221. result.m_values[1] = _mm_sub_ps(_mm_setzero_ps(), m_values[1]);
  222. return result;
  223. }
  224. inline Float operator*(const Float& other) const
  225. {
  226. Float result;
  227. result.m_values[0] = _mm_mul_ps(m_values[0], other.m_values[0]);
  228. result.m_values[1] = _mm_mul_ps(m_values[1], other.m_values[1]);
  229. return result;
  230. }
  231. inline Float operator*(float other) const
  232. {
  233. Float result;
  234. result.m_values[0] = _mm_mul_ps(m_values[0], _mm_set1_ps(other));
  235. result.m_values[1] = _mm_mul_ps(m_values[1], _mm_set1_ps(other));
  236. return result;
  237. }
  238. inline Float operator/(const Float &other) const
  239. {
  240. Float result;
  241. result.m_values[0] = _mm_div_ps(m_values[0], other.m_values[0]);
  242. result.m_values[1] = _mm_div_ps(m_values[1], other.m_values[1]);
  243. return result;
  244. }
  245. inline Float operator/(float other) const
  246. {
  247. Float result;
  248. result.m_values[0] = _mm_div_ps(m_values[0], _mm_set1_ps(other));
  249. result.m_values[1] = _mm_div_ps(m_values[1], _mm_set1_ps(other));
  250. return result;
  251. }
  252. };
  253. struct Int16CompFlag
  254. {
  255. __m128i m_value;
  256. inline Int16CompFlag operator&(const Int16CompFlag &other) const
  257. {
  258. Int16CompFlag result;
  259. result.m_value = _mm_and_si128(m_value, other.m_value);
  260. return result;
  261. }
  262. inline Int16CompFlag operator|(const Int16CompFlag &other) const
  263. {
  264. Int16CompFlag result;
  265. result.m_value = _mm_or_si128(m_value, other.m_value);
  266. return result;
  267. }
  268. };
  269. struct FloatCompFlag
  270. {
  271. __m128 m_values[2];
  272. };
  273. template<int TSubtype>
  274. static VInt16<TSubtype> AbstractAdd(const VInt16<TSubtype> &a, const VInt16<TSubtype> &b)
  275. {
  276. VInt16<TSubtype> result;
  277. result.m_value = _mm_add_epi16(a.m_value, b.m_value);
  278. return result;
  279. }
  280. template<int TSubtype>
  281. static VInt16<TSubtype> AbstractSubtract(const VInt16<TSubtype> &a, const VInt16<TSubtype> &b)
  282. {
  283. VInt16<TSubtype> result;
  284. result.m_value = _mm_sub_epi16(a.m_value, b.m_value);
  285. return result;
  286. }
  287. static Float Select(const FloatCompFlag &flag, const Float &a, const Float &b)
  288. {
  289. Float result;
  290. for (int i = 0; i < 2; i++)
  291. result.m_values[i] = _mm_or_ps(_mm_and_ps(flag.m_values[i], a.m_values[i]), _mm_andnot_ps(flag.m_values[i], b.m_values[i]));
  292. return result;
  293. }
  294. template<int TSubtype>
  295. static VInt16<TSubtype> Select(const Int16CompFlag &flag, const VInt16<TSubtype> &a, const VInt16<TSubtype> &b)
  296. {
  297. VInt16<TSubtype> result;
  298. result.m_value = _mm_or_si128(_mm_and_si128(flag.m_value, a.m_value), _mm_andnot_si128(flag.m_value, b.m_value));
  299. return result;
  300. }
  301. template<int TSubtype>
  302. static VInt16<TSubtype> SelectOrZero(const Int16CompFlag &flag, const VInt16<TSubtype> &a)
  303. {
  304. VInt16<TSubtype> result;
  305. result.m_value = _mm_and_si128(flag.m_value, a.m_value);
  306. return result;
  307. }
  308. template<int TSubtype>
  309. static void ConditionalSet(VInt16<TSubtype> &dest, const Int16CompFlag &flag, const VInt16<TSubtype> &src)
  310. {
  311. dest.m_value = _mm_or_si128(_mm_andnot_si128(flag.m_value, dest.m_value), _mm_and_si128(flag.m_value, src.m_value));
  312. }
  313. static SInt16 ConditionalNegate(const Int16CompFlag &flag, const SInt16 &v)
  314. {
  315. SInt16 result;
  316. result.m_value = _mm_add_epi16(_mm_xor_si128(flag.m_value, v.m_value), _mm_srli_epi16(flag.m_value, 15));
  317. return result;
  318. }
  319. template<int TSubtype>
  320. static void NotConditionalSet(VInt16<TSubtype> &dest, const Int16CompFlag &flag, const VInt16<TSubtype> &src)
  321. {
  322. dest.m_value = _mm_or_si128(_mm_and_si128(flag.m_value, dest.m_value), _mm_andnot_si128(flag.m_value, src.m_value));
  323. }
  324. static void ConditionalSet(Float &dest, const FloatCompFlag &flag, const Float &src)
  325. {
  326. for (int i = 0; i < 2; i++)
  327. dest.m_values[i] = _mm_or_ps(_mm_andnot_ps(flag.m_values[i], dest.m_values[i]), _mm_and_ps(flag.m_values[i], src.m_values[i]));
  328. }
  329. static void NotConditionalSet(Float &dest, const FloatCompFlag &flag, const Float &src)
  330. {
  331. for (int i = 0; i < 2; i++)
  332. dest.m_values[i] = _mm_or_ps(_mm_and_ps(flag.m_values[i], dest.m_values[i]), _mm_andnot_ps(flag.m_values[i], src.m_values[i]));
  333. }
  334. static void MakeSafeDenominator(Float& v)
  335. {
  336. ConditionalSet(v, Equal(v, MakeFloatZero()), MakeFloat(1.0f));
  337. }
  338. static SInt16 TruncateToPrecisionSigned(const SInt16 &v, int precision)
  339. {
  340. int lostBits = 16 - precision;
  341. if (lostBits == 0)
  342. return v;
  343. SInt16 result;
  344. result.m_value = _mm_srai_epi16(_mm_slli_epi16(v.m_value, lostBits), lostBits);
  345. return result;
  346. }
  347. static UInt16 TruncateToPrecisionUnsigned(const UInt16 &v, int precision)
  348. {
  349. int lostBits = 16 - precision;
  350. if (lostBits == 0)
  351. return v;
  352. UInt16 result;
  353. result.m_value = _mm_srli_epi16(_mm_slli_epi16(v.m_value, lostBits), lostBits);
  354. return result;
  355. }
  356. static UInt16 Min(const UInt16 &a, const UInt16 &b)
  357. {
  358. __m128i bitFlip = _mm_set1_epi16(-32768);
  359. UInt16 result;
  360. result.m_value = _mm_xor_si128(_mm_min_epi16(_mm_xor_si128(a.m_value, bitFlip), _mm_xor_si128(b.m_value, bitFlip)), bitFlip);
  361. return result;
  362. }
  363. static SInt16 Min(const SInt16 &a, const SInt16 &b)
  364. {
  365. SInt16 result;
  366. result.m_value = _mm_min_epi16(a.m_value, b.m_value);
  367. return result;
  368. }
  369. static UInt15 Min(const UInt15 &a, const UInt15 &b)
  370. {
  371. UInt15 result;
  372. result.m_value = _mm_min_epi16(a.m_value, b.m_value);
  373. return result;
  374. }
  375. static Float Min(const Float &a, const Float &b)
  376. {
  377. Float result;
  378. for (int i = 0; i < 2; i++)
  379. result.m_values[i] = _mm_min_ps(a.m_values[i], b.m_values[i]);
  380. return result;
  381. }
  382. static UInt16 Max(const UInt16 &a, const UInt16 &b)
  383. {
  384. __m128i bitFlip = _mm_set1_epi16(-32768);
  385. UInt16 result;
  386. result.m_value = _mm_xor_si128(_mm_max_epi16(_mm_xor_si128(a.m_value, bitFlip), _mm_xor_si128(b.m_value, bitFlip)), bitFlip);
  387. return result;
  388. }
  389. static SInt16 Max(const SInt16 &a, const SInt16 &b)
  390. {
  391. SInt16 result;
  392. result.m_value = _mm_max_epi16(a.m_value, b.m_value);
  393. return result;
  394. }
  395. static UInt15 Max(const UInt15 &a, const UInt15 &b)
  396. {
  397. UInt15 result;
  398. result.m_value = _mm_max_epi16(a.m_value, b.m_value);
  399. return result;
  400. }
  401. static Float Max(const Float &a, const Float &b)
  402. {
  403. Float result;
  404. for (int i = 0; i < 2; i++)
  405. result.m_values[i] = _mm_max_ps(a.m_values[i], b.m_values[i]);
  406. return result;
  407. }
  408. static Float Clamp(const Float &v, float min, float max)
  409. {
  410. Float result;
  411. for (int i = 0; i < 2; i++)
  412. result.m_values[i] = _mm_max_ps(_mm_min_ps(v.m_values[i], _mm_set1_ps(max)), _mm_set1_ps(min));
  413. return result;
  414. }
  415. static Float Reciprocal(const Float &v)
  416. {
  417. Float result;
  418. for (int i = 0; i < 2; i++)
  419. result.m_values[i] = _mm_rcp_ps(v.m_values[i]);
  420. return result;
  421. }
  422. static void ConvertLDRInputs(const PixelBlockU8* inputBlocks, int pxOffset, int channel, UInt15 &chOut)
  423. {
  424. int16_t values[8];
  425. for (int i = 0; i < 8; i++)
  426. values[i] = inputBlocks[i].m_pixels[pxOffset][channel];
  427. chOut.m_value = _mm_set_epi16(values[7], values[6], values[5], values[4], values[3], values[2], values[1], values[0]);
  428. }
  429. static void ConvertHDRInputs(const PixelBlockF16* inputBlocks, int pxOffset, int channel, SInt16 &chOut)
  430. {
  431. int16_t values[8];
  432. for (int i = 0; i < 8; i++)
  433. values[i] = inputBlocks[i].m_pixels[pxOffset][channel];
  434. chOut.m_value = _mm_set_epi16(values[7], values[6], values[5], values[4], values[3], values[2], values[1], values[0]);
  435. }
  436. static Float MakeFloat(float v)
  437. {
  438. Float f;
  439. f.m_values[0] = f.m_values[1] = _mm_set1_ps(v);
  440. return f;
  441. }
  442. static Float MakeFloatZero()
  443. {
  444. Float f;
  445. f.m_values[0] = f.m_values[1] = _mm_setzero_ps();
  446. return f;
  447. }
  448. static UInt16 MakeUInt16(uint16_t v)
  449. {
  450. UInt16 result;
  451. result.m_value = _mm_set1_epi16(static_cast<short>(v));
  452. return result;
  453. }
  454. static SInt16 MakeSInt16(int16_t v)
  455. {
  456. SInt16 result;
  457. result.m_value = _mm_set1_epi16(static_cast<short>(v));
  458. return result;
  459. }
  460. static AInt16 MakeAInt16(int16_t v)
  461. {
  462. AInt16 result;
  463. result.m_value = _mm_set1_epi16(static_cast<short>(v));
  464. return result;
  465. }
  466. static UInt15 MakeUInt15(uint16_t v)
  467. {
  468. UInt15 result;
  469. result.m_value = _mm_set1_epi16(static_cast<short>(v));
  470. return result;
  471. }
  472. static SInt32 MakeSInt32(int32_t v)
  473. {
  474. SInt32 result;
  475. result.m_values[0] = _mm_set1_epi32(v);
  476. result.m_values[1] = _mm_set1_epi32(v);
  477. return result;
  478. }
  479. static UInt31 MakeUInt31(uint32_t v)
  480. {
  481. UInt31 result;
  482. result.m_values[0] = _mm_set1_epi32(v);
  483. result.m_values[1] = _mm_set1_epi32(v);
  484. return result;
  485. }
  486. static uint16_t Extract(const UInt16 &v, int offset)
  487. {
  488. return reinterpret_cast<const uint16_t*>(&v.m_value)[offset];
  489. }
  490. static int16_t Extract(const SInt16 &v, int offset)
  491. {
  492. return reinterpret_cast<const int16_t*>(&v.m_value)[offset];
  493. }
  494. static uint16_t Extract(const UInt15 &v, int offset)
  495. {
  496. return reinterpret_cast<const uint16_t*>(&v.m_value)[offset];
  497. }
  498. static int16_t Extract(const AInt16 &v, int offset)
  499. {
  500. return reinterpret_cast<const int16_t*>(&v.m_value)[offset];
  501. }
  502. static void PutUInt16(UInt16 &dest, int offset, uint16_t v)
  503. {
  504. reinterpret_cast<uint16_t*>(&dest)[offset] = v;
  505. }
  506. static void PutUInt15(UInt15 &dest, int offset, uint16_t v)
  507. {
  508. reinterpret_cast<uint16_t*>(&dest)[offset] = v;
  509. }
  510. static void PutSInt16(SInt16 &dest, int offset, int16_t v)
  511. {
  512. reinterpret_cast<int16_t*>(&dest)[offset] = v;
  513. }
  514. static float ExtractFloat(const Float& v, int offset)
  515. {
  516. return reinterpret_cast<const float*>(&v)[offset];
  517. }
  518. static void PutFloat(Float &dest, int offset, float v)
  519. {
  520. reinterpret_cast<float*>(&dest)[offset] = v;
  521. }
  522. static Int16CompFlag Less(const SInt16 &a, const SInt16 &b)
  523. {
  524. Int16CompFlag result;
  525. result.m_value = _mm_cmplt_epi16(a.m_value, b.m_value);
  526. return result;
  527. }
  528. static Int16CompFlag Less(const UInt15 &a, const UInt15 &b)
  529. {
  530. Int16CompFlag result;
  531. result.m_value = _mm_cmplt_epi16(a.m_value, b.m_value);
  532. return result;
  533. }
  534. static Int16CompFlag LessOrEqual(const UInt15 &a, const UInt15 &b)
  535. {
  536. Int16CompFlag result;
  537. result.m_value = _mm_cmplt_epi16(a.m_value, b.m_value);
  538. return result;
  539. }
  540. static FloatCompFlag Less(const Float &a, const Float &b)
  541. {
  542. FloatCompFlag result;
  543. for (int i = 0; i < 2; i++)
  544. result.m_values[i] = _mm_cmplt_ps(a.m_values[i], b.m_values[i]);
  545. return result;
  546. }
  547. static FloatCompFlag LessOrEqual(const Float &a, const Float &b)
  548. {
  549. FloatCompFlag result;
  550. for (int i = 0; i < 2; i++)
  551. result.m_values[i] = _mm_cmple_ps(a.m_values[i], b.m_values[i]);
  552. return result;
  553. }
  554. template<int TSubtype>
  555. static Int16CompFlag Equal(const VInt16<TSubtype> &a, const VInt16<TSubtype> &b)
  556. {
  557. Int16CompFlag result;
  558. result.m_value = _mm_cmpeq_epi16(a.m_value, b.m_value);
  559. return result;
  560. }
  561. static FloatCompFlag Equal(const Float &a, const Float &b)
  562. {
  563. FloatCompFlag result;
  564. for (int i = 0; i < 2; i++)
  565. result.m_values[i] = _mm_cmpeq_ps(a.m_values[i], b.m_values[i]);
  566. return result;
  567. }
  568. static Float ToFloat(const UInt16 &v)
  569. {
  570. Float result;
  571. result.m_values[0] = _mm_cvtepi32_ps(_mm_unpacklo_epi16(v.m_value, _mm_setzero_si128()));
  572. result.m_values[1] = _mm_cvtepi32_ps(_mm_unpackhi_epi16(v.m_value, _mm_setzero_si128()));
  573. return result;
  574. }
  575. static UInt31 ToUInt31(const UInt16 &v)
  576. {
  577. UInt31 result;
  578. result.m_values[0] = _mm_unpacklo_epi16(v.m_value, _mm_setzero_si128());
  579. result.m_values[1] = _mm_unpackhi_epi16(v.m_value, _mm_setzero_si128());
  580. return result;
  581. }
  582. static SInt32 ToInt32(const UInt16 &v)
  583. {
  584. SInt32 result;
  585. result.m_values[0] = _mm_unpacklo_epi16(v.m_value, _mm_setzero_si128());
  586. result.m_values[1] = _mm_unpackhi_epi16(v.m_value, _mm_setzero_si128());
  587. return result;
  588. }
  589. static SInt32 ToInt32(const SInt16 &v)
  590. {
  591. SInt32 result;
  592. result.m_values[0] = _mm_srai_epi32(_mm_unpacklo_epi16(_mm_setzero_si128(), v.m_value), 16);
  593. result.m_values[1] = _mm_srai_epi32(_mm_unpackhi_epi16(_mm_setzero_si128(), v.m_value), 16);
  594. return result;
  595. }
  596. static Float ToFloat(const SInt16 &v)
  597. {
  598. Float result;
  599. result.m_values[0] = _mm_cvtepi32_ps(_mm_srai_epi32(_mm_unpacklo_epi16(_mm_setzero_si128(), v.m_value), 16));
  600. result.m_values[1] = _mm_cvtepi32_ps(_mm_srai_epi32(_mm_unpackhi_epi16(_mm_setzero_si128(), v.m_value), 16));
  601. return result;
  602. }
  603. static Float ToFloat(const UInt15 &v)
  604. {
  605. Float result;
  606. result.m_values[0] = _mm_cvtepi32_ps(_mm_unpacklo_epi16(v.m_value, _mm_setzero_si128()));
  607. result.m_values[1] = _mm_cvtepi32_ps(_mm_unpackhi_epi16(v.m_value, _mm_setzero_si128()));
  608. return result;
  609. }
  610. static Float ToFloat(const UInt31 &v)
  611. {
  612. Float result;
  613. result.m_values[0] = _mm_cvtepi32_ps(v.m_values[0]);
  614. result.m_values[1] = _mm_cvtepi32_ps(v.m_values[1]);
  615. return result;
  616. }
  617. static Int16CompFlag FloatFlagToInt16(const FloatCompFlag &v)
  618. {
  619. __m128i lo = _mm_castps_si128(v.m_values[0]);
  620. __m128i hi = _mm_castps_si128(v.m_values[1]);
  621. Int16CompFlag result;
  622. result.m_value = _mm_packs_epi32(lo, hi);
  623. return result;
  624. }
  625. static FloatCompFlag Int16FlagToFloat(const Int16CompFlag &v)
  626. {
  627. __m128i lo = _mm_unpacklo_epi16(v.m_value, v.m_value);
  628. __m128i hi = _mm_unpackhi_epi16(v.m_value, v.m_value);
  629. FloatCompFlag result;
  630. result.m_values[0] = _mm_castsi128_ps(lo);
  631. result.m_values[1] = _mm_castsi128_ps(hi);
  632. return result;
  633. }
  634. static Int16CompFlag MakeBoolInt16(bool b)
  635. {
  636. Int16CompFlag result;
  637. if (b)
  638. result.m_value = _mm_set1_epi16(-1);
  639. else
  640. result.m_value = _mm_setzero_si128();
  641. return result;
  642. }
  643. static FloatCompFlag MakeBoolFloat(bool b)
  644. {
  645. FloatCompFlag result;
  646. if (b)
  647. result.m_values[0] = result.m_values[1] = _mm_castsi128_ps(_mm_set1_epi32(-1));
  648. else
  649. result.m_values[0] = result.m_values[1] = _mm_setzero_ps();
  650. return result;
  651. }
  652. static Int16CompFlag AndNot(const Int16CompFlag &a, const Int16CompFlag &b)
  653. {
  654. Int16CompFlag result;
  655. result.m_value = _mm_andnot_si128(b.m_value, a.m_value);
  656. return result;
  657. }
  658. static UInt16 RoundAndConvertToU16(const Float &v, const void* /*roundingMode*/)
  659. {
  660. __m128i lo = _mm_cvtps_epi32(_mm_add_ps(v.m_values[0], _mm_set1_ps(-32768)));
  661. __m128i hi = _mm_cvtps_epi32(_mm_add_ps(v.m_values[1], _mm_set1_ps(-32768)));
  662. __m128i packed = _mm_packs_epi32(lo, hi);
  663. UInt16 result;
  664. result.m_value = _mm_xor_si128(packed, _mm_set1_epi16(-32768));
  665. return result;
  666. }
  667. static UInt15 RoundAndConvertToU15(const Float &v, const void* /*roundingMode*/)
  668. {
  669. __m128i lo = _mm_cvtps_epi32(v.m_values[0]);
  670. __m128i hi = _mm_cvtps_epi32(v.m_values[1]);
  671. __m128i packed = _mm_packs_epi32(lo, hi);
  672. UInt15 result;
  673. result.m_value = _mm_packs_epi32(lo, hi);
  674. return result;
  675. }
  676. static SInt16 RoundAndConvertToS16(const Float &v, const void* /*roundingMode*/)
  677. {
  678. __m128i lo = _mm_cvtps_epi32(v.m_values[0]);
  679. __m128i hi = _mm_cvtps_epi32(v.m_values[1]);
  680. __m128i packed = _mm_packs_epi32(lo, hi);
  681. SInt16 result;
  682. result.m_value = _mm_packs_epi32(lo, hi);
  683. return result;
  684. }
  685. static Float Sqrt(const Float &f)
  686. {
  687. Float result;
  688. for (int i = 0; i < 2; i++)
  689. result.m_values[i] = _mm_sqrt_ps(f.m_values[i]);
  690. return result;
  691. }
  692. static UInt16 Abs(const SInt16 &a)
  693. {
  694. __m128i signBitsXor = _mm_srai_epi16(a.m_value, 15);
  695. __m128i signBitsAdd = _mm_srli_epi16(a.m_value, 15);
  696. UInt16 result;
  697. result.m_value = _mm_add_epi16(_mm_xor_si128(a.m_value, signBitsXor), signBitsAdd);
  698. return result;
  699. }
  700. static Float Abs(const Float& a)
  701. {
  702. __m128 invMask = _mm_set1_ps(-0.0f);
  703. Float result;
  704. result.m_values[0] = _mm_andnot_ps(invMask, a.m_values[0]);
  705. result.m_values[1] = _mm_andnot_ps(invMask, a.m_values[1]);
  706. return result;
  707. }
  708. static UInt16 SqDiffUInt8(const UInt15 &a, const UInt15 &b)
  709. {
  710. __m128i diff = _mm_sub_epi16(a.m_value, b.m_value);
  711. UInt16 result;
  712. result.m_value = _mm_mullo_epi16(diff, diff);
  713. return result;
  714. }
  715. static Float SqDiffSInt16(const SInt16 &a, const SInt16 &b)
  716. {
  717. __m128i diffU = _mm_sub_epi16(_mm_max_epi16(a.m_value, b.m_value), _mm_min_epi16(a.m_value, b.m_value));
  718. __m128i mulHi = _mm_mulhi_epu16(diffU, diffU);
  719. __m128i mulLo = _mm_mullo_epi16(diffU, diffU);
  720. __m128i sqDiffHi = _mm_unpackhi_epi16(mulLo, mulHi);
  721. __m128i sqDiffLo = _mm_unpacklo_epi16(mulLo, mulHi);
  722. Float result;
  723. result.m_values[0] = _mm_cvtepi32_ps(sqDiffLo);
  724. result.m_values[1] = _mm_cvtepi32_ps(sqDiffHi);
  725. return result;
  726. }
  727. static Float TwosCLHalfToFloat(const SInt16 &v)
  728. {
  729. __m128i absV = _mm_add_epi16(_mm_xor_si128(v.m_value, _mm_srai_epi16(v.m_value, 15)), _mm_srli_epi16(v.m_value, 15));
  730. __m128i signBits = _mm_and_si128(v.m_value, _mm_set1_epi16(-32768));
  731. __m128i mantissa = _mm_and_si128(v.m_value, _mm_set1_epi16(0x03ff));
  732. __m128i exponent = _mm_and_si128(v.m_value, _mm_set1_epi16(0x7c00));
  733. __m128i isDenormal = _mm_cmpeq_epi16(exponent, _mm_setzero_si128());
  734. // Convert exponent to high-bits
  735. exponent = _mm_add_epi16(_mm_srli_epi16(exponent, 3), _mm_set1_epi16(14336));
  736. __m128i denormalCorrectionHigh = _mm_and_si128(isDenormal, _mm_or_si128(signBits, _mm_set1_epi16(14336)));
  737. __m128i highBits = _mm_or_si128(signBits, _mm_or_si128(exponent, _mm_srli_epi16(mantissa, 3)));
  738. __m128i lowBits = _mm_slli_epi16(mantissa, 13);
  739. __m128i flow = _mm_unpacklo_epi16(lowBits, highBits);
  740. __m128i fhigh = _mm_unpackhi_epi16(lowBits, highBits);
  741. __m128i correctionLow = _mm_unpacklo_epi16(_mm_setzero_si128(), denormalCorrectionHigh);
  742. __m128i correctionHigh = _mm_unpackhi_epi16(_mm_setzero_si128(), denormalCorrectionHigh);
  743. Float result;
  744. result.m_values[0] = _mm_sub_ps(_mm_castsi128_ps(flow), _mm_castsi128_ps(correctionLow));
  745. result.m_values[1] = _mm_sub_ps(_mm_castsi128_ps(fhigh), _mm_castsi128_ps(correctionHigh));
  746. return result;
  747. }
  748. static Float SqDiff2CLFloat(const SInt16 &a, const Float &b)
  749. {
  750. Float fa = TwosCLHalfToFloat(a);
  751. Float diff = fa - b;
  752. return diff * diff;
  753. }
  754. static Float SqDiff2CL(const SInt16 &a, const SInt16 &b)
  755. {
  756. Float fa = TwosCLHalfToFloat(a);
  757. Float fb = TwosCLHalfToFloat(b);
  758. Float diff = fa - fb;
  759. return diff * diff;
  760. }
  761. static Float SqDiff2CLFloat(const SInt16 &a, float aWeight, const Float &b)
  762. {
  763. Float fa = TwosCLHalfToFloat(a) * aWeight;
  764. Float diff = fa - b;
  765. return diff * diff;
  766. }
  767. static UInt16 RightShift(const UInt16 &v, int bits)
  768. {
  769. UInt16 result;
  770. result.m_value = _mm_srli_epi16(v.m_value, bits);
  771. return result;
  772. }
  773. static UInt31 RightShift(const UInt31 &v, int bits)
  774. {
  775. UInt31 result;
  776. result.m_values[0] = _mm_srli_epi32(v.m_values[0], bits);
  777. result.m_values[1] = _mm_srli_epi32(v.m_values[1], bits);
  778. return result;
  779. }
  780. static SInt16 RightShift(const SInt16 &v, int bits)
  781. {
  782. SInt16 result;
  783. result.m_value = _mm_srai_epi16(v.m_value, bits);
  784. return result;
  785. }
  786. static UInt15 RightShift(const UInt15 &v, int bits)
  787. {
  788. UInt15 result;
  789. result.m_value = _mm_srli_epi16(v.m_value, bits);
  790. return result;
  791. }
  792. static SInt32 RightShift(const SInt32 &v, int bits)
  793. {
  794. SInt32 result;
  795. result.m_values[0] = _mm_srai_epi32(v.m_values[0], bits);
  796. result.m_values[1] = _mm_srai_epi32(v.m_values[1], bits);
  797. return result;
  798. }
  799. static SInt16 ToSInt16(const SInt32 &v)
  800. {
  801. SInt16 result;
  802. result.m_value = _mm_packs_epi32(v.m_values[0], v.m_values[1]);
  803. return result;
  804. }
  805. static UInt16 ToUInt16(const UInt32 &v)
  806. {
  807. __m128i low = _mm_srai_epi32(_mm_slli_epi32(v.m_values[0], 16), 16);
  808. __m128i high = _mm_srai_epi32(_mm_slli_epi32(v.m_values[1], 16), 16);
  809. UInt16 result;
  810. result.m_value = _mm_packs_epi32(low, high);
  811. return result;
  812. }
  813. static UInt16 ToUInt16(const UInt31 &v)
  814. {
  815. __m128i low = _mm_srai_epi32(_mm_slli_epi32(v.m_values[0], 16), 16);
  816. __m128i high = _mm_srai_epi32(_mm_slli_epi32(v.m_values[1], 16), 16);
  817. UInt16 result;
  818. result.m_value = _mm_packs_epi32(low, high);
  819. return result;
  820. }
  821. static UInt15 ToUInt15(const UInt31 &v)
  822. {
  823. UInt15 result;
  824. result.m_value = _mm_packs_epi32(v.m_values[0], v.m_values[1]);
  825. return result;
  826. }
  827. static SInt32 XMultiply(const SInt16 &a, const SInt16 &b)
  828. {
  829. __m128i high = _mm_mulhi_epi16(a.m_value, b.m_value);
  830. __m128i low = _mm_mullo_epi16(a.m_value, b.m_value);
  831. SInt32 result;
  832. result.m_values[0] = _mm_unpacklo_epi16(low, high);
  833. result.m_values[1] = _mm_unpackhi_epi16(low, high);
  834. return result;
  835. }
  836. static SInt32 XMultiply(const SInt16 &a, const UInt15 &b)
  837. {
  838. __m128i high = _mm_mulhi_epi16(a.m_value, b.m_value);
  839. __m128i low = _mm_mullo_epi16(a.m_value, b.m_value);
  840. SInt32 result;
  841. result.m_values[0] = _mm_unpacklo_epi16(low, high);
  842. result.m_values[1] = _mm_unpackhi_epi16(low, high);
  843. return result;
  844. }
  845. static SInt32 XMultiply(const UInt15 &a, const SInt16 &b)
  846. {
  847. return XMultiply(b, a);
  848. }
  849. static UInt32 XMultiply(const UInt16 &a, const UInt16 &b)
  850. {
  851. __m128i high = _mm_mulhi_epu16(a.m_value, b.m_value);
  852. __m128i low = _mm_mullo_epi16(a.m_value, b.m_value);
  853. UInt32 result;
  854. result.m_values[0] = _mm_unpacklo_epi16(low, high);
  855. result.m_values[1] = _mm_unpackhi_epi16(low, high);
  856. return result;
  857. }
  858. static UInt16 CompactMultiply(const UInt16 &a, const UInt15 &b)
  859. {
  860. UInt16 result;
  861. result.m_value = _mm_mullo_epi16(a.m_value, b.m_value);
  862. return result;
  863. }
  864. static UInt16 CompactMultiply(const UInt15 &a, const UInt15 &b)
  865. {
  866. UInt16 result;
  867. result.m_value = _mm_mullo_epi16(a.m_value, b.m_value);
  868. return result;
  869. }
  870. static UInt31 XMultiply(const UInt15 &a, const UInt15 &b)
  871. {
  872. __m128i high = _mm_mulhi_epu16(a.m_value, b.m_value);
  873. __m128i low = _mm_mullo_epi16(a.m_value, b.m_value);
  874. UInt31 result;
  875. result.m_values[0] = _mm_unpacklo_epi16(low, high);
  876. result.m_values[1] = _mm_unpackhi_epi16(low, high);
  877. return result;
  878. }
  879. static UInt31 XMultiply(const UInt16 &a, const UInt15 &b)
  880. {
  881. __m128i high = _mm_mulhi_epu16(a.m_value, b.m_value);
  882. __m128i low = _mm_mullo_epi16(a.m_value, b.m_value);
  883. UInt31 result;
  884. result.m_values[0] = _mm_unpacklo_epi16(low, high);
  885. result.m_values[1] = _mm_unpackhi_epi16(low, high);
  886. return result;
  887. }
  888. static UInt31 XMultiply(const UInt15 &a, const UInt16 &b)
  889. {
  890. return XMultiply(b, a);
  891. }
  892. static bool AnySet(const Int16CompFlag &v)
  893. {
  894. return _mm_movemask_epi8(v.m_value) != 0;
  895. }
  896. static bool AllSet(const Int16CompFlag &v)
  897. {
  898. return _mm_movemask_epi8(v.m_value) == 0xffff;
  899. }
  900. static bool AnySet(const FloatCompFlag &v)
  901. {
  902. return _mm_movemask_ps(v.m_values[0]) != 0 || _mm_movemask_ps(v.m_values[1]) != 0;
  903. }
  904. static bool AllSet(const FloatCompFlag &v)
  905. {
  906. return _mm_movemask_ps(v.m_values[0]) == 0xf && _mm_movemask_ps(v.m_values[1]) == 0xf;
  907. }
  908. };
  909. #else
  910. // Scalar version
  911. struct ParallelMath
  912. {
  913. struct RoundTowardZeroForScope
  914. {
  915. };
  916. struct RoundTowardNearestForScope
  917. {
  918. };
  919. struct RoundUpForScope
  920. {
  921. };
  922. struct RoundDownForScope
  923. {
  924. };
  925. static const int ParallelSize = 1;
  926. enum Int16Subtype
  927. {
  928. IntSubtype_Signed,
  929. IntSubtype_UnsignedFull,
  930. IntSubtype_UnsignedTruncated,
  931. IntSubtype_Abstract,
  932. };
  933. typedef int32_t SInt16;
  934. typedef int32_t UInt15;
  935. typedef int32_t UInt16;
  936. typedef int32_t AInt16;
  937. typedef int32_t SInt32;
  938. typedef int32_t UInt31;
  939. typedef int32_t UInt32;
  940. typedef int32_t AInt32;
  941. typedef int32_t ScalarUInt16;
  942. typedef int32_t ScalarSInt16;
  943. typedef float Float;
  944. template<class TTargetType>
  945. struct LosslessCast
  946. {
  947. static const int32_t& Cast(const int32_t &src)
  948. {
  949. return src;
  950. }
  951. };
  952. typedef bool Int16CompFlag;
  953. typedef bool FloatCompFlag;
  954. static int32_t AbstractAdd(const int32_t &a, const int32_t &b)
  955. {
  956. return a + b;
  957. }
  958. static int32_t AbstractSubtract(const int32_t &a, const int32_t &b)
  959. {
  960. return a - b;
  961. }
  962. static float Select(bool flag, float a, float b)
  963. {
  964. return flag ? a : b;
  965. }
  966. static int32_t Select(bool flag, int32_t a, int32_t b)
  967. {
  968. return flag ? a : b;
  969. }
  970. static int32_t SelectOrZero(bool flag, int32_t a)
  971. {
  972. return flag ? a : 0;
  973. }
  974. static void ConditionalSet(int32_t& dest, bool flag, int32_t src)
  975. {
  976. if (flag)
  977. dest = src;
  978. }
  979. static int32_t ConditionalNegate(bool flag, int32_t v)
  980. {
  981. return (flag) ? -v : v;
  982. }
  983. static void NotConditionalSet(int32_t& dest, bool flag, int32_t src)
  984. {
  985. if (!flag)
  986. dest = src;
  987. }
  988. static void ConditionalSet(float& dest, bool flag, float src)
  989. {
  990. if (flag)
  991. dest = src;
  992. }
  993. static void NotConditionalSet(float& dest, bool flag, float src)
  994. {
  995. if (!flag)
  996. dest = src;
  997. }
  998. static void MakeSafeDenominator(float& v)
  999. {
  1000. if (v == 0.0f)
  1001. v = 1.0f;
  1002. }
  1003. static int32_t SignedRightShift(int32_t v, int bits)
  1004. {
  1005. return v >> bits;
  1006. }
  1007. static int32_t TruncateToPrecisionSigned(int32_t v, int precision)
  1008. {
  1009. v = (v << (32 - precision)) & 0xffffffff;
  1010. return SignedRightShift(v, 32 - precision);
  1011. }
  1012. static int32_t TruncateToPrecisionUnsigned(int32_t v, int precision)
  1013. {
  1014. return v & ((1 << precision) - 1);
  1015. }
  1016. static int32_t Min(int32_t a, int32_t b)
  1017. {
  1018. if (a < b)
  1019. return a;
  1020. return b;
  1021. }
  1022. static float Min(float a, float b)
  1023. {
  1024. if (a < b)
  1025. return a;
  1026. return b;
  1027. }
  1028. static int32_t Max(int32_t a, int32_t b)
  1029. {
  1030. if (a > b)
  1031. return a;
  1032. return b;
  1033. }
  1034. static float Max(float a, float b)
  1035. {
  1036. if (a > b)
  1037. return a;
  1038. return b;
  1039. }
  1040. static float Abs(float a)
  1041. {
  1042. return fabsf(a);
  1043. }
  1044. static int32_t Abs(int32_t a)
  1045. {
  1046. if (a < 0)
  1047. return -a;
  1048. return a;
  1049. }
  1050. static float Clamp(float v, float min, float max)
  1051. {
  1052. if (v < min)
  1053. return min;
  1054. if (v > max)
  1055. return max;
  1056. return v;
  1057. }
  1058. static float Reciprocal(float v)
  1059. {
  1060. return 1.0f / v;
  1061. }
  1062. static void ConvertLDRInputs(const PixelBlockU8* inputBlocks, int pxOffset, int channel, int32_t& chOut)
  1063. {
  1064. chOut = inputBlocks[0].m_pixels[pxOffset][channel];
  1065. }
  1066. static void ConvertHDRInputs(const PixelBlockF16* inputBlocks, int pxOffset, int channel, int32_t& chOut)
  1067. {
  1068. chOut = inputBlocks[0].m_pixels[pxOffset][channel];
  1069. }
  1070. static float MakeFloat(float v)
  1071. {
  1072. return v;
  1073. }
  1074. static float MakeFloatZero()
  1075. {
  1076. return 0.0f;
  1077. }
  1078. static int32_t MakeUInt16(uint16_t v)
  1079. {
  1080. return v;
  1081. }
  1082. static int32_t MakeSInt16(int16_t v)
  1083. {
  1084. return v;
  1085. }
  1086. static int32_t MakeAInt16(int16_t v)
  1087. {
  1088. return v;
  1089. }
  1090. static int32_t MakeUInt15(uint16_t v)
  1091. {
  1092. return v;
  1093. }
  1094. static int32_t MakeSInt32(int32_t v)
  1095. {
  1096. return v;
  1097. }
  1098. static int32_t MakeUInt31(int32_t v)
  1099. {
  1100. return v;
  1101. }
  1102. static int32_t Extract(int32_t v, int offset)
  1103. {
  1104. UNREFERENCED_PARAMETER(offset);
  1105. return v;
  1106. }
  1107. static void PutUInt16(int32_t &dest, int offset, ParallelMath::ScalarUInt16 v)
  1108. {
  1109. UNREFERENCED_PARAMETER(offset);
  1110. dest = v;
  1111. }
  1112. static void PutUInt15(int32_t &dest, int offset, ParallelMath::ScalarUInt16 v)
  1113. {
  1114. UNREFERENCED_PARAMETER(offset);
  1115. dest = v;
  1116. }
  1117. static void PutSInt16(int32_t &dest, int offset, ParallelMath::ScalarSInt16 v)
  1118. {
  1119. UNREFERENCED_PARAMETER(offset);
  1120. dest = v;
  1121. }
  1122. static float ExtractFloat(float v, int offset)
  1123. {
  1124. UNREFERENCED_PARAMETER(offset);
  1125. return v;
  1126. }
  1127. static void PutFloat(float &dest, int offset, float v)
  1128. {
  1129. UNREFERENCED_PARAMETER(offset);
  1130. dest = v;
  1131. }
  1132. static bool Less(int32_t a, int32_t b)
  1133. {
  1134. return a < b;
  1135. }
  1136. static bool Less(float a, float b)
  1137. {
  1138. return a < b;
  1139. }
  1140. static bool LessOrEqual(int32_t a, int32_t b)
  1141. {
  1142. return a < b;
  1143. }
  1144. static bool LessOrEqual(float a, float b)
  1145. {
  1146. return a < b;
  1147. }
  1148. static bool Equal(int32_t a, int32_t b)
  1149. {
  1150. return a == b;
  1151. }
  1152. static bool Equal(float a, float b)
  1153. {
  1154. return a == b;
  1155. }
  1156. static float ToFloat(int32_t v)
  1157. {
  1158. return static_cast<float>(v);
  1159. }
  1160. static int32_t ToUInt31(int32_t v)
  1161. {
  1162. return v;
  1163. }
  1164. static int32_t ToInt32(int32_t v)
  1165. {
  1166. return v;
  1167. }
  1168. static bool FloatFlagToInt16(bool v)
  1169. {
  1170. return v;
  1171. }
  1172. static bool Int16FlagToFloat(bool v)
  1173. {
  1174. return v;
  1175. }
  1176. static bool MakeBoolInt16(bool b)
  1177. {
  1178. return b;
  1179. }
  1180. static bool MakeBoolFloat(bool b)
  1181. {
  1182. return b;
  1183. }
  1184. static bool AndNot(bool a, bool b)
  1185. {
  1186. return a && !b;
  1187. }
  1188. static int32_t RoundAndConvertToInt(float v, const ParallelMath::RoundTowardZeroForScope *rtz)
  1189. {
  1190. UNREFERENCED_PARAMETER(rtz);
  1191. return static_cast<int>(v);
  1192. }
  1193. static int32_t RoundAndConvertToInt(float v, const ParallelMath::RoundUpForScope *ru)
  1194. {
  1195. UNREFERENCED_PARAMETER(ru);
  1196. return static_cast<int>(ceilf(v));
  1197. }
  1198. static int32_t RoundAndConvertToInt(float v, const ParallelMath::RoundDownForScope *rd)
  1199. {
  1200. UNREFERENCED_PARAMETER(rd);
  1201. return static_cast<int>(floorf(v));
  1202. }
  1203. static int32_t RoundAndConvertToInt(float v, const ParallelMath::RoundTowardNearestForScope *rtn)
  1204. {
  1205. UNREFERENCED_PARAMETER(rtn);
  1206. return static_cast<int>(floorf(v + 0.5f));
  1207. }
  1208. template<class TRoundMode>
  1209. static int32_t RoundAndConvertToU16(float v, const TRoundMode *roundingMode)
  1210. {
  1211. return RoundAndConvertToInt(v, roundingMode);
  1212. }
  1213. template<class TRoundMode>
  1214. static int32_t RoundAndConvertToU15(float v, const TRoundMode *roundingMode)
  1215. {
  1216. return RoundAndConvertToInt(v, roundingMode);
  1217. }
  1218. template<class TRoundMode>
  1219. static int32_t RoundAndConvertToS16(float v, const TRoundMode *roundingMode)
  1220. {
  1221. return RoundAndConvertToInt(v, roundingMode);
  1222. }
  1223. static float Sqrt(float f)
  1224. {
  1225. return sqrtf(f);
  1226. }
  1227. static int32_t SqDiffUInt8(int32_t a, int32_t b)
  1228. {
  1229. int32_t delta = a - b;
  1230. return delta * delta;
  1231. }
  1232. static int32_t SqDiffInt16(int32_t a, int32_t b)
  1233. {
  1234. int32_t delta = a - b;
  1235. return delta * delta;
  1236. }
  1237. static int32_t SqDiffSInt16(int32_t a, int32_t b)
  1238. {
  1239. int32_t delta = a - b;
  1240. return delta * delta;
  1241. }
  1242. static float TwosCLHalfToFloat(int32_t v)
  1243. {
  1244. int32_t absV = (v < 0) ? -v : v;
  1245. int32_t signBits = (absV & -32768);
  1246. int32_t mantissa = (absV & 0x03ff);
  1247. int32_t exponent = (absV & 0x7c00);
  1248. bool isDenormal = (exponent == 0);
  1249. // Convert exponent to high-bits
  1250. exponent = (exponent >> 3) + 14336;
  1251. int32_t denormalCorrection = (isDenormal ? (signBits | 14336) : 0) << 16;
  1252. int32_t fBits = ((exponent | signBits) << 16) | (mantissa << 13);
  1253. float f, correction;
  1254. memcpy(&f, &fBits, 4);
  1255. memcpy(&correction, &denormalCorrection, 4);
  1256. return f - correction;
  1257. }
  1258. static Float SqDiff2CLFloat(const SInt16 &a, const Float &b)
  1259. {
  1260. Float fa = TwosCLHalfToFloat(a);
  1261. Float diff = fa - b;
  1262. return diff * diff;
  1263. }
  1264. static Float SqDiff2CL(const SInt16 &a, const SInt16 &b)
  1265. {
  1266. Float fa = TwosCLHalfToFloat(a);
  1267. Float fb = TwosCLHalfToFloat(b);
  1268. Float diff = fa - fb;
  1269. return diff * diff;
  1270. }
  1271. static Float SqDiff2CLFloat(const SInt16 &a, float aWeight, const Float &b)
  1272. {
  1273. Float fa = TwosCLHalfToFloat(a) * aWeight;
  1274. Float diff = fa - b;
  1275. return diff * diff;
  1276. }
  1277. static int32_t RightShift(int32_t v, int bits)
  1278. {
  1279. return SignedRightShift(v, bits);
  1280. }
  1281. static int32_t ToSInt16(int32_t v)
  1282. {
  1283. return v;
  1284. }
  1285. static int32_t ToUInt16(int32_t v)
  1286. {
  1287. return v;
  1288. }
  1289. static int32_t ToUInt15(int32_t v)
  1290. {
  1291. return v;
  1292. }
  1293. static int32_t XMultiply(int32_t a, int32_t b)
  1294. {
  1295. return a * b;
  1296. }
  1297. static int32_t CompactMultiply(int32_t a, int32_t b)
  1298. {
  1299. return a * b;
  1300. }
  1301. static bool AnySet(bool v)
  1302. {
  1303. return v;
  1304. }
  1305. static bool AllSet(bool v)
  1306. {
  1307. return v;
  1308. }
  1309. };
  1310. #endif
  1311. namespace Internal
  1312. {
  1313. namespace BC7Data
  1314. {
  1315. enum AlphaMode
  1316. {
  1317. AlphaMode_Combined,
  1318. AlphaMode_Separate,
  1319. AlphaMode_None,
  1320. };
  1321. enum PBitMode
  1322. {
  1323. PBitMode_PerEndpoint,
  1324. PBitMode_PerSubset,
  1325. PBitMode_None
  1326. };
  1327. struct BC7ModeInfo
  1328. {
  1329. PBitMode m_pBitMode;
  1330. AlphaMode m_alphaMode;
  1331. int m_rgbBits;
  1332. int m_alphaBits;
  1333. int m_partitionBits;
  1334. int m_numSubsets;
  1335. int m_indexBits;
  1336. int m_alphaIndexBits;
  1337. bool m_hasIndexSelector;
  1338. };
  1339. BC7ModeInfo g_modes[] =
  1340. {
  1341. { PBitMode_PerEndpoint, AlphaMode_None, 4, 0, 4, 3, 3, 0, false }, // 0
  1342. { PBitMode_PerSubset, AlphaMode_None, 6, 0, 6, 2, 3, 0, false }, // 1
  1343. { PBitMode_None, AlphaMode_None, 5, 0, 6, 3, 2, 0, false }, // 2
  1344. { PBitMode_PerEndpoint, AlphaMode_None, 7, 0, 6, 2, 2, 0, false }, // 3 (Mode reference has an error, P-bit is really per-endpoint)
  1345. { PBitMode_None, AlphaMode_Separate, 5, 6, 0, 1, 2, 3, true }, // 4
  1346. { PBitMode_None, AlphaMode_Separate, 7, 8, 0, 1, 2, 2, false }, // 5
  1347. { PBitMode_PerEndpoint, AlphaMode_Combined, 7, 7, 0, 1, 4, 0, false }, // 6
  1348. { PBitMode_PerEndpoint, AlphaMode_Combined, 5, 5, 6, 2, 2, 0, false } // 7
  1349. };
  1350. const int g_weight2[] = { 0, 21, 43, 64 };
  1351. const int g_weight3[] = { 0, 9, 18, 27, 37, 46, 55, 64 };
  1352. const int g_weight4[] = { 0, 4, 9, 13, 17, 21, 26, 30, 34, 38, 43, 47, 51, 55, 60, 64 };
  1353. const int *g_weightTables[] =
  1354. {
  1355. NULL,
  1356. NULL,
  1357. g_weight2,
  1358. g_weight3,
  1359. g_weight4
  1360. };
  1361. struct BC6HModeInfo
  1362. {
  1363. uint16_t m_modeID;
  1364. bool m_partitioned;
  1365. bool m_transformed;
  1366. int m_aPrec;
  1367. int m_bPrec[3];
  1368. };
  1369. // [partitioned][precision]
  1370. bool g_hdrModesExistForPrecision[2][17] =
  1371. {
  1372. //0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
  1373. { false, false, false, false, false, false, false, false, false, false, true, true, true, false, false, false, true },
  1374. { false, false, false, false, false, false, true, true, true, true, true, true, false, false, false, false, false },
  1375. };
  1376. BC6HModeInfo g_hdrModes[] =
  1377. {
  1378. { 0x00, true, true, 10,{ 5, 5, 5 } },
  1379. { 0x01, true, true, 7,{ 6, 6, 6 } },
  1380. { 0x02, true, true, 11,{ 5, 4, 4 } },
  1381. { 0x06, true, true, 11,{ 4, 5, 4 } },
  1382. { 0x0a, true, true, 11,{ 4, 4, 5 } },
  1383. { 0x0e, true, true, 9,{ 5, 5, 5 } },
  1384. { 0x12, true, true, 8,{ 6, 5, 5 } },
  1385. { 0x16, true, true, 8,{ 5, 6, 5 } },
  1386. { 0x1a, true, true, 8,{ 5, 5, 6 } },
  1387. { 0x1e, true, false, 6,{ 6, 6, 6 } },
  1388. { 0x03, false, false, 10,{ 10, 10, 10 } },
  1389. { 0x07, false, true, 11,{ 9, 9, 9 } },
  1390. { 0x0b, false, true, 12,{ 8, 8, 8 } },
  1391. { 0x0f, false, true, 16,{ 4, 4, 4 } },
  1392. };
  1393. const int g_maxHDRPrecision = 16;
  1394. static const size_t g_numHDRModes = sizeof(g_hdrModes) / sizeof(g_hdrModes[0]);
  1395. static uint16_t g_partitionMap[64] =
  1396. {
  1397. 0xCCCC, 0x8888, 0xEEEE, 0xECC8,
  1398. 0xC880, 0xFEEC, 0xFEC8, 0xEC80,
  1399. 0xC800, 0xFFEC, 0xFE80, 0xE800,
  1400. 0xFFE8, 0xFF00, 0xFFF0, 0xF000,
  1401. 0xF710, 0x008E, 0x7100, 0x08CE,
  1402. 0x008C, 0x7310, 0x3100, 0x8CCE,
  1403. 0x088C, 0x3110, 0x6666, 0x366C,
  1404. 0x17E8, 0x0FF0, 0x718E, 0x399C,
  1405. 0xaaaa, 0xf0f0, 0x5a5a, 0x33cc,
  1406. 0x3c3c, 0x55aa, 0x9696, 0xa55a,
  1407. 0x73ce, 0x13c8, 0x324c, 0x3bdc,
  1408. 0x6996, 0xc33c, 0x9966, 0x660,
  1409. 0x272, 0x4e4, 0x4e40, 0x2720,
  1410. 0xc936, 0x936c, 0x39c6, 0x639c,
  1411. 0x9336, 0x9cc6, 0x817e, 0xe718,
  1412. 0xccf0, 0xfcc, 0x7744, 0xee22,
  1413. };
  1414. static uint32_t g_partitionMap2[64] =
  1415. {
  1416. 0xaa685050, 0x6a5a5040, 0x5a5a4200, 0x5450a0a8,
  1417. 0xa5a50000, 0xa0a05050, 0x5555a0a0, 0x5a5a5050,
  1418. 0xaa550000, 0xaa555500, 0xaaaa5500, 0x90909090,
  1419. 0x94949494, 0xa4a4a4a4, 0xa9a59450, 0x2a0a4250,
  1420. 0xa5945040, 0x0a425054, 0xa5a5a500, 0x55a0a0a0,
  1421. 0xa8a85454, 0x6a6a4040, 0xa4a45000, 0x1a1a0500,
  1422. 0x0050a4a4, 0xaaa59090, 0x14696914, 0x69691400,
  1423. 0xa08585a0, 0xaa821414, 0x50a4a450, 0x6a5a0200,
  1424. 0xa9a58000, 0x5090a0a8, 0xa8a09050, 0x24242424,
  1425. 0x00aa5500, 0x24924924, 0x24499224, 0x50a50a50,
  1426. 0x500aa550, 0xaaaa4444, 0x66660000, 0xa5a0a5a0,
  1427. 0x50a050a0, 0x69286928, 0x44aaaa44, 0x66666600,
  1428. 0xaa444444, 0x54a854a8, 0x95809580, 0x96969600,
  1429. 0xa85454a8, 0x80959580, 0xaa141414, 0x96960000,
  1430. 0xaaaa1414, 0xa05050a0, 0xa0a5a5a0, 0x96000000,
  1431. 0x40804080, 0xa9a8a9a8, 0xaaaaaa44, 0x2a4a5254,
  1432. };
  1433. static int g_fixupIndexes2[64] =
  1434. {
  1435. 15,15,15,15,
  1436. 15,15,15,15,
  1437. 15,15,15,15,
  1438. 15,15,15,15,
  1439. 15, 2, 8, 2,
  1440. 2, 8, 8,15,
  1441. 2, 8, 2, 2,
  1442. 8, 8, 2, 2,
  1443. 15,15, 6, 8,
  1444. 2, 8,15,15,
  1445. 2, 8, 2, 2,
  1446. 2,15,15, 6,
  1447. 6, 2, 6, 8,
  1448. 15,15, 2, 2,
  1449. 15,15,15,15,
  1450. 15, 2, 2,15,
  1451. };
  1452. static int g_fixupIndexes3[64][2] =
  1453. {
  1454. { 3,15 },{ 3, 8 },{ 15, 8 },{ 15, 3 },
  1455. { 8,15 },{ 3,15 },{ 15, 3 },{ 15, 8 },
  1456. { 8,15 },{ 8,15 },{ 6,15 },{ 6,15 },
  1457. { 6,15 },{ 5,15 },{ 3,15 },{ 3, 8 },
  1458. { 3,15 },{ 3, 8 },{ 8,15 },{ 15, 3 },
  1459. { 3,15 },{ 3, 8 },{ 6,15 },{ 10, 8 },
  1460. { 5, 3 },{ 8,15 },{ 8, 6 },{ 6,10 },
  1461. { 8,15 },{ 5,15 },{ 15,10 },{ 15, 8 },
  1462. { 8,15 },{ 15, 3 },{ 3,15 },{ 5,10 },
  1463. { 6,10 },{ 10, 8 },{ 8, 9 },{ 15,10 },
  1464. { 15, 6 },{ 3,15 },{ 15, 8 },{ 5,15 },
  1465. { 15, 3 },{ 15, 6 },{ 15, 6 },{ 15, 8 },
  1466. { 3,15 },{ 15, 3 },{ 5,15 },{ 5,15 },
  1467. { 5,15 },{ 8,15 },{ 5,15 },{ 10,15 },
  1468. { 5,15 },{ 10,15 },{ 8,15 },{ 13,15 },
  1469. { 15, 3 },{ 12,15 },{ 3,15 },{ 3, 8 },
  1470. };
  1471. static const unsigned char g_fragments[] =
  1472. {
  1473. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, // 0, 16
  1474. 0, 1, 2, 3, // 16, 4
  1475. 0, 1, 4, // 20, 3
  1476. 0, 1, 2, 4, // 23, 4
  1477. 2, 3, 7, // 27, 3
  1478. 1, 2, 3, 7, // 30, 4
  1479. 0, 1, 2, 3, 4, 5, 6, 7, // 34, 8
  1480. 0, 1, 4, 8, // 42, 4
  1481. 0, 1, 2, 4, 5, 8, // 46, 6
  1482. 0, 1, 2, 3, 4, 5, 6, 8, // 52, 8
  1483. 1, 4, 5, 6, 9, // 60, 5
  1484. 2, 5, 6, 7, 10, // 65, 5
  1485. 5, 6, 9, 10, // 70, 4
  1486. 2, 3, 7, 11, // 74, 4
  1487. 1, 2, 3, 6, 7, 11, // 78, 6
  1488. 0, 1, 2, 3, 5, 6, 7, 11, // 84, 8
  1489. 0, 1, 2, 3, 8, 9, 10, 11, // 92, 8
  1490. 2, 3, 6, 7, 8, 9, 10, 11, // 100, 8
  1491. 4, 5, 6, 7, 8, 9, 10, 11, // 108, 8
  1492. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, // 116, 12
  1493. 0, 4, 8, 12, // 128, 4
  1494. 0, 2, 3, 4, 6, 7, 8, 12, // 132, 8
  1495. 0, 1, 2, 4, 5, 8, 9, 12, // 140, 8
  1496. 0, 1, 2, 3, 4, 5, 6, 8, 9, 12, // 148, 10
  1497. 3, 6, 7, 8, 9, 12, // 158, 6
  1498. 3, 5, 6, 7, 8, 9, 10, 12, // 164, 8
  1499. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, // 172, 12
  1500. 0, 1, 2, 5, 6, 7, 11, 12, // 184, 8
  1501. 5, 8, 9, 10, 13, // 192, 5
  1502. 8, 12, 13, // 197, 3
  1503. 4, 8, 12, 13, // 200, 4
  1504. 2, 3, 6, 9, 12, 13, // 204, 6
  1505. 0, 1, 2, 3, 8, 9, 12, 13, // 210, 8
  1506. 0, 1, 4, 5, 8, 9, 12, 13, // 218, 8
  1507. 2, 3, 6, 7, 8, 9, 12, 13, // 226, 8
  1508. 2, 3, 5, 6, 9, 10, 12, 13, // 234, 8
  1509. 0, 3, 6, 7, 9, 10, 12, 13, // 242, 8
  1510. 0, 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 13, // 250, 12
  1511. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 13, // 262, 13
  1512. 2, 3, 4, 7, 8, 11, 12, 13, // 275, 8
  1513. 1, 2, 6, 7, 8, 11, 12, 13, // 283, 8
  1514. 2, 3, 4, 6, 7, 8, 9, 11, 12, 13, // 291, 10
  1515. 2, 3, 4, 5, 10, 11, 12, 13, // 301, 8
  1516. 0, 1, 6, 7, 10, 11, 12, 13, // 309, 8
  1517. 6, 9, 10, 11, 14, // 317, 5
  1518. 0, 2, 4, 6, 8, 10, 12, 14, // 322, 8
  1519. 1, 3, 5, 7, 8, 10, 12, 14, // 330, 8
  1520. 1, 3, 4, 6, 9, 11, 12, 14, // 338, 8
  1521. 0, 2, 5, 7, 9, 11, 12, 14, // 346, 8
  1522. 0, 3, 4, 5, 8, 9, 13, 14, // 354, 8
  1523. 2, 3, 4, 7, 8, 9, 13, 14, // 362, 8
  1524. 1, 2, 5, 6, 9, 10, 13, 14, // 370, 8
  1525. 0, 3, 4, 7, 9, 10, 13, 14, // 378, 8
  1526. 0, 3, 5, 6, 8, 11, 13, 14, // 386, 8
  1527. 1, 2, 4, 7, 8, 11, 13, 14, // 394, 8
  1528. 0, 1, 4, 7, 10, 11, 13, 14, // 402, 8
  1529. 0, 3, 6, 7, 10, 11, 13, 14, // 410, 8
  1530. 8, 12, 13, 14, // 418, 4
  1531. 1, 2, 3, 7, 8, 12, 13, 14, // 422, 8
  1532. 4, 8, 9, 12, 13, 14, // 430, 6
  1533. 0, 4, 5, 8, 9, 12, 13, 14, // 436, 8
  1534. 1, 2, 3, 6, 7, 8, 9, 12, 13, 14, // 444, 10
  1535. 2, 6, 8, 9, 10, 12, 13, 14, // 454, 8
  1536. 0, 1, 2, 4, 5, 6, 8, 9, 10, 12, 13, 14, // 462, 12
  1537. 0, 7, 9, 10, 11, 12, 13, 14, // 474, 8
  1538. 1, 2, 3, 4, 5, 6, 8, 15, // 482, 8
  1539. 3, 7, 11, 15, // 490, 4
  1540. 0, 1, 3, 4, 5, 7, 11, 15, // 494, 8
  1541. 0, 4, 5, 10, 11, 15, // 502, 6
  1542. 1, 2, 3, 6, 7, 10, 11, 15, // 508, 8
  1543. 0, 1, 2, 3, 5, 6, 7, 10, 11, 15, // 516, 10
  1544. 0, 4, 5, 6, 9, 10, 11, 15, // 526, 8
  1545. 0, 1, 2, 3, 4, 5, 6, 7, 9, 10, 11, 15, // 534, 12
  1546. 1, 2, 4, 5, 8, 9, 12, 15, // 546, 8
  1547. 2, 3, 5, 6, 8, 9, 12, 15, // 554, 8
  1548. 0, 3, 5, 6, 9, 10, 12, 15, // 562, 8
  1549. 1, 2, 4, 7, 9, 10, 12, 15, // 570, 8
  1550. 1, 2, 5, 6, 8, 11, 12, 15, // 578, 8
  1551. 0, 3, 4, 7, 8, 11, 12, 15, // 586, 8
  1552. 0, 1, 5, 6, 10, 11, 12, 15, // 594, 8
  1553. 1, 2, 6, 7, 10, 11, 12, 15, // 602, 8
  1554. 1, 3, 4, 6, 8, 10, 13, 15, // 610, 8
  1555. 0, 2, 5, 7, 8, 10, 13, 15, // 618, 8
  1556. 0, 2, 4, 6, 9, 11, 13, 15, // 626, 8
  1557. 1, 3, 5, 7, 9, 11, 13, 15, // 634, 8
  1558. 0, 1, 2, 3, 4, 5, 7, 8, 12, 13, 15, // 642, 11
  1559. 2, 3, 4, 5, 8, 9, 14, 15, // 653, 8
  1560. 0, 1, 6, 7, 8, 9, 14, 15, // 661, 8
  1561. 0, 1, 5, 10, 14, 15, // 669, 6
  1562. 0, 3, 4, 5, 9, 10, 14, 15, // 675, 8
  1563. 0, 1, 5, 6, 9, 10, 14, 15, // 683, 8
  1564. 11, 14, 15, // 691, 3
  1565. 7, 11, 14, 15, // 694, 4
  1566. 1, 2, 4, 5, 8, 11, 14, 15, // 698, 8
  1567. 0, 1, 4, 7, 8, 11, 14, 15, // 706, 8
  1568. 0, 1, 4, 5, 10, 11, 14, 15, // 714, 8
  1569. 2, 3, 6, 7, 10, 11, 14, 15, // 722, 8
  1570. 4, 5, 6, 7, 10, 11, 14, 15, // 730, 8
  1571. 0, 1, 4, 5, 7, 8, 10, 11, 14, 15, // 738, 10
  1572. 0, 1, 2, 3, 5, 6, 7, 9, 10, 11, 14, 15, // 748, 12
  1573. 0, 1, 2, 3, 4, 5, 6, 7, 9, 10, 11, 14, 15, // 760, 13
  1574. 0, 1, 2, 3, 4, 6, 7, 11, 12, 14, 15, // 773, 11
  1575. 3, 4, 8, 9, 10, 13, 14, 15, // 784, 8
  1576. 11, 13, 14, 15, // 792, 4
  1577. 0, 1, 2, 4, 11, 13, 14, 15, // 796, 8
  1578. 0, 1, 2, 4, 5, 10, 11, 13, 14, 15, // 804, 10
  1579. 7, 10, 11, 13, 14, 15, // 814, 6
  1580. 3, 6, 7, 10, 11, 13, 14, 15, // 820, 8
  1581. 1, 5, 9, 10, 11, 13, 14, 15, // 828, 8
  1582. 1, 2, 3, 5, 6, 7, 9, 10, 11, 13, 14, 15, // 836, 12
  1583. 12, 13, 14, 15, // 848, 4
  1584. 0, 1, 2, 3, 12, 13, 14, 15, // 852, 8
  1585. 0, 1, 4, 5, 12, 13, 14, 15, // 860, 8
  1586. 4, 5, 6, 7, 12, 13, 14, 15, // 868, 8
  1587. 4, 8, 9, 10, 12, 13, 14, 15, // 876, 8
  1588. 0, 4, 5, 8, 9, 10, 12, 13, 14, 15, // 884, 10
  1589. 0, 1, 4, 5, 6, 8, 9, 10, 12, 13, 14, 15, // 894, 12
  1590. 0, 1, 2, 3, 4, 7, 8, 11, 12, 13, 14, 15, // 906, 12
  1591. 0, 1, 3, 4, 8, 9, 11, 12, 13, 14, 15, // 918, 11
  1592. 0, 2, 3, 7, 8, 10, 11, 12, 13, 14, 15, // 929, 11
  1593. 7, 9, 10, 11, 12, 13, 14, 15, // 940, 8
  1594. 3, 6, 7, 9, 10, 11, 12, 13, 14, 15, // 948, 10
  1595. 2, 3, 5, 6, 7, 9, 10, 11, 12, 13, 14, 15, // 958, 12
  1596. 8, 9, 10, 11, 12, 13, 14, 15, // 970, 8
  1597. 0, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, // 978, 12
  1598. 0, 1, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, // 990, 13
  1599. 3, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, // 1003, 12
  1600. 2, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, // 1015, 13
  1601. 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, // 1028, 12
  1602. 0, 2, // 1040, 2
  1603. 1, 3, // 1042, 2
  1604. 0, 1, 4, 5, // 1044, 4
  1605. 0, 1, 2, 4, 5, // 1048, 5
  1606. 2, 3, 6, // 1053, 3
  1607. 0, 2, 4, 6, // 1056, 4
  1608. 1, 2, 5, 6, // 1060, 4
  1609. 0, 1, 2, 3, 5, 6, // 1064, 6
  1610. 0, 1, 2, 4, 5, 6, // 1070, 6
  1611. 0, 1, 2, 3, 4, 5, 6, // 1076, 7
  1612. 0, 3, 4, 7, // 1083, 4
  1613. 0, 1, 2, 3, 4, 7, // 1087, 6
  1614. 1, 3, 5, 7, // 1093, 4
  1615. 2, 3, 6, 7, // 1097, 4
  1616. 1, 2, 3, 6, 7, // 1101, 5
  1617. 1, 2, 3, 5, 6, 7, // 1106, 6
  1618. 0, 1, 2, 3, 5, 6, 7, // 1112, 7
  1619. 4, 5, 6, 7, // 1119, 4
  1620. 0, 8, // 1123, 2
  1621. 0, 1, 4, 5, 8, // 1125, 5
  1622. 0, 1, 8, 9, // 1130, 4
  1623. 4, 5, 8, 9, // 1134, 4
  1624. 0, 1, 4, 5, 8, 9, // 1138, 6
  1625. 2, 6, 8, 9, // 1144, 4
  1626. 6, 7, 8, 9, // 1148, 4
  1627. 0, 2, 4, 6, 8, 10, // 1152, 6
  1628. 1, 2, 5, 6, 9, 10, // 1158, 6
  1629. 0, 3, 4, 7, 9, 10, // 1164, 6
  1630. 0, 1, 2, 8, 9, 10, // 1170, 6
  1631. 4, 5, 6, 8, 9, 10, // 1176, 6
  1632. 3, 11, // 1182, 2
  1633. 2, 3, 6, 7, 11, // 1184, 5
  1634. 0, 3, 8, 11, // 1189, 4
  1635. 0, 3, 4, 7, 8, 11, // 1193, 6
  1636. 1, 3, 5, 7, 9, 11, // 1199, 6
  1637. 2, 3, 10, 11, // 1205, 4
  1638. 1, 5, 10, 11, // 1209, 4
  1639. 4, 5, 10, 11, // 1213, 4
  1640. 6, 7, 10, 11, // 1217, 4
  1641. 2, 3, 6, 7, 10, 11, // 1221, 6
  1642. 1, 2, 3, 9, 10, 11, // 1227, 6
  1643. 5, 6, 7, 9, 10, 11, // 1233, 6
  1644. 8, 9, 10, 11, // 1239, 4
  1645. 4, 12, // 1243, 2
  1646. 0, 1, 2, 3, 4, 5, 8, 12, // 1245, 8
  1647. 8, 9, 12, // 1253, 3
  1648. 0, 4, 5, 8, 9, 12, // 1256, 6
  1649. 0, 1, 4, 5, 8, 9, 12, // 1262, 7
  1650. 2, 3, 5, 6, 8, 9, 12, // 1269, 7
  1651. 1, 5, 9, 13, // 1276, 4
  1652. 6, 7, 9, 13, // 1280, 4
  1653. 1, 4, 7, 10, 13, // 1284, 5
  1654. 1, 6, 8, 11, 13, // 1289, 5
  1655. 0, 1, 12, 13, // 1294, 4
  1656. 4, 5, 12, 13, // 1298, 4
  1657. 0, 1, 6, 7, 12, 13, // 1302, 6
  1658. 0, 1, 4, 8, 12, 13, // 1308, 6
  1659. 8, 9, 12, 13, // 1314, 4
  1660. 4, 8, 9, 12, 13, // 1318, 5
  1661. 4, 5, 8, 9, 12, 13, // 1323, 6
  1662. 0, 4, 5, 8, 9, 12, 13, // 1329, 7
  1663. 0, 1, 6, 10, 12, 13, // 1336, 6
  1664. 3, 6, 7, 9, 10, 12, 13, // 1342, 7
  1665. 0, 1, 10, 11, 12, 13, // 1349, 6
  1666. 2, 4, 7, 9, 14, // 1355, 5
  1667. 4, 5, 10, 14, // 1360, 4
  1668. 2, 6, 10, 14, // 1364, 4
  1669. 2, 5, 8, 11, 14, // 1368, 5
  1670. 0, 2, 12, 14, // 1373, 4
  1671. 8, 10, 12, 14, // 1377, 4
  1672. 4, 6, 8, 10, 12, 14, // 1381, 6
  1673. 13, 14, // 1387, 2
  1674. 9, 10, 13, 14, // 1389, 4
  1675. 5, 6, 9, 10, 13, 14, // 1393, 6
  1676. 0, 1, 2, 12, 13, 14, // 1399, 6
  1677. 4, 5, 6, 12, 13, 14, // 1405, 6
  1678. 8, 9, 12, 13, 14, // 1411, 5
  1679. 8, 9, 10, 12, 13, 14, // 1416, 6
  1680. 7, 15, // 1422, 2
  1681. 0, 5, 10, 15, // 1424, 4
  1682. 0, 1, 2, 3, 6, 7, 11, 15, // 1428, 8
  1683. 10, 11, 15, // 1436, 3
  1684. 0, 1, 5, 6, 10, 11, 15, // 1439, 7
  1685. 3, 6, 7, 10, 11, 15, // 1446, 6
  1686. 12, 15, // 1452, 2
  1687. 0, 3, 12, 15, // 1454, 4
  1688. 4, 7, 12, 15, // 1458, 4
  1689. 0, 3, 6, 9, 12, 15, // 1462, 6
  1690. 0, 3, 5, 10, 12, 15, // 1468, 6
  1691. 8, 11, 12, 15, // 1474, 4
  1692. 5, 6, 8, 11, 12, 15, // 1478, 6
  1693. 4, 7, 8, 11, 12, 15, // 1484, 6
  1694. 1, 3, 13, 15, // 1490, 4
  1695. 9, 11, 13, 15, // 1494, 4
  1696. 5, 7, 9, 11, 13, 15, // 1498, 6
  1697. 2, 3, 14, 15, // 1504, 4
  1698. 2, 3, 4, 5, 14, 15, // 1508, 6
  1699. 6, 7, 14, 15, // 1514, 4
  1700. 2, 3, 5, 9, 14, 15, // 1518, 6
  1701. 2, 3, 8, 9, 14, 15, // 1524, 6
  1702. 10, 14, 15, // 1530, 3
  1703. 0, 4, 5, 9, 10, 14, 15, // 1533, 7
  1704. 2, 3, 7, 11, 14, 15, // 1540, 6
  1705. 10, 11, 14, 15, // 1546, 4
  1706. 7, 10, 11, 14, 15, // 1550, 5
  1707. 6, 7, 10, 11, 14, 15, // 1555, 6
  1708. 1, 2, 3, 13, 14, 15, // 1561, 6
  1709. 5, 6, 7, 13, 14, 15, // 1567, 6
  1710. 10, 11, 13, 14, 15, // 1573, 5
  1711. 9, 10, 11, 13, 14, 15, // 1578, 6
  1712. 0, 4, 8, 9, 12, 13, 14, 15, // 1584, 8
  1713. 9, 10, 12, 13, 14, 15, // 1592, 6
  1714. 8, 11, 12, 13, 14, 15, // 1598, 6
  1715. 3, 7, 10, 11, 12, 13, 14, 15, // 1604, 8
  1716. };
  1717. static const int g_shapeRanges[][2] =
  1718. {
  1719. { 0, 16 },{ 16, 4 },{ 20, 3 },{ 23, 4 },{ 27, 3 },{ 30, 4 },{ 34, 8 },{ 42, 4 },{ 46, 6 },{ 52, 8 },{ 60, 5 },
  1720. { 65, 5 },{ 70, 4 },{ 74, 4 },{ 78, 6 },{ 84, 8 },{ 92, 8 },{ 100, 8 },{ 108, 8 },{ 116, 12 },{ 128, 4 },{ 132, 8 },
  1721. { 140, 8 },{ 148, 10 },{ 158, 6 },{ 164, 8 },{ 172, 12 },{ 184, 8 },{ 192, 5 },{ 197, 3 },{ 200, 4 },{ 204, 6 },{ 210, 8 },
  1722. { 218, 8 },{ 226, 8 },{ 234, 8 },{ 242, 8 },{ 250, 12 },{ 262, 13 },{ 275, 8 },{ 283, 8 },{ 291, 10 },{ 301, 8 },{ 309, 8 },
  1723. { 317, 5 },{ 322, 8 },{ 330, 8 },{ 338, 8 },{ 346, 8 },{ 354, 8 },{ 362, 8 },{ 370, 8 },{ 378, 8 },{ 386, 8 },{ 394, 8 },
  1724. { 402, 8 },{ 410, 8 },{ 418, 4 },{ 422, 8 },{ 430, 6 },{ 436, 8 },{ 444, 10 },{ 454, 8 },{ 462, 12 },{ 474, 8 },{ 482, 8 },
  1725. { 490, 4 },{ 494, 8 },{ 502, 6 },{ 508, 8 },{ 516, 10 },{ 526, 8 },{ 534, 12 },{ 546, 8 },{ 554, 8 },{ 562, 8 },{ 570, 8 },
  1726. { 578, 8 },{ 586, 8 },{ 594, 8 },{ 602, 8 },{ 610, 8 },{ 618, 8 },{ 626, 8 },{ 634, 8 },{ 642, 11 },{ 653, 8 },{ 661, 8 },
  1727. { 669, 6 },{ 675, 8 },{ 683, 8 },{ 691, 3 },{ 694, 4 },{ 698, 8 },{ 706, 8 },{ 714, 8 },{ 722, 8 },{ 730, 8 },{ 738, 10 },
  1728. { 748, 12 },{ 760, 13 },{ 773, 11 },{ 784, 8 },{ 792, 4 },{ 796, 8 },{ 804, 10 },{ 814, 6 },{ 820, 8 },{ 828, 8 },{ 836, 12 },
  1729. { 848, 4 },{ 852, 8 },{ 860, 8 },{ 868, 8 },{ 876, 8 },{ 884, 10 },{ 894, 12 },{ 906, 12 },{ 918, 11 },{ 929, 11 },{ 940, 8 },
  1730. { 948, 10 },{ 958, 12 },{ 970, 8 },{ 978, 12 },{ 990, 13 },{ 1003, 12 },{ 1015, 13 },{ 1028, 12 },{ 1040, 2 },{ 1042, 2 },{ 1044, 4 },
  1731. { 1048, 5 },{ 1053, 3 },{ 1056, 4 },{ 1060, 4 },{ 1064, 6 },{ 1070, 6 },{ 1076, 7 },{ 1083, 4 },{ 1087, 6 },{ 1093, 4 },{ 1097, 4 },
  1732. { 1101, 5 },{ 1106, 6 },{ 1112, 7 },{ 1119, 4 },{ 1123, 2 },{ 1125, 5 },{ 1130, 4 },{ 1134, 4 },{ 1138, 6 },{ 1144, 4 },{ 1148, 4 },
  1733. { 1152, 6 },{ 1158, 6 },{ 1164, 6 },{ 1170, 6 },{ 1176, 6 },{ 1182, 2 },{ 1184, 5 },{ 1189, 4 },{ 1193, 6 },{ 1199, 6 },{ 1205, 4 },
  1734. { 1209, 4 },{ 1213, 4 },{ 1217, 4 },{ 1221, 6 },{ 1227, 6 },{ 1233, 6 },{ 1239, 4 },{ 1243, 2 },{ 1245, 8 },{ 1253, 3 },{ 1256, 6 },
  1735. { 1262, 7 },{ 1269, 7 },{ 1276, 4 },{ 1280, 4 },{ 1284, 5 },{ 1289, 5 },{ 1294, 4 },{ 1298, 4 },{ 1302, 6 },{ 1308, 6 },{ 1314, 4 },
  1736. { 1318, 5 },{ 1323, 6 },{ 1329, 7 },{ 1336, 6 },{ 1342, 7 },{ 1349, 6 },{ 1355, 5 },{ 1360, 4 },{ 1364, 4 },{ 1368, 5 },{ 1373, 4 },
  1737. { 1377, 4 },{ 1381, 6 },{ 1387, 2 },{ 1389, 4 },{ 1393, 6 },{ 1399, 6 },{ 1405, 6 },{ 1411, 5 },{ 1416, 6 },{ 1422, 2 },{ 1424, 4 },
  1738. { 1428, 8 },{ 1436, 3 },{ 1439, 7 },{ 1446, 6 },{ 1452, 2 },{ 1454, 4 },{ 1458, 4 },{ 1462, 6 },{ 1468, 6 },{ 1474, 4 },{ 1478, 6 },
  1739. { 1484, 6 },{ 1490, 4 },{ 1494, 4 },{ 1498, 6 },{ 1504, 4 },{ 1508, 6 },{ 1514, 4 },{ 1518, 6 },{ 1524, 6 },{ 1530, 3 },{ 1533, 7 },
  1740. { 1540, 6 },{ 1546, 4 },{ 1550, 5 },{ 1555, 6 },{ 1561, 6 },{ 1567, 6 },{ 1573, 5 },{ 1578, 6 },{ 1584, 8 },{ 1592, 6 },{ 1598, 6 },
  1741. { 1604, 8 },
  1742. };
  1743. static const int g_shapes1[][2] =
  1744. {
  1745. { 0, 16 }
  1746. };
  1747. static const int g_shapes2[64][2] =
  1748. {
  1749. { 33, 96 },{ 63, 66 },{ 20, 109 },{ 22, 107 },{ 37, 92 },{ 7, 122 },{ 8, 121 },{ 23, 106 },
  1750. { 38, 91 },{ 2, 127 },{ 9, 120 },{ 26, 103 },{ 3, 126 },{ 6, 123 },{ 1, 128 },{ 19, 110 },
  1751. { 15, 114 },{ 124, 5 },{ 72, 57 },{ 115, 14 },{ 125, 4 },{ 70, 59 },{ 100, 29 },{ 60, 69 },
  1752. { 116, 13 },{ 99, 30 },{ 78, 51 },{ 94, 35 },{ 104, 25 },{ 111, 18 },{ 71, 58 },{ 90, 39 },
  1753. { 45, 84 },{ 16, 113 },{ 82, 47 },{ 95, 34 },{ 87, 42 },{ 83, 46 },{ 53, 76 },{ 48, 81 },
  1754. { 68, 61 },{ 105, 24 },{ 98, 31 },{ 88, 41 },{ 75, 54 },{ 43, 86 },{ 52, 77 },{ 117, 12 },
  1755. { 119, 10 },{ 118, 11 },{ 85, 44 },{ 101, 28 },{ 36, 93 },{ 55, 74 },{ 89, 40 },{ 79, 50 },
  1756. { 56, 73 },{ 49, 80 },{ 64, 65 },{ 27, 102 },{ 32, 97 },{ 112, 17 },{ 67, 62 },{ 21, 108 },
  1757. };
  1758. static const int g_shapes3[64][3] =
  1759. {
  1760. { 148, 160, 240 },{ 132, 212, 205 },{ 136, 233, 187 },{ 175, 237, 143 },{ 6, 186, 232 },{ 33, 142, 232 },{ 131, 123, 142 },{ 131, 96, 186 },
  1761. { 6, 171, 110 },{ 1, 18, 110 },{ 1, 146, 123 },{ 33, 195, 66 },{ 20, 51, 66 },{ 20, 178, 96 },{ 2, 177, 106 },{ 211, 4, 59 },
  1762. { 8, 191, 91 },{ 230, 14, 29 },{ 1, 188, 234 },{ 151, 110, 168 },{ 20, 144, 238 },{ 137, 66, 206 },{ 173, 179, 232 },{ 209, 194, 186 },
  1763. { 239, 165, 142 },{ 131, 152, 242 },{ 214, 54, 12 },{ 140, 219, 201 },{ 190, 150, 231 },{ 156, 135, 241 },{ 185, 227, 167 },{ 145, 210, 59 },
  1764. { 138, 174, 106 },{ 189, 229, 14 },{ 176, 133, 106 },{ 78, 178, 195 },{ 111, 146, 171 },{ 216, 180, 196 },{ 217, 181, 193 },{ 184, 228, 166 },
  1765. { 192, 225, 153 },{ 134, 141, 123 },{ 6, 222, 198 },{ 149, 183, 96 },{ 33, 226, 164 },{ 161, 215, 51 },{ 197, 221, 18 },{ 1, 223, 199 },
  1766. { 154, 163, 110 },{ 20, 236, 169 },{ 157, 204, 66 },{ 1, 202, 220 },{ 20, 170, 235 },{ 203, 158, 66 },{ 162, 155, 110 },{ 6, 201, 218 },
  1767. { 139, 135, 123 },{ 33, 167, 224 },{ 182, 150, 96 },{ 19, 200, 213 },{ 63, 207, 159 },{ 147, 172, 109 },{ 129, 130, 128 },{ 208, 14, 59 },
  1768. };
  1769. static const int g_shapeList1[] =
  1770. {
  1771. 0,
  1772. };
  1773. static const int g_shapeList1Collapse[] =
  1774. {
  1775. 0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1776. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1777. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1778. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1779. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1780. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1781. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1782. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1783. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1784. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1785. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1786. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1787. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1788. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1789. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1790. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1791. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1792. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1793. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1794. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1795. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1796. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1797. -1,
  1798. };
  1799. static const int g_shapeList2[] =
  1800. {
  1801. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
  1802. 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
  1803. 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
  1804. 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44,
  1805. 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55,
  1806. 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66,
  1807. 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77,
  1808. 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88,
  1809. 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99,
  1810. 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110,
  1811. 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121,
  1812. 122, 123, 124, 125, 126, 127, 128,
  1813. };
  1814. static const int g_shapeList2Collapse[] =
  1815. {
  1816. -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  1817. 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
  1818. 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
  1819. 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42,
  1820. 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53,
  1821. 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64,
  1822. 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75,
  1823. 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86,
  1824. 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97,
  1825. 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108,
  1826. 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119,
  1827. 120, 121, 122, 123, 124, 125, 126, 127, -1, -1, -1,
  1828. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1829. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1830. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1831. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1832. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1833. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1834. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1835. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1836. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1837. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1838. -1,
  1839. };
  1840. static const int g_shapeList12[] =
  1841. {
  1842. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
  1843. 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
  1844. 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
  1845. 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43,
  1846. 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54,
  1847. 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65,
  1848. 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76,
  1849. 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87,
  1850. 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98,
  1851. 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109,
  1852. 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120,
  1853. 121, 122, 123, 124, 125, 126, 127, 128,
  1854. };
  1855. static const int g_shapeList12Collapse[] =
  1856. {
  1857. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
  1858. 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
  1859. 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
  1860. 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43,
  1861. 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54,
  1862. 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65,
  1863. 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76,
  1864. 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87,
  1865. 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98,
  1866. 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109,
  1867. 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120,
  1868. 121, 122, 123, 124, 125, 126, 127, 128, -1, -1, -1,
  1869. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1870. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1871. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1872. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1873. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1874. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1875. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1876. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1877. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1878. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1879. -1,
  1880. };
  1881. static const int g_shapeList3[] =
  1882. {
  1883. 1, 2, 4, 6, 8, 12, 14, 18, 19, 20, 29,
  1884. 33, 51, 54, 59, 63, 66, 78, 91, 96, 106, 109,
  1885. 110, 111, 123, 128, 129, 130, 131, 132, 133, 134, 135,
  1886. 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146,
  1887. 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157,
  1888. 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168,
  1889. 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179,
  1890. 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190,
  1891. 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201,
  1892. 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212,
  1893. 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223,
  1894. 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234,
  1895. 235, 236, 237, 238, 239, 240, 241, 242,
  1896. };
  1897. static const int g_shapeList3Collapse[] =
  1898. {
  1899. -1, 0, 1, -1, 2, -1, 3, -1, 4, -1, -1,
  1900. -1, 5, -1, 6, -1, -1, -1, 7, 8, 9, -1,
  1901. -1, -1, -1, -1, -1, -1, -1, 10, -1, -1, -1,
  1902. 11, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1903. -1, -1, -1, -1, -1, -1, -1, 12, -1, -1, 13,
  1904. -1, -1, -1, -1, 14, -1, -1, -1, 15, -1, -1,
  1905. 16, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1906. -1, 17, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1907. -1, -1, -1, 18, -1, -1, -1, -1, 19, -1, -1,
  1908. -1, -1, -1, -1, -1, -1, -1, 20, -1, -1, 21,
  1909. 22, 23, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1910. -1, -1, 24, -1, -1, -1, -1, 25, 26, 27, 28,
  1911. 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
  1912. 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
  1913. 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,
  1914. 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72,
  1915. 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83,
  1916. 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94,
  1917. 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105,
  1918. 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116,
  1919. 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127,
  1920. 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138,
  1921. 139,
  1922. };
  1923. static const int g_shapeList3Short[] =
  1924. {
  1925. 1, 2, 4, 6, 18, 20, 33, 51, 59, 66, 96,
  1926. 106, 110, 123, 131, 132, 136, 142, 143, 146, 148, 160,
  1927. 171, 175, 177, 178, 186, 187, 195, 205, 211, 212, 232,
  1928. 233, 237, 240,
  1929. };
  1930. static const int g_shapeList3ShortCollapse[] =
  1931. {
  1932. -1, 0, 1, -1, 2, -1, 3, -1, -1, -1, -1,
  1933. -1, -1, -1, -1, -1, -1, -1, 4, -1, 5, -1,
  1934. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1935. 6, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1936. -1, -1, -1, -1, -1, -1, -1, 7, -1, -1, -1,
  1937. -1, -1, -1, -1, 8, -1, -1, -1, -1, -1, -1,
  1938. 9, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1939. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1940. -1, -1, -1, -1, -1, -1, -1, -1, 10, -1, -1,
  1941. -1, -1, -1, -1, -1, -1, -1, 11, -1, -1, -1,
  1942. 12, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1943. -1, -1, 13, -1, -1, -1, -1, -1, -1, -1, 14,
  1944. 15, -1, -1, -1, 16, -1, -1, -1, -1, -1, 17,
  1945. 18, -1, -1, 19, -1, 20, -1, -1, -1, -1, -1,
  1946. -1, -1, -1, -1, -1, -1, 21, -1, -1, -1, -1,
  1947. -1, -1, -1, -1, -1, -1, 22, -1, -1, -1, 23,
  1948. -1, 24, 25, -1, -1, -1, -1, -1, -1, -1, 26,
  1949. 27, -1, -1, -1, -1, -1, -1, -1, 28, -1, -1,
  1950. -1, -1, -1, -1, -1, -1, -1, 29, -1, -1, -1,
  1951. -1, -1, 30, 31, -1, -1, -1, -1, -1, -1, -1,
  1952. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  1953. -1, 32, 33, -1, -1, -1, 34, -1, -1, 35, -1,
  1954. -1,
  1955. };
  1956. static const int g_shapeListAll[] =
  1957. {
  1958. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
  1959. 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
  1960. 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
  1961. 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43,
  1962. 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54,
  1963. 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65,
  1964. 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76,
  1965. 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87,
  1966. 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98,
  1967. 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109,
  1968. 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120,
  1969. 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131,
  1970. 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142,
  1971. 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153,
  1972. 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164,
  1973. 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175,
  1974. 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186,
  1975. 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197,
  1976. 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208,
  1977. 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219,
  1978. 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230,
  1979. 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241,
  1980. 242,
  1981. };
  1982. static const int g_numShapes1 = sizeof(g_shapeList1) / sizeof(g_shapeList1[0]);
  1983. static const int g_numShapes2 = sizeof(g_shapeList2) / sizeof(g_shapeList2[0]);
  1984. static const int g_numShapes12 = sizeof(g_shapeList12) / sizeof(g_shapeList12[0]);
  1985. static const int g_numShapes3 = sizeof(g_shapeList3) / sizeof(g_shapeList3[0]);
  1986. static const int g_numShapes3Short = sizeof(g_shapeList3Short) / sizeof(g_shapeList3Short[0]);
  1987. static const int g_numShapesAll = sizeof(g_shapeListAll) / sizeof(g_shapeListAll[0]);
  1988. static const int g_numFragments = sizeof(g_fragments) / sizeof(g_fragments[0]);
  1989. static const int g_maxFragmentsPerMode = (g_numShapes2 > g_numShapes3) ? g_numShapes2 : g_numShapes3;
  1990. }
  1991. namespace BC6HData
  1992. {
  1993. enum EField
  1994. {
  1995. NA, // N/A
  1996. M, // Mode
  1997. D, // Shape
  1998. RW,
  1999. RX,
  2000. RY,
  2001. RZ,
  2002. GW,
  2003. GX,
  2004. GY,
  2005. GZ,
  2006. BW,
  2007. BX,
  2008. BY,
  2009. BZ,
  2010. };
  2011. struct ModeDescriptor
  2012. {
  2013. EField m_eField;
  2014. uint8_t m_uBit;
  2015. };
  2016. const ModeDescriptor g_modeDescriptors[14][82] =
  2017. {
  2018. { // Mode 1 (0x00) - 10 5 5 5
  2019. { M, 0 },{ M, 1 },{ GY, 4 },{ BY, 4 },{ BZ, 4 },{ RW, 0 },{ RW, 1 },{ RW, 2 },{ RW, 3 },{ RW, 4 },
  2020. { RW, 5 },{ RW, 6 },{ RW, 7 },{ RW, 8 },{ RW, 9 },{ GW, 0 },{ GW, 1 },{ GW, 2 },{ GW, 3 },{ GW, 4 },
  2021. { GW, 5 },{ GW, 6 },{ GW, 7 },{ GW, 8 },{ GW, 9 },{ BW, 0 },{ BW, 1 },{ BW, 2 },{ BW, 3 },{ BW, 4 },
  2022. { BW, 5 },{ BW, 6 },{ BW, 7 },{ BW, 8 },{ BW, 9 },{ RX, 0 },{ RX, 1 },{ RX, 2 },{ RX, 3 },{ RX, 4 },
  2023. { GZ, 4 },{ GY, 0 },{ GY, 1 },{ GY, 2 },{ GY, 3 },{ GX, 0 },{ GX, 1 },{ GX, 2 },{ GX, 3 },{ GX, 4 },
  2024. { BZ, 0 },{ GZ, 0 },{ GZ, 1 },{ GZ, 2 },{ GZ, 3 },{ BX, 0 },{ BX, 1 },{ BX, 2 },{ BX, 3 },{ BX, 4 },
  2025. { BZ, 1 },{ BY, 0 },{ BY, 1 },{ BY, 2 },{ BY, 3 },{ RY, 0 },{ RY, 1 },{ RY, 2 },{ RY, 3 },{ RY, 4 },
  2026. { BZ, 2 },{ RZ, 0 },{ RZ, 1 },{ RZ, 2 },{ RZ, 3 },{ RZ, 4 },{ BZ, 3 },{ D, 0 },{ D, 1 },{ D, 2 },
  2027. { D, 3 },{ D, 4 },
  2028. },
  2029. { // Mode 2 (0x01) - 7 6 6 6
  2030. { M, 0 },{ M, 1 },{ GY, 5 },{ GZ, 4 },{ GZ, 5 },{ RW, 0 },{ RW, 1 },{ RW, 2 },{ RW, 3 },{ RW, 4 },
  2031. { RW, 5 },{ RW, 6 },{ BZ, 0 },{ BZ, 1 },{ BY, 4 },{ GW, 0 },{ GW, 1 },{ GW, 2 },{ GW, 3 },{ GW, 4 },
  2032. { GW, 5 },{ GW, 6 },{ BY, 5 },{ BZ, 2 },{ GY, 4 },{ BW, 0 },{ BW, 1 },{ BW, 2 },{ BW, 3 },{ BW, 4 },
  2033. { BW, 5 },{ BW, 6 },{ BZ, 3 },{ BZ, 5 },{ BZ, 4 },{ RX, 0 },{ RX, 1 },{ RX, 2 },{ RX, 3 },{ RX, 4 },
  2034. { RX, 5 },{ GY, 0 },{ GY, 1 },{ GY, 2 },{ GY, 3 },{ GX, 0 },{ GX, 1 },{ GX, 2 },{ GX, 3 },{ GX, 4 },
  2035. { GX, 5 },{ GZ, 0 },{ GZ, 1 },{ GZ, 2 },{ GZ, 3 },{ BX, 0 },{ BX, 1 },{ BX, 2 },{ BX, 3 },{ BX, 4 },
  2036. { BX, 5 },{ BY, 0 },{ BY, 1 },{ BY, 2 },{ BY, 3 },{ RY, 0 },{ RY, 1 },{ RY, 2 },{ RY, 3 },{ RY, 4 },
  2037. { RY, 5 },{ RZ, 0 },{ RZ, 1 },{ RZ, 2 },{ RZ, 3 },{ RZ, 4 },{ RZ, 5 },{ D, 0 },{ D, 1 },{ D, 2 },
  2038. { D, 3 },{ D, 4 },
  2039. },
  2040. { // Mode 3 (0x02) - 11 5 4 4
  2041. { M, 0 },{ M, 1 },{ M, 2 },{ M, 3 },{ M, 4 },{ RW, 0 },{ RW, 1 },{ RW, 2 },{ RW, 3 },{ RW, 4 },
  2042. { RW, 5 },{ RW, 6 },{ RW, 7 },{ RW, 8 },{ RW, 9 },{ GW, 0 },{ GW, 1 },{ GW, 2 },{ GW, 3 },{ GW, 4 },
  2043. { GW, 5 },{ GW, 6 },{ GW, 7 },{ GW, 8 },{ GW, 9 },{ BW, 0 },{ BW, 1 },{ BW, 2 },{ BW, 3 },{ BW, 4 },
  2044. { BW, 5 },{ BW, 6 },{ BW, 7 },{ BW, 8 },{ BW, 9 },{ RX, 0 },{ RX, 1 },{ RX, 2 },{ RX, 3 },{ RX, 4 },
  2045. { RW,10 },{ GY, 0 },{ GY, 1 },{ GY, 2 },{ GY, 3 },{ GX, 0 },{ GX, 1 },{ GX, 2 },{ GX, 3 },{ GW,10 },
  2046. { BZ, 0 },{ GZ, 0 },{ GZ, 1 },{ GZ, 2 },{ GZ, 3 },{ BX, 0 },{ BX, 1 },{ BX, 2 },{ BX, 3 },{ BW,10 },
  2047. { BZ, 1 },{ BY, 0 },{ BY, 1 },{ BY, 2 },{ BY, 3 },{ RY, 0 },{ RY, 1 },{ RY, 2 },{ RY, 3 },{ RY, 4 },
  2048. { BZ, 2 },{ RZ, 0 },{ RZ, 1 },{ RZ, 2 },{ RZ, 3 },{ RZ, 4 },{ BZ, 3 },{ D, 0 },{ D, 1 },{ D, 2 },
  2049. { D, 3 },{ D, 4 },
  2050. },
  2051. { // Mode 4 (0x06) - 11 4 5 4
  2052. { M, 0 },{ M, 1 },{ M, 2 },{ M, 3 },{ M, 4 },{ RW, 0 },{ RW, 1 },{ RW, 2 },{ RW, 3 },{ RW, 4 },
  2053. { RW, 5 },{ RW, 6 },{ RW, 7 },{ RW, 8 },{ RW, 9 },{ GW, 0 },{ GW, 1 },{ GW, 2 },{ GW, 3 },{ GW, 4 },
  2054. { GW, 5 },{ GW, 6 },{ GW, 7 },{ GW, 8 },{ GW, 9 },{ BW, 0 },{ BW, 1 },{ BW, 2 },{ BW, 3 },{ BW, 4 },
  2055. { BW, 5 },{ BW, 6 },{ BW, 7 },{ BW, 8 },{ BW, 9 },{ RX, 0 },{ RX, 1 },{ RX, 2 },{ RX, 3 },{ RW,10 },
  2056. { GZ, 4 },{ GY, 0 },{ GY, 1 },{ GY, 2 },{ GY, 3 },{ GX, 0 },{ GX, 1 },{ GX, 2 },{ GX, 3 },{ GX, 4 },
  2057. { GW,10 },{ GZ, 0 },{ GZ, 1 },{ GZ, 2 },{ GZ, 3 },{ BX, 0 },{ BX, 1 },{ BX, 2 },{ BX, 3 },{ BW,10 },
  2058. { BZ, 1 },{ BY, 0 },{ BY, 1 },{ BY, 2 },{ BY, 3 },{ RY, 0 },{ RY, 1 },{ RY, 2 },{ RY, 3 },{ BZ, 0 },
  2059. { BZ, 2 },{ RZ, 0 },{ RZ, 1 },{ RZ, 2 },{ RZ, 3 },{ GY, 4 },{ BZ, 3 },{ D, 0 },{ D, 1 },{ D, 2 },
  2060. { D, 3 },{ D, 4 },
  2061. },
  2062. { // Mode 5 (0x0a) - 11 4 4 5
  2063. { M, 0 },{ M, 1 },{ M, 2 },{ M, 3 },{ M, 4 },{ RW, 0 },{ RW, 1 },{ RW, 2 },{ RW, 3 },{ RW, 4 },
  2064. { RW, 5 },{ RW, 6 },{ RW, 7 },{ RW, 8 },{ RW, 9 },{ GW, 0 },{ GW, 1 },{ GW, 2 },{ GW, 3 },{ GW, 4 },
  2065. { GW, 5 },{ GW, 6 },{ GW, 7 },{ GW, 8 },{ GW, 9 },{ BW, 0 },{ BW, 1 },{ BW, 2 },{ BW, 3 },{ BW, 4 },
  2066. { BW, 5 },{ BW, 6 },{ BW, 7 },{ BW, 8 },{ BW, 9 },{ RX, 0 },{ RX, 1 },{ RX, 2 },{ RX, 3 },{ RW,10 },
  2067. { BY, 4 },{ GY, 0 },{ GY, 1 },{ GY, 2 },{ GY, 3 },{ GX, 0 },{ GX, 1 },{ GX, 2 },{ GX, 3 },{ GW,10 },
  2068. { BZ, 0 },{ GZ, 0 },{ GZ, 1 },{ GZ, 2 },{ GZ, 3 },{ BX, 0 },{ BX, 1 },{ BX, 2 },{ BX, 3 },{ BX, 4 },
  2069. { BW,10 },{ BY, 0 },{ BY, 1 },{ BY, 2 },{ BY, 3 },{ RY, 0 },{ RY, 1 },{ RY, 2 },{ RY, 3 },{ BZ, 1 },
  2070. { BZ, 2 },{ RZ, 0 },{ RZ, 1 },{ RZ, 2 },{ RZ, 3 },{ BZ, 4 },{ BZ, 3 },{ D, 0 },{ D, 1 },{ D, 2 },
  2071. { D, 3 },{ D, 4 },
  2072. },
  2073. { // Mode 6 (0x0e) - 9 5 5 5
  2074. { M, 0 },{ M, 1 },{ M, 2 },{ M, 3 },{ M, 4 },{ RW, 0 },{ RW, 1 },{ RW, 2 },{ RW, 3 },{ RW, 4 },
  2075. { RW, 5 },{ RW, 6 },{ RW, 7 },{ RW, 8 },{ BY, 4 },{ GW, 0 },{ GW, 1 },{ GW, 2 },{ GW, 3 },{ GW, 4 },
  2076. { GW, 5 },{ GW, 6 },{ GW, 7 },{ GW, 8 },{ GY, 4 },{ BW, 0 },{ BW, 1 },{ BW, 2 },{ BW, 3 },{ BW, 4 },
  2077. { BW, 5 },{ BW, 6 },{ BW, 7 },{ BW, 8 },{ BZ, 4 },{ RX, 0 },{ RX, 1 },{ RX, 2 },{ RX, 3 },{ RX, 4 },
  2078. { GZ, 4 },{ GY, 0 },{ GY, 1 },{ GY, 2 },{ GY, 3 },{ GX, 0 },{ GX, 1 },{ GX, 2 },{ GX, 3 },{ GX, 4 },
  2079. { BZ, 0 },{ GZ, 0 },{ GZ, 1 },{ GZ, 2 },{ GZ, 3 },{ BX, 0 },{ BX, 1 },{ BX, 2 },{ BX, 3 },{ BX, 4 },
  2080. { BZ, 1 },{ BY, 0 },{ BY, 1 },{ BY, 2 },{ BY, 3 },{ RY, 0 },{ RY, 1 },{ RY, 2 },{ RY, 3 },{ RY, 4 },
  2081. { BZ, 2 },{ RZ, 0 },{ RZ, 1 },{ RZ, 2 },{ RZ, 3 },{ RZ, 4 },{ BZ, 3 },{ D, 0 },{ D, 1 },{ D, 2 },
  2082. { D, 3 },{ D, 4 },
  2083. },
  2084. { // Mode 7 (0x12) - 8 6 5 5
  2085. { M, 0 },{ M, 1 },{ M, 2 },{ M, 3 },{ M, 4 },{ RW, 0 },{ RW, 1 },{ RW, 2 },{ RW, 3 },{ RW, 4 },
  2086. { RW, 5 },{ RW, 6 },{ RW, 7 },{ GZ, 4 },{ BY, 4 },{ GW, 0 },{ GW, 1 },{ GW, 2 },{ GW, 3 },{ GW, 4 },
  2087. { GW, 5 },{ GW, 6 },{ GW, 7 },{ BZ, 2 },{ GY, 4 },{ BW, 0 },{ BW, 1 },{ BW, 2 },{ BW, 3 },{ BW, 4 },
  2088. { BW, 5 },{ BW, 6 },{ BW, 7 },{ BZ, 3 },{ BZ, 4 },{ RX, 0 },{ RX, 1 },{ RX, 2 },{ RX, 3 },{ RX, 4 },
  2089. { RX, 5 },{ GY, 0 },{ GY, 1 },{ GY, 2 },{ GY, 3 },{ GX, 0 },{ GX, 1 },{ GX, 2 },{ GX, 3 },{ GX, 4 },
  2090. { BZ, 0 },{ GZ, 0 },{ GZ, 1 },{ GZ, 2 },{ GZ, 3 },{ BX, 0 },{ BX, 1 },{ BX, 2 },{ BX, 3 },{ BX, 4 },
  2091. { BZ, 1 },{ BY, 0 },{ BY, 1 },{ BY, 2 },{ BY, 3 },{ RY, 0 },{ RY, 1 },{ RY, 2 },{ RY, 3 },{ RY, 4 },
  2092. { RY, 5 },{ RZ, 0 },{ RZ, 1 },{ RZ, 2 },{ RZ, 3 },{ RZ, 4 },{ RZ, 5 },{ D, 0 },{ D, 1 },{ D, 2 },
  2093. { D, 3 },{ D, 4 },
  2094. },
  2095. { // Mode 8 (0x16) - 8 5 6 5
  2096. { M, 0 },{ M, 1 },{ M, 2 },{ M, 3 },{ M, 4 },{ RW, 0 },{ RW, 1 },{ RW, 2 },{ RW, 3 },{ RW, 4 },
  2097. { RW, 5 },{ RW, 6 },{ RW, 7 },{ BZ, 0 },{ BY, 4 },{ GW, 0 },{ GW, 1 },{ GW, 2 },{ GW, 3 },{ GW, 4 },
  2098. { GW, 5 },{ GW, 6 },{ GW, 7 },{ GY, 5 },{ GY, 4 },{ BW, 0 },{ BW, 1 },{ BW, 2 },{ BW, 3 },{ BW, 4 },
  2099. { BW, 5 },{ BW, 6 },{ BW, 7 },{ GZ, 5 },{ BZ, 4 },{ RX, 0 },{ RX, 1 },{ RX, 2 },{ RX, 3 },{ RX, 4 },
  2100. { GZ, 4 },{ GY, 0 },{ GY, 1 },{ GY, 2 },{ GY, 3 },{ GX, 0 },{ GX, 1 },{ GX, 2 },{ GX, 3 },{ GX, 4 },
  2101. { GX, 5 },{ GZ, 0 },{ GZ, 1 },{ GZ, 2 },{ GZ, 3 },{ BX, 0 },{ BX, 1 },{ BX, 2 },{ BX, 3 },{ BX, 4 },
  2102. { BZ, 1 },{ BY, 0 },{ BY, 1 },{ BY, 2 },{ BY, 3 },{ RY, 0 },{ RY, 1 },{ RY, 2 },{ RY, 3 },{ RY, 4 },
  2103. { BZ, 2 },{ RZ, 0 },{ RZ, 1 },{ RZ, 2 },{ RZ, 3 },{ RZ, 4 },{ BZ, 3 },{ D, 0 },{ D, 1 },{ D, 2 },
  2104. { D, 3 },{ D, 4 },
  2105. },
  2106. { // Mode 9 (0x1a) - 8 5 5 6
  2107. { M, 0 },{ M, 1 },{ M, 2 },{ M, 3 },{ M, 4 },{ RW, 0 },{ RW, 1 },{ RW, 2 },{ RW, 3 },{ RW, 4 },
  2108. { RW, 5 },{ RW, 6 },{ RW, 7 },{ BZ, 1 },{ BY, 4 },{ GW, 0 },{ GW, 1 },{ GW, 2 },{ GW, 3 },{ GW, 4 },
  2109. { GW, 5 },{ GW, 6 },{ GW, 7 },{ BY, 5 },{ GY, 4 },{ BW, 0 },{ BW, 1 },{ BW, 2 },{ BW, 3 },{ BW, 4 },
  2110. { BW, 5 },{ BW, 6 },{ BW, 7 },{ BZ, 5 },{ BZ, 4 },{ RX, 0 },{ RX, 1 },{ RX, 2 },{ RX, 3 },{ RX, 4 },
  2111. { GZ, 4 },{ GY, 0 },{ GY, 1 },{ GY, 2 },{ GY, 3 },{ GX, 0 },{ GX, 1 },{ GX, 2 },{ GX, 3 },{ GX, 4 },
  2112. { BZ, 0 },{ GZ, 0 },{ GZ, 1 },{ GZ, 2 },{ GZ, 3 },{ BX, 0 },{ BX, 1 },{ BX, 2 },{ BX, 3 },{ BX, 4 },
  2113. { BX, 5 },{ BY, 0 },{ BY, 1 },{ BY, 2 },{ BY, 3 },{ RY, 0 },{ RY, 1 },{ RY, 2 },{ RY, 3 },{ RY, 4 },
  2114. { BZ, 2 },{ RZ, 0 },{ RZ, 1 },{ RZ, 2 },{ RZ, 3 },{ RZ, 4 },{ BZ, 3 },{ D, 0 },{ D, 1 },{ D, 2 },
  2115. { D, 3 },{ D, 4 },
  2116. },
  2117. { // Mode 10 (0x1e) - 6 6 6 6
  2118. { M, 0 },{ M, 1 },{ M, 2 },{ M, 3 },{ M, 4 },{ RW, 0 },{ RW, 1 },{ RW, 2 },{ RW, 3 },{ RW, 4 },
  2119. { RW, 5 },{ GZ, 4 },{ BZ, 0 },{ BZ, 1 },{ BY, 4 },{ GW, 0 },{ GW, 1 },{ GW, 2 },{ GW, 3 },{ GW, 4 },
  2120. { GW, 5 },{ GY, 5 },{ BY, 5 },{ BZ, 2 },{ GY, 4 },{ BW, 0 },{ BW, 1 },{ BW, 2 },{ BW, 3 },{ BW, 4 },
  2121. { BW, 5 },{ GZ, 5 },{ BZ, 3 },{ BZ, 5 },{ BZ, 4 },{ RX, 0 },{ RX, 1 },{ RX, 2 },{ RX, 3 },{ RX, 4 },
  2122. { RX, 5 },{ GY, 0 },{ GY, 1 },{ GY, 2 },{ GY, 3 },{ GX, 0 },{ GX, 1 },{ GX, 2 },{ GX, 3 },{ GX, 4 },
  2123. { GX, 5 },{ GZ, 0 },{ GZ, 1 },{ GZ, 2 },{ GZ, 3 },{ BX, 0 },{ BX, 1 },{ BX, 2 },{ BX, 3 },{ BX, 4 },
  2124. { BX, 5 },{ BY, 0 },{ BY, 1 },{ BY, 2 },{ BY, 3 },{ RY, 0 },{ RY, 1 },{ RY, 2 },{ RY, 3 },{ RY, 4 },
  2125. { RY, 5 },{ RZ, 0 },{ RZ, 1 },{ RZ, 2 },{ RZ, 3 },{ RZ, 4 },{ RZ, 5 },{ D, 0 },{ D, 1 },{ D, 2 },
  2126. { D, 3 },{ D, 4 },
  2127. },
  2128. { // Mode 11 (0x03) - 10 10
  2129. { M, 0 },{ M, 1 },{ M, 2 },{ M, 3 },{ M, 4 },{ RW, 0 },{ RW, 1 },{ RW, 2 },{ RW, 3 },{ RW, 4 },
  2130. { RW, 5 },{ RW, 6 },{ RW, 7 },{ RW, 8 },{ RW, 9 },{ GW, 0 },{ GW, 1 },{ GW, 2 },{ GW, 3 },{ GW, 4 },
  2131. { GW, 5 },{ GW, 6 },{ GW, 7 },{ GW, 8 },{ GW, 9 },{ BW, 0 },{ BW, 1 },{ BW, 2 },{ BW, 3 },{ BW, 4 },
  2132. { BW, 5 },{ BW, 6 },{ BW, 7 },{ BW, 8 },{ BW, 9 },{ RX, 0 },{ RX, 1 },{ RX, 2 },{ RX, 3 },{ RX, 4 },
  2133. { RX, 5 },{ RX, 6 },{ RX, 7 },{ RX, 8 },{ RX, 9 },{ GX, 0 },{ GX, 1 },{ GX, 2 },{ GX, 3 },{ GX, 4 },
  2134. { GX, 5 },{ GX, 6 },{ GX, 7 },{ GX, 8 },{ GX, 9 },{ BX, 0 },{ BX, 1 },{ BX, 2 },{ BX, 3 },{ BX, 4 },
  2135. { BX, 5 },{ BX, 6 },{ BX, 7 },{ BX, 8 },{ BX, 9 },{ NA, 0 },{ NA, 0 },{ NA, 0 },{ NA, 0 },{ NA, 0 },
  2136. { NA, 0 },{ NA, 0 },{ NA, 0 },{ NA, 0 },{ NA, 0 },{ NA, 0 },{ NA, 0 },{ NA, 0 },{ NA, 0 },{ NA, 0 },
  2137. { NA, 0 },{ NA, 0 },
  2138. },
  2139. { // Mode 12 (0x07) - 11 9
  2140. { M, 0 },{ M, 1 },{ M, 2 },{ M, 3 },{ M, 4 },{ RW, 0 },{ RW, 1 },{ RW, 2 },{ RW, 3 },{ RW, 4 },
  2141. { RW, 5 },{ RW, 6 },{ RW, 7 },{ RW, 8 },{ RW, 9 },{ GW, 0 },{ GW, 1 },{ GW, 2 },{ GW, 3 },{ GW, 4 },
  2142. { GW, 5 },{ GW, 6 },{ GW, 7 },{ GW, 8 },{ GW, 9 },{ BW, 0 },{ BW, 1 },{ BW, 2 },{ BW, 3 },{ BW, 4 },
  2143. { BW, 5 },{ BW, 6 },{ BW, 7 },{ BW, 8 },{ BW, 9 },{ RX, 0 },{ RX, 1 },{ RX, 2 },{ RX, 3 },{ RX, 4 },
  2144. { RX, 5 },{ RX, 6 },{ RX, 7 },{ RX, 8 },{ RW,10 },{ GX, 0 },{ GX, 1 },{ GX, 2 },{ GX, 3 },{ GX, 4 },
  2145. { GX, 5 },{ GX, 6 },{ GX, 7 },{ GX, 8 },{ GW,10 },{ BX, 0 },{ BX, 1 },{ BX, 2 },{ BX, 3 },{ BX, 4 },
  2146. { BX, 5 },{ BX, 6 },{ BX, 7 },{ BX, 8 },{ BW,10 },{ NA, 0 },{ NA, 0 },{ NA, 0 },{ NA, 0 },{ NA, 0 },
  2147. { NA, 0 },{ NA, 0 },{ NA, 0 },{ NA, 0 },{ NA, 0 },{ NA, 0 },{ NA, 0 },{ NA, 0 },{ NA, 0 },{ NA, 0 },
  2148. { NA, 0 },{ NA, 0 },
  2149. },
  2150. { // Mode 13 (0x0b) - 12 8
  2151. { M, 0 },{ M, 1 },{ M, 2 },{ M, 3 },{ M, 4 },{ RW, 0 },{ RW, 1 },{ RW, 2 },{ RW, 3 },{ RW, 4 },
  2152. { RW, 5 },{ RW, 6 },{ RW, 7 },{ RW, 8 },{ RW, 9 },{ GW, 0 },{ GW, 1 },{ GW, 2 },{ GW, 3 },{ GW, 4 },
  2153. { GW, 5 },{ GW, 6 },{ GW, 7 },{ GW, 8 },{ GW, 9 },{ BW, 0 },{ BW, 1 },{ BW, 2 },{ BW, 3 },{ BW, 4 },
  2154. { BW, 5 },{ BW, 6 },{ BW, 7 },{ BW, 8 },{ BW, 9 },{ RX, 0 },{ RX, 1 },{ RX, 2 },{ RX, 3 },{ RX, 4 },
  2155. { RX, 5 },{ RX, 6 },{ RX, 7 },{ RW,11 },{ RW,10 },{ GX, 0 },{ GX, 1 },{ GX, 2 },{ GX, 3 },{ GX, 4 },
  2156. { GX, 5 },{ GX, 6 },{ GX, 7 },{ GW,11 },{ GW,10 },{ BX, 0 },{ BX, 1 },{ BX, 2 },{ BX, 3 },{ BX, 4 },
  2157. { BX, 5 },{ BX, 6 },{ BX, 7 },{ BW,11 },{ BW,10 },{ NA, 0 },{ NA, 0 },{ NA, 0 },{ NA, 0 },{ NA, 0 },
  2158. { NA, 0 },{ NA, 0 },{ NA, 0 },{ NA, 0 },{ NA, 0 },{ NA, 0 },{ NA, 0 },{ NA, 0 },{ NA, 0 },{ NA, 0 },
  2159. { NA, 0 },{ NA, 0 },
  2160. },
  2161. { // Mode 14 (0x0f) - 16 4
  2162. { M, 0 },{ M, 1 },{ M, 2 },{ M, 3 },{ M, 4 },{ RW, 0 },{ RW, 1 },{ RW, 2 },{ RW, 3 },{ RW, 4 },
  2163. { RW, 5 },{ RW, 6 },{ RW, 7 },{ RW, 8 },{ RW, 9 },{ GW, 0 },{ GW, 1 },{ GW, 2 },{ GW, 3 },{ GW, 4 },
  2164. { GW, 5 },{ GW, 6 },{ GW, 7 },{ GW, 8 },{ GW, 9 },{ BW, 0 },{ BW, 1 },{ BW, 2 },{ BW, 3 },{ BW, 4 },
  2165. { BW, 5 },{ BW, 6 },{ BW, 7 },{ BW, 8 },{ BW, 9 },{ RX, 0 },{ RX, 1 },{ RX, 2 },{ RX, 3 },{ RW,15 },
  2166. { RW,14 },{ RW,13 },{ RW,12 },{ RW,11 },{ RW,10 },{ GX, 0 },{ GX, 1 },{ GX, 2 },{ GX, 3 },{ GW,15 },
  2167. { GW,14 },{ GW,13 },{ GW,12 },{ GW,11 },{ GW,10 },{ BX, 0 },{ BX, 1 },{ BX, 2 },{ BX, 3 },{ BW,15 },
  2168. { BW,14 },{ BW,13 },{ BW,12 },{ BW,11 },{ BW,10 },{ NA, 0 },{ NA, 0 },{ NA, 0 },{ NA, 0 },{ NA, 0 },
  2169. { NA, 0 },{ NA, 0 },{ NA, 0 },{ NA, 0 },{ NA, 0 },{ NA, 0 },{ NA, 0 },{ NA, 0 },{ NA, 0 },{ NA, 0 },
  2170. { NA, 0 },{ NA, 0 },
  2171. },
  2172. };
  2173. }
  2174. struct PackingVector
  2175. {
  2176. uint32_t m_vector[4];
  2177. int m_offset;
  2178. void Init()
  2179. {
  2180. for (int i = 0; i < 4; i++)
  2181. m_vector[i] = 0;
  2182. m_offset = 0;
  2183. }
  2184. inline void Pack(ParallelMath::ScalarUInt16 value, int bits)
  2185. {
  2186. int vOffset = m_offset >> 5;
  2187. int bitOffset = m_offset & 0x1f;
  2188. m_vector[vOffset] |= (static_cast<uint32_t>(value) << bitOffset) & static_cast<uint32_t>(0xffffffff);
  2189. int overflowBits = bitOffset + bits - 32;
  2190. if (overflowBits > 0)
  2191. m_vector[vOffset + 1] |= (static_cast<uint32_t>(value) >> (bits - overflowBits));
  2192. m_offset += bits;
  2193. }
  2194. inline void Flush(uint8_t* output)
  2195. {
  2196. assert(m_offset == 128);
  2197. for (int v = 0; v < 4; v++)
  2198. {
  2199. uint32_t chunk = m_vector[v];
  2200. for (int b = 0; b < 4; b++)
  2201. output[v * 4 + b] = static_cast<uint8_t>((chunk >> (b * 8)) & 0xff);
  2202. }
  2203. }
  2204. };
  2205. struct UnpackingVector
  2206. {
  2207. uint32_t m_vector[4];
  2208. void Init(const uint8_t *bytes)
  2209. {
  2210. for (int i = 0; i < 4; i++)
  2211. m_vector[i] = 0;
  2212. for (int b = 0; b < 16; b++)
  2213. m_vector[b / 4] |= (bytes[b] << ((b % 4) * 8));
  2214. }
  2215. inline ParallelMath::ScalarUInt16 Unpack(int bits)
  2216. {
  2217. uint32_t bitMask = (1 << bits) - 1;
  2218. ParallelMath::ScalarUInt16 result = static_cast<ParallelMath::ScalarUInt16>(m_vector[0] & bitMask);
  2219. for (int i = 0; i < 4; i++)
  2220. {
  2221. m_vector[i] >>= bits;
  2222. if (i != 3)
  2223. m_vector[i] |= (m_vector[i + 1] & bitMask) << (32 - bits);
  2224. }
  2225. return result;
  2226. }
  2227. };
  2228. void ComputeTweakFactors(int tweak, int range, float *outFactors)
  2229. {
  2230. int totalUnits = range - 1;
  2231. int minOutsideUnits = ((tweak >> 1) & 1);
  2232. int maxOutsideUnits = (tweak & 1);
  2233. int insideUnits = totalUnits - minOutsideUnits - maxOutsideUnits;
  2234. outFactors[0] = -static_cast<float>(minOutsideUnits) / static_cast<float>(insideUnits);
  2235. outFactors[1] = static_cast<float>(maxOutsideUnits) / static_cast<float>(insideUnits) + 1.0f;
  2236. }
  2237. ParallelMath::Float ScaleHDRValue(const ParallelMath::Float &v, bool isSigned)
  2238. {
  2239. if (isSigned)
  2240. {
  2241. ParallelMath::Float offset = ParallelMath::Select(ParallelMath::Less(v, ParallelMath::MakeFloatZero()), ParallelMath::MakeFloat(-30.0f), ParallelMath::MakeFloat(30.0f));
  2242. return (v * 32.0f + offset) / 31.0f;
  2243. }
  2244. else
  2245. return (v * 64.0f + 30.0f) / 31.0f;
  2246. }
  2247. ParallelMath::SInt16 UnscaleHDRValueSigned(const ParallelMath::SInt16 &v)
  2248. {
  2249. #ifdef CVTT_ENABLE_ASSERTS
  2250. for (int i = 0; i < ParallelMath::ParallelSize; i++)
  2251. assert(ParallelMath::Extract(v, i) != -32768)
  2252. #endif
  2253. ParallelMath::Int16CompFlag negative = ParallelMath::Less(v, ParallelMath::MakeSInt16(0));
  2254. ParallelMath::UInt15 absComp = ParallelMath::LosslessCast<ParallelMath::UInt15>::Cast(ParallelMath::Select(negative, ParallelMath::SInt16(ParallelMath::MakeSInt16(0) - v), v));
  2255. ParallelMath::UInt31 multiplied = ParallelMath::XMultiply(absComp, ParallelMath::MakeUInt15(31));
  2256. ParallelMath::UInt31 shifted = ParallelMath::RightShift(multiplied, 5);
  2257. ParallelMath::UInt15 absCompScaled = ParallelMath::ToUInt15(shifted);
  2258. ParallelMath::SInt16 signBits = ParallelMath::SelectOrZero(negative, ParallelMath::MakeSInt16(-32768));
  2259. return ParallelMath::LosslessCast<ParallelMath::SInt16>::Cast(absCompScaled) | signBits;
  2260. }
  2261. ParallelMath::UInt15 UnscaleHDRValueUnsigned(const ParallelMath::UInt16 &v)
  2262. {
  2263. return ParallelMath::ToUInt15(ParallelMath::RightShift(ParallelMath::XMultiply(v, ParallelMath::MakeUInt15(31)), 6));
  2264. }
  2265. void UnscaleHDREndpoints(const ParallelMath::AInt16 inEP[2][3], ParallelMath::AInt16 outEP[2][3], bool isSigned)
  2266. {
  2267. for (int epi = 0; epi < 2; epi++)
  2268. {
  2269. for (int ch = 0; ch < 3; ch++)
  2270. {
  2271. if (isSigned)
  2272. outEP[epi][ch] = ParallelMath::LosslessCast<ParallelMath::AInt16>::Cast(UnscaleHDRValueSigned(ParallelMath::LosslessCast<ParallelMath::SInt16>::Cast(inEP[epi][ch])));
  2273. else
  2274. outEP[epi][ch] = ParallelMath::LosslessCast<ParallelMath::AInt16>::Cast(UnscaleHDRValueUnsigned(ParallelMath::LosslessCast<ParallelMath::UInt16>::Cast(inEP[epi][ch])));
  2275. }
  2276. }
  2277. }
  2278. template<int TVectorSize>
  2279. class UnfinishedEndpoints
  2280. {
  2281. public:
  2282. typedef ParallelMath::Float MFloat;
  2283. typedef ParallelMath::UInt16 MUInt16;
  2284. typedef ParallelMath::UInt15 MUInt15;
  2285. typedef ParallelMath::SInt16 MSInt16;
  2286. typedef ParallelMath::SInt32 MSInt32;
  2287. UnfinishedEndpoints()
  2288. {
  2289. }
  2290. UnfinishedEndpoints(const MFloat *base, const MFloat *offset)
  2291. {
  2292. for (int ch = 0; ch < TVectorSize; ch++)
  2293. m_base[ch] = base[ch];
  2294. for (int ch = 0; ch < TVectorSize; ch++)
  2295. m_offset[ch] = offset[ch];
  2296. }
  2297. UnfinishedEndpoints(const UnfinishedEndpoints& other)
  2298. {
  2299. for (int ch = 0; ch < TVectorSize; ch++)
  2300. m_base[ch] = other.m_base[ch];
  2301. for (int ch = 0; ch < TVectorSize; ch++)
  2302. m_offset[ch] = other.m_offset[ch];
  2303. }
  2304. void FinishHDRUnsigned(int tweak, int range, MSInt16 *outEP0, MSInt16 *outEP1, ParallelMath::RoundTowardNearestForScope *roundingMode)
  2305. {
  2306. float tweakFactors[2];
  2307. ComputeTweakFactors(tweak, range, tweakFactors);
  2308. for (int ch = 0; ch < TVectorSize; ch++)
  2309. {
  2310. MUInt15 channelEPs[2];
  2311. for (int epi = 0; epi < 2; epi++)
  2312. {
  2313. MFloat f = ParallelMath::Clamp(m_base[ch] + m_offset[ch] * tweakFactors[epi], 0.0f, 31743.0f);
  2314. channelEPs[epi] = ParallelMath::RoundAndConvertToU15(f, roundingMode);
  2315. }
  2316. outEP0[ch] = ParallelMath::LosslessCast<MSInt16>::Cast(channelEPs[0]);
  2317. outEP1[ch] = ParallelMath::LosslessCast<MSInt16>::Cast(channelEPs[1]);
  2318. }
  2319. }
  2320. void FinishHDRSigned(int tweak, int range, MSInt16* outEP0, MSInt16* outEP1, ParallelMath::RoundTowardNearestForScope* roundingMode)
  2321. {
  2322. float tweakFactors[2];
  2323. ComputeTweakFactors(tweak, range, tweakFactors);
  2324. for (int ch = 0; ch < TVectorSize; ch++)
  2325. {
  2326. MSInt16 channelEPs[2];
  2327. for (int epi = 0; epi < 2; epi++)
  2328. {
  2329. MFloat f = ParallelMath::Clamp(m_base[ch] + m_offset[ch] * tweakFactors[epi], -31743.0f, 31743.0f);
  2330. channelEPs[epi] = ParallelMath::RoundAndConvertToS16(f, roundingMode);
  2331. }
  2332. outEP0[ch] = channelEPs[0];
  2333. outEP1[ch] = channelEPs[1];
  2334. }
  2335. }
  2336. void FinishLDR(int tweak, int range, MUInt15* outEP0, MUInt15* outEP1)
  2337. {
  2338. ParallelMath::RoundTowardNearestForScope roundingMode;
  2339. float tweakFactors[2];
  2340. ComputeTweakFactors(tweak, range, tweakFactors);
  2341. for (int ch = 0; ch < TVectorSize; ch++)
  2342. {
  2343. MFloat ep0f = ParallelMath::Clamp(m_base[ch] + m_offset[ch] * tweakFactors[0], 0.0f, 255.0f);
  2344. MFloat ep1f = ParallelMath::Clamp(m_base[ch] + m_offset[ch] * tweakFactors[1], 0.0f, 255.0f);
  2345. outEP0[ch] = ParallelMath::RoundAndConvertToU15(ep0f, &roundingMode);
  2346. outEP1[ch] = ParallelMath::RoundAndConvertToU15(ep1f, &roundingMode);
  2347. }
  2348. }
  2349. template<int TNewVectorSize>
  2350. UnfinishedEndpoints<TNewVectorSize> ExpandTo(float filler)
  2351. {
  2352. MFloat newBase[TNewVectorSize];
  2353. MFloat newOffset[TNewVectorSize];
  2354. for (int ch = 0; ch < TNewVectorSize && ch < TVectorSize; ch++)
  2355. {
  2356. newBase[ch] = m_base[ch];
  2357. newOffset[ch] = m_offset[ch];
  2358. }
  2359. MFloat fillerV = ParallelMath::MakeFloat(filler);
  2360. for (int ch = TVectorSize; ch < TNewVectorSize; ch++)
  2361. {
  2362. newBase[ch] = fillerV;
  2363. newOffset[ch] = ParallelMath::MakeFloatZero();
  2364. }
  2365. return UnfinishedEndpoints<TNewVectorSize>(newBase, newOffset);
  2366. }
  2367. private:
  2368. MFloat m_base[TVectorSize];
  2369. MFloat m_offset[TVectorSize];
  2370. };
  2371. template<int TMatrixSize>
  2372. class PackedCovarianceMatrix
  2373. {
  2374. public:
  2375. // 0: xx,
  2376. // 1: xy, yy
  2377. // 3: xz, yz, zz
  2378. // 6: xw, yw, zw, ww
  2379. // ... etc.
  2380. static const int PyramidSize = (TMatrixSize * (TMatrixSize + 1)) / 2;
  2381. typedef ParallelMath::Float MFloat;
  2382. PackedCovarianceMatrix()
  2383. {
  2384. for (int i = 0; i < PyramidSize; i++)
  2385. m_values[i] = ParallelMath::MakeFloatZero();
  2386. }
  2387. void Add(const ParallelMath::Float *vec, const ParallelMath::Float &weight)
  2388. {
  2389. int index = 0;
  2390. for (int row = 0; row < TMatrixSize; row++)
  2391. {
  2392. for (int col = 0; col <= row; col++)
  2393. {
  2394. m_values[index] = m_values[index] + vec[row] * vec[col] * weight;
  2395. index++;
  2396. }
  2397. }
  2398. }
  2399. void Product(MFloat *outVec, const MFloat *inVec)
  2400. {
  2401. for (int row = 0; row < TMatrixSize; row++)
  2402. {
  2403. MFloat sum = ParallelMath::MakeFloatZero();
  2404. int index = (row * (row + 1)) >> 1;
  2405. for (int col = 0; col < TMatrixSize; col++)
  2406. {
  2407. sum = sum + inVec[col] * m_values[index];
  2408. if (col >= row)
  2409. index += col + 1;
  2410. else
  2411. index++;
  2412. }
  2413. outVec[row] = sum;
  2414. }
  2415. }
  2416. private:
  2417. ParallelMath::Float m_values[PyramidSize];
  2418. };
  2419. static const int NumEndpointSelectorPasses = 3;
  2420. template<int TVectorSize, int TIterationCount>
  2421. class EndpointSelector
  2422. {
  2423. public:
  2424. typedef ParallelMath::Float MFloat;
  2425. EndpointSelector()
  2426. {
  2427. for (int ch = 0; ch < TVectorSize; ch++)
  2428. {
  2429. m_centroid[ch] = ParallelMath::MakeFloatZero();
  2430. m_direction[ch] = ParallelMath::MakeFloatZero();
  2431. }
  2432. m_weightTotal = ParallelMath::MakeFloatZero();
  2433. m_minDist = ParallelMath::MakeFloat(FLT_MAX);
  2434. m_maxDist = ParallelMath::MakeFloat(-FLT_MAX);
  2435. }
  2436. void ContributePass(const MFloat *value, int pass, const MFloat &weight)
  2437. {
  2438. if (pass == 0)
  2439. ContributeCentroid(value, weight);
  2440. else if (pass == 1)
  2441. ContributeDirection(value, weight);
  2442. else if (pass == 2)
  2443. ContributeMinMax(value);
  2444. }
  2445. void FinishPass(int pass)
  2446. {
  2447. if (pass == 0)
  2448. FinishCentroid();
  2449. else if (pass == 1)
  2450. FinishDirection();
  2451. }
  2452. UnfinishedEndpoints<TVectorSize> GetEndpoints(const float channelWeights[TVectorSize]) const
  2453. {
  2454. MFloat unweightedBase[TVectorSize];
  2455. MFloat unweightedOffset[TVectorSize];
  2456. for (int ch = 0; ch < TVectorSize; ch++)
  2457. {
  2458. MFloat min = m_centroid[ch] + m_direction[ch] * m_minDist;
  2459. MFloat max = m_centroid[ch] + m_direction[ch] * m_maxDist;
  2460. float safeWeight = channelWeights[ch];
  2461. if (safeWeight == 0.f)
  2462. safeWeight = 1.0f;
  2463. unweightedBase[ch] = min / channelWeights[ch];
  2464. unweightedOffset[ch] = (max - min) / channelWeights[ch];
  2465. }
  2466. return UnfinishedEndpoints<TVectorSize>(unweightedBase, unweightedOffset);
  2467. }
  2468. private:
  2469. void ContributeCentroid(const MFloat *value, const MFloat &weight)
  2470. {
  2471. for (int ch = 0; ch < TVectorSize; ch++)
  2472. m_centroid[ch] = m_centroid[ch] + value[ch] * weight;
  2473. m_weightTotal = m_weightTotal + weight;
  2474. }
  2475. void FinishCentroid()
  2476. {
  2477. MFloat denom = m_weightTotal;
  2478. ParallelMath::MakeSafeDenominator(denom);
  2479. for (int ch = 0; ch < TVectorSize; ch++)
  2480. m_centroid[ch] = m_centroid[ch] / denom;
  2481. }
  2482. void ContributeDirection(const MFloat *value, const MFloat &weight)
  2483. {
  2484. MFloat diff[TVectorSize];
  2485. for (int ch = 0; ch < TVectorSize; ch++)
  2486. diff[ch] = value[ch] - m_centroid[ch];
  2487. m_covarianceMatrix.Add(diff, weight);
  2488. }
  2489. void FinishDirection()
  2490. {
  2491. MFloat approx[TVectorSize];
  2492. for (int ch = 0; ch < TVectorSize; ch++)
  2493. approx[ch] = ParallelMath::MakeFloat(1.0f);
  2494. for (int i = 0; i < TIterationCount; i++)
  2495. {
  2496. MFloat product[TVectorSize];
  2497. m_covarianceMatrix.Product(product, approx);
  2498. MFloat largestComponent = product[0];
  2499. for (int ch = 1; ch < TVectorSize; ch++)
  2500. largestComponent = ParallelMath::Max(largestComponent, product[ch]);
  2501. // product = largestComponent*newApprox
  2502. ParallelMath::MakeSafeDenominator(largestComponent);
  2503. for (int ch = 0; ch < TVectorSize; ch++)
  2504. approx[ch] = product[ch] / largestComponent;
  2505. }
  2506. // Normalize
  2507. MFloat approxLen = ParallelMath::MakeFloatZero();
  2508. for (int ch = 0; ch < TVectorSize; ch++)
  2509. approxLen = approxLen + approx[ch] * approx[ch];
  2510. approxLen = ParallelMath::Sqrt(approxLen);
  2511. ParallelMath::MakeSafeDenominator(approxLen);
  2512. for (int ch = 0; ch < TVectorSize; ch++)
  2513. m_direction[ch] = approx[ch] / approxLen;
  2514. }
  2515. void ContributeMinMax(const MFloat *value)
  2516. {
  2517. MFloat dist = ParallelMath::MakeFloatZero();
  2518. for (int ch = 0; ch < TVectorSize; ch++)
  2519. dist = dist + m_direction[ch] * (value[ch] - m_centroid[ch]);
  2520. m_minDist = ParallelMath::Min(m_minDist, dist);
  2521. m_maxDist = ParallelMath::Max(m_maxDist, dist);
  2522. }
  2523. ParallelMath::Float m_centroid[TVectorSize];
  2524. ParallelMath::Float m_direction[TVectorSize];
  2525. PackedCovarianceMatrix<TVectorSize> m_covarianceMatrix;
  2526. ParallelMath::Float m_weightTotal;
  2527. ParallelMath::Float m_minDist;
  2528. ParallelMath::Float m_maxDist;
  2529. };
  2530. static const ParallelMath::UInt16 g_weightReciprocals[] =
  2531. {
  2532. ParallelMath::MakeUInt16(0), // -1
  2533. ParallelMath::MakeUInt16(0), // 0
  2534. ParallelMath::MakeUInt16(32768), // 1
  2535. ParallelMath::MakeUInt16(16384), // 2
  2536. ParallelMath::MakeUInt16(10923), // 3
  2537. ParallelMath::MakeUInt16(8192), // 4
  2538. ParallelMath::MakeUInt16(6554), // 5
  2539. ParallelMath::MakeUInt16(5461), // 6
  2540. ParallelMath::MakeUInt16(4681), // 7
  2541. ParallelMath::MakeUInt16(4096), // 8
  2542. ParallelMath::MakeUInt16(3641), // 9
  2543. ParallelMath::MakeUInt16(3277), // 10
  2544. ParallelMath::MakeUInt16(2979), // 11
  2545. ParallelMath::MakeUInt16(2731), // 12
  2546. ParallelMath::MakeUInt16(2521), // 13
  2547. ParallelMath::MakeUInt16(2341), // 14
  2548. ParallelMath::MakeUInt16(2185), // 15
  2549. };
  2550. template<int TVectorSize>
  2551. class IndexSelector
  2552. {
  2553. public:
  2554. typedef ParallelMath::Float MFloat;
  2555. typedef ParallelMath::UInt16 MUInt16;
  2556. typedef ParallelMath::UInt15 MUInt15;
  2557. typedef ParallelMath::SInt16 MSInt16;
  2558. typedef ParallelMath::AInt16 MAInt16;
  2559. typedef ParallelMath::SInt32 MSInt32;
  2560. typedef ParallelMath::UInt31 MUInt31;
  2561. template<class TInterpolationEPType, class TColorEPType>
  2562. void Init(const float *channelWeights, const TInterpolationEPType interpolationEndPoints[2][TVectorSize], const TColorEPType colorSpaceEndpoints[2][TVectorSize], int range)
  2563. {
  2564. // In BC6H, the interpolation endpoints are higher-precision than the endpoints in color space.
  2565. // We need to select indexes using the color-space endpoints.
  2566. m_isUniform = true;
  2567. for (int ch = 1; ch < TVectorSize; ch++)
  2568. {
  2569. if (channelWeights[ch] != channelWeights[0])
  2570. m_isUniform = false;
  2571. }
  2572. // To work with channel weights, we need something where:
  2573. // pxDiff = px - ep[0]
  2574. // epDiff = ep[1] - ep[0]
  2575. //
  2576. // weightedEPDiff = epDiff * channelWeights
  2577. // normalizedWeightedAxis = weightedEPDiff / len(weightedEPDiff)
  2578. // normalizedIndex = dot(pxDiff * channelWeights, normalizedWeightedAxis) / len(weightedEPDiff)
  2579. // index = normalizedIndex * maxValue
  2580. //
  2581. // Equivalent to:
  2582. // axis = channelWeights * maxValue * epDiff * channelWeights / lenSquared(epDiff * channelWeights)
  2583. // index = dot(axis, pxDiff)
  2584. for (int ep = 0; ep < 2; ep++)
  2585. for (int ch = 0; ch < TVectorSize; ch++)
  2586. m_endPoint[ep][ch] = ParallelMath::LosslessCast<MAInt16>::Cast(interpolationEndPoints[ep][ch]);
  2587. m_range = range;
  2588. m_maxValue = static_cast<float>(range - 1);
  2589. MFloat epDiffWeighted[TVectorSize];
  2590. for (int ch = 0; ch < TVectorSize; ch++)
  2591. {
  2592. m_origin[ch] = ParallelMath::ToFloat(colorSpaceEndpoints[0][ch]);
  2593. MFloat opposingOriginCh = ParallelMath::ToFloat(colorSpaceEndpoints[1][ch]);
  2594. epDiffWeighted[ch] = (opposingOriginCh - m_origin[ch]) * channelWeights[ch];
  2595. }
  2596. MFloat lenSquared = epDiffWeighted[0] * epDiffWeighted[0];
  2597. for (int ch = 1; ch < TVectorSize; ch++)
  2598. lenSquared = lenSquared + epDiffWeighted[ch] * epDiffWeighted[ch];
  2599. ParallelMath::MakeSafeDenominator(lenSquared);
  2600. MFloat maxValueDividedByLengthSquared = ParallelMath::MakeFloat(m_maxValue) / lenSquared;
  2601. for (int ch = 0; ch < TVectorSize; ch++)
  2602. m_axis[ch] = epDiffWeighted[ch] * channelWeights[ch] * maxValueDividedByLengthSquared;
  2603. }
  2604. template<bool TSigned>
  2605. void Init(const float channelWeights[TVectorSize], const MUInt15 endPoints[2][TVectorSize], int range)
  2606. {
  2607. MAInt16 converted[2][TVectorSize];
  2608. for (int epi = 0; epi < 2; epi++)
  2609. for (int ch = 0; ch < TVectorSize; ch++)
  2610. converted[epi][ch] = ParallelMath::LosslessCast<MAInt16>::Cast(endPoints[epi][ch]);
  2611. Init<MUInt15, MUInt15>(channelWeights, endPoints, endPoints, range);
  2612. }
  2613. void ReconstructLDR_BC7(const MUInt15 &index, MUInt15* pixel, int numRealChannels)
  2614. {
  2615. MUInt15 weight = ParallelMath::LosslessCast<MUInt15>::Cast(ParallelMath::RightShift(ParallelMath::CompactMultiply(g_weightReciprocals[m_range], index) + 256, 9));
  2616. for (int ch = 0; ch < numRealChannels; ch++)
  2617. {
  2618. MUInt15 ep0f = ParallelMath::LosslessCast<MUInt15>::Cast(ParallelMath::CompactMultiply((ParallelMath::MakeUInt15(64) - weight), ParallelMath::LosslessCast<MUInt15>::Cast(m_endPoint[0][ch])));
  2619. MUInt15 ep1f = ParallelMath::LosslessCast<MUInt15>::Cast(ParallelMath::CompactMultiply(weight, ParallelMath::LosslessCast<MUInt15>::Cast(m_endPoint[1][ch])));
  2620. pixel[ch] = ParallelMath::LosslessCast<MUInt15>::Cast(ParallelMath::RightShift(ep0f + ep1f + ParallelMath::MakeUInt15(32), 6));
  2621. }
  2622. }
  2623. void ReconstructLDRPrecise(const MUInt15 &index, MUInt15* pixel, int numRealChannels)
  2624. {
  2625. MUInt15 weight = ParallelMath::LosslessCast<MUInt15>::Cast(ParallelMath::RightShift(ParallelMath::CompactMultiply(g_weightReciprocals[m_range], index) + 64, 7));
  2626. for (int ch = 0; ch < numRealChannels; ch++)
  2627. {
  2628. MUInt15 ep0f = ParallelMath::LosslessCast<MUInt15>::Cast(ParallelMath::CompactMultiply((ParallelMath::MakeUInt15(256) - weight), ParallelMath::LosslessCast<MUInt15>::Cast(m_endPoint[0][ch])));
  2629. MUInt15 ep1f = ParallelMath::LosslessCast<MUInt15>::Cast(ParallelMath::CompactMultiply(weight, ParallelMath::LosslessCast<MUInt15>::Cast(m_endPoint[1][ch])));
  2630. pixel[ch] = ParallelMath::LosslessCast<MUInt15>::Cast(ParallelMath::RightShift(ep0f + ep1f + ParallelMath::MakeUInt15(128), 8));
  2631. }
  2632. }
  2633. void ReconstructLDR_BC7(const MUInt15 &index, MUInt15* pixel)
  2634. {
  2635. ReconstructLDR_BC7(index, pixel, TVectorSize);
  2636. }
  2637. void ReconstructLDRPrecise(const MUInt15 &index, MUInt15* pixel)
  2638. {
  2639. ReconstructLDRPrecise(index, pixel, TVectorSize);
  2640. }
  2641. MUInt15 SelectIndexLDR(const MFloat* pixel, const ParallelMath::RoundTowardNearestForScope* rtn) const
  2642. {
  2643. MFloat dist = (pixel[0] - m_origin[0]) * m_axis[0];
  2644. for (int ch = 1; ch < TVectorSize; ch++)
  2645. dist = dist + (pixel[ch] - m_origin[ch]) * m_axis[ch];
  2646. return ParallelMath::RoundAndConvertToU15(ParallelMath::Clamp(dist, 0.0f, m_maxValue), rtn);
  2647. }
  2648. protected:
  2649. MAInt16 m_endPoint[2][TVectorSize];
  2650. private:
  2651. MFloat m_origin[TVectorSize];
  2652. MFloat m_axis[TVectorSize];
  2653. int m_range;
  2654. float m_maxValue;
  2655. bool m_isUniform;
  2656. };
  2657. template<int TVectorSize>
  2658. class IndexSelectorHDR : public IndexSelector<TVectorSize>
  2659. {
  2660. public:
  2661. typedef ParallelMath::UInt15 MUInt15;
  2662. typedef ParallelMath::UInt16 MUInt16;
  2663. typedef ParallelMath::UInt31 MUInt31;
  2664. typedef ParallelMath::SInt16 MSInt16;
  2665. typedef ParallelMath::SInt32 MSInt32;
  2666. typedef ParallelMath::Float MFloat;
  2667. private:
  2668. MUInt15 InvertSingle(const MUInt15& anIndex) const
  2669. {
  2670. MUInt15 inverted = m_maxValueMinusOne - anIndex;
  2671. return ParallelMath::Select(m_isInverted, inverted, anIndex);
  2672. }
  2673. void ReconstructHDRSignedUninverted(const MUInt15 &index, MSInt16* pixel) const
  2674. {
  2675. MUInt15 weight = ParallelMath::LosslessCast<MUInt15>::Cast(ParallelMath::RightShift(ParallelMath::CompactMultiply(g_weightReciprocals[m_range], index) + 256, 9));
  2676. for (int ch = 0; ch < TVectorSize; ch++)
  2677. {
  2678. MSInt16 ep0 = ParallelMath::LosslessCast<MSInt16>::Cast(this->m_endPoint[0][ch]);
  2679. MSInt16 ep1 = ParallelMath::LosslessCast<MSInt16>::Cast(this->m_endPoint[1][ch]);
  2680. MSInt32 pixel32 = ParallelMath::XMultiply((ParallelMath::MakeUInt15(64) - weight), ep0) + ParallelMath::XMultiply(weight, ep1);
  2681. pixel32 = ParallelMath::RightShift(pixel32 + ParallelMath::MakeSInt32(32), 6);
  2682. pixel[ch] = UnscaleHDRValueSigned(ParallelMath::ToSInt16(pixel32));
  2683. }
  2684. }
  2685. void ReconstructHDRUnsignedUninverted(const MUInt15 &index, MSInt16* pixel) const
  2686. {
  2687. MUInt15 weight = ParallelMath::LosslessCast<MUInt15>::Cast(ParallelMath::RightShift(ParallelMath::CompactMultiply(g_weightReciprocals[m_range], index) + 256, 9));
  2688. for (int ch = 0; ch < TVectorSize; ch++)
  2689. {
  2690. MUInt16 ep0 = ParallelMath::LosslessCast<MUInt16>::Cast(this->m_endPoint[0][ch]);
  2691. MUInt16 ep1 = ParallelMath::LosslessCast<MUInt16>::Cast(this->m_endPoint[1][ch]);
  2692. MUInt31 pixel31 = ParallelMath::XMultiply((ParallelMath::MakeUInt15(64) - weight), ep0) + ParallelMath::XMultiply(weight, ep1);
  2693. pixel31 = ParallelMath::RightShift(pixel31 + ParallelMath::MakeUInt31(32), 6);
  2694. pixel[ch] = ParallelMath::LosslessCast<MSInt16>::Cast(UnscaleHDRValueUnsigned(ParallelMath::ToUInt16(pixel31)));
  2695. }
  2696. }
  2697. MFloat ErrorForInterpolatorComponent(int index, int ch, const MFloat *pixel) const
  2698. {
  2699. MFloat diff = pixel[ch] - m_reconstructedInterpolators[index][ch];
  2700. return diff * diff;
  2701. }
  2702. MFloat ErrorForInterpolator(int index, const MFloat *pixel) const
  2703. {
  2704. MFloat error = ErrorForInterpolatorComponent(index, 0, pixel);
  2705. for (int ch = 1; ch < TVectorSize; ch++)
  2706. error = error + ErrorForInterpolatorComponent(index, ch, pixel);
  2707. return error;
  2708. }
  2709. public:
  2710. void InitHDR(int range, bool isSigned, bool fastIndexing, const float *channelWeights)
  2711. {
  2712. assert(range <= 16);
  2713. m_range = range;
  2714. m_isInverted = ParallelMath::MakeBoolInt16(false);
  2715. m_maxValueMinusOne = ParallelMath::MakeUInt15(static_cast<uint16_t>(range - 1));
  2716. if (!fastIndexing)
  2717. {
  2718. for (int i = 0; i < range; i++)
  2719. {
  2720. MSInt16 recon2CL[TVectorSize];
  2721. if (isSigned)
  2722. ReconstructHDRSignedUninverted(ParallelMath::MakeUInt15(static_cast<uint16_t>(i)), recon2CL);
  2723. else
  2724. ReconstructHDRUnsignedUninverted(ParallelMath::MakeUInt15(static_cast<uint16_t>(i)), recon2CL);
  2725. for (int ch = 0; ch < TVectorSize; ch++)
  2726. m_reconstructedInterpolators[i][ch] = ParallelMath::TwosCLHalfToFloat(recon2CL[ch]) * channelWeights[ch];
  2727. }
  2728. }
  2729. }
  2730. void ReconstructHDRSigned(const MUInt15 &index, MSInt16* pixel) const
  2731. {
  2732. ReconstructHDRSignedUninverted(InvertSingle(index), pixel);
  2733. }
  2734. void ReconstructHDRUnsigned(const MUInt15 &index, MSInt16* pixel) const
  2735. {
  2736. ReconstructHDRUnsignedUninverted(InvertSingle(index), pixel);
  2737. }
  2738. void ConditionalInvert(const ParallelMath::Int16CompFlag &invert)
  2739. {
  2740. m_isInverted = invert;
  2741. }
  2742. MUInt15 SelectIndexHDRSlow(const MFloat* pixel, const ParallelMath::RoundTowardNearestForScope*) const
  2743. {
  2744. MUInt15 index = ParallelMath::MakeUInt15(0);
  2745. MFloat bestError = ErrorForInterpolator(0, pixel);
  2746. for (int i = 1; i < m_range; i++)
  2747. {
  2748. MFloat error = ErrorForInterpolator(i, pixel);
  2749. ParallelMath::FloatCompFlag errorBetter = ParallelMath::Less(error, bestError);
  2750. ParallelMath::ConditionalSet(index, ParallelMath::FloatFlagToInt16(errorBetter), ParallelMath::MakeUInt15(static_cast<uint16_t>(i)));
  2751. bestError = ParallelMath::Min(bestError, error);
  2752. }
  2753. return InvertSingle(index);
  2754. }
  2755. MUInt15 SelectIndexHDRFast(const MFloat* pixel, const ParallelMath::RoundTowardNearestForScope* rtn) const
  2756. {
  2757. return InvertSingle(this->SelectIndexLDR(pixel, rtn));
  2758. }
  2759. private:
  2760. MFloat m_reconstructedInterpolators[16][TVectorSize];
  2761. ParallelMath::Int16CompFlag m_isInverted;
  2762. MUInt15 m_maxValueMinusOne;
  2763. int m_range;
  2764. };
  2765. // Solve for a, b where v = a*t + b
  2766. // This allows endpoints to be mapped to where T=0 and T=1
  2767. // Least squares from totals:
  2768. // a = (tv - t*v/w)/(tt - t*t/w)
  2769. // b = (v - a*t)/w
  2770. template<int TVectorSize>
  2771. class EndpointRefiner
  2772. {
  2773. public:
  2774. typedef ParallelMath::Float MFloat;
  2775. typedef ParallelMath::UInt16 MUInt16;
  2776. typedef ParallelMath::UInt15 MUInt15;
  2777. typedef ParallelMath::AInt16 MAInt16;
  2778. typedef ParallelMath::SInt16 MSInt16;
  2779. typedef ParallelMath::SInt32 MSInt32;
  2780. MFloat m_tv[TVectorSize];
  2781. MFloat m_v[TVectorSize];
  2782. MFloat m_tt;
  2783. MFloat m_t;
  2784. MFloat m_w;
  2785. int m_wu;
  2786. float m_rcpMaxIndex;
  2787. float m_channelWeights[TVectorSize];
  2788. float m_rcpChannelWeights[TVectorSize];
  2789. void Init(int indexRange, const float channelWeights[TVectorSize])
  2790. {
  2791. for (int ch = 0; ch < TVectorSize; ch++)
  2792. {
  2793. m_tv[ch] = ParallelMath::MakeFloatZero();
  2794. m_v[ch] = ParallelMath::MakeFloatZero();
  2795. }
  2796. m_tt = ParallelMath::MakeFloatZero();
  2797. m_t = ParallelMath::MakeFloatZero();
  2798. m_w = ParallelMath::MakeFloatZero();
  2799. m_rcpMaxIndex = 1.0f / static_cast<float>(indexRange - 1);
  2800. for (int ch = 0; ch < TVectorSize; ch++)
  2801. {
  2802. m_channelWeights[ch] = channelWeights[ch];
  2803. m_rcpChannelWeights[ch] = 1.0f;
  2804. if (m_channelWeights[ch] != 0.0f)
  2805. m_rcpChannelWeights[ch] = 1.0f / channelWeights[ch];
  2806. }
  2807. m_wu = 0;
  2808. }
  2809. void ContributePW(const MFloat *pwFloatPixel, const MUInt15 &index, const MFloat &weight)
  2810. {
  2811. MFloat t = ParallelMath::ToFloat(index) * m_rcpMaxIndex;
  2812. for (int ch = 0; ch < TVectorSize; ch++)
  2813. {
  2814. MFloat v = pwFloatPixel[ch] * weight;
  2815. m_tv[ch] = m_tv[ch] + t * v;
  2816. m_v[ch] = m_v[ch] + v;
  2817. }
  2818. m_tt = m_tt + weight * t * t;
  2819. m_t = m_t + weight * t;
  2820. m_w = m_w + weight;
  2821. }
  2822. void ContributeUnweightedPW(const MFloat *pwFloatPixel, const MUInt15 &index, int numRealChannels)
  2823. {
  2824. MFloat t = ParallelMath::ToFloat(index) * m_rcpMaxIndex;
  2825. for (int ch = 0; ch < numRealChannels; ch++)
  2826. {
  2827. MFloat v = pwFloatPixel[ch];
  2828. m_tv[ch] = m_tv[ch] + t * v;
  2829. m_v[ch] = m_v[ch] + v;
  2830. }
  2831. m_tt = m_tt + t * t;
  2832. m_t = m_t + t;
  2833. m_wu++;
  2834. }
  2835. void ContributeUnweightedPW(const MFloat *floatPixel, const MUInt15 &index)
  2836. {
  2837. ContributeUnweightedPW(floatPixel, index, TVectorSize);
  2838. }
  2839. void GetRefinedEndpoints(MFloat endPoint[2][TVectorSize])
  2840. {
  2841. // a = (tv - t*v/w)/(tt - t*t/w)
  2842. // b = (v - a*t)/w
  2843. MFloat w = m_w + ParallelMath::MakeFloat(static_cast<float>(m_wu));
  2844. ParallelMath::MakeSafeDenominator(w);
  2845. MFloat wRcp = ParallelMath::Reciprocal(w);
  2846. MFloat adenom = (m_tt * w - m_t * m_t) * wRcp;
  2847. ParallelMath::FloatCompFlag adenomZero = ParallelMath::Equal(adenom, ParallelMath::MakeFloatZero());
  2848. ParallelMath::ConditionalSet(adenom, adenomZero, ParallelMath::MakeFloat(1.0f));
  2849. for (int ch = 0; ch < TVectorSize; ch++)
  2850. {
  2851. /*
  2852. if (adenom == 0.0)
  2853. p1 = p2 = er.v / er.w;
  2854. else
  2855. {
  2856. float4 a = (er.tv - er.t*er.v / er.w) / adenom;
  2857. float4 b = (er.v - a * er.t) / er.w;
  2858. p1 = b;
  2859. p2 = a + b;
  2860. }
  2861. */
  2862. MFloat a = (m_tv[ch] - m_t * m_v[ch] * wRcp) / adenom;
  2863. MFloat b = (m_v[ch] - a * m_t) * wRcp;
  2864. MFloat p1 = b;
  2865. MFloat p2 = a + b;
  2866. ParallelMath::ConditionalSet(p1, adenomZero, (m_v[ch] * wRcp));
  2867. ParallelMath::ConditionalSet(p2, adenomZero, p1);
  2868. // Unweight
  2869. float inverseWeight = m_rcpChannelWeights[ch];
  2870. endPoint[0][ch] = p1 * inverseWeight;
  2871. endPoint[1][ch] = p2 * inverseWeight;
  2872. }
  2873. }
  2874. void GetRefinedEndpointsLDR(MUInt15 endPoint[2][TVectorSize], int numRealChannels, const ParallelMath::RoundTowardNearestForScope *roundingMode)
  2875. {
  2876. MFloat floatEndPoint[2][TVectorSize];
  2877. GetRefinedEndpoints(floatEndPoint);
  2878. for (int epi = 0; epi < 2; epi++)
  2879. for (int ch = 0; ch < TVectorSize; ch++)
  2880. endPoint[epi][ch] = ParallelMath::RoundAndConvertToU15(ParallelMath::Clamp(floatEndPoint[epi][ch], 0.0f, 255.0f), roundingMode);
  2881. }
  2882. void GetRefinedEndpointsLDR(MUInt15 endPoint[2][TVectorSize], const ParallelMath::RoundTowardNearestForScope *roundingMode)
  2883. {
  2884. GetRefinedEndpointsLDR(endPoint, TVectorSize, roundingMode);
  2885. }
  2886. void GetRefinedEndpointsHDR(MSInt16 endPoint[2][TVectorSize], bool isSigned, const ParallelMath::RoundTowardNearestForScope *roundingMode)
  2887. {
  2888. MFloat floatEndPoint[2][TVectorSize];
  2889. GetRefinedEndpoints(floatEndPoint);
  2890. for (int epi = 0; epi < 2; epi++)
  2891. {
  2892. for (int ch = 0; ch < TVectorSize; ch++)
  2893. {
  2894. MFloat f = floatEndPoint[epi][ch];
  2895. if (isSigned)
  2896. endPoint[epi][ch] = ParallelMath::LosslessCast<MSInt16>::Cast(ParallelMath::RoundAndConvertToS16(ParallelMath::Clamp(f, -31743.0f, 31743.0f), roundingMode));
  2897. else
  2898. endPoint[epi][ch] = ParallelMath::LosslessCast<MSInt16>::Cast(ParallelMath::RoundAndConvertToU15(ParallelMath::Clamp(f, 0.0f, 31743.0f), roundingMode));
  2899. }
  2900. }
  2901. }
  2902. };
  2903. template<int TVectorSize>
  2904. class AggregatedError
  2905. {
  2906. public:
  2907. typedef ParallelMath::UInt16 MUInt16;
  2908. typedef ParallelMath::UInt31 MUInt31;
  2909. typedef ParallelMath::Float MFloat;
  2910. AggregatedError()
  2911. {
  2912. for (int ch = 0; ch < TVectorSize; ch++)
  2913. m_errorUnweighted[ch] = ParallelMath::MakeUInt31(0);
  2914. }
  2915. void Add(const MUInt16 &channelErrorUnweighted, int ch)
  2916. {
  2917. m_errorUnweighted[ch] = m_errorUnweighted[ch] + ParallelMath::ToUInt31(channelErrorUnweighted);
  2918. }
  2919. MFloat Finalize(uint32_t flags, const float channelWeightsSq[TVectorSize]) const
  2920. {
  2921. if (flags & cvtt::Flags::Uniform)
  2922. {
  2923. MUInt31 total = m_errorUnweighted[0];
  2924. for (int ch = 1; ch < TVectorSize; ch++)
  2925. total = total + m_errorUnweighted[ch];
  2926. return ParallelMath::ToFloat(total);
  2927. }
  2928. else
  2929. {
  2930. MFloat total = ParallelMath::ToFloat(m_errorUnweighted[0]) * channelWeightsSq[0];
  2931. for (int ch = 1; ch < TVectorSize; ch++)
  2932. total = total + ParallelMath::ToFloat(m_errorUnweighted[ch]) * channelWeightsSq[ch];
  2933. return total;
  2934. }
  2935. }
  2936. private:
  2937. MUInt31 m_errorUnweighted[TVectorSize];
  2938. };
  2939. class BCCommon
  2940. {
  2941. public:
  2942. typedef ParallelMath::Float MFloat;
  2943. typedef ParallelMath::UInt16 MUInt16;
  2944. typedef ParallelMath::UInt15 MUInt15;
  2945. typedef ParallelMath::AInt16 MAInt16;
  2946. typedef ParallelMath::SInt16 MSInt16;
  2947. typedef ParallelMath::SInt32 MSInt32;
  2948. static int TweakRoundsForRange(int range)
  2949. {
  2950. if (range == 3)
  2951. return 3;
  2952. return 4;
  2953. }
  2954. template<int TVectorSize>
  2955. static void ComputeErrorLDR(uint32_t flags, const MUInt15 reconstructed[TVectorSize], const MUInt15 original[TVectorSize], int numRealChannels, AggregatedError<TVectorSize> &aggError)
  2956. {
  2957. for (int ch = 0; ch < numRealChannels; ch++)
  2958. aggError.Add(ParallelMath::SqDiffUInt8(reconstructed[ch], original[ch]), ch);
  2959. }
  2960. template<int TVectorSize>
  2961. static void ComputeErrorLDR(uint32_t flags, const MUInt15 reconstructed[TVectorSize], const MUInt15 original[TVectorSize], AggregatedError<TVectorSize> &aggError)
  2962. {
  2963. ComputeErrorLDR<TVectorSize>(flags, reconstructed, original, TVectorSize, aggError);
  2964. }
  2965. template<int TVectorSize>
  2966. static MFloat ComputeErrorLDRSimple(uint32_t flags, const MUInt15 reconstructed[TVectorSize], const MUInt15 original[TVectorSize], int numRealChannels, const float *channelWeightsSq)
  2967. {
  2968. AggregatedError<TVectorSize> aggError;
  2969. ComputeErrorLDR<TVectorSize>(flags, reconstructed, original, numRealChannels, aggError);
  2970. return aggError.Finalize(flags, channelWeightsSq);
  2971. }
  2972. template<int TVectorSize>
  2973. static MFloat ComputeErrorHDRFast(uint32_t flags, const MSInt16 reconstructed[TVectorSize], const MSInt16 original[TVectorSize], const float channelWeightsSq[TVectorSize])
  2974. {
  2975. MFloat error = ParallelMath::MakeFloatZero();
  2976. if (flags & Flags::Uniform)
  2977. {
  2978. for (int ch = 0; ch < TVectorSize; ch++)
  2979. error = error + ParallelMath::SqDiffSInt16(reconstructed[ch], original[ch]);
  2980. }
  2981. else
  2982. {
  2983. for (int ch = 0; ch < TVectorSize; ch++)
  2984. error = error + ParallelMath::SqDiffSInt16(reconstructed[ch], original[ch]) * ParallelMath::MakeFloat(channelWeightsSq[ch]);
  2985. }
  2986. return error;
  2987. }
  2988. template<int TVectorSize>
  2989. static MFloat ComputeErrorHDRSlow(uint32_t flags, const MSInt16 reconstructed[TVectorSize], const MSInt16 original[TVectorSize], const float channelWeightsSq[TVectorSize])
  2990. {
  2991. MFloat error = ParallelMath::MakeFloatZero();
  2992. if (flags & Flags::Uniform)
  2993. {
  2994. for (int ch = 0; ch < TVectorSize; ch++)
  2995. error = error + ParallelMath::SqDiff2CL(reconstructed[ch], original[ch]);
  2996. }
  2997. else
  2998. {
  2999. for (int ch = 0; ch < TVectorSize; ch++)
  3000. error = error + ParallelMath::SqDiff2CL(reconstructed[ch], original[ch]) * ParallelMath::MakeFloat(channelWeightsSq[ch]);
  3001. }
  3002. return error;
  3003. }
  3004. template<int TChannelCount>
  3005. static void PreWeightPixelsLDR(MFloat preWeightedPixels[16][TChannelCount], const MUInt15 pixels[16][TChannelCount], const float channelWeights[TChannelCount])
  3006. {
  3007. for (int px = 0; px < 16; px++)
  3008. {
  3009. for (int ch = 0; ch < TChannelCount; ch++)
  3010. preWeightedPixels[px][ch] = ParallelMath::ToFloat(pixels[px][ch]) * channelWeights[ch];
  3011. }
  3012. }
  3013. template<int TChannelCount>
  3014. static void PreWeightPixelsHDR(MFloat preWeightedPixels[16][TChannelCount], const MSInt16 pixels[16][TChannelCount], const float channelWeights[TChannelCount])
  3015. {
  3016. for (int px = 0; px < 16; px++)
  3017. {
  3018. for (int ch = 0; ch < TChannelCount; ch++)
  3019. preWeightedPixels[px][ch] = ParallelMath::ToFloat(pixels[px][ch]) * channelWeights[ch];
  3020. }
  3021. }
  3022. };
  3023. class BC7Computer
  3024. {
  3025. public:
  3026. static const int MaxTweakRounds = 4;
  3027. typedef ParallelMath::SInt16 MSInt16;
  3028. typedef ParallelMath::UInt15 MUInt15;
  3029. typedef ParallelMath::UInt16 MUInt16;
  3030. typedef ParallelMath::SInt32 MSInt32;
  3031. typedef ParallelMath::Float MFloat;
  3032. struct WorkInfo
  3033. {
  3034. MUInt15 m_mode;
  3035. MFloat m_error;
  3036. MUInt15 m_ep[3][2][4];
  3037. MUInt15 m_indexes[16];
  3038. MUInt15 m_indexes2[16];
  3039. union
  3040. {
  3041. MUInt15 m_partition;
  3042. struct IndexSelectorAndRotation
  3043. {
  3044. MUInt15 m_indexSelector;
  3045. MUInt15 m_rotation;
  3046. } m_isr;
  3047. } m_u;
  3048. };
  3049. static void TweakAlpha(const MUInt15 original[2], int tweak, int range, MUInt15 result[2])
  3050. {
  3051. ParallelMath::RoundTowardNearestForScope roundingMode;
  3052. float tf[2];
  3053. ComputeTweakFactors(tweak, range, tf);
  3054. MFloat base = ParallelMath::ToFloat(original[0]);
  3055. MFloat offs = ParallelMath::ToFloat(original[1]) - base;
  3056. result[0] = ParallelMath::RoundAndConvertToU15(ParallelMath::Clamp(base + offs * tf[0], 0.0f, 255.0f), &roundingMode);
  3057. result[1] = ParallelMath::RoundAndConvertToU15(ParallelMath::Clamp(base + offs * tf[1], 0.0f, 255.0f), &roundingMode);
  3058. }
  3059. static void Quantize(MUInt15* color, int bits, int channels, const ParallelMath::RoundTowardNearestForScope *roundingMode)
  3060. {
  3061. float maxColor = static_cast<float>((1 << bits) - 1);
  3062. for (int i = 0; i < channels; i++)
  3063. color[i] = ParallelMath::RoundAndConvertToU15(ParallelMath::Clamp(ParallelMath::ToFloat(color[i]) * ParallelMath::MakeFloat(1.0f / 255.0f) * maxColor, 0.f, 255.f), roundingMode);
  3064. }
  3065. static void QuantizeP(MUInt15* color, int bits, uint16_t p, int channels, const ParallelMath::RoundTowardNearestForScope *roundingMode)
  3066. {
  3067. uint16_t pShift = static_cast<uint16_t>(1 << (7 - bits));
  3068. MUInt15 pShiftV = ParallelMath::MakeUInt15(pShift);
  3069. float maxColorF = static_cast<float>(255 - (1 << (7 - bits)));
  3070. float maxQuantized = static_cast<float>((1 << bits) - 1);
  3071. for (int ch = 0; ch < channels; ch++)
  3072. {
  3073. MUInt15 clr = color[ch];
  3074. if (p)
  3075. clr = ParallelMath::Max(clr, pShiftV) - pShiftV;
  3076. MFloat rerangedColor = ParallelMath::ToFloat(clr) * maxQuantized / maxColorF;
  3077. clr = ParallelMath::RoundAndConvertToU15(ParallelMath::Clamp(rerangedColor, 0.0f, maxQuantized), roundingMode) << 1;
  3078. if (p)
  3079. clr = clr | ParallelMath::MakeUInt15(1);
  3080. color[ch] = clr;
  3081. }
  3082. }
  3083. static void Unquantize(MUInt15* color, int bits, int channels)
  3084. {
  3085. for (int ch = 0; ch < channels; ch++)
  3086. {
  3087. MUInt15 clr = color[ch];
  3088. clr = clr << (8 - bits);
  3089. color[ch] = clr | ParallelMath::RightShift(clr, bits);
  3090. }
  3091. }
  3092. static void CompressEndpoints0(MUInt15 ep[2][4], uint16_t p[2], const ParallelMath::RoundTowardNearestForScope *roundingMode)
  3093. {
  3094. for (int j = 0; j < 2; j++)
  3095. {
  3096. QuantizeP(ep[j], 4, p[j], 3, roundingMode);
  3097. Unquantize(ep[j], 5, 3);
  3098. ep[j][3] = ParallelMath::MakeUInt15(255);
  3099. }
  3100. }
  3101. static void CompressEndpoints1(MUInt15 ep[2][4], uint16_t p, const ParallelMath::RoundTowardNearestForScope *roundingMode)
  3102. {
  3103. for (int j = 0; j < 2; j++)
  3104. {
  3105. QuantizeP(ep[j], 6, p, 3, roundingMode);
  3106. Unquantize(ep[j], 7, 3);
  3107. ep[j][3] = ParallelMath::MakeUInt15(255);
  3108. }
  3109. }
  3110. static void CompressEndpoints2(MUInt15 ep[2][4], const ParallelMath::RoundTowardNearestForScope *roundingMode)
  3111. {
  3112. for (int j = 0; j < 2; j++)
  3113. {
  3114. Quantize(ep[j], 5, 3, roundingMode);
  3115. Unquantize(ep[j], 5, 3);
  3116. ep[j][3] = ParallelMath::MakeUInt15(255);
  3117. }
  3118. }
  3119. static void CompressEndpoints3(MUInt15 ep[2][4], uint16_t p[2], const ParallelMath::RoundTowardNearestForScope *roundingMode)
  3120. {
  3121. for (int j = 0; j < 2; j++)
  3122. {
  3123. QuantizeP(ep[j], 7, p[j], 3, roundingMode);
  3124. ep[j][3] = ParallelMath::MakeUInt15(255);
  3125. }
  3126. }
  3127. static void CompressEndpoints4(MUInt15 epRGB[2][3], MUInt15 epA[2], const ParallelMath::RoundTowardNearestForScope *roundingMode)
  3128. {
  3129. for (int j = 0; j < 2; j++)
  3130. {
  3131. Quantize(epRGB[j], 5, 3, roundingMode);
  3132. Unquantize(epRGB[j], 5, 3);
  3133. Quantize(epA + j, 6, 1, roundingMode);
  3134. Unquantize(epA + j, 6, 1);
  3135. }
  3136. }
  3137. static void CompressEndpoints5(MUInt15 epRGB[2][3], MUInt15 epA[2], const ParallelMath::RoundTowardNearestForScope *roundingMode)
  3138. {
  3139. for (int j = 0; j < 2; j++)
  3140. {
  3141. Quantize(epRGB[j], 7, 3, roundingMode);
  3142. Unquantize(epRGB[j], 7, 3);
  3143. }
  3144. // Alpha is full precision
  3145. (void)epA;
  3146. }
  3147. static void CompressEndpoints6(MUInt15 ep[2][4], uint16_t p[2], const ParallelMath::RoundTowardNearestForScope *roundingMode)
  3148. {
  3149. for (int j = 0; j < 2; j++)
  3150. QuantizeP(ep[j], 7, p[j], 4, roundingMode);
  3151. }
  3152. static void CompressEndpoints7(MUInt15 ep[2][4], uint16_t p[2], const ParallelMath::RoundTowardNearestForScope *roundingMode)
  3153. {
  3154. for (int j = 0; j < 2; j++)
  3155. {
  3156. QuantizeP(ep[j], 5, p[j], 4, roundingMode);
  3157. Unquantize(ep[j], 6, 4);
  3158. }
  3159. }
  3160. struct SinglePlaneTemporaries
  3161. {
  3162. UnfinishedEndpoints<3> unfinishedRGB[BC7Data::g_numShapesAll];
  3163. UnfinishedEndpoints<4> unfinishedRGBA[BC7Data::g_numShapes12];
  3164. MUInt15 fragmentBestIndexes[BC7Data::g_numFragments];
  3165. MUInt15 shapeBestEP[BC7Data::g_maxFragmentsPerMode][2][4];
  3166. MFloat shapeBestError[BC7Data::g_maxFragmentsPerMode];
  3167. };
  3168. static void TrySingleColorRGBAMultiTable(uint32_t flags, const MUInt15 pixels[16][4], const MFloat average[4], int numRealChannels, const uint8_t *fragmentStart, int shapeLength, const MFloat &staticAlphaError, const ParallelMath::Int16CompFlag punchThroughInvalid[4], MFloat& shapeBestError, MUInt15 shapeBestEP[2][4], MUInt15 *fragmentBestIndexes, const float *channelWeightsSq, const cvtt::Tables::BC7SC::Table*const* tables, int numTables, const ParallelMath::RoundTowardNearestForScope *rtn)
  3169. {
  3170. MFloat bestAverageError = ParallelMath::MakeFloat(FLT_MAX);
  3171. MUInt15 intAverage[4];
  3172. for (int ch = 0; ch < 4; ch++)
  3173. intAverage[ch] = ParallelMath::RoundAndConvertToU15(average[ch], rtn);
  3174. MUInt15 eps[2][4];
  3175. MUInt15 reconstructed[4];
  3176. MUInt15 index = ParallelMath::MakeUInt15(0);
  3177. for (int epi = 0; epi < 2; epi++)
  3178. {
  3179. for (int ch = 0; ch < 3; ch++)
  3180. eps[epi][ch] = ParallelMath::MakeUInt15(0);
  3181. eps[epi][3] = ParallelMath::MakeUInt15(255);
  3182. }
  3183. for (int ch = 0; ch < 3; ch++)
  3184. reconstructed[ch] = ParallelMath::MakeUInt15(0);
  3185. reconstructed[3] = ParallelMath::MakeUInt15(255);
  3186. // Depending on the target index and parity bits, there are multiple valid solid colors.
  3187. // We want to find the one closest to the actual average.
  3188. MFloat epsAverageDiff = ParallelMath::MakeFloat(FLT_MAX);
  3189. for (int t = 0; t < numTables; t++)
  3190. {
  3191. const cvtt::Tables::BC7SC::Table& table = *(tables[t]);
  3192. ParallelMath::Int16CompFlag pti = punchThroughInvalid[table.m_pBits];
  3193. MUInt15 candidateReconstructed[4];
  3194. MUInt15 candidateEPs[2][4];
  3195. for (int i = 0; i < ParallelMath::ParallelSize; i++)
  3196. {
  3197. for (int ch = 0; ch < numRealChannels; ch++)
  3198. {
  3199. ParallelMath::ScalarUInt16 avgValue = ParallelMath::Extract(intAverage[ch], i);
  3200. assert(avgValue >= 0 && avgValue <= 255);
  3201. const cvtt::Tables::BC7SC::TableEntry &entry = table.m_entries[avgValue];
  3202. ParallelMath::PutUInt15(candidateEPs[0][ch], i, entry.m_min);
  3203. ParallelMath::PutUInt15(candidateEPs[1][ch], i, entry.m_max);
  3204. ParallelMath::PutUInt15(candidateReconstructed[ch], i, entry.m_actualColor);
  3205. }
  3206. }
  3207. MFloat avgError = ParallelMath::MakeFloatZero();
  3208. for (int ch = 0; ch < numRealChannels; ch++)
  3209. {
  3210. MFloat delta = ParallelMath::ToFloat(candidateReconstructed[ch]) - average[ch];
  3211. avgError = avgError + delta * delta * channelWeightsSq[ch];
  3212. }
  3213. ParallelMath::Int16CompFlag better = ParallelMath::FloatFlagToInt16(ParallelMath::Less(avgError, bestAverageError));
  3214. better = ParallelMath::AndNot(pti, better); // Mask out punch-through invalidations
  3215. if (ParallelMath::AnySet(better))
  3216. {
  3217. ParallelMath::ConditionalSet(bestAverageError, ParallelMath::Int16FlagToFloat(better), avgError);
  3218. MUInt15 candidateIndex = ParallelMath::MakeUInt15(table.m_index);
  3219. ParallelMath::ConditionalSet(index, better, candidateIndex);
  3220. for (int ch = 0; ch < numRealChannels; ch++)
  3221. ParallelMath::ConditionalSet(reconstructed[ch], better, candidateReconstructed[ch]);
  3222. for (int epi = 0; epi < 2; epi++)
  3223. for (int ch = 0; ch < numRealChannels; ch++)
  3224. ParallelMath::ConditionalSet(eps[epi][ch], better, candidateEPs[epi][ch]);
  3225. }
  3226. }
  3227. AggregatedError<4> aggError;
  3228. for (int pxi = 0; pxi < shapeLength; pxi++)
  3229. {
  3230. int px = fragmentStart[pxi];
  3231. BCCommon::ComputeErrorLDR<4>(flags, reconstructed, pixels[px], numRealChannels, aggError);
  3232. }
  3233. MFloat error = aggError.Finalize(flags, channelWeightsSq) + staticAlphaError;
  3234. ParallelMath::Int16CompFlag better = ParallelMath::FloatFlagToInt16(ParallelMath::Less(error, shapeBestError));
  3235. if (ParallelMath::AnySet(better))
  3236. {
  3237. shapeBestError = ParallelMath::Min(shapeBestError, error);
  3238. for (int epi = 0; epi < 2; epi++)
  3239. {
  3240. for (int ch = 0; ch < numRealChannels; ch++)
  3241. ParallelMath::ConditionalSet(shapeBestEP[epi][ch], better, eps[epi][ch]);
  3242. }
  3243. for (int pxi = 0; pxi < shapeLength; pxi++)
  3244. ParallelMath::ConditionalSet(fragmentBestIndexes[pxi], better, index);
  3245. }
  3246. }
  3247. static void TrySinglePlane(uint32_t flags, const MUInt15 pixels[16][4], const MFloat floatPixels[16][4], const float channelWeights[4], int numTweakRounds, int numRefineRounds, WorkInfo& work, const ParallelMath::RoundTowardNearestForScope *rtn)
  3248. {
  3249. if (numRefineRounds < 1)
  3250. numRefineRounds = 1;
  3251. if (numTweakRounds < 1)
  3252. numTweakRounds = 1;
  3253. else if (numTweakRounds > MaxTweakRounds)
  3254. numTweakRounds = MaxTweakRounds;
  3255. float channelWeightsSq[4];
  3256. for (int ch = 0; ch < 4; ch++)
  3257. channelWeightsSq[ch] = channelWeights[ch] * channelWeights[ch];
  3258. SinglePlaneTemporaries temps;
  3259. MUInt15 maxAlpha = ParallelMath::MakeUInt15(0);
  3260. MUInt15 minAlpha = ParallelMath::MakeUInt15(255);
  3261. ParallelMath::Int16CompFlag isPunchThrough = ParallelMath::MakeBoolInt16(true);
  3262. for (int px = 0; px < 16; px++)
  3263. {
  3264. MUInt15 a = pixels[px][3];
  3265. maxAlpha = ParallelMath::Max(maxAlpha, a);
  3266. minAlpha = ParallelMath::Min(minAlpha, a);
  3267. isPunchThrough = (isPunchThrough & (ParallelMath::Equal(a, ParallelMath::MakeUInt15(0)) | ParallelMath::Equal(a, ParallelMath::MakeUInt15(255))));
  3268. }
  3269. ParallelMath::Int16CompFlag blockHasNonMaxAlpha = ParallelMath::Less(minAlpha, ParallelMath::MakeUInt15(255));
  3270. ParallelMath::Int16CompFlag blockHasNonZeroAlpha = ParallelMath::Less(ParallelMath::MakeUInt15(0), maxAlpha);
  3271. bool anyBlockHasAlpha = ParallelMath::AnySet(blockHasNonMaxAlpha);
  3272. // Try RGB modes if any block has a min alpha 251 or higher
  3273. bool allowRGBModes = ParallelMath::AnySet(ParallelMath::Less(ParallelMath::MakeUInt15(250), minAlpha));
  3274. // Try mode 7 if any block has alpha.
  3275. // Mode 7 is almost never selected for RGB blocks because mode 4 has very accurate 7.7.7.1 endpoints
  3276. // and its parity bit doesn't affect alpha, meaning mode 7 can only be better in extremely specific
  3277. // situations, and only by at most 1 unit of error per pixel.
  3278. bool allowMode7 = anyBlockHasAlpha;
  3279. MFloat preWeightedPixels[16][4];
  3280. BCCommon::PreWeightPixelsLDR<4>(preWeightedPixels, pixels, channelWeights);
  3281. const int *rgbInitialEPCollapseList = NULL;
  3282. // Get initial RGB endpoints
  3283. if (allowRGBModes)
  3284. {
  3285. const int *shapeList;
  3286. int numShapesToEvaluate;
  3287. if (flags & Flags::BC7_EnablePartitioning)
  3288. {
  3289. if (flags & Flags::BC7_Enable3Subsets)
  3290. {
  3291. shapeList = BC7Data::g_shapeListAll;
  3292. rgbInitialEPCollapseList = BC7Data::g_shapeListAll;
  3293. numShapesToEvaluate = BC7Data::g_numShapesAll;
  3294. }
  3295. else
  3296. {
  3297. shapeList = BC7Data::g_shapeList12;
  3298. rgbInitialEPCollapseList = BC7Data::g_shapeList12Collapse;
  3299. numShapesToEvaluate = BC7Data::g_numShapes12;
  3300. }
  3301. }
  3302. else
  3303. {
  3304. shapeList = BC7Data::g_shapeList1;
  3305. rgbInitialEPCollapseList = BC7Data::g_shapeList1Collapse;
  3306. numShapesToEvaluate = BC7Data::g_numShapes1;
  3307. }
  3308. for (int shapeIter = 0; shapeIter < numShapesToEvaluate; shapeIter++)
  3309. {
  3310. int shape = shapeList[shapeIter];
  3311. int shapeStart = BC7Data::g_shapeRanges[shape][0];
  3312. int shapeSize = BC7Data::g_shapeRanges[shape][1];
  3313. EndpointSelector<3, 8> epSelector;
  3314. for (int epPass = 0; epPass < NumEndpointSelectorPasses; epPass++)
  3315. {
  3316. for (int spx = 0; spx < shapeSize; spx++)
  3317. {
  3318. int px = BC7Data::g_fragments[shapeStart + spx];
  3319. epSelector.ContributePass(preWeightedPixels[px], epPass, ParallelMath::MakeFloat(1.0f));
  3320. }
  3321. epSelector.FinishPass(epPass);
  3322. }
  3323. temps.unfinishedRGB[shapeIter] = epSelector.GetEndpoints(channelWeights);
  3324. }
  3325. }
  3326. const int *rgbaInitialEPCollapseList = BC7Data::g_shapeList12Collapse;
  3327. // Get initial RGBA endpoints
  3328. {
  3329. const int *shapeList = BC7Data::g_shapeList12;
  3330. int numShapesToEvaluate = BC7Data::g_numShapes12;
  3331. for (int shapeIter = 0; shapeIter < numShapesToEvaluate; shapeIter++)
  3332. {
  3333. int shape = shapeList[shapeIter];
  3334. if (anyBlockHasAlpha || !allowRGBModes)
  3335. {
  3336. int shapeStart = BC7Data::g_shapeRanges[shape][0];
  3337. int shapeSize = BC7Data::g_shapeRanges[shape][1];
  3338. EndpointSelector<4, 8> epSelector;
  3339. for (int epPass = 0; epPass < NumEndpointSelectorPasses; epPass++)
  3340. {
  3341. for (int spx = 0; spx < shapeSize; spx++)
  3342. {
  3343. int px = BC7Data::g_fragments[shapeStart + spx];
  3344. epSelector.ContributePass(preWeightedPixels[px], epPass, ParallelMath::MakeFloat(1.0f));
  3345. }
  3346. epSelector.FinishPass(epPass);
  3347. }
  3348. temps.unfinishedRGBA[shapeIter] = epSelector.GetEndpoints(channelWeights);
  3349. }
  3350. else
  3351. {
  3352. temps.unfinishedRGBA[shapeIter] = temps.unfinishedRGB[rgbInitialEPCollapseList[shape]].ExpandTo<4>(255);
  3353. }
  3354. }
  3355. }
  3356. for (uint16_t mode = 0; mode <= 7; mode++)
  3357. {
  3358. if (!(flags & Flags::BC7_EnablePartitioning) && BC7Data::g_modes[mode].m_numSubsets != 1)
  3359. continue;
  3360. if (!(flags & Flags::BC7_Enable3Subsets) && BC7Data::g_modes[mode].m_numSubsets == 3)
  3361. continue;
  3362. if (mode == 4 || mode == 5)
  3363. continue;
  3364. if (mode < 4 && !allowRGBModes)
  3365. continue;
  3366. if (mode == 7 && !allowMode7)
  3367. continue;
  3368. bool isRGB = (mode < 4);
  3369. unsigned int numPartitions = 1 << BC7Data::g_modes[mode].m_partitionBits;
  3370. int numSubsets = BC7Data::g_modes[mode].m_numSubsets;
  3371. int indexPrec = BC7Data::g_modes[mode].m_indexBits;
  3372. int parityBitMax = 1;
  3373. if (BC7Data::g_modes[mode].m_pBitMode == BC7Data::PBitMode_PerEndpoint)
  3374. parityBitMax = 4;
  3375. else if (BC7Data::g_modes[mode].m_pBitMode == BC7Data::PBitMode_PerSubset)
  3376. parityBitMax = 2;
  3377. int numRealChannels = isRGB ? 3 : 4;
  3378. int numShapes;
  3379. const int *shapeList;
  3380. const int *shapeCollapseList;
  3381. if (numSubsets == 1)
  3382. {
  3383. numShapes = BC7Data::g_numShapes1;
  3384. shapeList = BC7Data::g_shapeList1;
  3385. shapeCollapseList = BC7Data::g_shapeList1Collapse;
  3386. }
  3387. else if (numSubsets == 2)
  3388. {
  3389. numShapes = BC7Data::g_numShapes2;
  3390. shapeList = BC7Data::g_shapeList2;
  3391. shapeCollapseList = BC7Data::g_shapeList2Collapse;
  3392. }
  3393. else
  3394. {
  3395. assert(numSubsets == 3);
  3396. if (numPartitions == 16)
  3397. {
  3398. numShapes = BC7Data::g_numShapes3Short;
  3399. shapeList = BC7Data::g_shapeList3Short;
  3400. shapeCollapseList = BC7Data::g_shapeList3ShortCollapse;
  3401. }
  3402. else
  3403. {
  3404. assert(numPartitions == 64);
  3405. numShapes = BC7Data::g_numShapes3;
  3406. shapeList = BC7Data::g_shapeList3;
  3407. shapeCollapseList = BC7Data::g_shapeList3Collapse;
  3408. }
  3409. }
  3410. for (int slot = 0; slot < BC7Data::g_maxFragmentsPerMode; slot++)
  3411. temps.shapeBestError[slot] = ParallelMath::MakeFloat(FLT_MAX);
  3412. for (int shapeIter = 0; shapeIter < numShapes; shapeIter++)
  3413. {
  3414. int shape = shapeList[shapeIter];
  3415. int shapeStart = BC7Data::g_shapeRanges[shape][0];
  3416. int shapeLength = BC7Data::g_shapeRanges[shape][1];
  3417. int shapeCollapsedEvalIndex = shapeCollapseList[shape];
  3418. AggregatedError<1> alphaAggError;
  3419. if (isRGB && anyBlockHasAlpha)
  3420. {
  3421. MUInt15 filledAlpha[1] = { ParallelMath::MakeUInt15(255) };
  3422. for (int pxi = 0; pxi < shapeLength; pxi++)
  3423. {
  3424. int px = BC7Data::g_fragments[shapeStart + pxi];
  3425. MUInt15 original[1] = { pixels[px][3] };
  3426. BCCommon::ComputeErrorLDR<1>(flags, filledAlpha, original, alphaAggError);
  3427. }
  3428. }
  3429. float alphaWeightsSq[1] = { channelWeightsSq[3] };
  3430. MFloat staticAlphaError = alphaAggError.Finalize(flags, alphaWeightsSq);
  3431. assert(shapeCollapsedEvalIndex >= 0);
  3432. MUInt15 tweakBaseEP[MaxTweakRounds][2][4];
  3433. for (int tweak = 0; tweak < numTweakRounds; tweak++)
  3434. {
  3435. if (isRGB)
  3436. {
  3437. temps.unfinishedRGB[rgbInitialEPCollapseList[shape]].FinishLDR(tweak, 1 << indexPrec, tweakBaseEP[tweak][0], tweakBaseEP[tweak][1]);
  3438. tweakBaseEP[tweak][0][3] = tweakBaseEP[tweak][1][3] = ParallelMath::MakeUInt15(255);
  3439. }
  3440. else
  3441. {
  3442. temps.unfinishedRGBA[rgbaInitialEPCollapseList[shape]].FinishLDR(tweak, 1 << indexPrec, tweakBaseEP[tweak][0], tweakBaseEP[tweak][1]);
  3443. }
  3444. }
  3445. ParallelMath::Int16CompFlag punchThroughInvalid[4];
  3446. for (int pIter = 0; pIter < parityBitMax; pIter++)
  3447. {
  3448. punchThroughInvalid[pIter] = ParallelMath::MakeBoolInt16(false);
  3449. if ((flags & Flags::BC7_RespectPunchThrough) && (mode == 6 || mode == 7))
  3450. {
  3451. // Modes 6 and 7 have parity bits that affect alpha
  3452. if (pIter == 0)
  3453. punchThroughInvalid[pIter] = (isPunchThrough & blockHasNonZeroAlpha);
  3454. else if (pIter == parityBitMax - 1)
  3455. punchThroughInvalid[pIter] = (isPunchThrough & blockHasNonMaxAlpha);
  3456. else
  3457. punchThroughInvalid[pIter] = isPunchThrough;
  3458. }
  3459. }
  3460. for (int pIter = 0; pIter < parityBitMax; pIter++)
  3461. {
  3462. if (ParallelMath::AllSet(punchThroughInvalid[pIter]))
  3463. continue;
  3464. bool needPunchThroughCheck = ParallelMath::AnySet(punchThroughInvalid[pIter]);
  3465. for (int tweak = 0; tweak < numTweakRounds; tweak++)
  3466. {
  3467. uint16_t p[2];
  3468. p[0] = (pIter & 1);
  3469. p[1] = ((pIter >> 1) & 1);
  3470. MUInt15 ep[2][4];
  3471. for (int epi = 0; epi < 2; epi++)
  3472. for (int ch = 0; ch < 4; ch++)
  3473. ep[epi][ch] = tweakBaseEP[tweak][epi][ch];
  3474. for (int refine = 0; refine < numRefineRounds; refine++)
  3475. {
  3476. switch (mode)
  3477. {
  3478. case 0:
  3479. CompressEndpoints0(ep, p, rtn);
  3480. break;
  3481. case 1:
  3482. CompressEndpoints1(ep, p[0], rtn);
  3483. break;
  3484. case 2:
  3485. CompressEndpoints2(ep, rtn);
  3486. break;
  3487. case 3:
  3488. CompressEndpoints3(ep, p, rtn);
  3489. break;
  3490. case 6:
  3491. CompressEndpoints6(ep, p, rtn);
  3492. break;
  3493. case 7:
  3494. CompressEndpoints7(ep, p, rtn);
  3495. break;
  3496. default:
  3497. assert(false);
  3498. break;
  3499. };
  3500. MFloat shapeError = ParallelMath::MakeFloatZero();
  3501. IndexSelector<4> indexSelector;
  3502. indexSelector.Init<false>(channelWeights, ep, 1 << indexPrec);
  3503. EndpointRefiner<4> epRefiner;
  3504. epRefiner.Init(1 << indexPrec, channelWeights);
  3505. MUInt15 indexes[16];
  3506. AggregatedError<4> aggError;
  3507. for (int pxi = 0; pxi < shapeLength; pxi++)
  3508. {
  3509. int px = BC7Data::g_fragments[shapeStart + pxi];
  3510. MUInt15 index;
  3511. MUInt15 reconstructed[4];
  3512. index = indexSelector.SelectIndexLDR(floatPixels[px], rtn);
  3513. indexSelector.ReconstructLDR_BC7(index, reconstructed, numRealChannels);
  3514. if (flags & cvtt::Flags::BC7_FastIndexing)
  3515. BCCommon::ComputeErrorLDR<4>(flags, reconstructed, pixels[px], numRealChannels, aggError);
  3516. else
  3517. {
  3518. MFloat error = BCCommon::ComputeErrorLDRSimple<4>(flags, reconstructed, pixels[px], numRealChannels, channelWeightsSq);
  3519. MUInt15 altIndexes[2];
  3520. altIndexes[0] = ParallelMath::Max(index, ParallelMath::MakeUInt15(1)) - ParallelMath::MakeUInt15(1);
  3521. altIndexes[1] = ParallelMath::Min(index + ParallelMath::MakeUInt15(1), ParallelMath::MakeUInt15(static_cast<uint16_t>((1 << indexPrec) - 1)));
  3522. for (int ii = 0; ii < 2; ii++)
  3523. {
  3524. indexSelector.ReconstructLDR_BC7(altIndexes[ii], reconstructed, numRealChannels);
  3525. MFloat altError = BCCommon::ComputeErrorLDRSimple<4>(flags, reconstructed, pixels[px], numRealChannels, channelWeightsSq);
  3526. ParallelMath::Int16CompFlag better = ParallelMath::FloatFlagToInt16(ParallelMath::Less(altError, error));
  3527. error = ParallelMath::Min(error, altError);
  3528. ParallelMath::ConditionalSet(index, better, altIndexes[ii]);
  3529. }
  3530. shapeError = shapeError + error;
  3531. }
  3532. if (refine != numRefineRounds - 1)
  3533. epRefiner.ContributeUnweightedPW(preWeightedPixels[px], index, numRealChannels);
  3534. indexes[pxi] = index;
  3535. }
  3536. if (flags & cvtt::Flags::BC7_FastIndexing)
  3537. shapeError = aggError.Finalize(flags, channelWeightsSq);
  3538. if (isRGB)
  3539. shapeError = shapeError + staticAlphaError;
  3540. ParallelMath::FloatCompFlag shapeErrorBetter;
  3541. ParallelMath::Int16CompFlag shapeErrorBetter16;
  3542. shapeErrorBetter = ParallelMath::Less(shapeError, temps.shapeBestError[shapeCollapsedEvalIndex]);
  3543. shapeErrorBetter16 = ParallelMath::FloatFlagToInt16(shapeErrorBetter);
  3544. if (ParallelMath::AnySet(shapeErrorBetter16))
  3545. {
  3546. bool punchThroughOK = true;
  3547. if (needPunchThroughCheck)
  3548. {
  3549. shapeErrorBetter16 = ParallelMath::AndNot(punchThroughInvalid[pIter], shapeErrorBetter16);
  3550. shapeErrorBetter = ParallelMath::Int16FlagToFloat(shapeErrorBetter16);
  3551. if (!ParallelMath::AnySet(shapeErrorBetter16))
  3552. punchThroughOK = false;
  3553. }
  3554. if (punchThroughOK)
  3555. {
  3556. ParallelMath::ConditionalSet(temps.shapeBestError[shapeCollapsedEvalIndex], shapeErrorBetter, shapeError);
  3557. for (int epi = 0; epi < 2; epi++)
  3558. for (int ch = 0; ch < numRealChannels; ch++)
  3559. ParallelMath::ConditionalSet(temps.shapeBestEP[shapeCollapsedEvalIndex][epi][ch], shapeErrorBetter16, ep[epi][ch]);
  3560. for (int pxi = 0; pxi < shapeLength; pxi++)
  3561. ParallelMath::ConditionalSet(temps.fragmentBestIndexes[shapeStart + pxi], shapeErrorBetter16, indexes[pxi]);
  3562. }
  3563. }
  3564. if (refine != numRefineRounds - 1)
  3565. epRefiner.GetRefinedEndpointsLDR(ep, numRealChannels, rtn);
  3566. } // refine
  3567. } // tweak
  3568. } // p
  3569. if (flags & cvtt::Flags::BC7_TrySingleColor)
  3570. {
  3571. MUInt15 total[4];
  3572. for (int ch = 0; ch < 4; ch++)
  3573. total[ch] = ParallelMath::MakeUInt15(0);
  3574. for (int pxi = 0; pxi < shapeLength; pxi++)
  3575. {
  3576. int px = BC7Data::g_fragments[shapeStart + pxi];
  3577. for (int ch = 0; ch < 4; ch++)
  3578. total[ch] = total[ch] + pixels[pxi][ch];
  3579. }
  3580. MFloat rcpShapeLength = ParallelMath::MakeFloat(1.0f / static_cast<float>(shapeLength));
  3581. MFloat average[4];
  3582. for (int ch = 0; ch < 4; ch++)
  3583. average[ch] = ParallelMath::ToFloat(total[ch]) * rcpShapeLength;
  3584. const uint8_t *fragment = BC7Data::g_fragments + shapeStart;
  3585. MFloat &shapeBestError = temps.shapeBestError[shapeCollapsedEvalIndex];
  3586. MUInt15(&shapeBestEP)[2][4] = temps.shapeBestEP[shapeCollapsedEvalIndex];
  3587. MUInt15 *fragmentBestIndexes = temps.fragmentBestIndexes + shapeStart;
  3588. const cvtt::Tables::BC7SC::Table **scTables = NULL;
  3589. int numSCTables = 0;
  3590. switch (mode)
  3591. {
  3592. case 0:
  3593. {
  3594. const cvtt::Tables::BC7SC::Table *tables[] =
  3595. {
  3596. &cvtt::Tables::BC7SC::g_mode0_p00_i1,
  3597. &cvtt::Tables::BC7SC::g_mode0_p00_i2,
  3598. &cvtt::Tables::BC7SC::g_mode0_p00_i3,
  3599. &cvtt::Tables::BC7SC::g_mode0_p01_i1,
  3600. &cvtt::Tables::BC7SC::g_mode0_p01_i2,
  3601. &cvtt::Tables::BC7SC::g_mode0_p01_i3,
  3602. &cvtt::Tables::BC7SC::g_mode0_p10_i1,
  3603. &cvtt::Tables::BC7SC::g_mode0_p10_i2,
  3604. &cvtt::Tables::BC7SC::g_mode0_p10_i3,
  3605. &cvtt::Tables::BC7SC::g_mode0_p11_i1,
  3606. &cvtt::Tables::BC7SC::g_mode0_p11_i2,
  3607. &cvtt::Tables::BC7SC::g_mode0_p11_i3,
  3608. };
  3609. scTables = tables;
  3610. numSCTables = sizeof(tables) / sizeof(tables[0]);
  3611. }
  3612. break;
  3613. case 1:
  3614. {
  3615. const cvtt::Tables::BC7SC::Table *tables[] =
  3616. {
  3617. &cvtt::Tables::BC7SC::g_mode1_p0_i1,
  3618. &cvtt::Tables::BC7SC::g_mode1_p0_i2,
  3619. &cvtt::Tables::BC7SC::g_mode1_p0_i3,
  3620. &cvtt::Tables::BC7SC::g_mode1_p1_i1,
  3621. &cvtt::Tables::BC7SC::g_mode1_p1_i2,
  3622. &cvtt::Tables::BC7SC::g_mode1_p1_i3,
  3623. };
  3624. scTables = tables;
  3625. numSCTables = sizeof(tables) / sizeof(tables[0]);
  3626. }
  3627. break;
  3628. case 2:
  3629. {
  3630. const cvtt::Tables::BC7SC::Table *tables[] =
  3631. {
  3632. &cvtt::Tables::BC7SC::g_mode2,
  3633. };
  3634. scTables = tables;
  3635. numSCTables = sizeof(tables) / sizeof(tables[0]);
  3636. }
  3637. break;
  3638. case 3:
  3639. {
  3640. const cvtt::Tables::BC7SC::Table *tables[] =
  3641. {
  3642. &cvtt::Tables::BC7SC::g_mode3_p0,
  3643. &cvtt::Tables::BC7SC::g_mode3_p1,
  3644. };
  3645. scTables = tables;
  3646. numSCTables = sizeof(tables) / sizeof(tables[0]);
  3647. }
  3648. break;
  3649. case 6:
  3650. {
  3651. const cvtt::Tables::BC7SC::Table *tables[] =
  3652. {
  3653. &cvtt::Tables::BC7SC::g_mode6_p0_i1,
  3654. &cvtt::Tables::BC7SC::g_mode6_p0_i2,
  3655. &cvtt::Tables::BC7SC::g_mode6_p0_i3,
  3656. &cvtt::Tables::BC7SC::g_mode6_p0_i4,
  3657. &cvtt::Tables::BC7SC::g_mode6_p0_i5,
  3658. &cvtt::Tables::BC7SC::g_mode6_p0_i6,
  3659. &cvtt::Tables::BC7SC::g_mode6_p0_i7,
  3660. &cvtt::Tables::BC7SC::g_mode6_p1_i1,
  3661. &cvtt::Tables::BC7SC::g_mode6_p1_i2,
  3662. &cvtt::Tables::BC7SC::g_mode6_p1_i3,
  3663. &cvtt::Tables::BC7SC::g_mode6_p1_i4,
  3664. &cvtt::Tables::BC7SC::g_mode6_p1_i5,
  3665. &cvtt::Tables::BC7SC::g_mode6_p1_i6,
  3666. &cvtt::Tables::BC7SC::g_mode6_p1_i7,
  3667. };
  3668. scTables = tables;
  3669. numSCTables = sizeof(tables) / sizeof(tables[0]);
  3670. }
  3671. break;
  3672. case 7:
  3673. {
  3674. const cvtt::Tables::BC7SC::Table *tables[] =
  3675. {
  3676. &cvtt::Tables::BC7SC::g_mode7_p00,
  3677. &cvtt::Tables::BC7SC::g_mode7_p01,
  3678. &cvtt::Tables::BC7SC::g_mode7_p10,
  3679. &cvtt::Tables::BC7SC::g_mode7_p11,
  3680. };
  3681. scTables = tables;
  3682. numSCTables = sizeof(tables) / sizeof(tables[0]);
  3683. }
  3684. break;
  3685. default:
  3686. assert(false);
  3687. break;
  3688. }
  3689. TrySingleColorRGBAMultiTable(flags, pixels, average, numRealChannels, fragment, shapeLength, staticAlphaError, punchThroughInvalid, shapeBestError, shapeBestEP, fragmentBestIndexes, channelWeightsSq, scTables, numSCTables, rtn);
  3690. }
  3691. } // shapeIter
  3692. for (uint16_t partition = 0; partition < numPartitions; partition++)
  3693. {
  3694. const int *partitionShapes;
  3695. if (numSubsets == 1)
  3696. partitionShapes = BC7Data::g_shapes1[partition];
  3697. else if (numSubsets == 2)
  3698. partitionShapes = BC7Data::g_shapes2[partition];
  3699. else
  3700. {
  3701. assert(numSubsets == 3);
  3702. partitionShapes = BC7Data::g_shapes3[partition];
  3703. }
  3704. MFloat totalError = ParallelMath::MakeFloatZero();
  3705. for (int subset = 0; subset < numSubsets; subset++)
  3706. totalError = totalError + temps.shapeBestError[shapeCollapseList[partitionShapes[subset]]];
  3707. ParallelMath::FloatCompFlag errorBetter = ParallelMath::Less(totalError, work.m_error);
  3708. ParallelMath::Int16CompFlag errorBetter16 = ParallelMath::FloatFlagToInt16(errorBetter);
  3709. if (ParallelMath::AnySet(errorBetter16))
  3710. {
  3711. for (int subset = 0; subset < numSubsets; subset++)
  3712. {
  3713. int shape = partitionShapes[subset];
  3714. int shapeStart = BC7Data::g_shapeRanges[shape][0];
  3715. int shapeLength = BC7Data::g_shapeRanges[shape][1];
  3716. int shapeCollapsedEvalIndex = shapeCollapseList[shape];
  3717. for (int epi = 0; epi < 2; epi++)
  3718. for (int ch = 0; ch < 4; ch++)
  3719. ParallelMath::ConditionalSet(work.m_ep[subset][epi][ch], errorBetter16, temps.shapeBestEP[shapeCollapsedEvalIndex][epi][ch]);
  3720. for (int pxi = 0; pxi < shapeLength; pxi++)
  3721. {
  3722. int px = BC7Data::g_fragments[shapeStart + pxi];
  3723. ParallelMath::ConditionalSet(work.m_indexes[px], errorBetter16, temps.fragmentBestIndexes[shapeStart + pxi]);
  3724. }
  3725. }
  3726. work.m_error = ParallelMath::Min(totalError, work.m_error);
  3727. ParallelMath::ConditionalSet(work.m_mode, errorBetter16, ParallelMath::MakeUInt15(mode));
  3728. ParallelMath::ConditionalSet(work.m_u.m_partition, errorBetter16, ParallelMath::MakeUInt15(partition));
  3729. }
  3730. }
  3731. }
  3732. }
  3733. static void TryDualPlane(uint32_t flags, const MUInt15 pixels[16][4], const MFloat floatPixels[16][4], const float channelWeights[4], int numTweakRounds, int numRefineRounds, WorkInfo& work, const ParallelMath::RoundTowardNearestForScope *rtn)
  3734. {
  3735. // TODO: These error calculations are not optimal for weight-by-alpha, but this routine needs to be mostly rewritten for that.
  3736. // The alpha/color solutions are co-dependent in that case, but a good way to solve it would probably be to
  3737. // solve the alpha channel first, then solve the RGB channels, which in turn breaks down into two cases:
  3738. // - Separate alpha channel, then weighted RGB
  3739. // - Alpha+2 other channels, then the independent channel
  3740. if (!(flags & Flags::BC7_EnableDualPlane))
  3741. return;
  3742. if (numRefineRounds < 1)
  3743. numRefineRounds = 1;
  3744. if (numTweakRounds < 1)
  3745. numTweakRounds = 1;
  3746. else if (numTweakRounds > MaxTweakRounds)
  3747. numTweakRounds = MaxTweakRounds;
  3748. float channelWeightsSq[4];
  3749. for (int ch = 0; ch < 4; ch++)
  3750. channelWeightsSq[ch] = channelWeights[ch] * channelWeights[ch];
  3751. for (uint16_t mode = 4; mode <= 5; mode++)
  3752. {
  3753. for (uint16_t rotation = 0; rotation < 4; rotation++)
  3754. {
  3755. int alphaChannel = (rotation + 3) & 3;
  3756. int redChannel = (rotation == 1) ? 3 : 0;
  3757. int greenChannel = (rotation == 2) ? 3 : 1;
  3758. int blueChannel = (rotation == 3) ? 3 : 2;
  3759. MUInt15 rotatedRGB[16][3];
  3760. MFloat floatRotatedRGB[16][3];
  3761. for (int px = 0; px < 16; px++)
  3762. {
  3763. rotatedRGB[px][0] = pixels[px][redChannel];
  3764. rotatedRGB[px][1] = pixels[px][greenChannel];
  3765. rotatedRGB[px][2] = pixels[px][blueChannel];
  3766. for (int ch = 0; ch < 3; ch++)
  3767. floatRotatedRGB[px][ch] = ParallelMath::ToFloat(rotatedRGB[px][ch]);
  3768. }
  3769. uint16_t maxIndexSelector = (mode == 4) ? 2 : 1;
  3770. float rotatedRGBWeights[3] = { channelWeights[redChannel], channelWeights[greenChannel], channelWeights[blueChannel] };
  3771. float rotatedRGBWeightsSq[3] = { channelWeightsSq[redChannel], channelWeightsSq[greenChannel], channelWeightsSq[blueChannel] };
  3772. float rotatedAlphaWeight[1] = { channelWeights[alphaChannel] };
  3773. float rotatedAlphaWeightSq[1] = { channelWeightsSq[alphaChannel] };
  3774. float uniformWeight[1] = { 1.0f }; // Since the alpha channel is independent, there's no need to bother with weights when doing refinement or selection, only error
  3775. MFloat preWeightedRotatedRGB[16][3];
  3776. BCCommon::PreWeightPixelsLDR<3>(preWeightedRotatedRGB, rotatedRGB, rotatedRGBWeights);
  3777. for (uint16_t indexSelector = 0; indexSelector < maxIndexSelector; indexSelector++)
  3778. {
  3779. EndpointSelector<3, 8> rgbSelector;
  3780. for (int epPass = 0; epPass < NumEndpointSelectorPasses; epPass++)
  3781. {
  3782. for (int px = 0; px < 16; px++)
  3783. rgbSelector.ContributePass(preWeightedRotatedRGB[px], epPass, ParallelMath::MakeFloat(1.0f));
  3784. rgbSelector.FinishPass(epPass);
  3785. }
  3786. MUInt15 alphaRange[2];
  3787. alphaRange[0] = alphaRange[1] = pixels[0][alphaChannel];
  3788. for (int px = 1; px < 16; px++)
  3789. {
  3790. alphaRange[0] = ParallelMath::Min(pixels[px][alphaChannel], alphaRange[0]);
  3791. alphaRange[1] = ParallelMath::Max(pixels[px][alphaChannel], alphaRange[1]);
  3792. }
  3793. int rgbPrec = 0;
  3794. int alphaPrec = 0;
  3795. if (mode == 4)
  3796. {
  3797. rgbPrec = indexSelector ? 3 : 2;
  3798. alphaPrec = indexSelector ? 2 : 3;
  3799. }
  3800. else
  3801. rgbPrec = alphaPrec = 2;
  3802. UnfinishedEndpoints<3> unfinishedRGB = rgbSelector.GetEndpoints(rotatedRGBWeights);
  3803. MFloat bestRGBError = ParallelMath::MakeFloat(FLT_MAX);
  3804. MFloat bestAlphaError = ParallelMath::MakeFloat(FLT_MAX);
  3805. MUInt15 bestRGBIndexes[16];
  3806. MUInt15 bestAlphaIndexes[16];
  3807. MUInt15 bestEP[2][4];
  3808. for (int px = 0; px < 16; px++)
  3809. bestRGBIndexes[px] = bestAlphaIndexes[px] = ParallelMath::MakeUInt15(0);
  3810. for (int tweak = 0; tweak < numTweakRounds; tweak++)
  3811. {
  3812. MUInt15 rgbEP[2][3];
  3813. MUInt15 alphaEP[2];
  3814. unfinishedRGB.FinishLDR(tweak, 1 << rgbPrec, rgbEP[0], rgbEP[1]);
  3815. TweakAlpha(alphaRange, tweak, 1 << alphaPrec, alphaEP);
  3816. for (int refine = 0; refine < numRefineRounds; refine++)
  3817. {
  3818. if (mode == 4)
  3819. CompressEndpoints4(rgbEP, alphaEP, rtn);
  3820. else
  3821. CompressEndpoints5(rgbEP, alphaEP, rtn);
  3822. IndexSelector<1> alphaIndexSelector;
  3823. IndexSelector<3> rgbIndexSelector;
  3824. {
  3825. MUInt15 alphaEPTemp[2][1] = { { alphaEP[0] },{ alphaEP[1] } };
  3826. alphaIndexSelector.Init<false>(uniformWeight, alphaEPTemp, 1 << alphaPrec);
  3827. }
  3828. rgbIndexSelector.Init<false>(rotatedRGBWeights, rgbEP, 1 << rgbPrec);
  3829. EndpointRefiner<3> rgbRefiner;
  3830. EndpointRefiner<1> alphaRefiner;
  3831. rgbRefiner.Init(1 << rgbPrec, rotatedRGBWeights);
  3832. alphaRefiner.Init(1 << alphaPrec, uniformWeight);
  3833. MFloat errorRGB = ParallelMath::MakeFloatZero();
  3834. MFloat errorA = ParallelMath::MakeFloatZero();
  3835. MUInt15 rgbIndexes[16];
  3836. MUInt15 alphaIndexes[16];
  3837. AggregatedError<3> rgbAggError;
  3838. AggregatedError<1> alphaAggError;
  3839. for (int px = 0; px < 16; px++)
  3840. {
  3841. MUInt15 rgbIndex = rgbIndexSelector.SelectIndexLDR(floatRotatedRGB[px], rtn);
  3842. MUInt15 alphaIndex = alphaIndexSelector.SelectIndexLDR(floatPixels[px] + alphaChannel, rtn);
  3843. MUInt15 reconstructedRGB[3];
  3844. MUInt15 reconstructedAlpha[1];
  3845. rgbIndexSelector.ReconstructLDR_BC7(rgbIndex, reconstructedRGB);
  3846. alphaIndexSelector.ReconstructLDR_BC7(alphaIndex, reconstructedAlpha);
  3847. if (flags & cvtt::Flags::BC7_FastIndexing)
  3848. {
  3849. BCCommon::ComputeErrorLDR<3>(flags, reconstructedRGB, rotatedRGB[px], rgbAggError);
  3850. BCCommon::ComputeErrorLDR<1>(flags, reconstructedAlpha, pixels[px] + alphaChannel, alphaAggError);
  3851. }
  3852. else
  3853. {
  3854. AggregatedError<3> baseRGBAggError;
  3855. AggregatedError<1> baseAlphaAggError;
  3856. BCCommon::ComputeErrorLDR<3>(flags, reconstructedRGB, rotatedRGB[px], baseRGBAggError);
  3857. BCCommon::ComputeErrorLDR<1>(flags, reconstructedAlpha, pixels[px] + alphaChannel, baseAlphaAggError);
  3858. MFloat rgbError = baseRGBAggError.Finalize(flags, rotatedRGBWeightsSq);
  3859. MFloat alphaError = baseAlphaAggError.Finalize(flags, rotatedAlphaWeightSq);
  3860. MUInt15 altRGBIndexes[2];
  3861. MUInt15 altAlphaIndexes[2];
  3862. altRGBIndexes[0] = ParallelMath::Max(rgbIndex, ParallelMath::MakeUInt15(1)) - ParallelMath::MakeUInt15(1);
  3863. altRGBIndexes[1] = ParallelMath::Min(rgbIndex + ParallelMath::MakeUInt15(1), ParallelMath::MakeUInt15(static_cast<uint16_t>((1 << rgbPrec) - 1)));
  3864. altAlphaIndexes[0] = ParallelMath::Max(alphaIndex, ParallelMath::MakeUInt15(1)) - ParallelMath::MakeUInt15(1);
  3865. altAlphaIndexes[1] = ParallelMath::Min(alphaIndex + ParallelMath::MakeUInt15(1), ParallelMath::MakeUInt15(static_cast<uint16_t>((1 << alphaPrec) - 1)));
  3866. for (int ii = 0; ii < 2; ii++)
  3867. {
  3868. rgbIndexSelector.ReconstructLDR_BC7(altRGBIndexes[ii], reconstructedRGB);
  3869. alphaIndexSelector.ReconstructLDR_BC7(altAlphaIndexes[ii], reconstructedAlpha);
  3870. AggregatedError<3> altRGBAggError;
  3871. AggregatedError<1> altAlphaAggError;
  3872. BCCommon::ComputeErrorLDR<3>(flags, reconstructedRGB, rotatedRGB[px], altRGBAggError);
  3873. BCCommon::ComputeErrorLDR<1>(flags, reconstructedAlpha, pixels[px] + alphaChannel, altAlphaAggError);
  3874. MFloat altRGBError = altRGBAggError.Finalize(flags, rotatedRGBWeightsSq);
  3875. MFloat altAlphaError = altAlphaAggError.Finalize(flags, rotatedAlphaWeightSq);
  3876. ParallelMath::Int16CompFlag rgbBetter = ParallelMath::FloatFlagToInt16(ParallelMath::Less(altRGBError, rgbError));
  3877. ParallelMath::Int16CompFlag alphaBetter = ParallelMath::FloatFlagToInt16(ParallelMath::Less(altAlphaError, alphaError));
  3878. rgbError = ParallelMath::Min(altRGBError, rgbError);
  3879. alphaError = ParallelMath::Min(altAlphaError, alphaError);
  3880. ParallelMath::ConditionalSet(rgbIndex, rgbBetter, altRGBIndexes[ii]);
  3881. ParallelMath::ConditionalSet(alphaIndex, alphaBetter, altAlphaIndexes[ii]);
  3882. }
  3883. errorRGB = errorRGB + rgbError;
  3884. errorA = errorA + alphaError;
  3885. }
  3886. if (refine != numRefineRounds - 1)
  3887. {
  3888. rgbRefiner.ContributeUnweightedPW(preWeightedRotatedRGB[px], rgbIndex);
  3889. alphaRefiner.ContributeUnweightedPW(floatPixels[px] + alphaChannel, alphaIndex);
  3890. }
  3891. if (flags & Flags::BC7_FastIndexing)
  3892. {
  3893. errorRGB = rgbAggError.Finalize(flags, rotatedRGBWeightsSq);
  3894. errorA = rgbAggError.Finalize(flags, rotatedAlphaWeightSq);
  3895. }
  3896. rgbIndexes[px] = rgbIndex;
  3897. alphaIndexes[px] = alphaIndex;
  3898. }
  3899. ParallelMath::FloatCompFlag rgbBetter = ParallelMath::Less(errorRGB, bestRGBError);
  3900. ParallelMath::FloatCompFlag alphaBetter = ParallelMath::Less(errorA, bestAlphaError);
  3901. ParallelMath::Int16CompFlag rgbBetterInt16 = ParallelMath::FloatFlagToInt16(rgbBetter);
  3902. ParallelMath::Int16CompFlag alphaBetterInt16 = ParallelMath::FloatFlagToInt16(alphaBetter);
  3903. if (ParallelMath::AnySet(rgbBetterInt16))
  3904. {
  3905. bestRGBError = ParallelMath::Min(errorRGB, bestRGBError);
  3906. for (int px = 0; px < 16; px++)
  3907. ParallelMath::ConditionalSet(bestRGBIndexes[px], rgbBetterInt16, rgbIndexes[px]);
  3908. for (int ep = 0; ep < 2; ep++)
  3909. {
  3910. for (int ch = 0; ch < 3; ch++)
  3911. ParallelMath::ConditionalSet(bestEP[ep][ch], rgbBetterInt16, rgbEP[ep][ch]);
  3912. }
  3913. }
  3914. if (ParallelMath::AnySet(alphaBetterInt16))
  3915. {
  3916. bestAlphaError = ParallelMath::Min(errorA, bestAlphaError);
  3917. for (int px = 0; px < 16; px++)
  3918. ParallelMath::ConditionalSet(bestAlphaIndexes[px], alphaBetterInt16, alphaIndexes[px]);
  3919. for (int ep = 0; ep < 2; ep++)
  3920. ParallelMath::ConditionalSet(bestEP[ep][3], alphaBetterInt16, alphaEP[ep]);
  3921. }
  3922. if (refine != numRefineRounds - 1)
  3923. {
  3924. rgbRefiner.GetRefinedEndpointsLDR(rgbEP, rtn);
  3925. MUInt15 alphaEPTemp[2][1];
  3926. alphaRefiner.GetRefinedEndpointsLDR(alphaEPTemp, rtn);
  3927. for (int i = 0; i < 2; i++)
  3928. alphaEP[i] = alphaEPTemp[i][0];
  3929. }
  3930. } // refine
  3931. } // tweak
  3932. MFloat combinedError = bestRGBError + bestAlphaError;
  3933. ParallelMath::FloatCompFlag errorBetter = ParallelMath::Less(combinedError, work.m_error);
  3934. ParallelMath::Int16CompFlag errorBetter16 = ParallelMath::FloatFlagToInt16(errorBetter);
  3935. work.m_error = ParallelMath::Min(combinedError, work.m_error);
  3936. ParallelMath::ConditionalSet(work.m_mode, errorBetter16, ParallelMath::MakeUInt15(mode));
  3937. ParallelMath::ConditionalSet(work.m_u.m_isr.m_rotation, errorBetter16, ParallelMath::MakeUInt15(rotation));
  3938. ParallelMath::ConditionalSet(work.m_u.m_isr.m_indexSelector, errorBetter16, ParallelMath::MakeUInt15(indexSelector));
  3939. for (int px = 0; px < 16; px++)
  3940. {
  3941. ParallelMath::ConditionalSet(work.m_indexes[px], errorBetter16, indexSelector ? bestAlphaIndexes[px] : bestRGBIndexes[px]);
  3942. ParallelMath::ConditionalSet(work.m_indexes2[px], errorBetter16, indexSelector ? bestRGBIndexes[px] : bestAlphaIndexes[px]);
  3943. }
  3944. for (int ep = 0; ep < 2; ep++)
  3945. for (int ch = 0; ch < 4; ch++)
  3946. ParallelMath::ConditionalSet(work.m_ep[0][ep][ch], errorBetter16, bestEP[ep][ch]);
  3947. }
  3948. }
  3949. }
  3950. }
  3951. template<class T>
  3952. static void Swap(T& a, T& b)
  3953. {
  3954. T temp = a;
  3955. a = b;
  3956. b = temp;
  3957. }
  3958. static void Pack(uint32_t flags, const PixelBlockU8* inputs, uint8_t* packedBlocks, const float channelWeights[4], int numTweakRounds, int numRefineRounds)
  3959. {
  3960. MUInt15 pixels[16][4];
  3961. MFloat floatPixels[16][4];
  3962. for (int px = 0; px < 16; px++)
  3963. {
  3964. for (int ch = 0; ch < 4; ch++)
  3965. ParallelMath::ConvertLDRInputs(inputs, px, ch, pixels[px][ch]);
  3966. }
  3967. for (int px = 0; px < 16; px++)
  3968. {
  3969. for (int ch = 0; ch < 4; ch++)
  3970. floatPixels[px][ch] = ParallelMath::ToFloat(pixels[px][ch]);
  3971. }
  3972. WorkInfo work;
  3973. memset(&work, 0, sizeof(work));
  3974. work.m_error = ParallelMath::MakeFloat(FLT_MAX);
  3975. {
  3976. ParallelMath::RoundTowardNearestForScope rtn;
  3977. TrySinglePlane(flags, pixels, floatPixels, channelWeights, numTweakRounds, numRefineRounds, work, &rtn);
  3978. TryDualPlane(flags, pixels, floatPixels, channelWeights, numTweakRounds, numRefineRounds, work, &rtn);
  3979. }
  3980. for (int block = 0; block < ParallelMath::ParallelSize; block++)
  3981. {
  3982. PackingVector pv;
  3983. pv.Init();
  3984. ParallelMath::ScalarUInt16 mode = ParallelMath::Extract(work.m_mode, block);
  3985. ParallelMath::ScalarUInt16 partition = ParallelMath::Extract(work.m_u.m_partition, block);
  3986. ParallelMath::ScalarUInt16 indexSelector = ParallelMath::Extract(work.m_u.m_isr.m_indexSelector, block);
  3987. const BC7Data::BC7ModeInfo& modeInfo = BC7Data::g_modes[mode];
  3988. ParallelMath::ScalarUInt16 indexes[16];
  3989. ParallelMath::ScalarUInt16 indexes2[16];
  3990. ParallelMath::ScalarUInt16 endPoints[3][2][4];
  3991. for (int i = 0; i < 16; i++)
  3992. {
  3993. indexes[i] = ParallelMath::Extract(work.m_indexes[i], block);
  3994. if (modeInfo.m_alphaMode == BC7Data::AlphaMode_Separate)
  3995. indexes2[i] = ParallelMath::Extract(work.m_indexes2[i], block);
  3996. }
  3997. for (int subset = 0; subset < 3; subset++)
  3998. {
  3999. for (int ep = 0; ep < 2; ep++)
  4000. {
  4001. for (int ch = 0; ch < 4; ch++)
  4002. endPoints[subset][ep][ch] = ParallelMath::Extract(work.m_ep[subset][ep][ch], block);
  4003. }
  4004. }
  4005. int fixups[3] = { 0, 0, 0 };
  4006. if (modeInfo.m_alphaMode == BC7Data::AlphaMode_Separate)
  4007. {
  4008. bool flipRGB = ((indexes[0] & (1 << (modeInfo.m_indexBits - 1))) != 0);
  4009. bool flipAlpha = ((indexes2[0] & (1 << (modeInfo.m_alphaIndexBits - 1))) != 0);
  4010. if (flipRGB)
  4011. {
  4012. uint16_t highIndex = (1 << modeInfo.m_indexBits) - 1;
  4013. for (int px = 0; px < 16; px++)
  4014. indexes[px] = highIndex - indexes[px];
  4015. }
  4016. if (flipAlpha)
  4017. {
  4018. uint16_t highIndex = (1 << modeInfo.m_alphaIndexBits) - 1;
  4019. for (int px = 0; px < 16; px++)
  4020. indexes2[px] = highIndex - indexes2[px];
  4021. }
  4022. if (indexSelector)
  4023. Swap(flipRGB, flipAlpha);
  4024. if (flipRGB)
  4025. {
  4026. for (int ch = 0; ch < 3; ch++)
  4027. Swap(endPoints[0][0][ch], endPoints[0][1][ch]);
  4028. }
  4029. if (flipAlpha)
  4030. Swap(endPoints[0][0][3], endPoints[0][1][3]);
  4031. }
  4032. else
  4033. {
  4034. if (modeInfo.m_numSubsets == 2)
  4035. fixups[1] = BC7Data::g_fixupIndexes2[partition];
  4036. else if (modeInfo.m_numSubsets == 3)
  4037. {
  4038. fixups[1] = BC7Data::g_fixupIndexes3[partition][0];
  4039. fixups[2] = BC7Data::g_fixupIndexes3[partition][1];
  4040. }
  4041. bool flip[3] = { false, false, false };
  4042. for (int subset = 0; subset < modeInfo.m_numSubsets; subset++)
  4043. flip[subset] = ((indexes[fixups[subset]] & (1 << (modeInfo.m_indexBits - 1))) != 0);
  4044. if (flip[0] || flip[1] || flip[2])
  4045. {
  4046. uint16_t highIndex = (1 << modeInfo.m_indexBits) - 1;
  4047. for (int px = 0; px < 16; px++)
  4048. {
  4049. int subset = 0;
  4050. if (modeInfo.m_numSubsets == 2)
  4051. subset = (BC7Data::g_partitionMap[partition] >> px) & 1;
  4052. else if (modeInfo.m_numSubsets == 3)
  4053. subset = (BC7Data::g_partitionMap2[partition] >> (px * 2)) & 3;
  4054. if (flip[subset])
  4055. indexes[px] = highIndex - indexes[px];
  4056. }
  4057. int maxCH = (modeInfo.m_alphaMode == BC7Data::AlphaMode_Combined) ? 4 : 3;
  4058. for (int subset = 0; subset < modeInfo.m_numSubsets; subset++)
  4059. {
  4060. if (flip[subset])
  4061. for (int ch = 0; ch < maxCH; ch++)
  4062. Swap(endPoints[subset][0][ch], endPoints[subset][1][ch]);
  4063. }
  4064. }
  4065. }
  4066. pv.Pack(static_cast<uint8_t>(1 << mode), mode + 1);
  4067. if (modeInfo.m_partitionBits)
  4068. pv.Pack(partition, modeInfo.m_partitionBits);
  4069. if (modeInfo.m_alphaMode == BC7Data::AlphaMode_Separate)
  4070. {
  4071. ParallelMath::ScalarUInt16 rotation = ParallelMath::Extract(work.m_u.m_isr.m_rotation, block);
  4072. pv.Pack(rotation, 2);
  4073. }
  4074. if (modeInfo.m_hasIndexSelector)
  4075. pv.Pack(indexSelector, 1);
  4076. // Encode RGB
  4077. for (int ch = 0; ch < 3; ch++)
  4078. {
  4079. for (int subset = 0; subset < modeInfo.m_numSubsets; subset++)
  4080. {
  4081. for (int ep = 0; ep < 2; ep++)
  4082. {
  4083. ParallelMath::ScalarUInt16 epPart = endPoints[subset][ep][ch];
  4084. epPart >>= (8 - modeInfo.m_rgbBits);
  4085. pv.Pack(epPart, modeInfo.m_rgbBits);
  4086. }
  4087. }
  4088. }
  4089. // Encode alpha
  4090. if (modeInfo.m_alphaMode != BC7Data::AlphaMode_None)
  4091. {
  4092. for (int subset = 0; subset < modeInfo.m_numSubsets; subset++)
  4093. {
  4094. for (int ep = 0; ep < 2; ep++)
  4095. {
  4096. ParallelMath::ScalarUInt16 epPart = endPoints[subset][ep][3];
  4097. epPart >>= (8 - modeInfo.m_alphaBits);
  4098. pv.Pack(epPart, modeInfo.m_alphaBits);
  4099. }
  4100. }
  4101. }
  4102. // Encode parity bits
  4103. if (modeInfo.m_pBitMode == BC7Data::PBitMode_PerSubset)
  4104. {
  4105. for (int subset = 0; subset < modeInfo.m_numSubsets; subset++)
  4106. {
  4107. ParallelMath::ScalarUInt16 epPart = endPoints[subset][0][0];
  4108. epPart >>= (7 - modeInfo.m_rgbBits);
  4109. epPart &= 1;
  4110. pv.Pack(epPart, 1);
  4111. }
  4112. }
  4113. else if (modeInfo.m_pBitMode == BC7Data::PBitMode_PerEndpoint)
  4114. {
  4115. for (int subset = 0; subset < modeInfo.m_numSubsets; subset++)
  4116. {
  4117. for (int ep = 0; ep < 2; ep++)
  4118. {
  4119. ParallelMath::ScalarUInt16 epPart = endPoints[subset][ep][0];
  4120. epPart >>= (7 - modeInfo.m_rgbBits);
  4121. epPart &= 1;
  4122. pv.Pack(epPart, 1);
  4123. }
  4124. }
  4125. }
  4126. // Encode indexes
  4127. for (int px = 0; px < 16; px++)
  4128. {
  4129. int bits = modeInfo.m_indexBits;
  4130. if ((px == 0) || (px == fixups[1]) || (px == fixups[2]))
  4131. bits--;
  4132. pv.Pack(indexes[px], bits);
  4133. }
  4134. // Encode secondary indexes
  4135. if (modeInfo.m_alphaMode == BC7Data::AlphaMode_Separate)
  4136. {
  4137. for (int px = 0; px < 16; px++)
  4138. {
  4139. int bits = modeInfo.m_alphaIndexBits;
  4140. if (px == 0)
  4141. bits--;
  4142. pv.Pack(indexes2[px], bits);
  4143. }
  4144. }
  4145. pv.Flush(packedBlocks);
  4146. packedBlocks += 16;
  4147. }
  4148. }
  4149. static void UnpackOne(PixelBlockU8 &output, const uint8_t* packedBlock)
  4150. {
  4151. UnpackingVector pv;
  4152. pv.Init(packedBlock);
  4153. int mode = 8;
  4154. for (int i = 0; i < 8; i++)
  4155. {
  4156. if (pv.Unpack(1) == 1)
  4157. {
  4158. mode = i;
  4159. break;
  4160. }
  4161. }
  4162. if (mode > 7)
  4163. {
  4164. for (int px = 0; px < 16; px++)
  4165. for (int ch = 0; ch < 4; ch++)
  4166. output.m_pixels[px][ch] = 0;
  4167. return;
  4168. }
  4169. const BC7Data::BC7ModeInfo &modeInfo = BC7Data::g_modes[mode];
  4170. int partition = 0;
  4171. if (modeInfo.m_partitionBits)
  4172. partition = pv.Unpack(modeInfo.m_partitionBits);
  4173. int rotation = 0;
  4174. if (modeInfo.m_alphaMode == BC7Data::AlphaMode_Separate)
  4175. rotation = pv.Unpack(2);
  4176. int indexSelector = 0;
  4177. if (modeInfo.m_hasIndexSelector)
  4178. indexSelector = pv.Unpack(1);
  4179. // Resolve fixups
  4180. int fixups[3] = { 0, 0, 0 };
  4181. if (modeInfo.m_alphaMode != BC7Data::AlphaMode_Separate)
  4182. {
  4183. if (modeInfo.m_numSubsets == 2)
  4184. fixups[1] = BC7Data::g_fixupIndexes2[partition];
  4185. else if (modeInfo.m_numSubsets == 3)
  4186. {
  4187. fixups[1] = BC7Data::g_fixupIndexes3[partition][0];
  4188. fixups[2] = BC7Data::g_fixupIndexes3[partition][1];
  4189. }
  4190. }
  4191. int endPoints[3][2][4];
  4192. // Decode RGB
  4193. for (int ch = 0; ch < 3; ch++)
  4194. {
  4195. for (int subset = 0; subset < modeInfo.m_numSubsets; subset++)
  4196. {
  4197. for (int ep = 0; ep < 2; ep++)
  4198. endPoints[subset][ep][ch] = (pv.Unpack(modeInfo.m_rgbBits) << (8 - modeInfo.m_rgbBits));
  4199. }
  4200. }
  4201. // Decode alpha
  4202. if (modeInfo.m_alphaMode != BC7Data::AlphaMode_None)
  4203. {
  4204. for (int subset = 0; subset < modeInfo.m_numSubsets; subset++)
  4205. {
  4206. for (int ep = 0; ep < 2; ep++)
  4207. endPoints[subset][ep][3] = (pv.Unpack(modeInfo.m_alphaBits) << (8 - modeInfo.m_alphaBits));
  4208. }
  4209. }
  4210. else
  4211. {
  4212. for (int subset = 0; subset < modeInfo.m_numSubsets; subset++)
  4213. {
  4214. for (int ep = 0; ep < 2; ep++)
  4215. endPoints[subset][ep][3] = 255;
  4216. }
  4217. }
  4218. int parityBits = 0;
  4219. // Decode parity bits
  4220. if (modeInfo.m_pBitMode == BC7Data::PBitMode_PerSubset)
  4221. {
  4222. for (int subset = 0; subset < modeInfo.m_numSubsets; subset++)
  4223. {
  4224. int p = pv.Unpack(1);
  4225. for (int ep = 0; ep < 2; ep++)
  4226. {
  4227. for (int ch = 0; ch < 3; ch++)
  4228. endPoints[subset][ep][ch] |= p << (7 - modeInfo.m_rgbBits);
  4229. if (modeInfo.m_alphaMode != BC7Data::AlphaMode_None)
  4230. endPoints[subset][ep][3] |= p << (7 - modeInfo.m_alphaBits);
  4231. }
  4232. }
  4233. parityBits = 1;
  4234. }
  4235. else if (modeInfo.m_pBitMode == BC7Data::PBitMode_PerEndpoint)
  4236. {
  4237. for (int subset = 0; subset < modeInfo.m_numSubsets; subset++)
  4238. {
  4239. for (int ep = 0; ep < 2; ep++)
  4240. {
  4241. int p = pv.Unpack(1);
  4242. for (int ch = 0; ch < 3; ch++)
  4243. endPoints[subset][ep][ch] |= p << (7 - modeInfo.m_rgbBits);
  4244. if (modeInfo.m_alphaMode != BC7Data::AlphaMode_None)
  4245. endPoints[subset][ep][3] |= p << (7 - modeInfo.m_alphaBits);
  4246. }
  4247. }
  4248. parityBits = 1;
  4249. }
  4250. // Fill endpoint bits
  4251. for (int subset = 0; subset < modeInfo.m_numSubsets; subset++)
  4252. {
  4253. for (int ep = 0; ep < 2; ep++)
  4254. {
  4255. for (int ch = 0; ch < 3; ch++)
  4256. endPoints[subset][ep][ch] |= (endPoints[subset][ep][ch] >> (modeInfo.m_rgbBits + parityBits));
  4257. if (modeInfo.m_alphaMode != BC7Data::AlphaMode_None)
  4258. endPoints[subset][ep][3] |= (endPoints[subset][ep][3] >> (modeInfo.m_alphaBits + parityBits));
  4259. }
  4260. }
  4261. int indexes[16];
  4262. int indexes2[16];
  4263. // Decode indexes
  4264. for (int px = 0; px < 16; px++)
  4265. {
  4266. int bits = modeInfo.m_indexBits;
  4267. if ((px == 0) || (px == fixups[1]) || (px == fixups[2]))
  4268. bits--;
  4269. indexes[px] = pv.Unpack(bits);
  4270. }
  4271. // Decode secondary indexes
  4272. if (modeInfo.m_alphaMode == BC7Data::AlphaMode_Separate)
  4273. {
  4274. for (int px = 0; px < 16; px++)
  4275. {
  4276. int bits = modeInfo.m_alphaIndexBits;
  4277. if (px == 0)
  4278. bits--;
  4279. indexes2[px] = pv.Unpack(bits);
  4280. }
  4281. }
  4282. else
  4283. {
  4284. for (int px = 0; px < 16; px++)
  4285. indexes2[px] = 0;
  4286. }
  4287. const int *alphaWeights = BC7Data::g_weightTables[modeInfo.m_alphaIndexBits];
  4288. const int *rgbWeights = BC7Data::g_weightTables[modeInfo.m_indexBits];
  4289. // Decode each pixel
  4290. for (int px = 0; px < 16; px++)
  4291. {
  4292. int rgbWeight = 0;
  4293. int alphaWeight = 0;
  4294. int rgbIndex = indexes[px];
  4295. rgbWeight = rgbWeights[indexes[px]];
  4296. if (modeInfo.m_alphaMode == BC7Data::AlphaMode_Combined)
  4297. alphaWeight = rgbWeight;
  4298. else if (modeInfo.m_alphaMode == BC7Data::AlphaMode_Separate)
  4299. alphaWeight = alphaWeights[indexes2[px]];
  4300. if (indexSelector == 1)
  4301. {
  4302. int temp = rgbWeight;
  4303. rgbWeight = alphaWeight;
  4304. alphaWeight = temp;
  4305. }
  4306. int pixel[4] = { 0, 0, 0, 255 };
  4307. int subset = 0;
  4308. if (modeInfo.m_numSubsets == 2)
  4309. subset = (BC7Data::g_partitionMap[partition] >> px) & 1;
  4310. else if (modeInfo.m_numSubsets == 3)
  4311. subset = (BC7Data::g_partitionMap2[partition] >> (px * 2)) & 3;
  4312. for (int ch = 0; ch < 3; ch++)
  4313. pixel[ch] = ((64 - rgbWeight) * endPoints[subset][0][ch] + rgbWeight * endPoints[subset][1][ch] + 32) >> 6;
  4314. if (modeInfo.m_alphaMode != BC7Data::AlphaMode_None)
  4315. pixel[3] = ((64 - alphaWeight) * endPoints[subset][0][3] + alphaWeight * endPoints[subset][1][3] + 32) >> 6;
  4316. if (rotation != 0)
  4317. {
  4318. int ch = rotation - 1;
  4319. int temp = pixel[ch];
  4320. pixel[ch] = pixel[3];
  4321. pixel[3] = temp;
  4322. }
  4323. for (int ch = 0; ch < 4; ch++)
  4324. output.m_pixels[px][ch] = static_cast<uint8_t>(pixel[ch]);
  4325. }
  4326. }
  4327. };
  4328. class BC6HComputer
  4329. {
  4330. public:
  4331. typedef ParallelMath::Float MFloat;
  4332. typedef ParallelMath::SInt16 MSInt16;
  4333. typedef ParallelMath::UInt16 MUInt16;
  4334. typedef ParallelMath::UInt15 MUInt15;
  4335. typedef ParallelMath::AInt16 MAInt16;
  4336. typedef ParallelMath::SInt32 MSInt32;
  4337. typedef ParallelMath::UInt31 MUInt31;
  4338. static const int MaxTweakRounds = 4;
  4339. static const int MaxRefineRounds = 3;
  4340. static MSInt16 QuantizeSingleEndpointElementSigned(const MSInt16 &elem2CL, int precision, const ParallelMath::RoundUpForScope* ru)
  4341. {
  4342. assert(ParallelMath::AllSet(ParallelMath::Less(elem2CL, ParallelMath::MakeSInt16(31744))));
  4343. assert(ParallelMath::AllSet(ParallelMath::Less(ParallelMath::MakeSInt16(-31744), elem2CL)));
  4344. // Expand to full range
  4345. ParallelMath::Int16CompFlag isNegative = ParallelMath::Less(elem2CL, ParallelMath::MakeSInt16(0));
  4346. MUInt15 absElem = ParallelMath::LosslessCast<MUInt15>::Cast(ParallelMath::Select(isNegative, ParallelMath::MakeSInt16(0) - elem2CL, elem2CL));
  4347. absElem = ParallelMath::RightShift(ParallelMath::RoundAndConvertToU15(ParallelMath::ToFloat(absElem) * 32.0f / 31.0f, ru), 16 - precision);
  4348. MSInt16 absElemS16 = ParallelMath::LosslessCast<MSInt16>::Cast(absElem);
  4349. return ParallelMath::Select(isNegative, ParallelMath::MakeSInt16(0) - absElemS16, absElemS16);
  4350. }
  4351. static MUInt15 QuantizeSingleEndpointElementUnsigned(const MUInt15 &elem, int precision, const ParallelMath::RoundUpForScope* ru)
  4352. {
  4353. MUInt16 expandedElem = ParallelMath::RoundAndConvertToU16(ParallelMath::Min(ParallelMath::ToFloat(elem) * 64.0f / 31.0f, ParallelMath::MakeFloat(65535.0f)), ru);
  4354. return ParallelMath::LosslessCast<MUInt15>::Cast(ParallelMath::RightShift(expandedElem, 16 - precision));
  4355. }
  4356. static void UnquantizeSingleEndpointElementSigned(const MSInt16 &comp, int precision, MSInt16 &outUnquantized, MSInt16 &outUnquantizedFinished2CL)
  4357. {
  4358. MSInt16 zero = ParallelMath::MakeSInt16(0);
  4359. ParallelMath::Int16CompFlag negative = ParallelMath::Less(comp, zero);
  4360. MUInt15 absComp = ParallelMath::LosslessCast<MUInt15>::Cast(ParallelMath::Select(negative, MSInt16(zero - comp), comp));
  4361. MSInt16 unq;
  4362. MUInt15 absUnq;
  4363. if (precision >= 16)
  4364. {
  4365. unq = comp;
  4366. absUnq = absComp;
  4367. }
  4368. else
  4369. {
  4370. MSInt16 maxCompMinusOne = ParallelMath::MakeSInt16(static_cast<int16_t>((1 << (precision - 1)) - 2));
  4371. ParallelMath::Int16CompFlag isZero = ParallelMath::Equal(comp, zero);
  4372. ParallelMath::Int16CompFlag isMax = ParallelMath::Less(maxCompMinusOne, comp);
  4373. absUnq = (absComp << (16 - precision)) + ParallelMath::MakeUInt15(static_cast<uint16_t>(0x4000 >> (precision - 1)));
  4374. ParallelMath::ConditionalSet(absUnq, isZero, ParallelMath::MakeUInt15(0));
  4375. ParallelMath::ConditionalSet(absUnq, isMax, ParallelMath::MakeUInt15(0x7fff));
  4376. unq = ParallelMath::ConditionalNegate(negative, ParallelMath::LosslessCast<MSInt16>::Cast(absUnq));
  4377. }
  4378. outUnquantized = unq;
  4379. MUInt15 funq = ParallelMath::ToUInt15(ParallelMath::RightShift(ParallelMath::XMultiply(absUnq, ParallelMath::MakeUInt15(31)), 5));
  4380. outUnquantizedFinished2CL = ParallelMath::ConditionalNegate(negative, ParallelMath::LosslessCast<MSInt16>::Cast(funq));
  4381. }
  4382. static void UnquantizeSingleEndpointElementUnsigned(const MUInt15 &comp, int precision, MUInt16 &outUnquantized, MUInt16 &outUnquantizedFinished)
  4383. {
  4384. MUInt16 unq = ParallelMath::LosslessCast<MUInt16>::Cast(comp);
  4385. if (precision < 15)
  4386. {
  4387. MUInt15 zero = ParallelMath::MakeUInt15(0);
  4388. MUInt15 maxCompMinusOne = ParallelMath::MakeUInt15(static_cast<uint16_t>((1 << precision) - 2));
  4389. ParallelMath::Int16CompFlag isZero = ParallelMath::Equal(comp, zero);
  4390. ParallelMath::Int16CompFlag isMax = ParallelMath::Less(maxCompMinusOne, comp);
  4391. unq = (ParallelMath::LosslessCast<MUInt16>::Cast(comp) << (16 - precision)) + ParallelMath::MakeUInt16(static_cast<uint16_t>(0x8000 >> precision));
  4392. ParallelMath::ConditionalSet(unq, isZero, ParallelMath::MakeUInt16(0));
  4393. ParallelMath::ConditionalSet(unq, isMax, ParallelMath::MakeUInt16(0xffff));
  4394. }
  4395. outUnquantized = unq;
  4396. outUnquantizedFinished = ParallelMath::ToUInt16(ParallelMath::RightShift(ParallelMath::XMultiply(unq, ParallelMath::MakeUInt15(31)), 6));
  4397. }
  4398. static void QuantizeEndpointsSigned(const MSInt16 endPoints[2][3], const MFloat floatPixelsColorSpace[16][3], const MFloat floatPixelsLinearWeighted[16][3], MAInt16 quantizedEndPoints[2][3], MUInt15 indexes[16], IndexSelectorHDR<3> &indexSelector, int fixupIndex, int precision, int indexRange, const float *channelWeights, bool fastIndexing, const ParallelMath::RoundTowardNearestForScope *rtn)
  4399. {
  4400. MSInt16 unquantizedEP[2][3];
  4401. MSInt16 finishedUnquantizedEP[2][3];
  4402. {
  4403. ParallelMath::RoundUpForScope ru;
  4404. for (int epi = 0; epi < 2; epi++)
  4405. {
  4406. for (int ch = 0; ch < 3; ch++)
  4407. {
  4408. MSInt16 qee = QuantizeSingleEndpointElementSigned(endPoints[epi][ch], precision, &ru);
  4409. UnquantizeSingleEndpointElementSigned(qee, precision, unquantizedEP[epi][ch], finishedUnquantizedEP[epi][ch]);
  4410. quantizedEndPoints[epi][ch] = ParallelMath::LosslessCast<MAInt16>::Cast(qee);
  4411. }
  4412. }
  4413. }
  4414. indexSelector.Init(channelWeights, unquantizedEP, finishedUnquantizedEP, indexRange);
  4415. indexSelector.InitHDR(indexRange, true, fastIndexing, channelWeights);
  4416. MUInt15 halfRangeMinusOne = ParallelMath::MakeUInt15(static_cast<uint16_t>(indexRange / 2) - 1);
  4417. MUInt15 index = fastIndexing ? indexSelector.SelectIndexHDRFast(floatPixelsColorSpace[fixupIndex], rtn) : indexSelector.SelectIndexHDRSlow(floatPixelsLinearWeighted[fixupIndex], rtn);
  4418. ParallelMath::Int16CompFlag invert = ParallelMath::Less(halfRangeMinusOne, index);
  4419. if (ParallelMath::AnySet(invert))
  4420. {
  4421. ParallelMath::ConditionalSet(index, invert, MUInt15(ParallelMath::MakeUInt15(static_cast<uint16_t>(indexRange - 1)) - index));
  4422. indexSelector.ConditionalInvert(invert);
  4423. for (int ch = 0; ch < 3; ch++)
  4424. {
  4425. MAInt16 firstEP = quantizedEndPoints[0][ch];
  4426. MAInt16 secondEP = quantizedEndPoints[1][ch];
  4427. quantizedEndPoints[0][ch] = ParallelMath::Select(invert, secondEP, firstEP);
  4428. quantizedEndPoints[1][ch] = ParallelMath::Select(invert, firstEP, secondEP);
  4429. }
  4430. }
  4431. indexes[fixupIndex] = index;
  4432. }
  4433. static void QuantizeEndpointsUnsigned(const MSInt16 endPoints[2][3], const MFloat floatPixelsColorSpace[16][3], const MFloat floatPixelsLinearWeighted[16][3], MAInt16 quantizedEndPoints[2][3], MUInt15 indexes[16], IndexSelectorHDR<3> &indexSelector, int fixupIndex, int precision, int indexRange, const float *channelWeights, bool fastIndexing, const ParallelMath::RoundTowardNearestForScope *rtn)
  4434. {
  4435. MUInt16 unquantizedEP[2][3];
  4436. MUInt16 finishedUnquantizedEP[2][3];
  4437. {
  4438. ParallelMath::RoundUpForScope ru;
  4439. for (int epi = 0; epi < 2; epi++)
  4440. {
  4441. for (int ch = 0; ch < 3; ch++)
  4442. {
  4443. MUInt15 qee = QuantizeSingleEndpointElementUnsigned(ParallelMath::LosslessCast<MUInt15>::Cast(endPoints[epi][ch]), precision, &ru);
  4444. UnquantizeSingleEndpointElementUnsigned(qee, precision, unquantizedEP[epi][ch], finishedUnquantizedEP[epi][ch]);
  4445. quantizedEndPoints[epi][ch] = ParallelMath::LosslessCast<MAInt16>::Cast(qee);
  4446. }
  4447. }
  4448. }
  4449. indexSelector.Init(channelWeights, unquantizedEP, finishedUnquantizedEP, indexRange);
  4450. indexSelector.InitHDR(indexRange, false, fastIndexing, channelWeights);
  4451. MUInt15 halfRangeMinusOne = ParallelMath::MakeUInt15(static_cast<uint16_t>(indexRange / 2) - 1);
  4452. MUInt15 index = fastIndexing ? indexSelector.SelectIndexHDRFast(floatPixelsColorSpace[fixupIndex], rtn) : indexSelector.SelectIndexHDRSlow(floatPixelsLinearWeighted[fixupIndex], rtn);
  4453. ParallelMath::Int16CompFlag invert = ParallelMath::Less(halfRangeMinusOne, index);
  4454. if (ParallelMath::AnySet(invert))
  4455. {
  4456. ParallelMath::ConditionalSet(index, invert, MUInt15(ParallelMath::MakeUInt15(static_cast<uint16_t>(indexRange - 1)) - index));
  4457. indexSelector.ConditionalInvert(invert);
  4458. for (int ch = 0; ch < 3; ch++)
  4459. {
  4460. MAInt16 firstEP = quantizedEndPoints[0][ch];
  4461. MAInt16 secondEP = quantizedEndPoints[1][ch];
  4462. quantizedEndPoints[0][ch] = ParallelMath::Select(invert, secondEP, firstEP);
  4463. quantizedEndPoints[1][ch] = ParallelMath::Select(invert, firstEP, secondEP);
  4464. }
  4465. }
  4466. indexes[fixupIndex] = index;
  4467. }
  4468. static void EvaluatePartitionedLegality(const MAInt16 ep0[2][3], const MAInt16 ep1[2][3], int aPrec, const int bPrec[3], bool isTransformed, MAInt16 outEncodedEPs[2][2][3], ParallelMath::Int16CompFlag& outIsLegal)
  4469. {
  4470. ParallelMath::Int16CompFlag allLegal = ParallelMath::MakeBoolInt16(true);
  4471. MAInt16 aSignificantMask = ParallelMath::MakeAInt16(static_cast<int16_t>((1 << aPrec) - 1));
  4472. for (int ch = 0; ch < 3; ch++)
  4473. {
  4474. outEncodedEPs[0][0][ch] = ep0[0][ch];
  4475. outEncodedEPs[0][1][ch] = ep0[1][ch];
  4476. outEncodedEPs[1][0][ch] = ep1[0][ch];
  4477. outEncodedEPs[1][1][ch] = ep1[1][ch];
  4478. if (isTransformed)
  4479. {
  4480. for (int subset = 0; subset < 2; subset++)
  4481. {
  4482. for (int epi = 0; epi < 2; epi++)
  4483. {
  4484. if (epi == 0 && subset == 0)
  4485. continue;
  4486. MAInt16 bReduced = (outEncodedEPs[subset][epi][ch] & aSignificantMask);
  4487. MSInt16 delta = ParallelMath::TruncateToPrecisionSigned(ParallelMath::LosslessCast<MSInt16>::Cast(ParallelMath::AbstractSubtract(outEncodedEPs[subset][epi][ch], outEncodedEPs[0][0][ch])), bPrec[ch]);
  4488. outEncodedEPs[subset][epi][ch] = ParallelMath::LosslessCast<MAInt16>::Cast(delta);
  4489. MAInt16 reconstructed = (ParallelMath::AbstractAdd(outEncodedEPs[subset][epi][ch], outEncodedEPs[0][0][ch]) & aSignificantMask);
  4490. allLegal = allLegal & ParallelMath::Equal(reconstructed, bReduced);
  4491. }
  4492. }
  4493. }
  4494. if (!ParallelMath::AnySet(allLegal))
  4495. break;
  4496. }
  4497. outIsLegal = allLegal;
  4498. }
  4499. static void EvaluateSingleLegality(const MAInt16 ep[2][3], int aPrec, const int bPrec[3], bool isTransformed, MAInt16 outEncodedEPs[2][3], ParallelMath::Int16CompFlag& outIsLegal)
  4500. {
  4501. ParallelMath::Int16CompFlag allLegal = ParallelMath::MakeBoolInt16(true);
  4502. MAInt16 aSignificantMask = ParallelMath::MakeAInt16(static_cast<int16_t>((1 << aPrec) - 1));
  4503. for (int ch = 0; ch < 3; ch++)
  4504. {
  4505. outEncodedEPs[0][ch] = ep[0][ch];
  4506. outEncodedEPs[1][ch] = ep[1][ch];
  4507. if (isTransformed)
  4508. {
  4509. MAInt16 bReduced = (outEncodedEPs[1][ch] & aSignificantMask);
  4510. MSInt16 delta = ParallelMath::TruncateToPrecisionSigned(ParallelMath::LosslessCast<MSInt16>::Cast(ParallelMath::AbstractSubtract(outEncodedEPs[1][ch], outEncodedEPs[0][ch])), bPrec[ch]);
  4511. outEncodedEPs[1][ch] = ParallelMath::LosslessCast<MAInt16>::Cast(delta);
  4512. MAInt16 reconstructed = (ParallelMath::AbstractAdd(outEncodedEPs[1][ch], outEncodedEPs[0][ch]) & aSignificantMask);
  4513. allLegal = allLegal & ParallelMath::Equal(reconstructed, bReduced);
  4514. }
  4515. }
  4516. outIsLegal = allLegal;
  4517. }
  4518. static void Pack(uint32_t flags, const PixelBlockF16* inputs, uint8_t* packedBlocks, const float channelWeights[4], bool isSigned, int numTweakRounds, int numRefineRounds)
  4519. {
  4520. if (numTweakRounds < 1)
  4521. numTweakRounds = 1;
  4522. else if (numTweakRounds > MaxTweakRounds)
  4523. numTweakRounds = MaxTweakRounds;
  4524. if (numRefineRounds < 1)
  4525. numRefineRounds = 1;
  4526. else if (numRefineRounds > MaxRefineRounds)
  4527. numRefineRounds = MaxRefineRounds;
  4528. bool fastIndexing = ((flags & cvtt::Flags::BC6H_FastIndexing) != 0);
  4529. float channelWeightsSq[3];
  4530. ParallelMath::RoundTowardNearestForScope rtn;
  4531. MSInt16 pixels[16][3];
  4532. MFloat floatPixels2CL[16][3];
  4533. MFloat floatPixelsLinearWeighted[16][3];
  4534. MSInt16 low15Bits = ParallelMath::MakeSInt16(32767);
  4535. for (int ch = 0; ch < 3; ch++)
  4536. channelWeightsSq[ch] = channelWeights[ch] * channelWeights[ch];
  4537. for (int px = 0; px < 16; px++)
  4538. {
  4539. for (int ch = 0; ch < 3; ch++)
  4540. {
  4541. MSInt16 pixelValue;
  4542. ParallelMath::ConvertHDRInputs(inputs, px, ch, pixelValue);
  4543. // Convert from sign+magnitude to 2CL
  4544. if (isSigned)
  4545. {
  4546. ParallelMath::Int16CompFlag negative = ParallelMath::Less(pixelValue, ParallelMath::MakeSInt16(0));
  4547. MSInt16 magnitude = (pixelValue & low15Bits);
  4548. ParallelMath::ConditionalSet(pixelValue, negative, ParallelMath::MakeSInt16(0) - magnitude);
  4549. pixelValue = ParallelMath::Max(pixelValue, ParallelMath::MakeSInt16(-31743));
  4550. }
  4551. else
  4552. pixelValue = ParallelMath::Max(pixelValue, ParallelMath::MakeSInt16(0));
  4553. pixelValue = ParallelMath::Min(pixelValue, ParallelMath::MakeSInt16(31743));
  4554. pixels[px][ch] = pixelValue;
  4555. floatPixels2CL[px][ch] = ParallelMath::ToFloat(pixelValue);
  4556. floatPixelsLinearWeighted[px][ch] = ParallelMath::TwosCLHalfToFloat(pixelValue) * channelWeights[ch];
  4557. }
  4558. }
  4559. MFloat preWeightedPixels[16][3];
  4560. BCCommon::PreWeightPixelsHDR<3>(preWeightedPixels, pixels, channelWeights);
  4561. MAInt16 bestEndPoints[2][2][3];
  4562. MUInt15 bestIndexes[16];
  4563. MFloat bestError = ParallelMath::MakeFloat(FLT_MAX);
  4564. MUInt15 bestMode = ParallelMath::MakeUInt15(0);
  4565. MUInt15 bestPartition = ParallelMath::MakeUInt15(0);
  4566. for (int px = 0; px < 16; px++)
  4567. bestIndexes[px] = ParallelMath::MakeUInt15(0);
  4568. for (int subset = 0; subset < 2; subset++)
  4569. for (int epi = 0; epi < 2; epi++)
  4570. for (int ch = 0; ch < 3; ch++)
  4571. bestEndPoints[subset][epi][ch] = ParallelMath::MakeAInt16(0);
  4572. UnfinishedEndpoints<3> partitionedUFEP[32][2];
  4573. UnfinishedEndpoints<3> singleUFEP;
  4574. // Generate UFEP for partitions
  4575. for (int p = 0; p < 32; p++)
  4576. {
  4577. int partitionMask = BC7Data::g_partitionMap[p];
  4578. EndpointSelector<3, 8> epSelectors[2];
  4579. for (int pass = 0; pass < NumEndpointSelectorPasses; pass++)
  4580. {
  4581. for (int px = 0; px < 16; px++)
  4582. {
  4583. int subset = (partitionMask >> px) & 1;
  4584. epSelectors[subset].ContributePass(preWeightedPixels[px], pass, ParallelMath::MakeFloat(1.0f));
  4585. }
  4586. for (int subset = 0; subset < 2; subset++)
  4587. epSelectors[subset].FinishPass(pass);
  4588. }
  4589. for (int subset = 0; subset < 2; subset++)
  4590. partitionedUFEP[p][subset] = epSelectors[subset].GetEndpoints(channelWeights);
  4591. }
  4592. // Generate UFEP for single
  4593. {
  4594. EndpointSelector<3, 8> epSelector;
  4595. for (int pass = 0; pass < NumEndpointSelectorPasses; pass++)
  4596. {
  4597. for (int px = 0; px < 16; px++)
  4598. epSelector.ContributePass(preWeightedPixels[px], pass, ParallelMath::MakeFloat(1.0f));
  4599. epSelector.FinishPass(pass);
  4600. }
  4601. singleUFEP = epSelector.GetEndpoints(channelWeights);
  4602. }
  4603. for (int partitionedInt = 0; partitionedInt < 2; partitionedInt++)
  4604. {
  4605. bool partitioned = (partitionedInt == 1);
  4606. for (int aPrec = BC7Data::g_maxHDRPrecision; aPrec >= 0; aPrec--)
  4607. {
  4608. if (!BC7Data::g_hdrModesExistForPrecision[partitionedInt][aPrec])
  4609. continue;
  4610. int numPartitions = partitioned ? 32 : 1;
  4611. int numSubsets = partitioned ? 2 : 1;
  4612. int indexBits = partitioned ? 3 : 4;
  4613. int indexRange = (1 << indexBits);
  4614. for (int p = 0; p < numPartitions; p++)
  4615. {
  4616. int partitionMask = partitioned ? BC7Data::g_partitionMap[p] : 0;
  4617. const int MaxMetaRounds = MaxTweakRounds * MaxRefineRounds;
  4618. MAInt16 metaEndPointsQuantized[MaxMetaRounds][2][2][3];
  4619. MUInt15 metaIndexes[MaxMetaRounds][16];
  4620. MFloat metaError[MaxMetaRounds][2];
  4621. bool roundValid[MaxMetaRounds][2];
  4622. for (int r = 0; r < MaxMetaRounds; r++)
  4623. for (int subset = 0; subset < 2; subset++)
  4624. roundValid[r][subset] = true;
  4625. for (int subset = 0; subset < numSubsets; subset++)
  4626. {
  4627. for (int tweak = 0; tweak < MaxTweakRounds; tweak++)
  4628. {
  4629. EndpointRefiner<3> refiners[2];
  4630. bool abortRemainingRefines = false;
  4631. for (int refinePass = 0; refinePass < MaxRefineRounds; refinePass++)
  4632. {
  4633. int metaRound = tweak * MaxRefineRounds + refinePass;
  4634. if (tweak >= numTweakRounds || refinePass >= numRefineRounds)
  4635. abortRemainingRefines = true;
  4636. if (abortRemainingRefines)
  4637. {
  4638. roundValid[metaRound][subset] = false;
  4639. continue;
  4640. }
  4641. MAInt16(&mrQuantizedEndPoints)[2][2][3] = metaEndPointsQuantized[metaRound];
  4642. MUInt15(&mrIndexes)[16] = metaIndexes[metaRound];
  4643. MSInt16 endPointsColorSpace[2][3];
  4644. if (refinePass == 0)
  4645. {
  4646. UnfinishedEndpoints<3> ufep = partitioned ? partitionedUFEP[p][subset] : singleUFEP;
  4647. if (isSigned)
  4648. ufep.FinishHDRSigned(tweak, indexRange, endPointsColorSpace[0], endPointsColorSpace[1], &rtn);
  4649. else
  4650. ufep.FinishHDRUnsigned(tweak, indexRange, endPointsColorSpace[0], endPointsColorSpace[1], &rtn);
  4651. }
  4652. else
  4653. refiners[subset].GetRefinedEndpointsHDR(endPointsColorSpace, isSigned, &rtn);
  4654. refiners[subset].Init(indexRange, channelWeights);
  4655. int fixupIndex = (subset == 0) ? 0 : BC7Data::g_fixupIndexes2[p];
  4656. IndexSelectorHDR<3> indexSelector;
  4657. if (isSigned)
  4658. QuantizeEndpointsSigned(endPointsColorSpace, floatPixels2CL, floatPixelsLinearWeighted, mrQuantizedEndPoints[subset], mrIndexes, indexSelector, fixupIndex, aPrec, indexRange, channelWeights, fastIndexing, &rtn);
  4659. else
  4660. QuantizeEndpointsUnsigned(endPointsColorSpace, floatPixels2CL, floatPixelsLinearWeighted, mrQuantizedEndPoints[subset], mrIndexes, indexSelector, fixupIndex, aPrec, indexRange, channelWeights, fastIndexing, &rtn);
  4661. if (metaRound > 0)
  4662. {
  4663. ParallelMath::Int16CompFlag anySame = ParallelMath::MakeBoolInt16(false);
  4664. for (int prevRound = 0; prevRound < metaRound; prevRound++)
  4665. {
  4666. MAInt16(&prevRoundEPs)[2][3] = metaEndPointsQuantized[prevRound][subset];
  4667. ParallelMath::Int16CompFlag same = ParallelMath::MakeBoolInt16(true);
  4668. for (int epi = 0; epi < 2; epi++)
  4669. for (int ch = 0; ch < 3; ch++)
  4670. same = (same & ParallelMath::Equal(prevRoundEPs[epi][ch], mrQuantizedEndPoints[subset][epi][ch]));
  4671. anySame = (anySame | same);
  4672. if (ParallelMath::AllSet(anySame))
  4673. break;
  4674. }
  4675. if (ParallelMath::AllSet(anySame))
  4676. {
  4677. roundValid[metaRound][subset] = false;
  4678. continue;
  4679. }
  4680. }
  4681. MFloat subsetError = ParallelMath::MakeFloatZero();
  4682. {
  4683. for (int px = 0; px < 16; px++)
  4684. {
  4685. if (subset != ((partitionMask >> px) & 1))
  4686. continue;
  4687. MUInt15 index;
  4688. if (px == fixupIndex)
  4689. index = mrIndexes[px];
  4690. else
  4691. {
  4692. index = fastIndexing ? indexSelector.SelectIndexHDRFast(floatPixels2CL[px], &rtn) : indexSelector.SelectIndexHDRSlow(floatPixelsLinearWeighted[px], &rtn);
  4693. mrIndexes[px] = index;
  4694. }
  4695. MSInt16 reconstructed[3];
  4696. if (isSigned)
  4697. indexSelector.ReconstructHDRSigned(mrIndexes[px], reconstructed);
  4698. else
  4699. indexSelector.ReconstructHDRUnsigned(mrIndexes[px], reconstructed);
  4700. subsetError = subsetError + (fastIndexing ? BCCommon::ComputeErrorHDRFast<3>(flags, reconstructed, pixels[px], channelWeightsSq) : BCCommon::ComputeErrorHDRSlow<3>(flags, reconstructed, pixels[px], channelWeightsSq));
  4701. if (refinePass != numRefineRounds - 1)
  4702. refiners[subset].ContributeUnweightedPW(preWeightedPixels[px], index);
  4703. }
  4704. }
  4705. metaError[metaRound][subset] = subsetError;
  4706. }
  4707. }
  4708. }
  4709. // Now we have a bunch of attempts, but not all of them will fit in the delta coding scheme
  4710. int numMeta1 = partitioned ? MaxMetaRounds : 1;
  4711. for (int meta0 = 0; meta0 < MaxMetaRounds; meta0++)
  4712. {
  4713. if (!roundValid[meta0][0])
  4714. continue;
  4715. for (int meta1 = 0; meta1 < numMeta1; meta1++)
  4716. {
  4717. MFloat combinedError = metaError[meta0][0];
  4718. if (partitioned)
  4719. {
  4720. if (!roundValid[meta1][1])
  4721. continue;
  4722. combinedError = combinedError + metaError[meta1][1];
  4723. }
  4724. ParallelMath::FloatCompFlag errorBetter = ParallelMath::Less(combinedError, bestError);
  4725. if (!ParallelMath::AnySet(errorBetter))
  4726. continue;
  4727. ParallelMath::Int16CompFlag needsCommit = ParallelMath::FloatFlagToInt16(errorBetter);
  4728. // Figure out if this is encodable
  4729. for (int mode = 0; mode < BC7Data::g_numHDRModes; mode++)
  4730. {
  4731. const BC7Data::BC6HModeInfo &modeInfo = BC7Data::g_hdrModes[mode];
  4732. if (modeInfo.m_partitioned != partitioned || modeInfo.m_aPrec != aPrec)
  4733. continue;
  4734. MAInt16 encodedEPs[2][2][3];
  4735. ParallelMath::Int16CompFlag isLegal;
  4736. if (partitioned)
  4737. EvaluatePartitionedLegality(metaEndPointsQuantized[meta0][0], metaEndPointsQuantized[meta1][1], modeInfo.m_aPrec, modeInfo.m_bPrec, modeInfo.m_transformed, encodedEPs, isLegal);
  4738. else
  4739. EvaluateSingleLegality(metaEndPointsQuantized[meta0][0], modeInfo.m_aPrec, modeInfo.m_bPrec, modeInfo.m_transformed, encodedEPs[0], isLegal);
  4740. ParallelMath::Int16CompFlag isLegalAndBetter = (ParallelMath::FloatFlagToInt16(errorBetter) & isLegal);
  4741. if (!ParallelMath::AnySet(isLegalAndBetter))
  4742. continue;
  4743. ParallelMath::FloatCompFlag isLegalAndBetterFloat = ParallelMath::Int16FlagToFloat(isLegalAndBetter);
  4744. ParallelMath::ConditionalSet(bestError, isLegalAndBetterFloat, combinedError);
  4745. ParallelMath::ConditionalSet(bestMode, isLegalAndBetter, ParallelMath::MakeUInt15(static_cast<uint16_t>(mode)));
  4746. ParallelMath::ConditionalSet(bestPartition, isLegalAndBetter, ParallelMath::MakeUInt15(static_cast<uint16_t>(p)));
  4747. for (int subset = 0; subset < numSubsets; subset++)
  4748. {
  4749. for (int epi = 0; epi < 2; epi++)
  4750. {
  4751. for (int ch = 0; ch < 3; ch++)
  4752. ParallelMath::ConditionalSet(bestEndPoints[subset][epi][ch], isLegalAndBetter, encodedEPs[subset][epi][ch]);
  4753. }
  4754. }
  4755. for (int px = 0; px < 16; px++)
  4756. {
  4757. int subset = ((partitionMask >> px) & 1);
  4758. if (subset == 0)
  4759. ParallelMath::ConditionalSet(bestIndexes[px], isLegalAndBetter, metaIndexes[meta0][px]);
  4760. else
  4761. ParallelMath::ConditionalSet(bestIndexes[px], isLegalAndBetter, metaIndexes[meta1][px]);
  4762. }
  4763. needsCommit = ParallelMath::AndNot(needsCommit, isLegalAndBetter);
  4764. if (!ParallelMath::AnySet(needsCommit))
  4765. break;
  4766. }
  4767. }
  4768. }
  4769. }
  4770. }
  4771. }
  4772. // At this point, everything should be set
  4773. for (int block = 0; block < ParallelMath::ParallelSize; block++)
  4774. {
  4775. ParallelMath::ScalarUInt16 mode = ParallelMath::Extract(bestMode, block);
  4776. ParallelMath::ScalarUInt16 partition = ParallelMath::Extract(bestPartition, block);
  4777. int32_t eps[2][2][3];
  4778. ParallelMath::ScalarUInt16 indexes[16];
  4779. const BC7Data::BC6HModeInfo& modeInfo = BC7Data::g_hdrModes[mode];
  4780. const BC6HData::ModeDescriptor* desc = BC6HData::g_modeDescriptors[mode];
  4781. const size_t headerBits = modeInfo.m_partitioned ? 82 : 65;
  4782. for (int subset = 0; subset < 2; subset++)
  4783. {
  4784. for (int epi = 0; epi < 2; epi++)
  4785. {
  4786. for (int ch = 0; ch < 3; ch++)
  4787. eps[subset][epi][ch] = ParallelMath::Extract(bestEndPoints[subset][epi][ch], block);
  4788. }
  4789. }
  4790. for (int px = 0; px < 16; px++)
  4791. indexes[px] = ParallelMath::Extract(bestIndexes[px], block);
  4792. uint16_t modeID = modeInfo.m_modeID;
  4793. PackingVector pv;
  4794. pv.Init();
  4795. for (size_t i = 0; i < headerBits; i++)
  4796. {
  4797. int32_t codedValue = 0;
  4798. switch (desc[i].m_eField)
  4799. {
  4800. case BC6HData::M: codedValue = modeID; break;
  4801. case BC6HData::D: codedValue = partition; break;
  4802. case BC6HData::RW: codedValue = eps[0][0][0]; break;
  4803. case BC6HData::RX: codedValue = eps[0][1][0]; break;
  4804. case BC6HData::RY: codedValue = eps[1][0][0]; break;
  4805. case BC6HData::RZ: codedValue = eps[1][1][0]; break;
  4806. case BC6HData::GW: codedValue = eps[0][0][1]; break;
  4807. case BC6HData::GX: codedValue = eps[0][1][1]; break;
  4808. case BC6HData::GY: codedValue = eps[1][0][1]; break;
  4809. case BC6HData::GZ: codedValue = eps[1][1][1]; break;
  4810. case BC6HData::BW: codedValue = eps[0][0][2]; break;
  4811. case BC6HData::BX: codedValue = eps[0][1][2]; break;
  4812. case BC6HData::BY: codedValue = eps[1][0][2]; break;
  4813. case BC6HData::BZ: codedValue = eps[1][1][2]; break;
  4814. default: assert(false); break;
  4815. }
  4816. pv.Pack(static_cast<uint16_t>((codedValue >> desc[i].m_uBit) & 1), 1);
  4817. }
  4818. int fixupIndex1 = 0;
  4819. int indexBits = 4;
  4820. if (modeInfo.m_partitioned)
  4821. {
  4822. fixupIndex1 = BC7Data::g_fixupIndexes2[partition];
  4823. indexBits = 3;
  4824. }
  4825. for (int px = 0; px < 16; px++)
  4826. {
  4827. ParallelMath::ScalarUInt16 index = ParallelMath::Extract(bestIndexes[px], block);
  4828. if (px == 0 || px == fixupIndex1)
  4829. pv.Pack(index, indexBits - 1);
  4830. else
  4831. pv.Pack(index, indexBits);
  4832. }
  4833. pv.Flush(packedBlocks + 16 * block);
  4834. }
  4835. }
  4836. static void SignExtendSingle(int &v, int bits)
  4837. {
  4838. if (v & (1 << (bits - 1)))
  4839. v |= -(1 << bits);
  4840. }
  4841. static void UnpackOne(PixelBlockF16 &output, const uint8_t *pBC, bool isSigned)
  4842. {
  4843. UnpackingVector pv;
  4844. pv.Init(pBC);
  4845. int numModeBits = 2;
  4846. int modeBits = pv.Unpack(2);
  4847. if (modeBits != 0 && modeBits != 1)
  4848. {
  4849. modeBits |= pv.Unpack(3) << 2;
  4850. numModeBits += 3;
  4851. }
  4852. int mode = -1;
  4853. for (int possibleMode = 0; possibleMode < BC7Data::g_numHDRModes; possibleMode++)
  4854. {
  4855. if (BC7Data::g_hdrModes[possibleMode].m_modeID == modeBits)
  4856. {
  4857. mode = possibleMode;
  4858. break;
  4859. }
  4860. }
  4861. if (mode < 0)
  4862. {
  4863. for (int px = 0; px < 16; px++)
  4864. {
  4865. for (int ch = 0; ch < 3; ch++)
  4866. output.m_pixels[px][ch] = 0;
  4867. output.m_pixels[px][3] = 0x3c00; // 1.0
  4868. }
  4869. return;
  4870. }
  4871. const BC7Data::BC6HModeInfo& modeInfo = BC7Data::g_hdrModes[mode];
  4872. const size_t headerBits = modeInfo.m_partitioned ? 82 : 65;
  4873. const BC6HData::ModeDescriptor* desc = BC6HData::g_modeDescriptors[mode];
  4874. int32_t partition = 0;
  4875. int32_t eps[2][2][3];
  4876. for (int subset = 0; subset < 2; subset++)
  4877. for (int epi = 0; epi < 2; epi++)
  4878. for (int ch = 0; ch < 3; ch++)
  4879. eps[subset][epi][ch] = 0;
  4880. for (size_t i = numModeBits; i < headerBits; i++)
  4881. {
  4882. int32_t *pCodedValue = NULL;
  4883. switch (desc[i].m_eField)
  4884. {
  4885. case BC6HData::D: pCodedValue = &partition; break;
  4886. case BC6HData::RW: pCodedValue = &eps[0][0][0]; break;
  4887. case BC6HData::RX: pCodedValue = &eps[0][1][0]; break;
  4888. case BC6HData::RY: pCodedValue = &eps[1][0][0]; break;
  4889. case BC6HData::RZ: pCodedValue = &eps[1][1][0]; break;
  4890. case BC6HData::GW: pCodedValue = &eps[0][0][1]; break;
  4891. case BC6HData::GX: pCodedValue = &eps[0][1][1]; break;
  4892. case BC6HData::GY: pCodedValue = &eps[1][0][1]; break;
  4893. case BC6HData::GZ: pCodedValue = &eps[1][1][1]; break;
  4894. case BC6HData::BW: pCodedValue = &eps[0][0][2]; break;
  4895. case BC6HData::BX: pCodedValue = &eps[0][1][2]; break;
  4896. case BC6HData::BY: pCodedValue = &eps[1][0][2]; break;
  4897. case BC6HData::BZ: pCodedValue = &eps[1][1][2]; break;
  4898. default: assert(false); break;
  4899. }
  4900. (*pCodedValue) |= pv.Unpack(1) << desc[i].m_uBit;
  4901. }
  4902. uint16_t modeID = modeInfo.m_modeID;
  4903. int fixupIndex1 = 0;
  4904. int indexBits = 4;
  4905. int numSubsets = 1;
  4906. if (modeInfo.m_partitioned)
  4907. {
  4908. fixupIndex1 = BC7Data::g_fixupIndexes2[partition];
  4909. indexBits = 3;
  4910. numSubsets = 2;
  4911. }
  4912. int indexes[16];
  4913. for (int px = 0; px < 16; px++)
  4914. {
  4915. if (px == 0 || px == fixupIndex1)
  4916. indexes[px] = pv.Unpack(indexBits - 1);
  4917. else
  4918. indexes[px] = pv.Unpack(indexBits);
  4919. }
  4920. if (modeInfo.m_partitioned)
  4921. {
  4922. for (int ch = 0; ch < 3; ch++)
  4923. {
  4924. if (isSigned)
  4925. SignExtendSingle(eps[0][0][ch], modeInfo.m_aPrec);
  4926. if (modeInfo.m_transformed || isSigned)
  4927. {
  4928. SignExtendSingle(eps[0][1][ch], modeInfo.m_bPrec[ch]);
  4929. SignExtendSingle(eps[1][0][ch], modeInfo.m_bPrec[ch]);
  4930. SignExtendSingle(eps[1][1][ch], modeInfo.m_bPrec[ch]);
  4931. }
  4932. }
  4933. }
  4934. else
  4935. {
  4936. for (int ch = 0; ch < 3; ch++)
  4937. {
  4938. if (isSigned)
  4939. SignExtendSingle(eps[0][0][ch], modeInfo.m_aPrec);
  4940. if (modeInfo.m_transformed || isSigned)
  4941. SignExtendSingle(eps[0][1][ch], modeInfo.m_bPrec[ch]);
  4942. }
  4943. }
  4944. int aPrec = modeInfo.m_aPrec;
  4945. if (modeInfo.m_transformed)
  4946. {
  4947. for (int ch = 0; ch < 3; ch++)
  4948. {
  4949. int wrapMask = (1 << aPrec) - 1;
  4950. eps[0][1][ch] = ((eps[0][0][ch] + eps[0][1][ch]) & wrapMask);
  4951. if (isSigned)
  4952. SignExtendSingle(eps[0][1][ch], aPrec);
  4953. if (modeInfo.m_partitioned)
  4954. {
  4955. eps[1][0][ch] = ((eps[0][0][ch] + eps[1][0][ch]) & wrapMask);
  4956. eps[1][1][ch] = ((eps[0][0][ch] + eps[1][1][ch]) & wrapMask);
  4957. if (isSigned)
  4958. {
  4959. SignExtendSingle(eps[1][0][ch], aPrec);
  4960. SignExtendSingle(eps[1][1][ch], aPrec);
  4961. }
  4962. }
  4963. }
  4964. }
  4965. // Unquantize endpoints
  4966. for (int subset = 0; subset < numSubsets; subset++)
  4967. {
  4968. for (int epi = 0; epi < 2; epi++)
  4969. {
  4970. for (int ch = 0; ch < 3; ch++)
  4971. {
  4972. int &v = eps[subset][epi][ch];
  4973. if (isSigned)
  4974. {
  4975. if (aPrec >= 16)
  4976. {
  4977. // Nothing
  4978. }
  4979. else
  4980. {
  4981. bool s = false;
  4982. int comp = v;
  4983. if (v < 0)
  4984. {
  4985. s = true;
  4986. comp = -comp;
  4987. }
  4988. int unq = 0;
  4989. if (comp == 0)
  4990. unq = 0;
  4991. else if (comp >= ((1 << (aPrec - 1)) - 1))
  4992. unq = 0x7fff;
  4993. else
  4994. unq = ((comp << 15) + 0x4000) >> (aPrec - 1);
  4995. if (s)
  4996. unq = -unq;
  4997. v = unq;
  4998. }
  4999. }
  5000. else
  5001. {
  5002. if (aPrec >= 15)
  5003. {
  5004. // Nothing
  5005. }
  5006. else if (v == 0)
  5007. {
  5008. // Nothing
  5009. }
  5010. else if (v == ((1 << aPrec) - 1))
  5011. v = 0xffff;
  5012. else
  5013. v = ((v << 16) + 0x8000) >> aPrec;
  5014. }
  5015. }
  5016. }
  5017. }
  5018. const int *weights = BC7Data::g_weightTables[indexBits];
  5019. for (int px = 0; px < 16; px++)
  5020. {
  5021. int subset = 0;
  5022. if (modeInfo.m_partitioned)
  5023. subset = (BC7Data::g_partitionMap[partition] >> px) & 1;
  5024. int w = weights[indexes[px]];
  5025. for (int ch = 0; ch < 3; ch++)
  5026. {
  5027. int comp = ((64 - w) * eps[subset][0][ch] + w * eps[subset][1][ch] + 32) >> 6;
  5028. if (isSigned)
  5029. {
  5030. if (comp < 0)
  5031. comp = -(((-comp) * 31) >> 5);
  5032. else
  5033. comp = (comp * 31) >> 5;
  5034. int s = 0;
  5035. if (comp < 0)
  5036. {
  5037. s = 0x8000;
  5038. comp = -comp;
  5039. }
  5040. output.m_pixels[px][ch] = static_cast<uint16_t>(s | comp);
  5041. }
  5042. else
  5043. {
  5044. comp = (comp * 31) >> 6;
  5045. output.m_pixels[px][ch] = static_cast<uint16_t>(comp);
  5046. }
  5047. }
  5048. output.m_pixels[px][3] = 0x3c00; // 1.0
  5049. }
  5050. }
  5051. };
  5052. namespace S3TCSingleColorTables
  5053. {
  5054. struct SingleColorTableEntry
  5055. {
  5056. uint8_t m_min;
  5057. uint8_t m_max;
  5058. uint8_t m_actualColor;
  5059. uint8_t m_span;
  5060. };
  5061. SingleColorTableEntry g_singleColor5_3[256] =
  5062. {
  5063. { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 8, 0, 2, 8 }, { 8, 0, 2, 8 }, { 0, 8, 5, 8 }, { 0, 8, 5, 8 }, { 0, 8, 5, 8 }, { 8, 8, 8, 0 },
  5064. { 8, 8, 8, 0 }, { 8, 8, 8, 0 }, { 16, 8, 10, 8 }, { 33, 0, 11, 33 }, { 8, 16, 13, 8 }, { 8, 16, 13, 8 }, { 8, 16, 13, 8 }, { 16, 16, 16, 0 },
  5065. { 16, 16, 16, 0 }, { 16, 16, 16, 0 }, { 24, 16, 18, 8 }, { 41, 8, 19, 33 }, { 16, 24, 21, 8 }, { 16, 24, 21, 8 }, { 0, 33, 22, 33 }, { 24, 24, 24, 0 },
  5066. { 24, 24, 24, 0 }, { 24, 24, 24, 0 }, { 33, 24, 27, 9 }, { 33, 24, 27, 9 }, { 33, 24, 27, 9 }, { 41, 24, 29, 17 }, { 24, 33, 30, 9 }, { 24, 33, 30, 9 },
  5067. { 16, 41, 32, 25 }, { 33, 33, 33, 0 }, { 33, 33, 33, 0 }, { 41, 33, 35, 8 }, { 41, 33, 35, 8 }, { 33, 41, 38, 8 }, { 33, 41, 38, 8 }, { 33, 41, 38, 8 },
  5068. { 24, 49, 40, 25 }, { 41, 41, 41, 0 }, { 41, 41, 41, 0 }, { 49, 41, 43, 8 }, { 66, 33, 44, 33 }, { 41, 49, 46, 8 }, { 41, 49, 46, 8 }, { 41, 49, 46, 8 },
  5069. { 49, 49, 49, 0 }, { 49, 49, 49, 0 }, { 49, 49, 49, 0 }, { 57, 49, 51, 8 }, { 74, 41, 52, 33 }, { 49, 57, 54, 8 }, { 49, 57, 54, 8 }, { 33, 66, 55, 33 },
  5070. { 57, 57, 57, 0 }, { 57, 57, 57, 0 }, { 57, 57, 57, 0 }, { 66, 57, 60, 9 }, { 66, 57, 60, 9 }, { 66, 57, 60, 9 }, { 74, 57, 62, 17 }, { 57, 66, 63, 9 },
  5071. { 57, 66, 63, 9 }, { 49, 74, 65, 25 }, { 66, 66, 66, 0 }, { 66, 66, 66, 0 }, { 74, 66, 68, 8 }, { 74, 66, 68, 8 }, { 66, 74, 71, 8 }, { 66, 74, 71, 8 },
  5072. { 66, 74, 71, 8 }, { 57, 82, 73, 25 }, { 74, 74, 74, 0 }, { 74, 74, 74, 0 }, { 82, 74, 76, 8 }, { 99, 66, 77, 33 }, { 74, 82, 79, 8 }, { 74, 82, 79, 8 },
  5073. { 74, 82, 79, 8 }, { 82, 82, 82, 0 }, { 82, 82, 82, 0 }, { 82, 82, 82, 0 }, { 90, 82, 84, 8 }, { 107, 74, 85, 33 }, { 82, 90, 87, 8 }, { 82, 90, 87, 8 },
  5074. { 66, 99, 88, 33 }, { 90, 90, 90, 0 }, { 90, 90, 90, 0 }, { 90, 90, 90, 0 }, { 99, 90, 93, 9 }, { 99, 90, 93, 9 }, { 99, 90, 93, 9 }, { 107, 90, 95, 17 },
  5075. { 90, 99, 96, 9 }, { 90, 99, 96, 9 }, { 82, 107, 98, 25 }, { 99, 99, 99, 0 }, { 99, 99, 99, 0 }, { 107, 99, 101, 8 }, { 107, 99, 101, 8 }, { 99, 107, 104, 8 },
  5076. { 99, 107, 104, 8 }, { 99, 107, 104, 8 }, { 90, 115, 106, 25 }, { 107, 107, 107, 0 }, { 107, 107, 107, 0 }, { 115, 107, 109, 8 }, { 132, 99, 110, 33 }, { 107, 115, 112, 8 },
  5077. { 107, 115, 112, 8 }, { 107, 115, 112, 8 }, { 115, 115, 115, 0 }, { 115, 115, 115, 0 }, { 115, 115, 115, 0 }, { 123, 115, 117, 8 }, { 140, 107, 118, 33 }, { 115, 123, 120, 8 },
  5078. { 115, 123, 120, 8 }, { 99, 132, 121, 33 }, { 123, 123, 123, 0 }, { 123, 123, 123, 0 }, { 123, 123, 123, 0 }, { 132, 123, 126, 9 }, { 132, 123, 126, 9 }, { 132, 123, 126, 9 },
  5079. { 140, 123, 128, 17 }, { 123, 132, 129, 9 }, { 123, 132, 129, 9 }, { 115, 140, 131, 25 }, { 132, 132, 132, 0 }, { 132, 132, 132, 0 }, { 140, 132, 134, 8 }, { 140, 132, 134, 8 },
  5080. { 132, 140, 137, 8 }, { 132, 140, 137, 8 }, { 132, 140, 137, 8 }, { 123, 148, 139, 25 }, { 140, 140, 140, 0 }, { 140, 140, 140, 0 }, { 148, 140, 142, 8 }, { 165, 132, 143, 33 },
  5081. { 140, 148, 145, 8 }, { 140, 148, 145, 8 }, { 140, 148, 145, 8 }, { 148, 148, 148, 0 }, { 148, 148, 148, 0 }, { 148, 148, 148, 0 }, { 156, 148, 150, 8 }, { 173, 140, 151, 33 },
  5082. { 148, 156, 153, 8 }, { 148, 156, 153, 8 }, { 132, 165, 154, 33 }, { 156, 156, 156, 0 }, { 156, 156, 156, 0 }, { 156, 156, 156, 0 }, { 165, 156, 159, 9 }, { 165, 156, 159, 9 },
  5083. { 165, 156, 159, 9 }, { 173, 156, 161, 17 }, { 156, 165, 162, 9 }, { 156, 165, 162, 9 }, { 148, 173, 164, 25 }, { 165, 165, 165, 0 }, { 165, 165, 165, 0 }, { 173, 165, 167, 8 },
  5084. { 173, 165, 167, 8 }, { 165, 173, 170, 8 }, { 165, 173, 170, 8 }, { 165, 173, 170, 8 }, { 156, 181, 172, 25 }, { 173, 173, 173, 0 }, { 173, 173, 173, 0 }, { 181, 173, 175, 8 },
  5085. { 198, 165, 176, 33 }, { 173, 181, 178, 8 }, { 173, 181, 178, 8 }, { 173, 181, 178, 8 }, { 181, 181, 181, 0 }, { 181, 181, 181, 0 }, { 181, 181, 181, 0 }, { 189, 181, 183, 8 },
  5086. { 206, 173, 184, 33 }, { 181, 189, 186, 8 }, { 181, 189, 186, 8 }, { 165, 198, 187, 33 }, { 189, 189, 189, 0 }, { 189, 189, 189, 0 }, { 189, 189, 189, 0 }, { 198, 189, 192, 9 },
  5087. { 198, 189, 192, 9 }, { 198, 189, 192, 9 }, { 206, 189, 194, 17 }, { 189, 198, 195, 9 }, { 189, 198, 195, 9 }, { 181, 206, 197, 25 }, { 198, 198, 198, 0 }, { 198, 198, 198, 0 },
  5088. { 206, 198, 200, 8 }, { 206, 198, 200, 8 }, { 198, 206, 203, 8 }, { 198, 206, 203, 8 }, { 198, 206, 203, 8 }, { 189, 214, 205, 25 }, { 206, 206, 206, 0 }, { 206, 206, 206, 0 },
  5089. { 214, 206, 208, 8 }, { 231, 198, 209, 33 }, { 206, 214, 211, 8 }, { 206, 214, 211, 8 }, { 206, 214, 211, 8 }, { 214, 214, 214, 0 }, { 214, 214, 214, 0 }, { 214, 214, 214, 0 },
  5090. { 222, 214, 216, 8 }, { 239, 206, 217, 33 }, { 214, 222, 219, 8 }, { 214, 222, 219, 8 }, { 198, 231, 220, 33 }, { 222, 222, 222, 0 }, { 222, 222, 222, 0 }, { 222, 222, 222, 0 },
  5091. { 231, 222, 225, 9 }, { 231, 222, 225, 9 }, { 231, 222, 225, 9 }, { 239, 222, 227, 17 }, { 222, 231, 228, 9 }, { 222, 231, 228, 9 }, { 214, 239, 230, 25 }, { 231, 231, 231, 0 },
  5092. { 231, 231, 231, 0 }, { 239, 231, 233, 8 }, { 239, 231, 233, 8 }, { 231, 239, 236, 8 }, { 231, 239, 236, 8 }, { 231, 239, 236, 8 }, { 222, 247, 238, 25 }, { 239, 239, 239, 0 },
  5093. { 239, 239, 239, 0 }, { 247, 239, 241, 8 }, { 247, 239, 241, 8 }, { 239, 247, 244, 8 }, { 239, 247, 244, 8 }, { 239, 247, 244, 8 }, { 247, 247, 247, 0 }, { 247, 247, 247, 0 },
  5094. { 247, 247, 247, 0 }, { 255, 247, 249, 8 }, { 255, 247, 249, 8 }, { 247, 255, 252, 8 }, { 247, 255, 252, 8 }, { 247, 255, 252, 8 }, { 255, 255, 255, 0 }, { 255, 255, 255, 0 },
  5095. };
  5096. SingleColorTableEntry g_singleColor6_3[256] =
  5097. {
  5098. { 0, 0, 0, 0 }, { 4, 0, 1, 4 }, { 0, 4, 2, 4 }, { 4, 4, 4, 0 }, { 4, 4, 4, 0 }, { 8, 4, 5, 4 }, { 4, 8, 6, 4 }, { 8, 8, 8, 0 },
  5099. { 8, 8, 8, 0 }, { 12, 8, 9, 4 }, { 8, 12, 10, 4 }, { 12, 12, 12, 0 }, { 12, 12, 12, 0 }, { 16, 12, 13, 4 }, { 12, 16, 14, 4 }, { 16, 16, 16, 0 },
  5100. { 16, 16, 16, 0 }, { 20, 16, 17, 4 }, { 16, 20, 18, 4 }, { 20, 20, 20, 0 }, { 20, 20, 20, 0 }, { 24, 20, 21, 4 }, { 20, 24, 22, 4 }, { 69, 0, 23, 69 },
  5101. { 24, 24, 24, 0 }, { 28, 24, 25, 4 }, { 24, 28, 26, 4 }, { 65, 8, 27, 57 }, { 28, 28, 28, 0 }, { 32, 28, 29, 4 }, { 28, 32, 30, 4 }, { 69, 12, 31, 57 },
  5102. { 32, 32, 32, 0 }, { 36, 32, 33, 4 }, { 32, 36, 34, 4 }, { 65, 20, 35, 45 }, { 36, 36, 36, 0 }, { 40, 36, 37, 4 }, { 36, 40, 38, 4 }, { 69, 24, 39, 45 },
  5103. { 40, 40, 40, 0 }, { 44, 40, 41, 4 }, { 40, 44, 42, 4 }, { 65, 32, 43, 33 }, { 44, 44, 44, 0 }, { 48, 44, 45, 4 }, { 44, 48, 46, 4 }, { 69, 36, 47, 33 },
  5104. { 48, 48, 48, 0 }, { 52, 48, 49, 4 }, { 48, 52, 50, 4 }, { 65, 44, 51, 21 }, { 52, 52, 52, 0 }, { 56, 52, 53, 4 }, { 52, 56, 54, 4 }, { 69, 48, 55, 21 },
  5105. { 56, 56, 56, 0 }, { 60, 56, 57, 4 }, { 56, 60, 58, 4 }, { 65, 56, 59, 9 }, { 60, 60, 60, 0 }, { 65, 60, 61, 5 }, { 56, 65, 62, 9 }, { 60, 65, 63, 5 },
  5106. { 56, 69, 64, 13 }, { 65, 65, 65, 0 }, { 69, 65, 66, 4 }, { 65, 69, 67, 4 }, { 60, 73, 68, 13 }, { 69, 69, 69, 0 }, { 73, 69, 70, 4 }, { 69, 73, 71, 4 },
  5107. { 56, 81, 72, 25 }, { 73, 73, 73, 0 }, { 77, 73, 74, 4 }, { 73, 77, 75, 4 }, { 60, 85, 76, 25 }, { 77, 77, 77, 0 }, { 81, 77, 78, 4 }, { 77, 81, 79, 4 },
  5108. { 56, 93, 80, 37 }, { 81, 81, 81, 0 }, { 85, 81, 82, 4 }, { 81, 85, 83, 4 }, { 60, 97, 84, 37 }, { 85, 85, 85, 0 }, { 89, 85, 86, 4 }, { 85, 89, 87, 4 },
  5109. { 56, 105, 88, 49 }, { 89, 89, 89, 0 }, { 93, 89, 90, 4 }, { 89, 93, 91, 4 }, { 60, 109, 92, 49 }, { 93, 93, 93, 0 }, { 97, 93, 94, 4 }, { 93, 97, 95, 4 },
  5110. { 134, 77, 96, 57 }, { 97, 97, 97, 0 }, { 101, 97, 98, 4 }, { 97, 101, 99, 4 }, { 130, 85, 100, 45 }, { 101, 101, 101, 0 }, { 105, 101, 102, 4 }, { 101, 105, 103, 4 },
  5111. { 134, 89, 104, 45 }, { 105, 105, 105, 0 }, { 109, 105, 106, 4 }, { 105, 109, 107, 4 }, { 130, 97, 108, 33 }, { 109, 109, 109, 0 }, { 113, 109, 110, 4 }, { 109, 113, 111, 4 },
  5112. { 134, 101, 112, 33 }, { 113, 113, 113, 0 }, { 117, 113, 114, 4 }, { 113, 117, 115, 4 }, { 130, 109, 116, 21 }, { 117, 117, 117, 0 }, { 121, 117, 118, 4 }, { 117, 121, 119, 4 },
  5113. { 134, 113, 120, 21 }, { 121, 121, 121, 0 }, { 125, 121, 122, 4 }, { 121, 125, 123, 4 }, { 130, 121, 124, 9 }, { 125, 125, 125, 0 }, { 130, 125, 126, 5 }, { 121, 130, 127, 9 },
  5114. { 125, 130, 128, 5 }, { 121, 134, 129, 13 }, { 130, 130, 130, 0 }, { 134, 130, 131, 4 }, { 130, 134, 132, 4 }, { 125, 138, 133, 13 }, { 134, 134, 134, 0 }, { 138, 134, 135, 4 },
  5115. { 134, 138, 136, 4 }, { 121, 146, 137, 25 }, { 138, 138, 138, 0 }, { 142, 138, 139, 4 }, { 138, 142, 140, 4 }, { 125, 150, 141, 25 }, { 142, 142, 142, 0 }, { 146, 142, 143, 4 },
  5116. { 142, 146, 144, 4 }, { 121, 158, 145, 37 }, { 146, 146, 146, 0 }, { 150, 146, 147, 4 }, { 146, 150, 148, 4 }, { 125, 162, 149, 37 }, { 150, 150, 150, 0 }, { 154, 150, 151, 4 },
  5117. { 150, 154, 152, 4 }, { 121, 170, 153, 49 }, { 154, 154, 154, 0 }, { 158, 154, 155, 4 }, { 154, 158, 156, 4 }, { 125, 174, 157, 49 }, { 158, 158, 158, 0 }, { 162, 158, 159, 4 },
  5118. { 158, 162, 160, 4 }, { 199, 142, 161, 57 }, { 162, 162, 162, 0 }, { 166, 162, 163, 4 }, { 162, 166, 164, 4 }, { 195, 150, 165, 45 }, { 166, 166, 166, 0 }, { 170, 166, 167, 4 },
  5119. { 166, 170, 168, 4 }, { 199, 154, 169, 45 }, { 170, 170, 170, 0 }, { 174, 170, 171, 4 }, { 170, 174, 172, 4 }, { 195, 162, 173, 33 }, { 174, 174, 174, 0 }, { 178, 174, 175, 4 },
  5120. { 174, 178, 176, 4 }, { 199, 166, 177, 33 }, { 178, 178, 178, 0 }, { 182, 178, 179, 4 }, { 178, 182, 180, 4 }, { 195, 174, 181, 21 }, { 182, 182, 182, 0 }, { 186, 182, 183, 4 },
  5121. { 182, 186, 184, 4 }, { 199, 178, 185, 21 }, { 186, 186, 186, 0 }, { 190, 186, 187, 4 }, { 186, 190, 188, 4 }, { 195, 186, 189, 9 }, { 190, 190, 190, 0 }, { 195, 190, 191, 5 },
  5122. { 186, 195, 192, 9 }, { 190, 195, 193, 5 }, { 186, 199, 194, 13 }, { 195, 195, 195, 0 }, { 199, 195, 196, 4 }, { 195, 199, 197, 4 }, { 190, 203, 198, 13 }, { 199, 199, 199, 0 },
  5123. { 203, 199, 200, 4 }, { 199, 203, 201, 4 }, { 186, 211, 202, 25 }, { 203, 203, 203, 0 }, { 207, 203, 204, 4 }, { 203, 207, 205, 4 }, { 190, 215, 206, 25 }, { 207, 207, 207, 0 },
  5124. { 211, 207, 208, 4 }, { 207, 211, 209, 4 }, { 186, 223, 210, 37 }, { 211, 211, 211, 0 }, { 215, 211, 212, 4 }, { 211, 215, 213, 4 }, { 190, 227, 214, 37 }, { 215, 215, 215, 0 },
  5125. { 219, 215, 216, 4 }, { 215, 219, 217, 4 }, { 186, 235, 218, 49 }, { 219, 219, 219, 0 }, { 223, 219, 220, 4 }, { 219, 223, 221, 4 }, { 190, 239, 222, 49 }, { 223, 223, 223, 0 },
  5126. { 227, 223, 224, 4 }, { 223, 227, 225, 4 }, { 186, 247, 226, 61 }, { 227, 227, 227, 0 }, { 231, 227, 228, 4 }, { 227, 231, 229, 4 }, { 190, 251, 230, 61 }, { 231, 231, 231, 0 },
  5127. { 235, 231, 232, 4 }, { 231, 235, 233, 4 }, { 235, 235, 235, 0 }, { 235, 235, 235, 0 }, { 239, 235, 236, 4 }, { 235, 239, 237, 4 }, { 239, 239, 239, 0 }, { 239, 239, 239, 0 },
  5128. { 243, 239, 240, 4 }, { 239, 243, 241, 4 }, { 243, 243, 243, 0 }, { 243, 243, 243, 0 }, { 247, 243, 244, 4 }, { 243, 247, 245, 4 }, { 247, 247, 247, 0 }, { 247, 247, 247, 0 },
  5129. { 251, 247, 248, 4 }, { 247, 251, 249, 4 }, { 251, 251, 251, 0 }, { 251, 251, 251, 0 }, { 255, 251, 252, 4 }, { 251, 255, 253, 4 }, { 255, 255, 255, 0 }, { 255, 255, 255, 0 },
  5130. };
  5131. SingleColorTableEntry g_singleColor5_2[256] =
  5132. {
  5133. { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 8, 4, 8 }, { 0, 8, 4, 8 }, { 0, 8, 4, 8 }, { 8, 8, 8, 0 }, { 8, 8, 8, 0 },
  5134. { 8, 8, 8, 0 }, { 8, 8, 8, 0 }, { 8, 8, 8, 0 }, { 8, 16, 12, 8 }, { 8, 16, 12, 8 }, { 8, 16, 12, 8 }, { 16, 16, 16, 0 }, { 16, 16, 16, 0 },
  5135. { 16, 16, 16, 0 }, { 16, 16, 16, 0 }, { 16, 16, 16, 0 }, { 16, 24, 20, 8 }, { 16, 24, 20, 8 }, { 16, 24, 20, 8 }, { 24, 24, 24, 0 }, { 24, 24, 24, 0 },
  5136. { 24, 24, 24, 0 }, { 24, 24, 24, 0 }, { 24, 24, 24, 0 }, { 24, 33, 28, 9 }, { 24, 33, 28, 9 }, { 24, 33, 28, 9 }, { 24, 33, 28, 9 }, { 24, 41, 32, 17 },
  5137. { 24, 41, 32, 17 }, { 33, 33, 33, 0 }, { 33, 33, 33, 0 }, { 24, 49, 36, 25 }, { 24, 49, 36, 25 }, { 33, 41, 37, 8 }, { 33, 41, 37, 8 }, { 24, 57, 40, 33 },
  5138. { 24, 57, 40, 33 }, { 41, 41, 41, 0 }, { 41, 41, 41, 0 }, { 41, 41, 41, 0 }, { 41, 49, 45, 8 }, { 41, 49, 45, 8 }, { 41, 49, 45, 8 }, { 49, 49, 49, 0 },
  5139. { 49, 49, 49, 0 }, { 49, 49, 49, 0 }, { 49, 49, 49, 0 }, { 49, 49, 49, 0 }, { 49, 57, 53, 8 }, { 49, 57, 53, 8 }, { 49, 57, 53, 8 }, { 57, 57, 57, 0 },
  5140. { 57, 57, 57, 0 }, { 57, 57, 57, 0 }, { 57, 57, 57, 0 }, { 57, 57, 57, 0 }, { 57, 66, 61, 9 }, { 57, 66, 61, 9 }, { 57, 66, 61, 9 }, { 57, 66, 61, 9 },
  5141. { 57, 74, 65, 17 }, { 57, 74, 65, 17 }, { 66, 66, 66, 0 }, { 66, 66, 66, 0 }, { 57, 82, 69, 25 }, { 57, 82, 69, 25 }, { 66, 74, 70, 8 }, { 66, 74, 70, 8 },
  5142. { 57, 90, 73, 33 }, { 57, 90, 73, 33 }, { 74, 74, 74, 0 }, { 74, 74, 74, 0 }, { 74, 74, 74, 0 }, { 74, 82, 78, 8 }, { 74, 82, 78, 8 }, { 74, 82, 78, 8 },
  5143. { 82, 82, 82, 0 }, { 82, 82, 82, 0 }, { 82, 82, 82, 0 }, { 82, 82, 82, 0 }, { 82, 82, 82, 0 }, { 82, 90, 86, 8 }, { 82, 90, 86, 8 }, { 82, 90, 86, 8 },
  5144. { 90, 90, 90, 0 }, { 90, 90, 90, 0 }, { 90, 90, 90, 0 }, { 90, 90, 90, 0 }, { 90, 90, 90, 0 }, { 90, 99, 94, 9 }, { 90, 99, 94, 9 }, { 90, 99, 94, 9 },
  5145. { 90, 99, 94, 9 }, { 90, 107, 98, 17 }, { 90, 107, 98, 17 }, { 99, 99, 99, 0 }, { 99, 99, 99, 0 }, { 90, 115, 102, 25 }, { 90, 115, 102, 25 }, { 99, 107, 103, 8 },
  5146. { 99, 107, 103, 8 }, { 90, 123, 106, 33 }, { 90, 123, 106, 33 }, { 107, 107, 107, 0 }, { 107, 107, 107, 0 }, { 107, 107, 107, 0 }, { 107, 115, 111, 8 }, { 107, 115, 111, 8 },
  5147. { 107, 115, 111, 8 }, { 115, 115, 115, 0 }, { 115, 115, 115, 0 }, { 115, 115, 115, 0 }, { 115, 115, 115, 0 }, { 115, 115, 115, 0 }, { 115, 123, 119, 8 }, { 115, 123, 119, 8 },
  5148. { 115, 123, 119, 8 }, { 123, 123, 123, 0 }, { 123, 123, 123, 0 }, { 123, 123, 123, 0 }, { 123, 123, 123, 0 }, { 123, 123, 123, 0 }, { 123, 132, 127, 9 }, { 123, 132, 127, 9 },
  5149. { 123, 132, 127, 9 }, { 123, 132, 127, 9 }, { 123, 140, 131, 17 }, { 123, 140, 131, 17 }, { 132, 132, 132, 0 }, { 132, 132, 132, 0 }, { 123, 148, 135, 25 }, { 123, 148, 135, 25 },
  5150. { 132, 140, 136, 8 }, { 132, 140, 136, 8 }, { 123, 156, 139, 33 }, { 123, 156, 139, 33 }, { 140, 140, 140, 0 }, { 140, 140, 140, 0 }, { 140, 140, 140, 0 }, { 140, 148, 144, 8 },
  5151. { 140, 148, 144, 8 }, { 140, 148, 144, 8 }, { 148, 148, 148, 0 }, { 148, 148, 148, 0 }, { 148, 148, 148, 0 }, { 148, 148, 148, 0 }, { 148, 148, 148, 0 }, { 148, 156, 152, 8 },
  5152. { 148, 156, 152, 8 }, { 148, 156, 152, 8 }, { 156, 156, 156, 0 }, { 156, 156, 156, 0 }, { 156, 156, 156, 0 }, { 156, 156, 156, 0 }, { 156, 156, 156, 0 }, { 156, 165, 160, 9 },
  5153. { 156, 165, 160, 9 }, { 156, 165, 160, 9 }, { 156, 165, 160, 9 }, { 156, 173, 164, 17 }, { 156, 173, 164, 17 }, { 165, 165, 165, 0 }, { 165, 165, 165, 0 }, { 156, 181, 168, 25 },
  5154. { 156, 181, 168, 25 }, { 165, 173, 169, 8 }, { 165, 173, 169, 8 }, { 156, 189, 172, 33 }, { 156, 189, 172, 33 }, { 173, 173, 173, 0 }, { 173, 173, 173, 0 }, { 173, 173, 173, 0 },
  5155. { 173, 181, 177, 8 }, { 173, 181, 177, 8 }, { 173, 181, 177, 8 }, { 181, 181, 181, 0 }, { 181, 181, 181, 0 }, { 181, 181, 181, 0 }, { 181, 181, 181, 0 }, { 181, 181, 181, 0 },
  5156. { 181, 189, 185, 8 }, { 181, 189, 185, 8 }, { 181, 189, 185, 8 }, { 189, 189, 189, 0 }, { 189, 189, 189, 0 }, { 189, 189, 189, 0 }, { 189, 189, 189, 0 }, { 189, 189, 189, 0 },
  5157. { 189, 198, 193, 9 }, { 189, 198, 193, 9 }, { 189, 198, 193, 9 }, { 189, 198, 193, 9 }, { 189, 206, 197, 17 }, { 189, 206, 197, 17 }, { 198, 198, 198, 0 }, { 198, 198, 198, 0 },
  5158. { 189, 214, 201, 25 }, { 189, 214, 201, 25 }, { 198, 206, 202, 8 }, { 198, 206, 202, 8 }, { 189, 222, 205, 33 }, { 189, 222, 205, 33 }, { 206, 206, 206, 0 }, { 206, 206, 206, 0 },
  5159. { 206, 206, 206, 0 }, { 206, 214, 210, 8 }, { 206, 214, 210, 8 }, { 206, 214, 210, 8 }, { 214, 214, 214, 0 }, { 214, 214, 214, 0 }, { 214, 214, 214, 0 }, { 214, 214, 214, 0 },
  5160. { 214, 214, 214, 0 }, { 214, 222, 218, 8 }, { 214, 222, 218, 8 }, { 214, 222, 218, 8 }, { 222, 222, 222, 0 }, { 222, 222, 222, 0 }, { 222, 222, 222, 0 }, { 222, 222, 222, 0 },
  5161. { 222, 222, 222, 0 }, { 222, 231, 226, 9 }, { 222, 231, 226, 9 }, { 222, 231, 226, 9 }, { 222, 231, 226, 9 }, { 222, 239, 230, 17 }, { 222, 239, 230, 17 }, { 231, 231, 231, 0 },
  5162. { 231, 231, 231, 0 }, { 222, 247, 234, 25 }, { 222, 247, 234, 25 }, { 231, 239, 235, 8 }, { 231, 239, 235, 8 }, { 222, 255, 238, 33 }, { 222, 255, 238, 33 }, { 239, 239, 239, 0 },
  5163. { 239, 239, 239, 0 }, { 239, 239, 239, 0 }, { 239, 247, 243, 8 }, { 239, 247, 243, 8 }, { 239, 247, 243, 8 }, { 247, 247, 247, 0 }, { 247, 247, 247, 0 }, { 247, 247, 247, 0 },
  5164. { 247, 247, 247, 0 }, { 247, 247, 247, 0 }, { 247, 255, 251, 8 }, { 247, 255, 251, 8 }, { 247, 255, 251, 8 }, { 255, 255, 255, 0 }, { 255, 255, 255, 0 }, { 255, 255, 255, 0 },
  5165. };
  5166. SingleColorTableEntry g_singleColor6_2[256] =
  5167. {
  5168. { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 4, 2, 4 }, { 4, 4, 4, 0 }, { 4, 4, 4, 0 }, { 4, 4, 4, 0 }, { 4, 8, 6, 4 }, { 8, 8, 8, 0 },
  5169. { 8, 8, 8, 0 }, { 8, 8, 8, 0 }, { 8, 12, 10, 4 }, { 12, 12, 12, 0 }, { 12, 12, 12, 0 }, { 12, 12, 12, 0 }, { 12, 16, 14, 4 }, { 16, 16, 16, 0 },
  5170. { 16, 16, 16, 0 }, { 16, 16, 16, 0 }, { 16, 20, 18, 4 }, { 20, 20, 20, 0 }, { 20, 20, 20, 0 }, { 20, 20, 20, 0 }, { 20, 24, 22, 4 }, { 24, 24, 24, 0 },
  5171. { 24, 24, 24, 0 }, { 24, 24, 24, 0 }, { 24, 28, 26, 4 }, { 28, 28, 28, 0 }, { 28, 28, 28, 0 }, { 28, 28, 28, 0 }, { 28, 32, 30, 4 }, { 32, 32, 32, 0 },
  5172. { 32, 32, 32, 0 }, { 32, 32, 32, 0 }, { 32, 36, 34, 4 }, { 36, 36, 36, 0 }, { 36, 36, 36, 0 }, { 36, 36, 36, 0 }, { 36, 40, 38, 4 }, { 40, 40, 40, 0 },
  5173. { 40, 40, 40, 0 }, { 40, 40, 40, 0 }, { 40, 44, 42, 4 }, { 44, 44, 44, 0 }, { 44, 44, 44, 0 }, { 44, 44, 44, 0 }, { 44, 48, 46, 4 }, { 48, 48, 48, 0 },
  5174. { 48, 48, 48, 0 }, { 48, 48, 48, 0 }, { 48, 52, 50, 4 }, { 52, 52, 52, 0 }, { 52, 52, 52, 0 }, { 52, 52, 52, 0 }, { 52, 56, 54, 4 }, { 56, 56, 56, 0 },
  5175. { 56, 56, 56, 0 }, { 56, 56, 56, 0 }, { 56, 60, 58, 4 }, { 60, 60, 60, 0 }, { 60, 60, 60, 0 }, { 60, 60, 60, 0 }, { 60, 65, 62, 5 }, { 60, 65, 62, 5 },
  5176. { 60, 69, 64, 9 }, { 65, 65, 65, 0 }, { 60, 73, 66, 13 }, { 65, 69, 67, 4 }, { 60, 77, 68, 17 }, { 69, 69, 69, 0 }, { 60, 81, 70, 21 }, { 69, 73, 71, 4 },
  5177. { 60, 85, 72, 25 }, { 73, 73, 73, 0 }, { 60, 89, 74, 29 }, { 73, 77, 75, 4 }, { 60, 93, 76, 33 }, { 77, 77, 77, 0 }, { 60, 97, 78, 37 }, { 77, 81, 79, 4 },
  5178. { 60, 101, 80, 41 }, { 81, 81, 81, 0 }, { 60, 105, 82, 45 }, { 81, 85, 83, 4 }, { 60, 109, 84, 49 }, { 85, 85, 85, 0 }, { 60, 113, 86, 53 }, { 85, 89, 87, 4 },
  5179. { 60, 117, 88, 57 }, { 89, 89, 89, 0 }, { 60, 121, 90, 61 }, { 89, 93, 91, 4 }, { 60, 125, 92, 65 }, { 93, 93, 93, 0 }, { 93, 93, 93, 0 }, { 93, 97, 95, 4 },
  5180. { 97, 97, 97, 0 }, { 97, 97, 97, 0 }, { 97, 97, 97, 0 }, { 97, 101, 99, 4 }, { 101, 101, 101, 0 }, { 101, 101, 101, 0 }, { 101, 101, 101, 0 }, { 101, 105, 103, 4 },
  5181. { 105, 105, 105, 0 }, { 105, 105, 105, 0 }, { 105, 105, 105, 0 }, { 105, 109, 107, 4 }, { 109, 109, 109, 0 }, { 109, 109, 109, 0 }, { 109, 109, 109, 0 }, { 109, 113, 111, 4 },
  5182. { 113, 113, 113, 0 }, { 113, 113, 113, 0 }, { 113, 113, 113, 0 }, { 113, 117, 115, 4 }, { 117, 117, 117, 0 }, { 117, 117, 117, 0 }, { 117, 117, 117, 0 }, { 117, 121, 119, 4 },
  5183. { 121, 121, 121, 0 }, { 121, 121, 121, 0 }, { 121, 121, 121, 0 }, { 121, 125, 123, 4 }, { 125, 125, 125, 0 }, { 125, 125, 125, 0 }, { 125, 125, 125, 0 }, { 125, 130, 127, 5 },
  5184. { 125, 130, 127, 5 }, { 125, 134, 129, 9 }, { 130, 130, 130, 0 }, { 125, 138, 131, 13 }, { 130, 134, 132, 4 }, { 125, 142, 133, 17 }, { 134, 134, 134, 0 }, { 125, 146, 135, 21 },
  5185. { 134, 138, 136, 4 }, { 125, 150, 137, 25 }, { 138, 138, 138, 0 }, { 125, 154, 139, 29 }, { 138, 142, 140, 4 }, { 125, 158, 141, 33 }, { 142, 142, 142, 0 }, { 125, 162, 143, 37 },
  5186. { 142, 146, 144, 4 }, { 125, 166, 145, 41 }, { 146, 146, 146, 0 }, { 125, 170, 147, 45 }, { 146, 150, 148, 4 }, { 125, 174, 149, 49 }, { 150, 150, 150, 0 }, { 125, 178, 151, 53 },
  5187. { 150, 154, 152, 4 }, { 125, 182, 153, 57 }, { 154, 154, 154, 0 }, { 125, 186, 155, 61 }, { 154, 158, 156, 4 }, { 125, 190, 157, 65 }, { 158, 158, 158, 0 }, { 158, 158, 158, 0 },
  5188. { 158, 162, 160, 4 }, { 162, 162, 162, 0 }, { 162, 162, 162, 0 }, { 162, 162, 162, 0 }, { 162, 166, 164, 4 }, { 166, 166, 166, 0 }, { 166, 166, 166, 0 }, { 166, 166, 166, 0 },
  5189. { 166, 170, 168, 4 }, { 170, 170, 170, 0 }, { 170, 170, 170, 0 }, { 170, 170, 170, 0 }, { 170, 174, 172, 4 }, { 174, 174, 174, 0 }, { 174, 174, 174, 0 }, { 174, 174, 174, 0 },
  5190. { 174, 178, 176, 4 }, { 178, 178, 178, 0 }, { 178, 178, 178, 0 }, { 178, 178, 178, 0 }, { 178, 182, 180, 4 }, { 182, 182, 182, 0 }, { 182, 182, 182, 0 }, { 182, 182, 182, 0 },
  5191. { 182, 186, 184, 4 }, { 186, 186, 186, 0 }, { 186, 186, 186, 0 }, { 186, 186, 186, 0 }, { 186, 190, 188, 4 }, { 190, 190, 190, 0 }, { 190, 190, 190, 0 }, { 190, 190, 190, 0 },
  5192. { 190, 195, 192, 5 }, { 190, 195, 192, 5 }, { 190, 199, 194, 9 }, { 195, 195, 195, 0 }, { 190, 203, 196, 13 }, { 195, 199, 197, 4 }, { 190, 207, 198, 17 }, { 199, 199, 199, 0 },
  5193. { 190, 211, 200, 21 }, { 199, 203, 201, 4 }, { 190, 215, 202, 25 }, { 203, 203, 203, 0 }, { 190, 219, 204, 29 }, { 203, 207, 205, 4 }, { 190, 223, 206, 33 }, { 207, 207, 207, 0 },
  5194. { 190, 227, 208, 37 }, { 207, 211, 209, 4 }, { 190, 231, 210, 41 }, { 211, 211, 211, 0 }, { 190, 235, 212, 45 }, { 211, 215, 213, 4 }, { 190, 239, 214, 49 }, { 215, 215, 215, 0 },
  5195. { 190, 243, 216, 53 }, { 215, 219, 217, 4 }, { 190, 247, 218, 57 }, { 219, 219, 219, 0 }, { 190, 251, 220, 61 }, { 219, 223, 221, 4 }, { 190, 255, 222, 65 }, { 223, 223, 223, 0 },
  5196. { 223, 223, 223, 0 }, { 223, 227, 225, 4 }, { 227, 227, 227, 0 }, { 227, 227, 227, 0 }, { 227, 227, 227, 0 }, { 227, 231, 229, 4 }, { 231, 231, 231, 0 }, { 231, 231, 231, 0 },
  5197. { 231, 231, 231, 0 }, { 231, 235, 233, 4 }, { 235, 235, 235, 0 }, { 235, 235, 235, 0 }, { 235, 235, 235, 0 }, { 235, 239, 237, 4 }, { 239, 239, 239, 0 }, { 239, 239, 239, 0 },
  5198. { 239, 239, 239, 0 }, { 239, 243, 241, 4 }, { 243, 243, 243, 0 }, { 243, 243, 243, 0 }, { 243, 243, 243, 0 }, { 243, 247, 245, 4 }, { 247, 247, 247, 0 }, { 247, 247, 247, 0 },
  5199. { 247, 247, 247, 0 }, { 247, 251, 249, 4 }, { 251, 251, 251, 0 }, { 251, 251, 251, 0 }, { 251, 251, 251, 0 }, { 251, 255, 253, 4 }, { 255, 255, 255, 0 }, { 255, 255, 255, 0 },
  5200. };
  5201. SingleColorTableEntry g_singleColor5_3_p[256] =
  5202. {
  5203. { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 8, 0, 2, 8 }, { 8, 0, 2, 8 }, { 0, 8, 5, 8 }, { 0, 8, 5, 8 }, { 0, 8, 5, 8 }, { 8, 8, 8, 0 },
  5204. { 8, 8, 8, 0 }, { 8, 8, 8, 0 }, { 16, 8, 10, 8 }, { 33, 0, 11, 33 }, { 8, 16, 13, 8 }, { 8, 16, 13, 8 }, { 8, 16, 13, 8 }, { 16, 16, 16, 0 },
  5205. { 16, 16, 16, 0 }, { 16, 16, 16, 0 }, { 24, 16, 18, 8 }, { 41, 8, 19, 33 }, { 16, 24, 21, 8 }, { 16, 24, 21, 8 }, { 0, 33, 22, 33 }, { 24, 24, 24, 0 },
  5206. { 24, 24, 24, 0 }, { 24, 24, 24, 0 }, { 33, 24, 27, 9 }, { 33, 24, 27, 9 }, { 33, 24, 27, 9 }, { 41, 24, 29, 17 }, { 24, 33, 30, 9 }, { 24, 33, 30, 9 },
  5207. { 16, 41, 32, 25 }, { 33, 33, 33, 0 }, { 33, 33, 33, 0 }, { 41, 33, 35, 8 }, { 41, 33, 35, 8 }, { 33, 41, 38, 8 }, { 33, 41, 38, 8 }, { 33, 41, 38, 8 },
  5208. { 24, 49, 40, 25 }, { 41, 41, 41, 0 }, { 41, 41, 41, 0 }, { 49, 41, 43, 8 }, { 66, 33, 44, 33 }, { 41, 49, 46, 8 }, { 41, 49, 46, 8 }, { 41, 49, 46, 8 },
  5209. { 49, 49, 49, 0 }, { 49, 49, 49, 0 }, { 49, 49, 49, 0 }, { 57, 49, 51, 8 }, { 74, 41, 52, 33 }, { 49, 57, 54, 8 }, { 49, 57, 54, 8 }, { 33, 66, 55, 33 },
  5210. { 57, 57, 57, 0 }, { 57, 57, 57, 0 }, { 57, 57, 57, 0 }, { 66, 57, 60, 9 }, { 66, 57, 60, 9 }, { 66, 57, 60, 9 }, { 74, 57, 62, 17 }, { 57, 66, 63, 9 },
  5211. { 57, 66, 63, 9 }, { 49, 74, 65, 25 }, { 66, 66, 66, 0 }, { 66, 66, 66, 0 }, { 74, 66, 68, 8 }, { 74, 66, 68, 8 }, { 66, 74, 71, 8 }, { 66, 74, 71, 8 },
  5212. { 66, 74, 71, 8 }, { 57, 82, 73, 25 }, { 74, 74, 74, 0 }, { 74, 74, 74, 0 }, { 82, 74, 76, 8 }, { 99, 66, 77, 33 }, { 74, 82, 79, 8 }, { 74, 82, 79, 8 },
  5213. { 74, 82, 79, 8 }, { 82, 82, 82, 0 }, { 82, 82, 82, 0 }, { 82, 82, 82, 0 }, { 90, 82, 84, 8 }, { 107, 74, 85, 33 }, { 82, 90, 87, 8 }, { 82, 90, 87, 8 },
  5214. { 66, 99, 88, 33 }, { 90, 90, 90, 0 }, { 90, 90, 90, 0 }, { 90, 90, 90, 0 }, { 99, 90, 93, 9 }, { 99, 90, 93, 9 }, { 99, 90, 93, 9 }, { 107, 90, 95, 17 },
  5215. { 90, 99, 96, 9 }, { 90, 99, 96, 9 }, { 82, 107, 98, 25 }, { 99, 99, 99, 0 }, { 99, 99, 99, 0 }, { 107, 99, 101, 8 }, { 107, 99, 101, 8 }, { 99, 107, 104, 8 },
  5216. { 99, 107, 104, 8 }, { 99, 107, 104, 8 }, { 90, 115, 106, 25 }, { 107, 107, 107, 0 }, { 107, 107, 107, 0 }, { 115, 107, 109, 8 }, { 132, 99, 110, 33 }, { 107, 115, 112, 8 },
  5217. { 107, 115, 112, 8 }, { 107, 115, 112, 8 }, { 115, 115, 115, 0 }, { 115, 115, 115, 0 }, { 115, 115, 115, 0 }, { 123, 115, 117, 8 }, { 140, 107, 118, 33 }, { 115, 123, 120, 8 },
  5218. { 115, 123, 120, 8 }, { 99, 132, 121, 33 }, { 123, 123, 123, 0 }, { 123, 123, 123, 0 }, { 123, 123, 123, 0 }, { 132, 123, 126, 9 }, { 132, 123, 126, 9 }, { 132, 123, 126, 9 },
  5219. { 140, 123, 128, 17 }, { 123, 132, 129, 9 }, { 123, 132, 129, 9 }, { 115, 140, 131, 25 }, { 132, 132, 132, 0 }, { 132, 132, 132, 0 }, { 140, 132, 134, 8 }, { 140, 132, 134, 8 },
  5220. { 132, 140, 137, 8 }, { 132, 140, 137, 8 }, { 132, 140, 137, 8 }, { 123, 148, 139, 25 }, { 140, 140, 140, 0 }, { 140, 140, 140, 0 }, { 148, 140, 142, 8 }, { 165, 132, 143, 33 },
  5221. { 140, 148, 145, 8 }, { 140, 148, 145, 8 }, { 140, 148, 145, 8 }, { 148, 148, 148, 0 }, { 148, 148, 148, 0 }, { 148, 148, 148, 0 }, { 156, 148, 150, 8 }, { 173, 140, 151, 33 },
  5222. { 148, 156, 153, 8 }, { 148, 156, 153, 8 }, { 132, 165, 154, 33 }, { 156, 156, 156, 0 }, { 156, 156, 156, 0 }, { 156, 156, 156, 0 }, { 165, 156, 159, 9 }, { 165, 156, 159, 9 },
  5223. { 165, 156, 159, 9 }, { 173, 156, 161, 17 }, { 156, 165, 162, 9 }, { 156, 165, 162, 9 }, { 148, 173, 164, 25 }, { 165, 165, 165, 0 }, { 165, 165, 165, 0 }, { 173, 165, 167, 8 },
  5224. { 173, 165, 167, 8 }, { 165, 173, 170, 8 }, { 165, 173, 170, 8 }, { 165, 173, 170, 8 }, { 156, 181, 172, 25 }, { 173, 173, 173, 0 }, { 173, 173, 173, 0 }, { 181, 173, 175, 8 },
  5225. { 198, 165, 176, 33 }, { 173, 181, 178, 8 }, { 173, 181, 178, 8 }, { 173, 181, 178, 8 }, { 181, 181, 181, 0 }, { 181, 181, 181, 0 }, { 181, 181, 181, 0 }, { 189, 181, 183, 8 },
  5226. { 206, 173, 184, 33 }, { 181, 189, 186, 8 }, { 181, 189, 186, 8 }, { 165, 198, 187, 33 }, { 189, 189, 189, 0 }, { 189, 189, 189, 0 }, { 189, 189, 189, 0 }, { 198, 189, 192, 9 },
  5227. { 198, 189, 192, 9 }, { 198, 189, 192, 9 }, { 206, 189, 194, 17 }, { 189, 198, 195, 9 }, { 189, 198, 195, 9 }, { 181, 206, 197, 25 }, { 198, 198, 198, 0 }, { 198, 198, 198, 0 },
  5228. { 206, 198, 200, 8 }, { 206, 198, 200, 8 }, { 198, 206, 203, 8 }, { 198, 206, 203, 8 }, { 198, 206, 203, 8 }, { 189, 214, 205, 25 }, { 206, 206, 206, 0 }, { 206, 206, 206, 0 },
  5229. { 214, 206, 208, 8 }, { 231, 198, 209, 33 }, { 206, 214, 211, 8 }, { 206, 214, 211, 8 }, { 206, 214, 211, 8 }, { 214, 214, 214, 0 }, { 214, 214, 214, 0 }, { 214, 214, 214, 0 },
  5230. { 222, 214, 216, 8 }, { 239, 206, 217, 33 }, { 214, 222, 219, 8 }, { 214, 222, 219, 8 }, { 198, 231, 220, 33 }, { 222, 222, 222, 0 }, { 222, 222, 222, 0 }, { 222, 222, 222, 0 },
  5231. { 231, 222, 225, 9 }, { 231, 222, 225, 9 }, { 231, 222, 225, 9 }, { 239, 222, 227, 17 }, { 222, 231, 228, 9 }, { 222, 231, 228, 9 }, { 214, 239, 230, 25 }, { 231, 231, 231, 0 },
  5232. { 231, 231, 231, 0 }, { 239, 231, 233, 8 }, { 239, 231, 233, 8 }, { 231, 239, 236, 8 }, { 231, 239, 236, 8 }, { 231, 239, 236, 8 }, { 222, 247, 238, 25 }, { 239, 239, 239, 0 },
  5233. { 239, 239, 239, 0 }, { 247, 239, 241, 8 }, { 247, 239, 241, 8 }, { 239, 247, 244, 8 }, { 239, 247, 244, 8 }, { 239, 247, 244, 8 }, { 247, 247, 247, 0 }, { 247, 247, 247, 0 },
  5234. { 247, 247, 247, 0 }, { 255, 247, 249, 8 }, { 255, 247, 249, 8 }, { 247, 255, 252, 8 }, { 247, 255, 252, 8 }, { 247, 255, 252, 8 }, { 255, 255, 255, 0 }, { 255, 255, 255, 0 },
  5235. };
  5236. SingleColorTableEntry g_singleColor6_3_p[256] =
  5237. {
  5238. { 0, 0, 0, 0 }, { 4, 0, 1, 4 }, { 0, 4, 2, 4 }, { 4, 4, 4, 0 }, { 4, 4, 4, 0 }, { 8, 4, 5, 4 }, { 4, 8, 6, 4 }, { 8, 8, 8, 0 },
  5239. { 8, 8, 8, 0 }, { 12, 8, 9, 4 }, { 8, 12, 10, 4 }, { 12, 12, 12, 0 }, { 12, 12, 12, 0 }, { 16, 12, 13, 4 }, { 12, 16, 14, 4 }, { 16, 16, 16, 0 },
  5240. { 16, 16, 16, 0 }, { 20, 16, 17, 4 }, { 16, 20, 18, 4 }, { 20, 20, 20, 0 }, { 20, 20, 20, 0 }, { 24, 20, 21, 4 }, { 20, 24, 22, 4 }, { 24, 24, 24, 0 },
  5241. { 24, 24, 24, 0 }, { 28, 24, 25, 4 }, { 24, 28, 26, 4 }, { 28, 28, 28, 0 }, { 28, 28, 28, 0 }, { 32, 28, 29, 4 }, { 28, 32, 30, 4 }, { 32, 32, 32, 0 },
  5242. { 32, 32, 32, 0 }, { 36, 32, 33, 4 }, { 32, 36, 34, 4 }, { 36, 36, 36, 0 }, { 36, 36, 36, 0 }, { 40, 36, 37, 4 }, { 36, 40, 38, 4 }, { 40, 40, 40, 0 },
  5243. { 40, 40, 40, 0 }, { 44, 40, 41, 4 }, { 40, 44, 42, 4 }, { 65, 32, 43, 33 }, { 44, 44, 44, 0 }, { 48, 44, 45, 4 }, { 44, 48, 46, 4 }, { 69, 36, 47, 33 },
  5244. { 48, 48, 48, 0 }, { 52, 48, 49, 4 }, { 48, 52, 50, 4 }, { 65, 44, 51, 21 }, { 52, 52, 52, 0 }, { 56, 52, 53, 4 }, { 52, 56, 54, 4 }, { 69, 48, 55, 21 },
  5245. { 56, 56, 56, 0 }, { 60, 56, 57, 4 }, { 56, 60, 58, 4 }, { 65, 56, 59, 9 }, { 60, 60, 60, 0 }, { 65, 60, 61, 5 }, { 56, 65, 62, 9 }, { 60, 65, 63, 5 },
  5246. { 56, 69, 64, 13 }, { 65, 65, 65, 0 }, { 69, 65, 66, 4 }, { 65, 69, 67, 4 }, { 60, 73, 68, 13 }, { 69, 69, 69, 0 }, { 73, 69, 70, 4 }, { 69, 73, 71, 4 },
  5247. { 56, 81, 72, 25 }, { 73, 73, 73, 0 }, { 77, 73, 74, 4 }, { 73, 77, 75, 4 }, { 60, 85, 76, 25 }, { 77, 77, 77, 0 }, { 81, 77, 78, 4 }, { 77, 81, 79, 4 },
  5248. { 81, 81, 81, 0 }, { 81, 81, 81, 0 }, { 85, 81, 82, 4 }, { 81, 85, 83, 4 }, { 85, 85, 85, 0 }, { 85, 85, 85, 0 }, { 89, 85, 86, 4 }, { 85, 89, 87, 4 },
  5249. { 89, 89, 89, 0 }, { 89, 89, 89, 0 }, { 93, 89, 90, 4 }, { 89, 93, 91, 4 }, { 93, 93, 93, 0 }, { 93, 93, 93, 0 }, { 97, 93, 94, 4 }, { 93, 97, 95, 4 },
  5250. { 97, 97, 97, 0 }, { 97, 97, 97, 0 }, { 101, 97, 98, 4 }, { 97, 101, 99, 4 }, { 101, 101, 101, 0 }, { 101, 101, 101, 0 }, { 105, 101, 102, 4 }, { 101, 105, 103, 4 },
  5251. { 105, 105, 105, 0 }, { 105, 105, 105, 0 }, { 109, 105, 106, 4 }, { 105, 109, 107, 4 }, { 130, 97, 108, 33 }, { 109, 109, 109, 0 }, { 113, 109, 110, 4 }, { 109, 113, 111, 4 },
  5252. { 134, 101, 112, 33 }, { 113, 113, 113, 0 }, { 117, 113, 114, 4 }, { 113, 117, 115, 4 }, { 130, 109, 116, 21 }, { 117, 117, 117, 0 }, { 121, 117, 118, 4 }, { 117, 121, 119, 4 },
  5253. { 134, 113, 120, 21 }, { 121, 121, 121, 0 }, { 125, 121, 122, 4 }, { 121, 125, 123, 4 }, { 130, 121, 124, 9 }, { 125, 125, 125, 0 }, { 130, 125, 126, 5 }, { 121, 130, 127, 9 },
  5254. { 125, 130, 128, 5 }, { 121, 134, 129, 13 }, { 130, 130, 130, 0 }, { 134, 130, 131, 4 }, { 130, 134, 132, 4 }, { 125, 138, 133, 13 }, { 134, 134, 134, 0 }, { 138, 134, 135, 4 },
  5255. { 134, 138, 136, 4 }, { 121, 146, 137, 25 }, { 138, 138, 138, 0 }, { 142, 138, 139, 4 }, { 138, 142, 140, 4 }, { 125, 150, 141, 25 }, { 142, 142, 142, 0 }, { 146, 142, 143, 4 },
  5256. { 142, 146, 144, 4 }, { 146, 146, 146, 0 }, { 146, 146, 146, 0 }, { 150, 146, 147, 4 }, { 146, 150, 148, 4 }, { 150, 150, 150, 0 }, { 150, 150, 150, 0 }, { 154, 150, 151, 4 },
  5257. { 150, 154, 152, 4 }, { 154, 154, 154, 0 }, { 154, 154, 154, 0 }, { 158, 154, 155, 4 }, { 154, 158, 156, 4 }, { 158, 158, 158, 0 }, { 158, 158, 158, 0 }, { 162, 158, 159, 4 },
  5258. { 158, 162, 160, 4 }, { 162, 162, 162, 0 }, { 162, 162, 162, 0 }, { 166, 162, 163, 4 }, { 162, 166, 164, 4 }, { 166, 166, 166, 0 }, { 166, 166, 166, 0 }, { 170, 166, 167, 4 },
  5259. { 166, 170, 168, 4 }, { 170, 170, 170, 0 }, { 170, 170, 170, 0 }, { 174, 170, 171, 4 }, { 170, 174, 172, 4 }, { 195, 162, 173, 33 }, { 174, 174, 174, 0 }, { 178, 174, 175, 4 },
  5260. { 174, 178, 176, 4 }, { 199, 166, 177, 33 }, { 178, 178, 178, 0 }, { 182, 178, 179, 4 }, { 178, 182, 180, 4 }, { 195, 174, 181, 21 }, { 182, 182, 182, 0 }, { 186, 182, 183, 4 },
  5261. { 182, 186, 184, 4 }, { 199, 178, 185, 21 }, { 186, 186, 186, 0 }, { 190, 186, 187, 4 }, { 186, 190, 188, 4 }, { 195, 186, 189, 9 }, { 190, 190, 190, 0 }, { 195, 190, 191, 5 },
  5262. { 186, 195, 192, 9 }, { 190, 195, 193, 5 }, { 186, 199, 194, 13 }, { 195, 195, 195, 0 }, { 199, 195, 196, 4 }, { 195, 199, 197, 4 }, { 190, 203, 198, 13 }, { 199, 199, 199, 0 },
  5263. { 203, 199, 200, 4 }, { 199, 203, 201, 4 }, { 186, 211, 202, 25 }, { 203, 203, 203, 0 }, { 207, 203, 204, 4 }, { 203, 207, 205, 4 }, { 190, 215, 206, 25 }, { 207, 207, 207, 0 },
  5264. { 211, 207, 208, 4 }, { 207, 211, 209, 4 }, { 211, 211, 211, 0 }, { 211, 211, 211, 0 }, { 215, 211, 212, 4 }, { 211, 215, 213, 4 }, { 215, 215, 215, 0 }, { 215, 215, 215, 0 },
  5265. { 219, 215, 216, 4 }, { 215, 219, 217, 4 }, { 219, 219, 219, 0 }, { 219, 219, 219, 0 }, { 223, 219, 220, 4 }, { 219, 223, 221, 4 }, { 223, 223, 223, 0 }, { 223, 223, 223, 0 },
  5266. { 227, 223, 224, 4 }, { 223, 227, 225, 4 }, { 227, 227, 227, 0 }, { 227, 227, 227, 0 }, { 231, 227, 228, 4 }, { 227, 231, 229, 4 }, { 231, 231, 231, 0 }, { 231, 231, 231, 0 },
  5267. { 235, 231, 232, 4 }, { 231, 235, 233, 4 }, { 235, 235, 235, 0 }, { 235, 235, 235, 0 }, { 239, 235, 236, 4 }, { 235, 239, 237, 4 }, { 239, 239, 239, 0 }, { 239, 239, 239, 0 },
  5268. { 243, 239, 240, 4 }, { 239, 243, 241, 4 }, { 243, 243, 243, 0 }, { 243, 243, 243, 0 }, { 247, 243, 244, 4 }, { 243, 247, 245, 4 }, { 247, 247, 247, 0 }, { 247, 247, 247, 0 },
  5269. { 251, 247, 248, 4 }, { 247, 251, 249, 4 }, { 251, 251, 251, 0 }, { 251, 251, 251, 0 }, { 255, 251, 252, 4 }, { 251, 255, 253, 4 }, { 255, 255, 255, 0 }, { 255, 255, 255, 0 },
  5270. };
  5271. SingleColorTableEntry g_singleColor5_2_p[256] =
  5272. {
  5273. { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 8, 4, 8 }, { 0, 8, 4, 8 }, { 0, 8, 4, 8 }, { 8, 8, 8, 0 }, { 8, 8, 8, 0 },
  5274. { 8, 8, 8, 0 }, { 8, 8, 8, 0 }, { 8, 8, 8, 0 }, { 8, 16, 12, 8 }, { 8, 16, 12, 8 }, { 8, 16, 12, 8 }, { 16, 16, 16, 0 }, { 16, 16, 16, 0 },
  5275. { 16, 16, 16, 0 }, { 16, 16, 16, 0 }, { 16, 16, 16, 0 }, { 16, 24, 20, 8 }, { 16, 24, 20, 8 }, { 16, 24, 20, 8 }, { 24, 24, 24, 0 }, { 24, 24, 24, 0 },
  5276. { 24, 24, 24, 0 }, { 24, 24, 24, 0 }, { 24, 24, 24, 0 }, { 24, 33, 28, 9 }, { 24, 33, 28, 9 }, { 24, 33, 28, 9 }, { 24, 33, 28, 9 }, { 24, 41, 32, 17 },
  5277. { 24, 41, 32, 17 }, { 33, 33, 33, 0 }, { 33, 33, 33, 0 }, { 24, 49, 36, 25 }, { 24, 49, 36, 25 }, { 33, 41, 37, 8 }, { 33, 41, 37, 8 }, { 24, 57, 40, 33 },
  5278. { 24, 57, 40, 33 }, { 41, 41, 41, 0 }, { 41, 41, 41, 0 }, { 41, 41, 41, 0 }, { 41, 49, 45, 8 }, { 41, 49, 45, 8 }, { 41, 49, 45, 8 }, { 49, 49, 49, 0 },
  5279. { 49, 49, 49, 0 }, { 49, 49, 49, 0 }, { 49, 49, 49, 0 }, { 49, 49, 49, 0 }, { 49, 57, 53, 8 }, { 49, 57, 53, 8 }, { 49, 57, 53, 8 }, { 57, 57, 57, 0 },
  5280. { 57, 57, 57, 0 }, { 57, 57, 57, 0 }, { 57, 57, 57, 0 }, { 57, 57, 57, 0 }, { 57, 66, 61, 9 }, { 57, 66, 61, 9 }, { 57, 66, 61, 9 }, { 57, 66, 61, 9 },
  5281. { 57, 74, 65, 17 }, { 57, 74, 65, 17 }, { 66, 66, 66, 0 }, { 66, 66, 66, 0 }, { 57, 82, 69, 25 }, { 57, 82, 69, 25 }, { 66, 74, 70, 8 }, { 66, 74, 70, 8 },
  5282. { 57, 90, 73, 33 }, { 57, 90, 73, 33 }, { 74, 74, 74, 0 }, { 74, 74, 74, 0 }, { 74, 74, 74, 0 }, { 74, 82, 78, 8 }, { 74, 82, 78, 8 }, { 74, 82, 78, 8 },
  5283. { 82, 82, 82, 0 }, { 82, 82, 82, 0 }, { 82, 82, 82, 0 }, { 82, 82, 82, 0 }, { 82, 82, 82, 0 }, { 82, 90, 86, 8 }, { 82, 90, 86, 8 }, { 82, 90, 86, 8 },
  5284. { 90, 90, 90, 0 }, { 90, 90, 90, 0 }, { 90, 90, 90, 0 }, { 90, 90, 90, 0 }, { 90, 90, 90, 0 }, { 90, 99, 94, 9 }, { 90, 99, 94, 9 }, { 90, 99, 94, 9 },
  5285. { 90, 99, 94, 9 }, { 90, 107, 98, 17 }, { 90, 107, 98, 17 }, { 99, 99, 99, 0 }, { 99, 99, 99, 0 }, { 90, 115, 102, 25 }, { 90, 115, 102, 25 }, { 99, 107, 103, 8 },
  5286. { 99, 107, 103, 8 }, { 90, 123, 106, 33 }, { 90, 123, 106, 33 }, { 107, 107, 107, 0 }, { 107, 107, 107, 0 }, { 107, 107, 107, 0 }, { 107, 115, 111, 8 }, { 107, 115, 111, 8 },
  5287. { 107, 115, 111, 8 }, { 115, 115, 115, 0 }, { 115, 115, 115, 0 }, { 115, 115, 115, 0 }, { 115, 115, 115, 0 }, { 115, 115, 115, 0 }, { 115, 123, 119, 8 }, { 115, 123, 119, 8 },
  5288. { 115, 123, 119, 8 }, { 123, 123, 123, 0 }, { 123, 123, 123, 0 }, { 123, 123, 123, 0 }, { 123, 123, 123, 0 }, { 123, 123, 123, 0 }, { 123, 132, 127, 9 }, { 123, 132, 127, 9 },
  5289. { 123, 132, 127, 9 }, { 123, 132, 127, 9 }, { 123, 140, 131, 17 }, { 123, 140, 131, 17 }, { 132, 132, 132, 0 }, { 132, 132, 132, 0 }, { 123, 148, 135, 25 }, { 123, 148, 135, 25 },
  5290. { 132, 140, 136, 8 }, { 132, 140, 136, 8 }, { 123, 156, 139, 33 }, { 123, 156, 139, 33 }, { 140, 140, 140, 0 }, { 140, 140, 140, 0 }, { 140, 140, 140, 0 }, { 140, 148, 144, 8 },
  5291. { 140, 148, 144, 8 }, { 140, 148, 144, 8 }, { 148, 148, 148, 0 }, { 148, 148, 148, 0 }, { 148, 148, 148, 0 }, { 148, 148, 148, 0 }, { 148, 148, 148, 0 }, { 148, 156, 152, 8 },
  5292. { 148, 156, 152, 8 }, { 148, 156, 152, 8 }, { 156, 156, 156, 0 }, { 156, 156, 156, 0 }, { 156, 156, 156, 0 }, { 156, 156, 156, 0 }, { 156, 156, 156, 0 }, { 156, 165, 160, 9 },
  5293. { 156, 165, 160, 9 }, { 156, 165, 160, 9 }, { 156, 165, 160, 9 }, { 156, 173, 164, 17 }, { 156, 173, 164, 17 }, { 165, 165, 165, 0 }, { 165, 165, 165, 0 }, { 156, 181, 168, 25 },
  5294. { 156, 181, 168, 25 }, { 165, 173, 169, 8 }, { 165, 173, 169, 8 }, { 156, 189, 172, 33 }, { 156, 189, 172, 33 }, { 173, 173, 173, 0 }, { 173, 173, 173, 0 }, { 173, 173, 173, 0 },
  5295. { 173, 181, 177, 8 }, { 173, 181, 177, 8 }, { 173, 181, 177, 8 }, { 181, 181, 181, 0 }, { 181, 181, 181, 0 }, { 181, 181, 181, 0 }, { 181, 181, 181, 0 }, { 181, 181, 181, 0 },
  5296. { 181, 189, 185, 8 }, { 181, 189, 185, 8 }, { 181, 189, 185, 8 }, { 189, 189, 189, 0 }, { 189, 189, 189, 0 }, { 189, 189, 189, 0 }, { 189, 189, 189, 0 }, { 189, 189, 189, 0 },
  5297. { 189, 198, 193, 9 }, { 189, 198, 193, 9 }, { 189, 198, 193, 9 }, { 189, 198, 193, 9 }, { 189, 206, 197, 17 }, { 189, 206, 197, 17 }, { 198, 198, 198, 0 }, { 198, 198, 198, 0 },
  5298. { 189, 214, 201, 25 }, { 189, 214, 201, 25 }, { 198, 206, 202, 8 }, { 198, 206, 202, 8 }, { 189, 222, 205, 33 }, { 189, 222, 205, 33 }, { 206, 206, 206, 0 }, { 206, 206, 206, 0 },
  5299. { 206, 206, 206, 0 }, { 206, 214, 210, 8 }, { 206, 214, 210, 8 }, { 206, 214, 210, 8 }, { 214, 214, 214, 0 }, { 214, 214, 214, 0 }, { 214, 214, 214, 0 }, { 214, 214, 214, 0 },
  5300. { 214, 214, 214, 0 }, { 214, 222, 218, 8 }, { 214, 222, 218, 8 }, { 214, 222, 218, 8 }, { 222, 222, 222, 0 }, { 222, 222, 222, 0 }, { 222, 222, 222, 0 }, { 222, 222, 222, 0 },
  5301. { 222, 222, 222, 0 }, { 222, 231, 226, 9 }, { 222, 231, 226, 9 }, { 222, 231, 226, 9 }, { 222, 231, 226, 9 }, { 222, 239, 230, 17 }, { 222, 239, 230, 17 }, { 231, 231, 231, 0 },
  5302. { 231, 231, 231, 0 }, { 222, 247, 234, 25 }, { 222, 247, 234, 25 }, { 231, 239, 235, 8 }, { 231, 239, 235, 8 }, { 222, 255, 238, 33 }, { 222, 255, 238, 33 }, { 239, 239, 239, 0 },
  5303. { 239, 239, 239, 0 }, { 239, 239, 239, 0 }, { 239, 247, 243, 8 }, { 239, 247, 243, 8 }, { 239, 247, 243, 8 }, { 247, 247, 247, 0 }, { 247, 247, 247, 0 }, { 247, 247, 247, 0 },
  5304. { 247, 247, 247, 0 }, { 247, 247, 247, 0 }, { 247, 255, 251, 8 }, { 247, 255, 251, 8 }, { 247, 255, 251, 8 }, { 255, 255, 255, 0 }, { 255, 255, 255, 0 }, { 255, 255, 255, 0 },
  5305. };
  5306. SingleColorTableEntry g_singleColor6_2_p[256] =
  5307. {
  5308. { 0, 0, 0, 0 }, { 0, 0, 0, 0 }, { 0, 4, 2, 4 }, { 4, 4, 4, 0 }, { 4, 4, 4, 0 }, { 4, 4, 4, 0 }, { 4, 8, 6, 4 }, { 8, 8, 8, 0 },
  5309. { 8, 8, 8, 0 }, { 8, 8, 8, 0 }, { 8, 12, 10, 4 }, { 12, 12, 12, 0 }, { 12, 12, 12, 0 }, { 12, 12, 12, 0 }, { 12, 16, 14, 4 }, { 16, 16, 16, 0 },
  5310. { 16, 16, 16, 0 }, { 16, 16, 16, 0 }, { 16, 20, 18, 4 }, { 20, 20, 20, 0 }, { 20, 20, 20, 0 }, { 20, 20, 20, 0 }, { 20, 24, 22, 4 }, { 24, 24, 24, 0 },
  5311. { 24, 24, 24, 0 }, { 24, 24, 24, 0 }, { 24, 28, 26, 4 }, { 28, 28, 28, 0 }, { 28, 28, 28, 0 }, { 28, 28, 28, 0 }, { 28, 32, 30, 4 }, { 32, 32, 32, 0 },
  5312. { 32, 32, 32, 0 }, { 32, 32, 32, 0 }, { 32, 36, 34, 4 }, { 36, 36, 36, 0 }, { 36, 36, 36, 0 }, { 36, 36, 36, 0 }, { 36, 40, 38, 4 }, { 40, 40, 40, 0 },
  5313. { 40, 40, 40, 0 }, { 40, 40, 40, 0 }, { 40, 44, 42, 4 }, { 44, 44, 44, 0 }, { 44, 44, 44, 0 }, { 44, 44, 44, 0 }, { 44, 48, 46, 4 }, { 48, 48, 48, 0 },
  5314. { 48, 48, 48, 0 }, { 48, 48, 48, 0 }, { 48, 52, 50, 4 }, { 52, 52, 52, 0 }, { 52, 52, 52, 0 }, { 52, 52, 52, 0 }, { 52, 56, 54, 4 }, { 56, 56, 56, 0 },
  5315. { 56, 56, 56, 0 }, { 56, 56, 56, 0 }, { 56, 60, 58, 4 }, { 60, 60, 60, 0 }, { 60, 60, 60, 0 }, { 60, 60, 60, 0 }, { 60, 65, 62, 5 }, { 60, 65, 62, 5 },
  5316. { 60, 69, 64, 9 }, { 65, 65, 65, 0 }, { 60, 73, 66, 13 }, { 65, 69, 67, 4 }, { 60, 77, 68, 17 }, { 69, 69, 69, 0 }, { 60, 81, 70, 21 }, { 69, 73, 71, 4 },
  5317. { 60, 85, 72, 25 }, { 73, 73, 73, 0 }, { 60, 89, 74, 29 }, { 73, 77, 75, 4 }, { 60, 93, 76, 33 }, { 77, 77, 77, 0 }, { 77, 77, 77, 0 }, { 77, 81, 79, 4 },
  5318. { 81, 81, 81, 0 }, { 81, 81, 81, 0 }, { 81, 81, 81, 0 }, { 81, 85, 83, 4 }, { 85, 85, 85, 0 }, { 85, 85, 85, 0 }, { 85, 85, 85, 0 }, { 85, 89, 87, 4 },
  5319. { 89, 89, 89, 0 }, { 89, 89, 89, 0 }, { 89, 89, 89, 0 }, { 89, 93, 91, 4 }, { 93, 93, 93, 0 }, { 93, 93, 93, 0 }, { 93, 93, 93, 0 }, { 93, 97, 95, 4 },
  5320. { 97, 97, 97, 0 }, { 97, 97, 97, 0 }, { 97, 97, 97, 0 }, { 97, 101, 99, 4 }, { 101, 101, 101, 0 }, { 101, 101, 101, 0 }, { 101, 101, 101, 0 }, { 101, 105, 103, 4 },
  5321. { 105, 105, 105, 0 }, { 105, 105, 105, 0 }, { 105, 105, 105, 0 }, { 105, 109, 107, 4 }, { 109, 109, 109, 0 }, { 109, 109, 109, 0 }, { 109, 109, 109, 0 }, { 109, 113, 111, 4 },
  5322. { 113, 113, 113, 0 }, { 113, 113, 113, 0 }, { 113, 113, 113, 0 }, { 113, 117, 115, 4 }, { 117, 117, 117, 0 }, { 117, 117, 117, 0 }, { 117, 117, 117, 0 }, { 117, 121, 119, 4 },
  5323. { 121, 121, 121, 0 }, { 121, 121, 121, 0 }, { 121, 121, 121, 0 }, { 121, 125, 123, 4 }, { 125, 125, 125, 0 }, { 125, 125, 125, 0 }, { 125, 125, 125, 0 }, { 125, 130, 127, 5 },
  5324. { 125, 130, 127, 5 }, { 125, 134, 129, 9 }, { 130, 130, 130, 0 }, { 125, 138, 131, 13 }, { 130, 134, 132, 4 }, { 125, 142, 133, 17 }, { 134, 134, 134, 0 }, { 125, 146, 135, 21 },
  5325. { 134, 138, 136, 4 }, { 125, 150, 137, 25 }, { 138, 138, 138, 0 }, { 125, 154, 139, 29 }, { 138, 142, 140, 4 }, { 125, 158, 141, 33 }, { 142, 142, 142, 0 }, { 142, 142, 142, 0 },
  5326. { 142, 146, 144, 4 }, { 146, 146, 146, 0 }, { 146, 146, 146, 0 }, { 146, 146, 146, 0 }, { 146, 150, 148, 4 }, { 150, 150, 150, 0 }, { 150, 150, 150, 0 }, { 150, 150, 150, 0 },
  5327. { 150, 154, 152, 4 }, { 154, 154, 154, 0 }, { 154, 154, 154, 0 }, { 154, 154, 154, 0 }, { 154, 158, 156, 4 }, { 158, 158, 158, 0 }, { 158, 158, 158, 0 }, { 158, 158, 158, 0 },
  5328. { 158, 162, 160, 4 }, { 162, 162, 162, 0 }, { 162, 162, 162, 0 }, { 162, 162, 162, 0 }, { 162, 166, 164, 4 }, { 166, 166, 166, 0 }, { 166, 166, 166, 0 }, { 166, 166, 166, 0 },
  5329. { 166, 170, 168, 4 }, { 170, 170, 170, 0 }, { 170, 170, 170, 0 }, { 170, 170, 170, 0 }, { 170, 174, 172, 4 }, { 174, 174, 174, 0 }, { 174, 174, 174, 0 }, { 174, 174, 174, 0 },
  5330. { 174, 178, 176, 4 }, { 178, 178, 178, 0 }, { 178, 178, 178, 0 }, { 178, 178, 178, 0 }, { 178, 182, 180, 4 }, { 182, 182, 182, 0 }, { 182, 182, 182, 0 }, { 182, 182, 182, 0 },
  5331. { 182, 186, 184, 4 }, { 186, 186, 186, 0 }, { 186, 186, 186, 0 }, { 186, 186, 186, 0 }, { 186, 190, 188, 4 }, { 190, 190, 190, 0 }, { 190, 190, 190, 0 }, { 190, 190, 190, 0 },
  5332. { 190, 195, 192, 5 }, { 190, 195, 192, 5 }, { 190, 199, 194, 9 }, { 195, 195, 195, 0 }, { 190, 203, 196, 13 }, { 195, 199, 197, 4 }, { 190, 207, 198, 17 }, { 199, 199, 199, 0 },
  5333. { 190, 211, 200, 21 }, { 199, 203, 201, 4 }, { 190, 215, 202, 25 }, { 203, 203, 203, 0 }, { 190, 219, 204, 29 }, { 203, 207, 205, 4 }, { 190, 223, 206, 33 }, { 207, 207, 207, 0 },
  5334. { 207, 207, 207, 0 }, { 207, 211, 209, 4 }, { 211, 211, 211, 0 }, { 211, 211, 211, 0 }, { 211, 211, 211, 0 }, { 211, 215, 213, 4 }, { 215, 215, 215, 0 }, { 215, 215, 215, 0 },
  5335. { 215, 215, 215, 0 }, { 215, 219, 217, 4 }, { 219, 219, 219, 0 }, { 219, 219, 219, 0 }, { 219, 219, 219, 0 }, { 219, 223, 221, 4 }, { 223, 223, 223, 0 }, { 223, 223, 223, 0 },
  5336. { 223, 223, 223, 0 }, { 223, 227, 225, 4 }, { 227, 227, 227, 0 }, { 227, 227, 227, 0 }, { 227, 227, 227, 0 }, { 227, 231, 229, 4 }, { 231, 231, 231, 0 }, { 231, 231, 231, 0 },
  5337. { 231, 231, 231, 0 }, { 231, 235, 233, 4 }, { 235, 235, 235, 0 }, { 235, 235, 235, 0 }, { 235, 235, 235, 0 }, { 235, 239, 237, 4 }, { 239, 239, 239, 0 }, { 239, 239, 239, 0 },
  5338. { 239, 239, 239, 0 }, { 239, 243, 241, 4 }, { 243, 243, 243, 0 }, { 243, 243, 243, 0 }, { 243, 243, 243, 0 }, { 243, 247, 245, 4 }, { 247, 247, 247, 0 }, { 247, 247, 247, 0 },
  5339. { 247, 247, 247, 0 }, { 247, 251, 249, 4 }, { 251, 251, 251, 0 }, { 251, 251, 251, 0 }, { 251, 251, 251, 0 }, { 251, 255, 253, 4 }, { 255, 255, 255, 0 }, { 255, 255, 255, 0 },
  5340. };
  5341. }
  5342. class S3TCComputer
  5343. {
  5344. public:
  5345. typedef ParallelMath::Float MFloat;
  5346. typedef ParallelMath::SInt16 MSInt16;
  5347. typedef ParallelMath::UInt15 MUInt15;
  5348. typedef ParallelMath::UInt16 MUInt16;
  5349. typedef ParallelMath::SInt32 MSInt32;
  5350. static void Init(MFloat& error)
  5351. {
  5352. error = ParallelMath::MakeFloat(FLT_MAX);
  5353. }
  5354. static void QuantizeTo6Bits(MUInt15& v)
  5355. {
  5356. MUInt15 reduced = ParallelMath::LosslessCast<MUInt15>::Cast(ParallelMath::RightShift(ParallelMath::CompactMultiply(v, ParallelMath::MakeUInt15(253)) + ParallelMath::MakeUInt16(512), 10));
  5357. v = (reduced << 2) | ParallelMath::RightShift(reduced, 4);
  5358. }
  5359. static void QuantizeTo5Bits(MUInt15& v)
  5360. {
  5361. MUInt15 reduced = ParallelMath::LosslessCast<MUInt15>::Cast(ParallelMath::RightShift(ParallelMath::CompactMultiply(v, ParallelMath::MakeUInt15(249)) + ParallelMath::MakeUInt16(1024), 11));
  5362. v = (reduced << 3) | ParallelMath::RightShift(reduced, 2);
  5363. }
  5364. static void QuantizeTo565(MUInt15 endPoint[3])
  5365. {
  5366. QuantizeTo5Bits(endPoint[0]);
  5367. QuantizeTo6Bits(endPoint[1]);
  5368. QuantizeTo5Bits(endPoint[2]);
  5369. }
  5370. static MFloat ParanoidFactorForSpan(const MSInt16& span)
  5371. {
  5372. return ParallelMath::Abs(ParallelMath::ToFloat(span)) * 0.03f;
  5373. }
  5374. static MFloat ParanoidDiff(const MUInt15& a, const MUInt15& b, const MFloat& d)
  5375. {
  5376. MFloat absDiff = ParallelMath::Abs(ParallelMath::ToFloat(ParallelMath::LosslessCast<MSInt16>::Cast(a) - ParallelMath::LosslessCast<MSInt16>::Cast(b)));
  5377. absDiff = absDiff + d;
  5378. return absDiff * absDiff;
  5379. }
  5380. static void TestSingleColor(uint32_t flags, const MUInt15 pixels[16][4], const MFloat floatPixels[16][4], int range, const float* channelWeights,
  5381. MFloat &bestError, MUInt15 bestEndpoints[2][3], MUInt15 bestIndexes[16], MUInt15 &bestRange, const ParallelMath::RoundTowardNearestForScope *rtn)
  5382. {
  5383. float channelWeightsSq[3];
  5384. for (int ch = 0; ch < 3; ch++)
  5385. channelWeightsSq[ch] = channelWeights[ch] * channelWeights[ch];
  5386. MUInt15 totals[3] = { ParallelMath::MakeUInt15(0), ParallelMath::MakeUInt15(0), ParallelMath::MakeUInt15(0) };
  5387. for (int px = 0; px < 16; px++)
  5388. {
  5389. for (int ch = 0; ch < 3; ch++)
  5390. totals[ch] = totals[ch] + pixels[px][ch];
  5391. }
  5392. MUInt15 average[3];
  5393. for (int ch = 0; ch < 3; ch++)
  5394. average[ch] = ParallelMath::RightShift(totals[ch] + ParallelMath::MakeUInt15(8), 4);
  5395. const S3TCSingleColorTables::SingleColorTableEntry* rbTable = NULL;
  5396. const S3TCSingleColorTables::SingleColorTableEntry* gTable = NULL;
  5397. if (flags & cvtt::Flags::S3TC_Paranoid)
  5398. {
  5399. if (range == 4)
  5400. {
  5401. rbTable = S3TCSingleColorTables::g_singleColor5_3_p;
  5402. gTable = S3TCSingleColorTables::g_singleColor6_3_p;
  5403. }
  5404. else
  5405. {
  5406. assert(range == 3);
  5407. rbTable = S3TCSingleColorTables::g_singleColor5_2_p;
  5408. gTable = S3TCSingleColorTables::g_singleColor6_2_p;
  5409. }
  5410. }
  5411. else
  5412. {
  5413. if (range == 4)
  5414. {
  5415. rbTable = S3TCSingleColorTables::g_singleColor5_3;
  5416. gTable = S3TCSingleColorTables::g_singleColor6_3;
  5417. }
  5418. else
  5419. {
  5420. assert(range == 3);
  5421. rbTable = S3TCSingleColorTables::g_singleColor5_2;
  5422. gTable = S3TCSingleColorTables::g_singleColor6_2;
  5423. }
  5424. }
  5425. MUInt15 interpolated[3];
  5426. MUInt15 eps[2][3];
  5427. MSInt16 spans[3];
  5428. for (int i = 0; i < ParallelMath::ParallelSize; i++)
  5429. {
  5430. for (int ch = 0; ch < 3; ch++)
  5431. {
  5432. uint16_t avg = ParallelMath::Extract(average[ch], i);
  5433. const S3TCSingleColorTables::SingleColorTableEntry& tableEntry = ((ch == 1) ? gTable[avg] : rbTable[avg]);
  5434. ParallelMath::PutUInt15(eps[0][ch], i, tableEntry.m_min);
  5435. ParallelMath::PutUInt15(eps[1][ch], i, tableEntry.m_max);
  5436. ParallelMath::PutUInt15(interpolated[ch], i, tableEntry.m_actualColor);
  5437. ParallelMath::PutSInt16(spans[ch], i, tableEntry.m_span);
  5438. }
  5439. }
  5440. MFloat error = ParallelMath::MakeFloatZero();
  5441. if (flags & cvtt::Flags::S3TC_Paranoid)
  5442. {
  5443. MFloat spanParanoidFactors[3];
  5444. for (int ch = 0; ch < 3; ch++)
  5445. spanParanoidFactors[ch] = ParanoidFactorForSpan(spans[ch]);
  5446. for (int px = 0; px < 16; px++)
  5447. {
  5448. for (int ch = 0; ch < 3; ch++)
  5449. error = error + ParanoidDiff(interpolated[ch], pixels[px][ch], spanParanoidFactors[ch]) * channelWeightsSq[ch];
  5450. }
  5451. }
  5452. else
  5453. {
  5454. for (int px = 0; px < 16; px++)
  5455. {
  5456. for (int ch = 0; ch < 3; ch++)
  5457. error = error + ParallelMath::ToFloat(ParallelMath::SqDiffUInt8(interpolated[ch], pixels[px][ch])) * channelWeightsSq[ch];
  5458. }
  5459. }
  5460. ParallelMath::FloatCompFlag better = ParallelMath::Less(error, bestError);
  5461. ParallelMath::Int16CompFlag better16 = ParallelMath::FloatFlagToInt16(better);
  5462. if (ParallelMath::AnySet(better16))
  5463. {
  5464. bestError = ParallelMath::Min(bestError, error);
  5465. for (int epi = 0; epi < 2; epi++)
  5466. for (int ch = 0; ch < 3; ch++)
  5467. ParallelMath::ConditionalSet(bestEndpoints[epi][ch], better16, eps[epi][ch]);
  5468. MUInt15 vindexes = ParallelMath::MakeUInt15(1);
  5469. for (int px = 0; px < 16; px++)
  5470. ParallelMath::ConditionalSet(bestIndexes[px], better16, vindexes);
  5471. ParallelMath::ConditionalSet(bestRange, better16, ParallelMath::MakeUInt15(range));
  5472. }
  5473. }
  5474. static void TestEndpoints(uint32_t flags, const MUInt15 pixels[16][4], const MFloat floatPixels[16][4], const MFloat preWeightedPixels[16][4], const MUInt15 unquantizedEndPoints[2][3], int range, const float* channelWeights,
  5475. MFloat &bestError, MUInt15 bestEndpoints[2][3], MUInt15 bestIndexes[16], MUInt15 &bestRange, EndpointRefiner<3> *refiner, const ParallelMath::RoundTowardNearestForScope *rtn)
  5476. {
  5477. float channelWeightsSq[3];
  5478. for (int ch = 0; ch < 3; ch++)
  5479. channelWeightsSq[ch] = channelWeights[ch] * channelWeights[ch];
  5480. MUInt15 endPoints[2][3];
  5481. for (int ep = 0; ep < 2; ep++)
  5482. for (int ch = 0; ch < 3; ch++)
  5483. endPoints[ep][ch] = unquantizedEndPoints[ep][ch];
  5484. QuantizeTo565(endPoints[0]);
  5485. QuantizeTo565(endPoints[1]);
  5486. IndexSelector<3> selector;
  5487. selector.Init<false>(channelWeights, endPoints, range);
  5488. MUInt15 indexes[16];
  5489. MFloat paranoidFactors[3];
  5490. for (int ch = 0; ch < 3; ch++)
  5491. paranoidFactors[ch] = ParanoidFactorForSpan(ParallelMath::LosslessCast<MSInt16>::Cast(endPoints[0][ch]) - ParallelMath::LosslessCast<MSInt16>::Cast(endPoints[1][ch]));
  5492. MFloat error = ParallelMath::MakeFloatZero();
  5493. AggregatedError<3> aggError;
  5494. for (int px = 0; px < 16; px++)
  5495. {
  5496. MUInt15 index = selector.SelectIndexLDR(floatPixels[px], rtn);
  5497. indexes[px] = index;
  5498. if (refiner)
  5499. refiner->ContributeUnweightedPW(preWeightedPixels[px], index);
  5500. MUInt15 reconstructed[3];
  5501. selector.ReconstructLDRPrecise(index, reconstructed);
  5502. if (flags & Flags::S3TC_Paranoid)
  5503. {
  5504. for (int ch = 0; ch < 3; ch++)
  5505. error = error + ParanoidDiff(reconstructed[ch], pixels[px][ch], paranoidFactors[ch]) * channelWeightsSq[ch];
  5506. }
  5507. else
  5508. BCCommon::ComputeErrorLDR<3>(flags, reconstructed, pixels[px], aggError);
  5509. }
  5510. if (!(flags & Flags::S3TC_Paranoid))
  5511. error = aggError.Finalize(flags, channelWeightsSq);
  5512. ParallelMath::FloatCompFlag better = ParallelMath::Less(error, bestError);
  5513. if (ParallelMath::AnySet(better))
  5514. {
  5515. ParallelMath::Int16CompFlag betterInt16 = ParallelMath::FloatFlagToInt16(better);
  5516. ParallelMath::ConditionalSet(bestError, better, error);
  5517. for (int ep = 0; ep < 2; ep++)
  5518. for (int ch = 0; ch < 3; ch++)
  5519. ParallelMath::ConditionalSet(bestEndpoints[ep][ch], betterInt16, endPoints[ep][ch]);
  5520. for (int px = 0; px < 16; px++)
  5521. ParallelMath::ConditionalSet(bestIndexes[px], betterInt16, indexes[px]);
  5522. ParallelMath::ConditionalSet(bestRange, betterInt16, ParallelMath::MakeUInt15(static_cast<uint16_t>(range)));
  5523. }
  5524. }
  5525. static void TestCounts(uint32_t flags, const int *counts, int nCounts, const MUInt15 &numElements, const MUInt15 pixels[16][4], const MFloat floatPixels[16][4], const MFloat preWeightedPixels[16][4], bool alphaTest,
  5526. const MFloat floatSortedInputs[16][4], const MFloat preWeightedFloatSortedInputs[16][4], const float *channelWeights, MFloat &bestError, MUInt15 bestEndpoints[2][3], MUInt15 bestIndexes[16], MUInt15 &bestRange,
  5527. const ParallelMath::RoundTowardNearestForScope* rtn)
  5528. {
  5529. UNREFERENCED_PARAMETER(alphaTest);
  5530. UNREFERENCED_PARAMETER(flags);
  5531. EndpointRefiner<3> refiner;
  5532. refiner.Init(nCounts, channelWeights);
  5533. bool escape = false;
  5534. int e = 0;
  5535. for (int i = 0; i < nCounts; i++)
  5536. {
  5537. for (int n = 0; n < counts[i]; n++)
  5538. {
  5539. ParallelMath::Int16CompFlag valid = ParallelMath::Less(ParallelMath::MakeUInt15(static_cast<uint16_t>(n)), numElements);
  5540. if (!ParallelMath::AnySet(valid))
  5541. {
  5542. escape = true;
  5543. break;
  5544. }
  5545. if (ParallelMath::AllSet(valid))
  5546. refiner.ContributeUnweightedPW(preWeightedFloatSortedInputs[e++], ParallelMath::MakeUInt15(static_cast<uint16_t>(i)));
  5547. else
  5548. {
  5549. MFloat weight = ParallelMath::Select(ParallelMath::Int16FlagToFloat(valid), ParallelMath::MakeFloat(1.0f), ParallelMath::MakeFloat(0.0f));
  5550. refiner.ContributePW(preWeightedFloatSortedInputs[e++], ParallelMath::MakeUInt15(static_cast<uint16_t>(i)), weight);
  5551. }
  5552. }
  5553. if (escape)
  5554. break;
  5555. }
  5556. MUInt15 endPoints[2][3];
  5557. refiner.GetRefinedEndpointsLDR(endPoints, rtn);
  5558. TestEndpoints(flags, pixels, floatPixels, preWeightedPixels, endPoints, nCounts, channelWeights, bestError, bestEndpoints, bestIndexes, bestRange, NULL, rtn);
  5559. }
  5560. static void PackExplicitAlpha(uint32_t flags, const PixelBlockU8* inputs, int inputChannel, uint8_t* packedBlocks, size_t packedBlockStride)
  5561. {
  5562. UNREFERENCED_PARAMETER(flags);
  5563. ParallelMath::RoundTowardNearestForScope rtn;
  5564. float weights[1] = { 1.0f };
  5565. MUInt15 pixels[16];
  5566. MFloat floatPixels[16];
  5567. for (int px = 0; px < 16; px++)
  5568. {
  5569. ParallelMath::ConvertLDRInputs(inputs, px, inputChannel, pixels[px]);
  5570. floatPixels[px] = ParallelMath::ToFloat(pixels[px]);
  5571. }
  5572. MUInt15 ep[2][1] = { { ParallelMath::MakeUInt15(0) },{ ParallelMath::MakeUInt15(255) } };
  5573. IndexSelector<1> selector;
  5574. selector.Init<false>(weights, ep, 16);
  5575. MUInt15 indexes[16];
  5576. for (int px = 0; px < 16; px++)
  5577. indexes[px] = selector.SelectIndexLDR(&floatPixels[px], &rtn);
  5578. for (int block = 0; block < ParallelMath::ParallelSize; block++)
  5579. {
  5580. for (int px = 0; px < 16; px += 8)
  5581. {
  5582. int index0 = ParallelMath::Extract(indexes[px], block);
  5583. int index1 = ParallelMath::Extract(indexes[px], block);
  5584. packedBlocks[px / 2] = static_cast<uint8_t>(index0 | (index1 << 4));
  5585. }
  5586. packedBlocks += packedBlockStride;
  5587. }
  5588. }
  5589. static void PackInterpolatedAlpha(uint32_t flags, const PixelBlockU8* inputs, int inputChannel, uint8_t* packedBlocks, size_t packedBlockStride, bool isSigned, int maxTweakRounds, int numRefineRounds)
  5590. {
  5591. if (maxTweakRounds < 1)
  5592. maxTweakRounds = 1;
  5593. if (numRefineRounds < 1)
  5594. numRefineRounds = 1;
  5595. ParallelMath::RoundTowardNearestForScope rtn;
  5596. float oneWeight[1] = { 1.0f };
  5597. MUInt15 pixels[16];
  5598. MFloat floatPixels[16];
  5599. MUInt15 highTerminal = isSigned ? ParallelMath::MakeUInt15(254) : ParallelMath::MakeUInt15(255);
  5600. MUInt15 highTerminalMinusOne = highTerminal - ParallelMath::MakeUInt15(1);
  5601. for (int px = 0; px < 16; px++)
  5602. {
  5603. ParallelMath::ConvertLDRInputs(inputs, px, inputChannel, pixels[px]);
  5604. if (isSigned)
  5605. pixels[px] = ParallelMath::Min(pixels[px], highTerminal);
  5606. floatPixels[px] = ParallelMath::ToFloat(pixels[px]);
  5607. }
  5608. MUInt15 sortedPixels[16];
  5609. for (int px = 0; px < 16; px++)
  5610. sortedPixels[px] = pixels[px];
  5611. for (int sortEnd = 15; sortEnd > 0; sortEnd--)
  5612. {
  5613. for (int sortOffset = 0; sortOffset < sortEnd; sortOffset++)
  5614. {
  5615. MUInt15 a = sortedPixels[sortOffset];
  5616. MUInt15 b = sortedPixels[sortOffset + 1];
  5617. sortedPixels[sortOffset] = ParallelMath::Min(a, b);
  5618. sortedPixels[sortOffset + 1] = ParallelMath::Max(a, b);
  5619. }
  5620. }
  5621. MUInt15 zero = ParallelMath::MakeUInt15(0);
  5622. MUInt15 one = ParallelMath::MakeUInt15(1);
  5623. MUInt15 bestIsFullRange = zero;
  5624. MFloat bestError = ParallelMath::MakeFloat(FLT_MAX);
  5625. MUInt15 bestEP[2] = { zero, zero };
  5626. MUInt15 bestIndexes[16] = {
  5627. zero, zero, zero, zero,
  5628. zero, zero, zero, zero,
  5629. zero, zero, zero, zero,
  5630. zero, zero, zero, zero
  5631. };
  5632. // Full-precision
  5633. {
  5634. MUInt15 minEP = sortedPixels[0];
  5635. MUInt15 maxEP = sortedPixels[15];
  5636. MFloat base[1] = { ParallelMath::ToFloat(minEP) };
  5637. MFloat offset[1] = { ParallelMath::ToFloat(maxEP - minEP) };
  5638. UnfinishedEndpoints<1> ufep = UnfinishedEndpoints<1>(base, offset);
  5639. int numTweakRounds = BCCommon::TweakRoundsForRange(8);
  5640. if (numTweakRounds > maxTweakRounds)
  5641. numTweakRounds = maxTweakRounds;
  5642. for (int tweak = 0; tweak < numTweakRounds; tweak++)
  5643. {
  5644. MUInt15 ep[2][1];
  5645. ufep.FinishLDR(tweak, 8, ep[0], ep[1]);
  5646. for (int refinePass = 0; refinePass < numRefineRounds; refinePass++)
  5647. {
  5648. EndpointRefiner<1> refiner;
  5649. refiner.Init(8, oneWeight);
  5650. if (isSigned)
  5651. for (int epi = 0; epi < 2; epi++)
  5652. ep[epi][0] = ParallelMath::Min(ep[epi][0], highTerminal);
  5653. IndexSelector<1> indexSelector;
  5654. indexSelector.Init<false>(oneWeight, ep, 8);
  5655. MUInt15 indexes[16];
  5656. AggregatedError<1> aggError;
  5657. for (int px = 0; px < 16; px++)
  5658. {
  5659. MUInt15 index = indexSelector.SelectIndexLDR(&floatPixels[px], &rtn);
  5660. MUInt15 reconstructedPixel;
  5661. indexSelector.ReconstructLDRPrecise(index, &reconstructedPixel);
  5662. BCCommon::ComputeErrorLDR<1>(flags, &reconstructedPixel, &pixels[px], aggError);
  5663. if (refinePass != numRefineRounds - 1)
  5664. refiner.ContributeUnweightedPW(&floatPixels[px], index);
  5665. indexes[px] = index;
  5666. }
  5667. MFloat error = aggError.Finalize(flags | Flags::Uniform, oneWeight);
  5668. ParallelMath::FloatCompFlag errorBetter = ParallelMath::Less(error, bestError);
  5669. ParallelMath::Int16CompFlag errorBetter16 = ParallelMath::FloatFlagToInt16(errorBetter);
  5670. if (ParallelMath::AnySet(errorBetter16))
  5671. {
  5672. bestError = ParallelMath::Min(error, bestError);
  5673. ParallelMath::ConditionalSet(bestIsFullRange, errorBetter16, one);
  5674. for (int px = 0; px < 16; px++)
  5675. ParallelMath::ConditionalSet(bestIndexes[px], errorBetter16, indexes[px]);
  5676. for (int epi = 0; epi < 2; epi++)
  5677. ParallelMath::ConditionalSet(bestEP[epi], errorBetter16, ep[epi][0]);
  5678. }
  5679. if (refinePass != numRefineRounds - 1)
  5680. refiner.GetRefinedEndpointsLDR(ep, &rtn);
  5681. }
  5682. }
  5683. }
  5684. // Reduced precision with special endpoints
  5685. {
  5686. MUInt15 bestHeuristicMin = sortedPixels[0];
  5687. MUInt15 bestHeuristicMax = sortedPixels[15];
  5688. ParallelMath::Int16CompFlag canTryClipping;
  5689. // In reduced precision, we want try putting endpoints at the reserved indexes at the ends.
  5690. // The heuristic we use is to assign indexes to the end as long as they aren't off by more than half of the index range.
  5691. // This will usually not find anything, but it's cheap to check.
  5692. {
  5693. MUInt15 largestPossibleRange = bestHeuristicMax - bestHeuristicMin; // Max: 255
  5694. MUInt15 lowestPossibleClearance = ParallelMath::Min(bestHeuristicMin, static_cast<MUInt15>(highTerminal - bestHeuristicMax));
  5695. MUInt15 lowestPossibleClearanceTimes10 = (lowestPossibleClearance << 2) + (lowestPossibleClearance << 4);
  5696. canTryClipping = ParallelMath::LessOrEqual(lowestPossibleClearanceTimes10, largestPossibleRange);
  5697. }
  5698. if (ParallelMath::AnySet(canTryClipping))
  5699. {
  5700. MUInt15 lowClearances[16];
  5701. MUInt15 highClearances[16];
  5702. MUInt15 bestSkipCount = ParallelMath::MakeUInt15(0);
  5703. lowClearances[0] = highClearances[0] = ParallelMath::MakeUInt15(0);
  5704. for (int px = 1; px < 16; px++)
  5705. {
  5706. lowClearances[px] = sortedPixels[px - 1];
  5707. highClearances[px] = highTerminal - sortedPixels[16 - px];
  5708. }
  5709. for (uint16_t firstIndex = 0; firstIndex < 16; firstIndex++)
  5710. {
  5711. uint16_t numSkippedLow = firstIndex;
  5712. MUInt15 lowClearance = lowClearances[firstIndex];
  5713. for (uint16_t lastIndex = firstIndex; lastIndex < 16; lastIndex++)
  5714. {
  5715. uint16_t numSkippedHigh = 15 - lastIndex;
  5716. uint16_t numSkipped = numSkippedLow + numSkippedHigh;
  5717. MUInt15 numSkippedV = ParallelMath::MakeUInt15(numSkipped);
  5718. ParallelMath::Int16CompFlag areMoreSkipped = ParallelMath::Less(bestSkipCount, numSkippedV);
  5719. if (!ParallelMath::AnySet(areMoreSkipped))
  5720. continue;
  5721. MUInt15 clearance = ParallelMath::Max(highClearances[numSkippedHigh], lowClearance);
  5722. MUInt15 clearanceTimes10 = (clearance << 2) + (clearance << 4);
  5723. MUInt15 range = sortedPixels[lastIndex] - sortedPixels[firstIndex];
  5724. ParallelMath::Int16CompFlag isBetter = (areMoreSkipped & ParallelMath::LessOrEqual(clearanceTimes10, range));
  5725. ParallelMath::ConditionalSet(bestHeuristicMin, isBetter, sortedPixels[firstIndex]);
  5726. ParallelMath::ConditionalSet(bestHeuristicMax, isBetter, sortedPixels[lastIndex]);
  5727. }
  5728. }
  5729. }
  5730. MUInt15 bestSimpleMin = one;
  5731. MUInt15 bestSimpleMax = highTerminalMinusOne;
  5732. for (int px = 0; px < 16; px++)
  5733. {
  5734. ParallelMath::ConditionalSet(bestSimpleMin, ParallelMath::Less(zero, sortedPixels[15 - px]), sortedPixels[15 - px]);
  5735. ParallelMath::ConditionalSet(bestSimpleMax, ParallelMath::Less(sortedPixels[px], highTerminal), sortedPixels[px]);
  5736. }
  5737. MUInt15 minEPs[2] = { bestSimpleMin, bestHeuristicMin };
  5738. MUInt15 maxEPs[2] = { bestSimpleMax, bestHeuristicMax };
  5739. int minEPRange = 2;
  5740. if (ParallelMath::AllSet(ParallelMath::Equal(minEPs[0], minEPs[1])))
  5741. minEPRange = 1;
  5742. int maxEPRange = 2;
  5743. if (ParallelMath::AllSet(ParallelMath::Equal(maxEPs[0], maxEPs[1])))
  5744. maxEPRange = 1;
  5745. for (int minEPIndex = 0; minEPIndex < minEPRange; minEPIndex++)
  5746. {
  5747. for (int maxEPIndex = 0; maxEPIndex < maxEPRange; maxEPIndex++)
  5748. {
  5749. MFloat base[1] = { ParallelMath::ToFloat(minEPs[minEPIndex]) };
  5750. MFloat offset[1] = { ParallelMath::ToFloat(maxEPs[maxEPIndex] - minEPs[minEPIndex]) };
  5751. UnfinishedEndpoints<1> ufep = UnfinishedEndpoints<1>(base, offset);
  5752. int numTweakRounds = BCCommon::TweakRoundsForRange(6);
  5753. if (numTweakRounds > maxTweakRounds)
  5754. numTweakRounds = maxTweakRounds;
  5755. for (int tweak = 0; tweak < numTweakRounds; tweak++)
  5756. {
  5757. MUInt15 ep[2][1];
  5758. ufep.FinishLDR(tweak, 8, ep[0], ep[1]);
  5759. for (int refinePass = 0; refinePass < numRefineRounds; refinePass++)
  5760. {
  5761. EndpointRefiner<1> refiner;
  5762. refiner.Init(6, oneWeight);
  5763. if (isSigned)
  5764. for (int epi = 0; epi < 2; epi++)
  5765. ep[epi][0] = ParallelMath::Min(ep[epi][0], highTerminal);
  5766. IndexSelector<1> indexSelector;
  5767. indexSelector.Init<false>(oneWeight, ep, 6);
  5768. MUInt15 indexes[16];
  5769. MFloat error = ParallelMath::MakeFloatZero();
  5770. for (int px = 0; px < 16; px++)
  5771. {
  5772. MUInt15 selectedIndex = indexSelector.SelectIndexLDR(&floatPixels[px], &rtn);
  5773. MUInt15 reconstructedPixel;
  5774. indexSelector.ReconstructLDRPrecise(selectedIndex, &reconstructedPixel);
  5775. MFloat zeroError = BCCommon::ComputeErrorLDRSimple<1>(flags | Flags::Uniform, &zero, &pixels[px], 1, oneWeight);
  5776. MFloat highTerminalError = BCCommon::ComputeErrorLDRSimple<1>(flags | Flags::Uniform, &highTerminal, &pixels[px], 1, oneWeight);
  5777. MFloat selectedIndexError = BCCommon::ComputeErrorLDRSimple<1>(flags | Flags::Uniform, &reconstructedPixel, &pixels[px], 1, oneWeight);
  5778. MFloat bestPixelError = zeroError;
  5779. MUInt15 index = ParallelMath::MakeUInt15(6);
  5780. ParallelMath::ConditionalSet(index, ParallelMath::FloatFlagToInt16(ParallelMath::Less(highTerminalError, bestPixelError)), ParallelMath::MakeUInt15(7));
  5781. bestPixelError = ParallelMath::Min(bestPixelError, highTerminalError);
  5782. ParallelMath::FloatCompFlag selectedIndexBetter = ParallelMath::Less(selectedIndexError, bestPixelError);
  5783. if (ParallelMath::AllSet(selectedIndexBetter))
  5784. {
  5785. if (refinePass != numRefineRounds - 1)
  5786. refiner.ContributeUnweightedPW(&floatPixels[px], selectedIndex);
  5787. }
  5788. else
  5789. {
  5790. MFloat refineWeight = ParallelMath::Select(selectedIndexBetter, ParallelMath::MakeFloat(1.0f), ParallelMath::MakeFloatZero());
  5791. if (refinePass != numRefineRounds - 1)
  5792. refiner.ContributePW(&floatPixels[px], selectedIndex, refineWeight);
  5793. }
  5794. ParallelMath::ConditionalSet(index, ParallelMath::FloatFlagToInt16(selectedIndexBetter), selectedIndex);
  5795. bestPixelError = ParallelMath::Min(bestPixelError, selectedIndexError);
  5796. error = error + bestPixelError;
  5797. indexes[px] = index;
  5798. }
  5799. ParallelMath::FloatCompFlag errorBetter = ParallelMath::Less(error, bestError);
  5800. ParallelMath::Int16CompFlag errorBetter16 = ParallelMath::FloatFlagToInt16(errorBetter);
  5801. if (ParallelMath::AnySet(errorBetter16))
  5802. {
  5803. bestError = ParallelMath::Min(error, bestError);
  5804. ParallelMath::ConditionalSet(bestIsFullRange, errorBetter16, zero);
  5805. for (int px = 0; px < 16; px++)
  5806. ParallelMath::ConditionalSet(bestIndexes[px], errorBetter16, indexes[px]);
  5807. for (int epi = 0; epi < 2; epi++)
  5808. ParallelMath::ConditionalSet(bestEP[epi], errorBetter16, ep[epi][0]);
  5809. }
  5810. if (refinePass != numRefineRounds - 1)
  5811. refiner.GetRefinedEndpointsLDR(ep, &rtn);
  5812. }
  5813. }
  5814. }
  5815. }
  5816. }
  5817. for (int block = 0; block < ParallelMath::ParallelSize; block++)
  5818. {
  5819. int ep0 = ParallelMath::Extract(bestEP[0], block);
  5820. int ep1 = ParallelMath::Extract(bestEP[1], block);
  5821. int isFullRange = ParallelMath::Extract(bestIsFullRange, block);
  5822. if (isSigned)
  5823. {
  5824. ep0 -= 127;
  5825. ep1 -= 127;
  5826. assert(ep0 >= -127 && ep0 <= 127);
  5827. assert(ep1 >= -127 && ep1 <= 127);
  5828. }
  5829. bool swapEndpoints = (isFullRange != 0) != (ep0 > ep1);
  5830. if (swapEndpoints)
  5831. std::swap(ep0, ep1);
  5832. uint16_t dumpBits = 0;
  5833. int dumpBitsOffset = 0;
  5834. int dumpByteOffset = 2;
  5835. packedBlocks[0] = static_cast<uint8_t>(ep0 & 0xff);
  5836. packedBlocks[1] = static_cast<uint8_t>(ep1 & 0xff);
  5837. int maxValue = (isFullRange != 0) ? 7 : 5;
  5838. for (int px = 0; px < 16; px++)
  5839. {
  5840. int index = ParallelMath::Extract(bestIndexes[px], block);
  5841. if (swapEndpoints && index <= maxValue)
  5842. index = maxValue - index;
  5843. if (index != 0)
  5844. {
  5845. if (index == maxValue)
  5846. index = 1;
  5847. else if (index < maxValue)
  5848. index++;
  5849. }
  5850. assert(index >= 0 && index < 8);
  5851. dumpBits |= static_cast<uint16_t>(index << dumpBitsOffset);
  5852. dumpBitsOffset += 3;
  5853. if (dumpBitsOffset >= 8)
  5854. {
  5855. assert(dumpByteOffset < 8);
  5856. packedBlocks[dumpByteOffset] = static_cast<uint8_t>(dumpBits & 0xff);
  5857. dumpBits >>= 8;
  5858. dumpBitsOffset -= 8;
  5859. dumpByteOffset++;
  5860. }
  5861. }
  5862. assert(dumpBitsOffset == 0);
  5863. assert(dumpByteOffset == 8);
  5864. packedBlocks += packedBlockStride;
  5865. }
  5866. }
  5867. static void PackRGB(uint32_t flags, const PixelBlockU8* inputs, uint8_t* packedBlocks, size_t packedBlockStride, const float channelWeights[4], bool alphaTest, float alphaThreshold, bool exhaustive, int maxTweakRounds, int numRefineRounds)
  5868. {
  5869. ParallelMath::RoundTowardNearestForScope rtn;
  5870. if (numRefineRounds < 1)
  5871. numRefineRounds = 1;
  5872. if (maxTweakRounds < 1)
  5873. maxTweakRounds = 1;
  5874. EndpointSelector<3, 8> endpointSelector;
  5875. MUInt15 pixels[16][4];
  5876. MFloat floatPixels[16][4];
  5877. MFloat preWeightedPixels[16][4];
  5878. for (int px = 0; px < 16; px++)
  5879. {
  5880. for (int ch = 0; ch < 4; ch++)
  5881. ParallelMath::ConvertLDRInputs(inputs, px, ch, pixels[px][ch]);
  5882. }
  5883. for (int px = 0; px < 16; px++)
  5884. {
  5885. for (int ch = 0; ch < 4; ch++)
  5886. floatPixels[px][ch] = ParallelMath::ToFloat(pixels[px][ch]);
  5887. }
  5888. if (alphaTest)
  5889. {
  5890. MUInt15 threshold = ParallelMath::MakeUInt15(static_cast<uint16_t>(floor(alphaThreshold * 255.0f + 0.5f)));
  5891. for (int px = 0; px < 16; px++)
  5892. {
  5893. ParallelMath::Int16CompFlag belowThreshold = ParallelMath::Less(pixels[px][3], threshold);
  5894. pixels[px][3] = ParallelMath::Select(belowThreshold, ParallelMath::MakeUInt15(0), ParallelMath::MakeUInt15(255));
  5895. }
  5896. }
  5897. BCCommon::PreWeightPixelsLDR<4>(preWeightedPixels, pixels, channelWeights);
  5898. MUInt15 minAlpha = ParallelMath::MakeUInt15(255);
  5899. for (int px = 0; px < 16; px++)
  5900. minAlpha = ParallelMath::Min(minAlpha, pixels[px][3]);
  5901. MFloat pixelWeights[16];
  5902. for (int px = 0; px < 16; px++)
  5903. {
  5904. pixelWeights[px] = ParallelMath::MakeFloat(1.0f);
  5905. if (alphaTest)
  5906. {
  5907. ParallelMath::Int16CompFlag isTransparent = ParallelMath::Less(pixels[px][3], ParallelMath::MakeUInt15(255));
  5908. ParallelMath::ConditionalSet(pixelWeights[px], ParallelMath::Int16FlagToFloat(isTransparent), ParallelMath::MakeFloatZero());
  5909. }
  5910. }
  5911. for (int pass = 0; pass < NumEndpointSelectorPasses; pass++)
  5912. {
  5913. for (int px = 0; px < 16; px++)
  5914. endpointSelector.ContributePass(preWeightedPixels[px], pass, pixelWeights[px]);
  5915. endpointSelector.FinishPass(pass);
  5916. }
  5917. UnfinishedEndpoints<3> ufep = endpointSelector.GetEndpoints(channelWeights);
  5918. MUInt15 bestEndpoints[2][3];
  5919. MUInt15 bestIndexes[16];
  5920. MUInt15 bestRange = ParallelMath::MakeUInt15(0);
  5921. MFloat bestError = ParallelMath::MakeFloat(FLT_MAX);
  5922. for (int px = 0; px < 16; px++)
  5923. bestIndexes[px] = ParallelMath::MakeUInt15(0);
  5924. for (int ep = 0; ep < 2; ep++)
  5925. for (int ch = 0; ch < 3; ch++)
  5926. bestEndpoints[ep][ch] = ParallelMath::MakeUInt15(0);
  5927. if (exhaustive)
  5928. {
  5929. MSInt16 sortBins[16];
  5930. {
  5931. // Compute an 11-bit index, change it to signed, stuff it in the high bits of the sort bins,
  5932. // and pack the original indexes into the low bits.
  5933. MUInt15 sortEP[2][3];
  5934. ufep.FinishLDR(0, 11, sortEP[0], sortEP[1]);
  5935. IndexSelector<3> sortSelector;
  5936. sortSelector.Init<false>(channelWeights, sortEP, 1 << 11);
  5937. for (int16_t px = 0; px < 16; px++)
  5938. {
  5939. MSInt16 sortBin = ParallelMath::LosslessCast<MSInt16>::Cast(sortSelector.SelectIndexLDR(floatPixels[px], &rtn) << 4);
  5940. if (alphaTest)
  5941. {
  5942. ParallelMath::Int16CompFlag isTransparent = ParallelMath::Less(pixels[px][3], ParallelMath::MakeUInt15(255));
  5943. ParallelMath::ConditionalSet(sortBin, isTransparent, ParallelMath::MakeSInt16(-16)); // 0xfff0
  5944. }
  5945. sortBin = sortBin + ParallelMath::MakeSInt16(px);
  5946. sortBins[px] = sortBin;
  5947. }
  5948. }
  5949. // Sort bins
  5950. for (int sortEnd = 1; sortEnd < 16; sortEnd++)
  5951. {
  5952. for (int sortLoc = sortEnd; sortLoc > 0; sortLoc--)
  5953. {
  5954. MSInt16 a = sortBins[sortLoc];
  5955. MSInt16 b = sortBins[sortLoc - 1];
  5956. sortBins[sortLoc] = ParallelMath::Max(a, b);
  5957. sortBins[sortLoc - 1] = ParallelMath::Min(a, b);
  5958. }
  5959. }
  5960. MUInt15 firstElement = ParallelMath::MakeUInt15(0);
  5961. for (uint16_t e = 0; e < 16; e++)
  5962. {
  5963. ParallelMath::Int16CompFlag isInvalid = ParallelMath::Less(sortBins[e], ParallelMath::MakeSInt16(0));
  5964. ParallelMath::ConditionalSet(firstElement, isInvalid, ParallelMath::MakeUInt15(e + 1));
  5965. if (!ParallelMath::AnySet(isInvalid))
  5966. break;
  5967. }
  5968. MUInt15 numElements = ParallelMath::MakeUInt15(16) - firstElement;
  5969. MUInt15 sortedInputs[16][4];
  5970. MFloat floatSortedInputs[16][4];
  5971. MFloat pwFloatSortedInputs[16][4];
  5972. for (int e = 0; e < 16; e++)
  5973. {
  5974. for (int ch = 0; ch < 4; ch++)
  5975. sortedInputs[e][ch] = ParallelMath::MakeUInt15(0);
  5976. }
  5977. for (int block = 0; block < ParallelMath::ParallelSize; block++)
  5978. {
  5979. for (int e = ParallelMath::Extract(firstElement, block); e < 16; e++)
  5980. {
  5981. ParallelMath::ScalarUInt16 sortBin = ParallelMath::Extract(sortBins[e], block);
  5982. int originalIndex = (sortBin & 15);
  5983. for (int ch = 0; ch < 4; ch++)
  5984. ParallelMath::PutUInt15(sortedInputs[15 - e][ch], block, ParallelMath::Extract(pixels[originalIndex][ch], block));
  5985. }
  5986. }
  5987. for (int e = 0; e < 16; e++)
  5988. {
  5989. for (int ch = 0; ch < 4; ch++)
  5990. {
  5991. MFloat f = ParallelMath::ToFloat(sortedInputs[e][ch]);
  5992. floatSortedInputs[e][ch] = f;
  5993. pwFloatSortedInputs[e][ch] = f * channelWeights[ch];
  5994. }
  5995. }
  5996. for (int n0 = 0; n0 <= 15; n0++)
  5997. {
  5998. int remainingFor1 = 16 - n0;
  5999. if (remainingFor1 == 16)
  6000. remainingFor1 = 15;
  6001. for (int n1 = 0; n1 <= remainingFor1; n1++)
  6002. {
  6003. int remainingFor2 = 16 - n1 - n0;
  6004. if (remainingFor2 == 16)
  6005. remainingFor2 = 15;
  6006. for (int n2 = 0; n2 <= remainingFor2; n2++)
  6007. {
  6008. int n3 = 16 - n2 - n1 - n0;
  6009. if (n3 == 16)
  6010. continue;
  6011. int counts[4] = { n0, n1, n2, n3 };
  6012. TestCounts(flags, counts, 4, numElements, pixels, floatPixels, preWeightedPixels, alphaTest, floatSortedInputs, pwFloatSortedInputs, channelWeights, bestError, bestEndpoints, bestIndexes, bestRange, &rtn);
  6013. }
  6014. }
  6015. }
  6016. TestSingleColor(flags, pixels, floatPixels, 4, channelWeights, bestError, bestEndpoints, bestIndexes, bestRange, &rtn);
  6017. if (alphaTest)
  6018. {
  6019. for (int n0 = 0; n0 <= 15; n0++)
  6020. {
  6021. int remainingFor1 = 16 - n0;
  6022. if (remainingFor1 == 16)
  6023. remainingFor1 = 15;
  6024. for (int n1 = 0; n1 <= remainingFor1; n1++)
  6025. {
  6026. int n2 = 16 - n1 - n0;
  6027. if (n2 == 16)
  6028. continue;
  6029. int counts[3] = { n0, n1, n2 };
  6030. TestCounts(flags, counts, 3, numElements, pixels, floatPixels, preWeightedPixels, alphaTest, floatSortedInputs, pwFloatSortedInputs, channelWeights, bestError, bestEndpoints, bestIndexes, bestRange, &rtn);
  6031. }
  6032. }
  6033. TestSingleColor(flags, pixels, floatPixels, 3, channelWeights, bestError, bestEndpoints, bestIndexes, bestRange, &rtn);
  6034. }
  6035. }
  6036. else
  6037. {
  6038. int minRange = alphaTest ? 3 : 4;
  6039. for (int range = minRange; range <= 4; range++)
  6040. {
  6041. int tweakRounds = BCCommon::TweakRoundsForRange(range);
  6042. if (tweakRounds > maxTweakRounds)
  6043. tweakRounds = maxTweakRounds;
  6044. for (int tweak = 0; tweak < tweakRounds; tweak++)
  6045. {
  6046. MUInt15 endPoints[2][3];
  6047. ufep.FinishLDR(tweak, range, endPoints[0], endPoints[1]);
  6048. for (int refine = 0; refine < numRefineRounds; refine++)
  6049. {
  6050. EndpointRefiner<3> refiner;
  6051. refiner.Init(range, channelWeights);
  6052. TestEndpoints(flags, pixels, floatPixels, preWeightedPixels, endPoints, range, channelWeights, bestError, bestEndpoints, bestIndexes, bestRange, &refiner, &rtn);
  6053. if (refine != numRefineRounds - 1)
  6054. refiner.GetRefinedEndpointsLDR(endPoints, &rtn);
  6055. }
  6056. }
  6057. }
  6058. }
  6059. for (int block = 0; block < ParallelMath::ParallelSize; block++)
  6060. {
  6061. ParallelMath::ScalarUInt16 range = ParallelMath::Extract(bestRange, block);
  6062. assert(range == 3 || range == 4);
  6063. ParallelMath::ScalarUInt16 compressedEP[2];
  6064. for (int ep = 0; ep < 2; ep++)
  6065. {
  6066. ParallelMath::ScalarUInt16 endPoint[3];
  6067. for (int ch = 0; ch < 3; ch++)
  6068. endPoint[ch] = ParallelMath::Extract(bestEndpoints[ep][ch], block);
  6069. int compressed = (endPoint[0] & 0xf8) << 8;
  6070. compressed |= (endPoint[1] & 0xfc) << 3;
  6071. compressed |= (endPoint[2] & 0xf8) >> 3;
  6072. compressedEP[ep] = static_cast<ParallelMath::ScalarUInt16>(compressed);
  6073. }
  6074. int indexOrder[4];
  6075. if (range == 4)
  6076. {
  6077. if (compressedEP[0] == compressedEP[1])
  6078. {
  6079. indexOrder[0] = 0;
  6080. indexOrder[1] = 0;
  6081. indexOrder[2] = 0;
  6082. indexOrder[3] = 0;
  6083. }
  6084. else if (compressedEP[0] < compressedEP[1])
  6085. {
  6086. std::swap(compressedEP[0], compressedEP[1]);
  6087. indexOrder[0] = 1;
  6088. indexOrder[1] = 3;
  6089. indexOrder[2] = 2;
  6090. indexOrder[3] = 0;
  6091. }
  6092. else
  6093. {
  6094. indexOrder[0] = 0;
  6095. indexOrder[1] = 2;
  6096. indexOrder[2] = 3;
  6097. indexOrder[3] = 1;
  6098. }
  6099. }
  6100. else
  6101. {
  6102. assert(range == 3);
  6103. if (compressedEP[0] > compressedEP[1])
  6104. {
  6105. std::swap(compressedEP[0], compressedEP[1]);
  6106. indexOrder[0] = 1;
  6107. indexOrder[1] = 2;
  6108. indexOrder[2] = 0;
  6109. }
  6110. else
  6111. {
  6112. indexOrder[0] = 0;
  6113. indexOrder[1] = 2;
  6114. indexOrder[2] = 1;
  6115. }
  6116. indexOrder[3] = 3;
  6117. }
  6118. packedBlocks[0] = static_cast<uint8_t>(compressedEP[0] & 0xff);
  6119. packedBlocks[1] = static_cast<uint8_t>((compressedEP[0] >> 8) & 0xff);
  6120. packedBlocks[2] = static_cast<uint8_t>(compressedEP[1] & 0xff);
  6121. packedBlocks[3] = static_cast<uint8_t>((compressedEP[1] >> 8) & 0xff);
  6122. for (int i = 0; i < 16; i += 4)
  6123. {
  6124. int packedIndexes = 0;
  6125. for (int subi = 0; subi < 4; subi++)
  6126. {
  6127. ParallelMath::ScalarUInt16 index = ParallelMath::Extract(bestIndexes[i + subi], block);
  6128. packedIndexes |= (indexOrder[index] << (subi * 2));
  6129. }
  6130. packedBlocks[4 + i / 4] = static_cast<uint8_t>(packedIndexes);
  6131. }
  6132. packedBlocks += packedBlockStride;
  6133. }
  6134. }
  6135. };
  6136. // Signed input blocks are converted into unsigned space, with the maximum value being 254
  6137. void BiasSignedInput(PixelBlockU8 inputNormalized[ParallelMath::ParallelSize], const PixelBlockS8 inputSigned[ParallelMath::ParallelSize])
  6138. {
  6139. for (size_t block = 0; block < ParallelMath::ParallelSize; block++)
  6140. {
  6141. const PixelBlockS8& inputSignedBlock = inputSigned[block];
  6142. PixelBlockU8& inputNormalizedBlock = inputNormalized[block];
  6143. for (size_t px = 0; px < 16; px++)
  6144. {
  6145. for (size_t ch = 0; ch < 4; ch++)
  6146. inputNormalizedBlock.m_pixels[px][ch] = static_cast<uint8_t>(std::max<int>(inputSignedBlock.m_pixels[px][ch], -127) + 127);
  6147. }
  6148. }
  6149. }
  6150. void FillWeights(const Options &options, float channelWeights[4])
  6151. {
  6152. if (options.flags & Flags::Uniform)
  6153. channelWeights[0] = channelWeights[1] = channelWeights[2] = channelWeights[3] = 1.0f;
  6154. else
  6155. {
  6156. channelWeights[0] = options.redWeight;
  6157. channelWeights[1] = options.greenWeight;
  6158. channelWeights[2] = options.blueWeight;
  6159. channelWeights[3] = options.alphaWeight;
  6160. }
  6161. }
  6162. }
  6163. namespace Kernels
  6164. {
  6165. void EncodeBC7(uint8_t *pBC, const PixelBlockU8 *pBlocks, const cvtt::Options &options)
  6166. {
  6167. assert(pBlocks);
  6168. assert(pBC);
  6169. float channelWeights[4];
  6170. Internal::FillWeights(options, channelWeights);
  6171. for (size_t blockBase = 0; blockBase < cvtt::NumParallelBlocks; blockBase += ParallelMath::ParallelSize)
  6172. {
  6173. Internal::BC7Computer::Pack(options.flags, pBlocks + blockBase, pBC, channelWeights, options.seedPoints, options.refineRoundsBC7);
  6174. pBC += ParallelMath::ParallelSize * 16;
  6175. }
  6176. }
  6177. void EncodeBC6HU(uint8_t *pBC, const PixelBlockF16 *pBlocks, const cvtt::Options &options)
  6178. {
  6179. assert(pBlocks);
  6180. assert(pBC);
  6181. float channelWeights[4];
  6182. Internal::FillWeights(options, channelWeights);
  6183. for (size_t blockBase = 0; blockBase < cvtt::NumParallelBlocks; blockBase += ParallelMath::ParallelSize)
  6184. {
  6185. Internal::BC6HComputer::Pack(options.flags, pBlocks + blockBase, pBC, channelWeights, false, options.seedPoints, options.refineRoundsBC6H);
  6186. pBC += ParallelMath::ParallelSize * 16;
  6187. }
  6188. }
  6189. void EncodeBC6HS(uint8_t *pBC, const PixelBlockF16 *pBlocks, const cvtt::Options &options)
  6190. {
  6191. assert(pBlocks);
  6192. assert(pBC);
  6193. float channelWeights[4];
  6194. Internal::FillWeights(options, channelWeights);
  6195. for (size_t blockBase = 0; blockBase < cvtt::NumParallelBlocks; blockBase += ParallelMath::ParallelSize)
  6196. {
  6197. Internal::BC6HComputer::Pack(options.flags, pBlocks + blockBase, pBC, channelWeights, true, options.seedPoints, options.refineRoundsBC6H);
  6198. pBC += ParallelMath::ParallelSize * 16;
  6199. }
  6200. }
  6201. void EncodeBC1(uint8_t *pBC, const PixelBlockU8 *pBlocks, const cvtt::Options &options)
  6202. {
  6203. assert(pBlocks);
  6204. assert(pBC);
  6205. float channelWeights[4];
  6206. Internal::FillWeights(options, channelWeights);
  6207. for (size_t blockBase = 0; blockBase < cvtt::NumParallelBlocks; blockBase += ParallelMath::ParallelSize)
  6208. {
  6209. Internal::S3TCComputer::PackRGB(options.flags, pBlocks + blockBase, pBC, 8, channelWeights, true, options.threshold, (options.flags & Flags::S3TC_Exhaustive) != 0, options.seedPoints, options.refineRoundsS3TC);
  6210. pBC += ParallelMath::ParallelSize * 8;
  6211. }
  6212. }
  6213. void EncodeBC2(uint8_t *pBC, const PixelBlockU8 *pBlocks, const Options &options)
  6214. {
  6215. assert(pBlocks);
  6216. assert(pBC);
  6217. float channelWeights[4];
  6218. Internal::FillWeights(options, channelWeights);
  6219. for (size_t blockBase = 0; blockBase < NumParallelBlocks; blockBase += ParallelMath::ParallelSize)
  6220. {
  6221. Internal::S3TCComputer::PackRGB(options.flags, pBlocks + blockBase, pBC + 8, 16, channelWeights, false, 1.0f, (options.flags & Flags::S3TC_Exhaustive) != 0, options.seedPoints, options.refineRoundsS3TC);
  6222. Internal::S3TCComputer::PackExplicitAlpha(options.flags, pBlocks + blockBase, 3, pBC, 16);
  6223. pBC += ParallelMath::ParallelSize * 16;
  6224. }
  6225. }
  6226. void EncodeBC3(uint8_t *pBC, const PixelBlockU8 *pBlocks, const Options &options)
  6227. {
  6228. assert(pBlocks);
  6229. assert(pBC);
  6230. float channelWeights[4];
  6231. Internal::FillWeights(options, channelWeights);
  6232. for (size_t blockBase = 0; blockBase < NumParallelBlocks; blockBase += ParallelMath::ParallelSize)
  6233. {
  6234. Internal::S3TCComputer::PackRGB(options.flags, pBlocks + blockBase, pBC + 8, 16, channelWeights, false, 1.0f, (options.flags & Flags::S3TC_Exhaustive) != 0, options.seedPoints, options.refineRoundsS3TC);
  6235. Internal::S3TCComputer::PackInterpolatedAlpha(options.flags, pBlocks + blockBase, 3, pBC, 16, false, options.seedPoints, options.refineRoundsIIC);
  6236. pBC += ParallelMath::ParallelSize * 16;
  6237. }
  6238. }
  6239. void EncodeBC4U(uint8_t *pBC, const PixelBlockU8 *pBlocks, const Options &options)
  6240. {
  6241. assert(pBlocks);
  6242. assert(pBC);
  6243. float channelWeights[4];
  6244. Internal::FillWeights(options, channelWeights);
  6245. for (size_t blockBase = 0; blockBase < NumParallelBlocks; blockBase += ParallelMath::ParallelSize)
  6246. {
  6247. Internal::S3TCComputer::PackInterpolatedAlpha(options.flags, pBlocks + blockBase, 0, pBC, 8, false, options.seedPoints, options.refineRoundsIIC);
  6248. pBC += ParallelMath::ParallelSize * 8;
  6249. }
  6250. }
  6251. void EncodeBC4S(uint8_t *pBC, const PixelBlockS8 *pBlocks, const Options &options)
  6252. {
  6253. assert(pBlocks);
  6254. assert(pBC);
  6255. float channelWeights[4];
  6256. Internal::FillWeights(options, channelWeights);
  6257. for (size_t blockBase = 0; blockBase < NumParallelBlocks; blockBase += ParallelMath::ParallelSize)
  6258. {
  6259. PixelBlockU8 inputBlocks[ParallelMath::ParallelSize];
  6260. Internal::BiasSignedInput(inputBlocks, pBlocks + blockBase);
  6261. Internal::S3TCComputer::PackInterpolatedAlpha(options.flags, inputBlocks, 0, pBC, 8, true, options.seedPoints, options.refineRoundsIIC);
  6262. pBC += ParallelMath::ParallelSize * 8;
  6263. }
  6264. }
  6265. void EncodeBC5U(uint8_t *pBC, const PixelBlockU8 *pBlocks, const Options &options)
  6266. {
  6267. assert(pBlocks);
  6268. assert(pBC);
  6269. float channelWeights[4];
  6270. Internal::FillWeights(options, channelWeights);
  6271. for (size_t blockBase = 0; blockBase < NumParallelBlocks; blockBase += ParallelMath::ParallelSize)
  6272. {
  6273. Internal::S3TCComputer::PackInterpolatedAlpha(options.flags, pBlocks + blockBase, 0, pBC, 16, false, options.seedPoints, options.refineRoundsIIC);
  6274. Internal::S3TCComputer::PackInterpolatedAlpha(options.flags, pBlocks + blockBase, 1, pBC + 8, 16, false, options.seedPoints, options.refineRoundsIIC);
  6275. pBC += ParallelMath::ParallelSize * 16;
  6276. }
  6277. }
  6278. void EncodeBC5S(uint8_t *pBC, const PixelBlockS8 *pBlocks, const Options &options)
  6279. {
  6280. assert(pBlocks);
  6281. assert(pBC);
  6282. float channelWeights[4];
  6283. Internal::FillWeights(options, channelWeights);
  6284. for (size_t blockBase = 0; blockBase < NumParallelBlocks; blockBase += ParallelMath::ParallelSize)
  6285. {
  6286. PixelBlockU8 inputBlocks[ParallelMath::ParallelSize];
  6287. Internal::BiasSignedInput(inputBlocks, pBlocks + blockBase);
  6288. Internal::S3TCComputer::PackInterpolatedAlpha(options.flags, inputBlocks, 0, pBC, 16, true, options.seedPoints, options.refineRoundsIIC);
  6289. Internal::S3TCComputer::PackInterpolatedAlpha(options.flags, inputBlocks, 1, pBC + 8, 16, true, options.seedPoints, options.refineRoundsIIC);
  6290. pBC += ParallelMath::ParallelSize * 16;
  6291. }
  6292. }
  6293. void DecodeBC7(PixelBlockU8 *pBlocks, const uint8_t *pBC)
  6294. {
  6295. assert(pBlocks);
  6296. assert(pBC);
  6297. for (size_t blockBase = 0; blockBase < cvtt::NumParallelBlocks; blockBase++)
  6298. {
  6299. Internal::BC7Computer::UnpackOne(pBlocks[blockBase], pBC);
  6300. pBC += 16;
  6301. }
  6302. }
  6303. void DecodeBC6HU(PixelBlockF16 *pBlocks, const uint8_t *pBC)
  6304. {
  6305. assert(pBlocks);
  6306. assert(pBC);
  6307. for (size_t blockBase = 0; blockBase < cvtt::NumParallelBlocks; blockBase++)
  6308. {
  6309. Internal::BC6HComputer::UnpackOne(pBlocks[blockBase], pBC, false);
  6310. pBC += 16;
  6311. }
  6312. }
  6313. void DecodeBC6HS(PixelBlockF16 *pBlocks, const uint8_t *pBC)
  6314. {
  6315. assert(pBlocks);
  6316. assert(pBC);
  6317. for (size_t blockBase = 0; blockBase < cvtt::NumParallelBlocks; blockBase++)
  6318. {
  6319. Internal::BC6HComputer::UnpackOne(pBlocks[blockBase], pBC, true);
  6320. pBC += 16;
  6321. }
  6322. }
  6323. }
  6324. }