basisu_uastc_enc.cpp 149 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194
  1. // basisu_uastc_enc.cpp
  2. // Copyright (C) 2019-2024 Binomial LLC. All Rights Reserved.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #include "basisu_uastc_enc.h"
  16. #include "3rdparty/android_astc_decomp.h"
  17. #include "basisu_gpu_texture.h"
  18. #include "basisu_bc7enc.h"
  19. #ifdef _DEBUG
  20. // When BASISU_VALIDATE_UASTC_ENC is 1, we pack and unpack to/from UASTC and ASTC, then validate that each codec returns the exact same results. This is slower.
  21. #define BASISU_VALIDATE_UASTC_ENC 1
  22. #endif
  23. #define BASISU_SUPPORT_FORCE_MODE 0
  24. using namespace basist;
  25. namespace basisu
  26. {
  27. const uint32_t MAX_ENCODE_RESULTS = 512;
  28. #if BASISU_VALIDATE_UASTC_ENC
  29. static void validate_func(bool condition, int line)
  30. {
  31. if (!condition)
  32. {
  33. fprintf(stderr, "basisu_uastc_enc: Internal validation failed on line %u!\n", line);
  34. }
  35. }
  36. #define VALIDATE(c) validate_func(c, __LINE__);
  37. #else
  38. #define VALIDATE(c)
  39. #endif
  40. enum dxt_constants
  41. {
  42. cDXT1SelectorBits = 2U, cDXT1SelectorValues = 1U << cDXT1SelectorBits, cDXT1SelectorMask = cDXT1SelectorValues - 1U,
  43. cDXT5SelectorBits = 3U, cDXT5SelectorValues = 1U << cDXT5SelectorBits, cDXT5SelectorMask = cDXT5SelectorValues - 1U,
  44. };
  45. struct dxt1_block
  46. {
  47. enum { cTotalEndpointBytes = 2, cTotalSelectorBytes = 4 };
  48. uint8_t m_low_color[cTotalEndpointBytes];
  49. uint8_t m_high_color[cTotalEndpointBytes];
  50. uint8_t m_selectors[cTotalSelectorBytes];
  51. inline void clear() { basisu::clear_obj(*this); }
  52. inline uint32_t get_high_color() const { return m_high_color[0] | (m_high_color[1] << 8U); }
  53. inline uint32_t get_low_color() const { return m_low_color[0] | (m_low_color[1] << 8U); }
  54. inline void set_low_color(uint16_t c) { m_low_color[0] = static_cast<uint8_t>(c & 0xFF); m_low_color[1] = static_cast<uint8_t>((c >> 8) & 0xFF); }
  55. inline void set_high_color(uint16_t c) { m_high_color[0] = static_cast<uint8_t>(c & 0xFF); m_high_color[1] = static_cast<uint8_t>((c >> 8) & 0xFF); }
  56. inline uint32_t get_selector(uint32_t x, uint32_t y) const { assert((x < 4U) && (y < 4U)); return (m_selectors[y] >> (x * cDXT1SelectorBits))& cDXT1SelectorMask; }
  57. inline void set_selector(uint32_t x, uint32_t y, uint32_t val) { assert((x < 4U) && (y < 4U) && (val < 4U)); m_selectors[y] &= (~(cDXT1SelectorMask << (x * cDXT1SelectorBits))); m_selectors[y] |= (val << (x * cDXT1SelectorBits)); }
  58. static uint16_t pack_color(const color_rgba& color, bool scaled, uint32_t bias = 127U)
  59. {
  60. uint32_t r = color.r, g = color.g, b = color.b;
  61. if (scaled)
  62. {
  63. r = (r * 31U + bias) / 255U;
  64. g = (g * 63U + bias) / 255U;
  65. b = (b * 31U + bias) / 255U;
  66. }
  67. return static_cast<uint16_t>(basisu::minimum(b, 31U) | (basisu::minimum(g, 63U) << 5U) | (basisu::minimum(r, 31U) << 11U));
  68. }
  69. static uint16_t pack_unscaled_color(uint32_t r, uint32_t g, uint32_t b) { return static_cast<uint16_t>(b | (g << 5U) | (r << 11U)); }
  70. };
  71. #define UASTC_WRITE_MODE_DESCS 0
  72. static inline void uastc_write_bits(uint8_t* pBuf, uint32_t& bit_offset, uint64_t code, uint32_t codesize, const char* pDesc)
  73. {
  74. (void)pDesc;
  75. #if UASTC_WRITE_MODE_DESCS
  76. if (pDesc)
  77. printf("%s: %u %u\n", pDesc, bit_offset, codesize);
  78. #endif
  79. assert((codesize == 64) || (code < (1ULL << codesize)));
  80. while (codesize)
  81. {
  82. uint32_t byte_bit_offset = bit_offset & 7;
  83. uint32_t bits_to_write = basisu::minimum<int>(codesize, 8 - byte_bit_offset);
  84. pBuf[bit_offset >> 3] |= (code << byte_bit_offset);
  85. code >>= bits_to_write;
  86. codesize -= bits_to_write;
  87. bit_offset += bits_to_write;
  88. }
  89. }
  90. void pack_uastc(basist::uastc_block& blk, const uastc_encode_results& result, const etc_block& etc1_blk, uint32_t etc1_bias, const eac_a8_block& etc_eac_a8_blk, bool bc1_hint0, bool bc1_hint1)
  91. {
  92. if ((g_uastc_mode_has_alpha[result.m_uastc_mode]) && (result.m_uastc_mode != UASTC_MODE_INDEX_SOLID_COLOR))
  93. {
  94. assert(etc_eac_a8_blk.m_multiplier >= 1);
  95. }
  96. uint8_t buf[32];
  97. memset(buf, 0, sizeof(buf));
  98. uint32_t block_bit_offset = 0;
  99. #if UASTC_WRITE_MODE_DESCS
  100. printf("**** Mode: %u\n", result.m_uastc_mode);
  101. #endif
  102. uastc_write_bits(buf, block_bit_offset, g_uastc_mode_huff_codes[result.m_uastc_mode][0], g_uastc_mode_huff_codes[result.m_uastc_mode][1], "mode");
  103. if (result.m_uastc_mode == UASTC_MODE_INDEX_SOLID_COLOR)
  104. {
  105. uastc_write_bits(buf, block_bit_offset, result.m_solid_color.r, 8, "R");
  106. uastc_write_bits(buf, block_bit_offset, result.m_solid_color.g, 8, "G");
  107. uastc_write_bits(buf, block_bit_offset, result.m_solid_color.b, 8, "B");
  108. uastc_write_bits(buf, block_bit_offset, result.m_solid_color.a, 8, "A");
  109. uastc_write_bits(buf, block_bit_offset, etc1_blk.get_diff_bit(), 1, "ETC1D");
  110. uastc_write_bits(buf, block_bit_offset, etc1_blk.get_inten_table(0), 3, "ETC1I");
  111. uastc_write_bits(buf, block_bit_offset, etc1_blk.get_selector(0, 0), 2, "ETC1S");
  112. uint32_t r, g, b;
  113. if (etc1_blk.get_diff_bit())
  114. etc_block::unpack_color5(r, g, b, etc1_blk.get_base5_color(), false);
  115. else
  116. etc_block::unpack_color4(r, g, b, etc1_blk.get_base4_color(0), false);
  117. uastc_write_bits(buf, block_bit_offset, r, 5, "ETC1R");
  118. uastc_write_bits(buf, block_bit_offset, g, 5, "ETC1G");
  119. uastc_write_bits(buf, block_bit_offset, b, 5, "ETC1B");
  120. memcpy(&blk, buf, sizeof(blk));
  121. return;
  122. }
  123. if (g_uastc_mode_has_bc1_hint0[result.m_uastc_mode])
  124. uastc_write_bits(buf, block_bit_offset, bc1_hint0, 1, "BC1H0");
  125. else
  126. {
  127. assert(bc1_hint0 == false);
  128. }
  129. if (g_uastc_mode_has_bc1_hint1[result.m_uastc_mode])
  130. uastc_write_bits(buf, block_bit_offset, bc1_hint1, 1, "BC1H1");
  131. else
  132. {
  133. assert(bc1_hint1 == false);
  134. }
  135. uastc_write_bits(buf, block_bit_offset, etc1_blk.get_flip_bit(), 1, "ETC1F");
  136. uastc_write_bits(buf, block_bit_offset, etc1_blk.get_diff_bit(), 1, "ETC1D");
  137. uastc_write_bits(buf, block_bit_offset, etc1_blk.get_inten_table(0), 3, "ETC1I0");
  138. uastc_write_bits(buf, block_bit_offset, etc1_blk.get_inten_table(1), 3, "ETC1I1");
  139. if (g_uastc_mode_has_etc1_bias[result.m_uastc_mode])
  140. uastc_write_bits(buf, block_bit_offset, etc1_bias, 5, "ETC1BIAS");
  141. else
  142. {
  143. assert(etc1_bias == 0);
  144. }
  145. if (g_uastc_mode_has_alpha[result.m_uastc_mode])
  146. {
  147. const uint32_t etc2_hints = etc_eac_a8_blk.m_table | (etc_eac_a8_blk.m_multiplier << 4);
  148. assert(etc2_hints > 0 && etc2_hints <= 0xFF);
  149. uastc_write_bits(buf, block_bit_offset, etc2_hints, 8, "ETC2TM");
  150. }
  151. uint32_t subsets = 1;
  152. switch (result.m_uastc_mode)
  153. {
  154. case 2:
  155. case 4:
  156. case 7:
  157. case 9:
  158. case 16:
  159. uastc_write_bits(buf, block_bit_offset, result.m_common_pattern, 5, "PAT");
  160. subsets = 2;
  161. break;
  162. case 3:
  163. uastc_write_bits(buf, block_bit_offset, result.m_common_pattern, 4, "PAT");
  164. subsets = 3;
  165. break;
  166. default:
  167. break;
  168. }
  169. #ifdef _DEBUG
  170. uint32_t part_seed = 0;
  171. switch (result.m_uastc_mode)
  172. {
  173. case 2:
  174. case 4:
  175. case 9:
  176. case 16:
  177. part_seed = g_astc_bc7_common_partitions2[result.m_common_pattern].m_astc;
  178. break;
  179. case 3:
  180. part_seed = g_astc_bc7_common_partitions3[result.m_common_pattern].m_astc;
  181. break;
  182. case 7:
  183. part_seed = g_bc7_3_astc2_common_partitions[result.m_common_pattern].m_astc2;
  184. break;
  185. default:
  186. break;
  187. }
  188. #endif
  189. uint32_t total_planes = 1;
  190. switch (result.m_uastc_mode)
  191. {
  192. case 6:
  193. case 11:
  194. case 13:
  195. uastc_write_bits(buf, block_bit_offset, result.m_astc.m_ccs, 2, "COMPSEL");
  196. total_planes = 2;
  197. break;
  198. case 17:
  199. // CCS field is always 3 for dual plane LA.
  200. assert(result.m_astc.m_ccs == 3);
  201. total_planes = 2;
  202. break;
  203. default:
  204. break;
  205. }
  206. uint8_t weights[32];
  207. memcpy(weights, result.m_astc.m_weights, 16 * total_planes);
  208. uint8_t endpoints[18];
  209. memcpy(endpoints, result.m_astc.m_endpoints, sizeof(endpoints));
  210. const uint32_t total_comps = g_uastc_mode_comps[result.m_uastc_mode];
  211. // LLAA
  212. // LLAA LLAA
  213. // LLAA LLAA LLAA
  214. // RRGGBB
  215. // RRGGBB RRGGBB
  216. // RRGGBB RRGGBB RRGGBB
  217. // RRGGBBAA
  218. // RRGGBBAA RRGGBBAA
  219. const uint32_t weight_bits = g_uastc_mode_weight_bits[result.m_uastc_mode];
  220. const uint8_t* pPartition_pattern;
  221. const uint8_t* pSubset_anchor_indices = basist::get_anchor_indices(subsets, result.m_uastc_mode, result.m_common_pattern, pPartition_pattern);
  222. for (uint32_t plane_index = 0; plane_index < total_planes; plane_index++)
  223. {
  224. for (uint32_t subset_index = 0; subset_index < subsets; subset_index++)
  225. {
  226. const uint32_t anchor_index = pSubset_anchor_indices[subset_index];
  227. #ifdef _DEBUG
  228. if (subsets >= 2)
  229. {
  230. for (uint32_t i = 0; i < 16; i++)
  231. {
  232. const uint32_t part_index = astc_compute_texel_partition(part_seed, i & 3, i >> 2, 0, subsets, true);
  233. if (part_index == subset_index)
  234. {
  235. assert(anchor_index == i);
  236. break;
  237. }
  238. }
  239. }
  240. else
  241. {
  242. assert(!anchor_index);
  243. }
  244. #endif
  245. // Check anchor weight's MSB - if it's set then invert this subset's weights and swap the endpoints
  246. if (weights[anchor_index * total_planes + plane_index] & (1 << (weight_bits - 1)))
  247. {
  248. for (uint32_t i = 0; i < 16; i++)
  249. {
  250. const uint32_t part_index = pPartition_pattern[i];
  251. #ifdef _DEBUG
  252. if (subsets >= 2)
  253. {
  254. assert(part_index == (uint32_t)astc_compute_texel_partition(part_seed, i & 3, i >> 2, 0, subsets, true));
  255. }
  256. else
  257. {
  258. assert(!part_index);
  259. }
  260. #endif
  261. if (part_index == subset_index)
  262. weights[i * total_planes + plane_index] = ((1 << weight_bits) - 1) - weights[i * total_planes + plane_index];
  263. }
  264. if (total_planes == 2)
  265. {
  266. for (int c = 0; c < (int)total_comps; c++)
  267. {
  268. const uint32_t comp_plane = (total_comps == 2) ? c : ((c == result.m_astc.m_ccs) ? 1 : 0);
  269. if (comp_plane == plane_index)
  270. std::swap(endpoints[c * 2 + 0], endpoints[c * 2 + 1]);
  271. }
  272. }
  273. else
  274. {
  275. for (uint32_t c = 0; c < total_comps; c++)
  276. std::swap(endpoints[subset_index * total_comps * 2 + c * 2 + 0], endpoints[subset_index * total_comps * 2 + c * 2 + 1]);
  277. }
  278. }
  279. } // subset_index
  280. } // plane_index
  281. const uint32_t total_values = total_comps * 2 * subsets;
  282. const uint32_t endpoint_range = g_uastc_mode_endpoint_ranges[result.m_uastc_mode];
  283. uint32_t bit_values[18];
  284. uint32_t tq_values[8];
  285. uint32_t total_tq_values = 0;
  286. uint32_t tq_accum = 0;
  287. uint32_t tq_mul = 1;
  288. const uint32_t ep_bits = g_astc_bise_range_table[endpoint_range][0];
  289. const uint32_t ep_trits = g_astc_bise_range_table[endpoint_range][1];
  290. const uint32_t ep_quints = g_astc_bise_range_table[endpoint_range][2];
  291. for (uint32_t i = 0; i < total_values; i++)
  292. {
  293. uint32_t val = endpoints[i];
  294. uint32_t bits = val & ((1 << ep_bits) - 1);
  295. uint32_t tq = val >> ep_bits;
  296. bit_values[i] = bits;
  297. if (ep_trits)
  298. {
  299. assert(tq < 3);
  300. tq_accum += tq * tq_mul;
  301. tq_mul *= 3;
  302. if (tq_mul == 243)
  303. {
  304. tq_values[total_tq_values++] = tq_accum;
  305. tq_accum = 0;
  306. tq_mul = 1;
  307. }
  308. }
  309. else if (ep_quints)
  310. {
  311. assert(tq < 5);
  312. tq_accum += tq * tq_mul;
  313. tq_mul *= 5;
  314. if (tq_mul == 125)
  315. {
  316. tq_values[total_tq_values++] = tq_accum;
  317. tq_accum = 0;
  318. tq_mul = 1;
  319. }
  320. }
  321. }
  322. uint32_t total_endpoint_bits = 0;
  323. (void)total_endpoint_bits;
  324. for (uint32_t i = 0; i < total_tq_values; i++)
  325. {
  326. const uint32_t num_bits = ep_trits ? 8 : 7;
  327. uastc_write_bits(buf, block_bit_offset, tq_values[i], num_bits, "ETQ");
  328. total_endpoint_bits += num_bits;
  329. }
  330. if (tq_mul > 1)
  331. {
  332. uint32_t num_bits;
  333. if (ep_trits)
  334. {
  335. if (tq_mul == 3)
  336. num_bits = 2;
  337. else if (tq_mul == 9)
  338. num_bits = 4;
  339. else if (tq_mul == 27)
  340. num_bits = 5;
  341. else //if (tq_mul == 81)
  342. num_bits = 7;
  343. }
  344. else
  345. {
  346. if (tq_mul == 5)
  347. num_bits = 3;
  348. else //if (tq_mul == 25)
  349. num_bits = 5;
  350. }
  351. uastc_write_bits(buf, block_bit_offset, tq_accum, num_bits, "ETQ");
  352. total_endpoint_bits += num_bits;
  353. }
  354. for (uint32_t i = 0; i < total_values; i++)
  355. {
  356. uastc_write_bits(buf, block_bit_offset, bit_values[i], ep_bits, "EBITS");
  357. total_endpoint_bits += ep_bits;
  358. }
  359. #if UASTC_WRITE_MODE_DESCS
  360. uint32_t weight_start = block_bit_offset;
  361. #endif
  362. uint32_t total_weight_bits = 0;
  363. (void)total_weight_bits;
  364. const uint32_t plane_shift = (total_planes == 2) ? 1 : 0;
  365. for (uint32_t i = 0; i < 16 * total_planes; i++)
  366. {
  367. uint32_t numbits = weight_bits;
  368. for (uint32_t s = 0; s < subsets; s++)
  369. {
  370. if (pSubset_anchor_indices[s] == (i >> plane_shift))
  371. {
  372. numbits--;
  373. break;
  374. }
  375. }
  376. uastc_write_bits(buf, block_bit_offset, weights[i], numbits, nullptr);
  377. total_weight_bits += numbits;
  378. }
  379. #if UASTC_WRITE_MODE_DESCS
  380. printf("WEIGHTS: %u %u\n", weight_start, total_weight_bits);
  381. #endif
  382. assert(block_bit_offset <= 128);
  383. memcpy(&blk, buf, sizeof(blk));
  384. #if UASTC_WRITE_MODE_DESCS
  385. printf("Total bits: %u, endpoint bits: %u, weight bits: %u\n", block_bit_offset, total_endpoint_bits, total_weight_bits);
  386. #endif
  387. }
  388. // MODE 0
  389. // 0. DualPlane: 0, WeightRange: 8 (16), Subsets: 1, CEM: 8 (RGB Direct ), EndpointRange: 19 (192) MODE6 RGB
  390. // 18. DualPlane: 0, WeightRange: 11 (32), Subsets: 1, CEM: 8 (RGB Direct ), EndpointRange: 11 (32) MODE6 RGB
  391. static void astc_mode0_or_18(uint32_t mode, const color_rgba block[4][4], uastc_encode_results* pResults, uint32_t& total_results, bc7enc_compress_block_params& comp_params, const uint8_t *pForce_selectors = nullptr)
  392. {
  393. const uint32_t endpoint_range = (mode == 18) ? 11 : 19;
  394. const uint32_t weight_range = (mode == 18) ? 11 : 8;
  395. color_cell_compressor_params ccell_params;
  396. memset(&ccell_params, 0, sizeof(ccell_params));
  397. ccell_params.m_num_pixels = 16;
  398. ccell_params.m_pPixels = (color_quad_u8*)&block[0][0];
  399. ccell_params.m_num_selector_weights = (mode == 18) ? 32 : 16;
  400. ccell_params.m_pSelector_weights = (mode == 18) ? g_astc_weights5 : g_astc_weights4;
  401. ccell_params.m_pSelector_weightsx = (mode == 18) ? (const bc7enc_vec4F*)g_astc_weights5x : (const bc7enc_vec4F*)g_astc_weights4x;
  402. ccell_params.m_astc_endpoint_range = endpoint_range;
  403. ccell_params.m_weights[0] = 1;
  404. ccell_params.m_weights[1] = 1;
  405. ccell_params.m_weights[2] = 1;
  406. ccell_params.m_weights[3] = 1;
  407. ccell_params.m_pForce_selectors = pForce_selectors;
  408. color_cell_compressor_results ccell_results;
  409. uint8_t ccell_result_selectors[16];
  410. uint8_t ccell_result_selectors_temp[16];
  411. memset(&ccell_results, 0, sizeof(ccell_results));
  412. ccell_results.m_pSelectors = &ccell_result_selectors[0];
  413. ccell_results.m_pSelectors_temp = &ccell_result_selectors_temp[0];
  414. uint64_t part_err = color_cell_compression(255, &ccell_params, &ccell_results, &comp_params);
  415. // ASTC
  416. astc_block_desc astc_results;
  417. memset(&astc_results, 0, sizeof(astc_results));
  418. astc_results.m_dual_plane = false;
  419. astc_results.m_weight_range = weight_range;// (mode == 18) ? 11 : 8;
  420. astc_results.m_ccs = 0;
  421. astc_results.m_subsets = 1;
  422. astc_results.m_partition_seed = 0;
  423. astc_results.m_cem = 8;
  424. astc_results.m_endpoints[0] = ccell_results.m_astc_low_endpoint.m_c[0];
  425. astc_results.m_endpoints[1] = ccell_results.m_astc_high_endpoint.m_c[0];
  426. astc_results.m_endpoints[2] = ccell_results.m_astc_low_endpoint.m_c[1];
  427. astc_results.m_endpoints[3] = ccell_results.m_astc_high_endpoint.m_c[1];
  428. astc_results.m_endpoints[4] = ccell_results.m_astc_low_endpoint.m_c[2];
  429. astc_results.m_endpoints[5] = ccell_results.m_astc_high_endpoint.m_c[2];
  430. bool invert = false;
  431. if (pForce_selectors == nullptr)
  432. {
  433. int s0 = g_astc_unquant[endpoint_range][astc_results.m_endpoints[0]].m_unquant + g_astc_unquant[endpoint_range][astc_results.m_endpoints[2]].m_unquant + g_astc_unquant[endpoint_range][astc_results.m_endpoints[4]].m_unquant;
  434. int s1 = g_astc_unquant[endpoint_range][astc_results.m_endpoints[1]].m_unquant + g_astc_unquant[endpoint_range][astc_results.m_endpoints[3]].m_unquant + g_astc_unquant[endpoint_range][astc_results.m_endpoints[5]].m_unquant;
  435. if (s1 < s0)
  436. {
  437. std::swap(astc_results.m_endpoints[0], astc_results.m_endpoints[1]);
  438. std::swap(astc_results.m_endpoints[2], astc_results.m_endpoints[3]);
  439. std::swap(astc_results.m_endpoints[4], astc_results.m_endpoints[5]);
  440. invert = true;
  441. }
  442. }
  443. for (uint32_t y = 0; y < 4; y++)
  444. {
  445. for (uint32_t x = 0; x < 4; x++)
  446. {
  447. astc_results.m_weights[x + y * 4] = ccell_result_selectors[x + y * 4];
  448. if (invert)
  449. astc_results.m_weights[x + y * 4] = ((mode == 18) ? 31 : 15) - astc_results.m_weights[x + y * 4];
  450. }
  451. }
  452. assert(total_results < MAX_ENCODE_RESULTS);
  453. if (total_results < MAX_ENCODE_RESULTS)
  454. {
  455. pResults[total_results].m_uastc_mode = mode;
  456. pResults[total_results].m_common_pattern = 0;
  457. pResults[total_results].m_astc = astc_results;
  458. pResults[total_results].m_astc_err = part_err;
  459. total_results++;
  460. }
  461. }
  462. // MODE 1
  463. // 1-subset, 2-bit indices, 8-bit endpoints, BC7 mode 3
  464. // DualPlane: 0, WeightRange: 2 (4), Subsets: 1, CEM: 8 (RGB Direct ), EndpointRange: 20 (256) MODE3 or MODE5 RGB
  465. static void astc_mode1(const color_rgba block[4][4], uastc_encode_results* pResults, uint32_t& total_results, bc7enc_compress_block_params& comp_params)
  466. {
  467. color_cell_compressor_params ccell_params;
  468. memset(&ccell_params, 0, sizeof(ccell_params));
  469. ccell_params.m_num_pixels = 16;
  470. ccell_params.m_pPixels = (color_quad_u8*)&block[0][0];
  471. ccell_params.m_num_selector_weights = 4;
  472. ccell_params.m_pSelector_weights = g_bc7_weights2;
  473. ccell_params.m_pSelector_weightsx = (const bc7enc_vec4F*)g_bc7_weights2x;
  474. ccell_params.m_astc_endpoint_range = 20;
  475. ccell_params.m_weights[0] = 1;
  476. ccell_params.m_weights[1] = 1;
  477. ccell_params.m_weights[2] = 1;
  478. ccell_params.m_weights[3] = 1;
  479. color_cell_compressor_results ccell_results;
  480. uint8_t ccell_result_selectors[16];
  481. uint8_t ccell_result_selectors_temp[16];
  482. memset(&ccell_results, 0, sizeof(ccell_results));
  483. ccell_results.m_pSelectors = &ccell_result_selectors[0];
  484. ccell_results.m_pSelectors_temp = &ccell_result_selectors_temp[0];
  485. uint64_t part_err = color_cell_compression(255, &ccell_params, &ccell_results, &comp_params);
  486. // ASTC
  487. astc_block_desc astc_results;
  488. memset(&astc_results, 0, sizeof(astc_results));
  489. astc_results.m_dual_plane = false;
  490. astc_results.m_weight_range = 2;
  491. astc_results.m_ccs = 0;
  492. astc_results.m_subsets = 1;
  493. astc_results.m_partition_seed = 0;
  494. astc_results.m_cem = 8;
  495. astc_results.m_endpoints[0] = ccell_results.m_astc_low_endpoint.m_c[0];
  496. astc_results.m_endpoints[1] = ccell_results.m_astc_high_endpoint.m_c[0];
  497. astc_results.m_endpoints[2] = ccell_results.m_astc_low_endpoint.m_c[1];
  498. astc_results.m_endpoints[3] = ccell_results.m_astc_high_endpoint.m_c[1];
  499. astc_results.m_endpoints[4] = ccell_results.m_astc_low_endpoint.m_c[2];
  500. astc_results.m_endpoints[5] = ccell_results.m_astc_high_endpoint.m_c[2];
  501. const uint32_t range = 20;
  502. bool invert = false;
  503. int s0 = g_astc_unquant[range][astc_results.m_endpoints[0]].m_unquant + g_astc_unquant[range][astc_results.m_endpoints[2]].m_unquant + g_astc_unquant[range][astc_results.m_endpoints[4]].m_unquant;
  504. int s1 = g_astc_unquant[range][astc_results.m_endpoints[1]].m_unquant + g_astc_unquant[range][astc_results.m_endpoints[3]].m_unquant + g_astc_unquant[range][astc_results.m_endpoints[5]].m_unquant;
  505. if (s1 < s0)
  506. {
  507. std::swap(astc_results.m_endpoints[0], astc_results.m_endpoints[1]);
  508. std::swap(astc_results.m_endpoints[2], astc_results.m_endpoints[3]);
  509. std::swap(astc_results.m_endpoints[4], astc_results.m_endpoints[5]);
  510. invert = true;
  511. }
  512. for (uint32_t y = 0; y < 4; y++)
  513. {
  514. for (uint32_t x = 0; x < 4; x++)
  515. {
  516. astc_results.m_weights[x + y * 4] = ccell_result_selectors[x + y * 4];
  517. if (invert)
  518. astc_results.m_weights[x + y * 4] = 3 - astc_results.m_weights[x + y * 4];
  519. }
  520. }
  521. assert(total_results < MAX_ENCODE_RESULTS);
  522. if (total_results < MAX_ENCODE_RESULTS)
  523. {
  524. pResults[total_results].m_uastc_mode = 1;
  525. pResults[total_results].m_common_pattern = 0;
  526. pResults[total_results].m_astc = astc_results;
  527. pResults[total_results].m_astc_err = part_err;
  528. total_results++;
  529. }
  530. }
  531. static uint32_t estimate_partition2(uint32_t num_weights, uint32_t num_comps, const uint32_t* pWeights, const color_rgba block[4][4], const uint32_t weights[4])
  532. {
  533. assert(pWeights[0] == 0 && pWeights[num_weights - 1] == 64);
  534. uint64_t best_err = UINT64_MAX;
  535. uint32_t best_common_pattern = 0;
  536. for (uint32_t common_pattern = 0; common_pattern < TOTAL_ASTC_BC7_COMMON_PARTITIONS2; common_pattern++)
  537. {
  538. const uint32_t bc7_pattern = g_astc_bc7_common_partitions2[common_pattern].m_bc7;
  539. const uint8_t* pPartition = &g_bc7_partition2[bc7_pattern * 16];
  540. color_quad_u8 subset_colors[2][16];
  541. uint32_t subset_total_colors[2] = { 0, 0 };
  542. for (uint32_t index = 0; index < 16; index++)
  543. subset_colors[pPartition[index]][subset_total_colors[pPartition[index]]++] = ((const color_quad_u8*)block)[index];
  544. uint64_t total_subset_err = 0;
  545. for (uint32_t subset = 0; (subset < 2) && (total_subset_err < best_err); subset++)
  546. total_subset_err += color_cell_compression_est_astc(num_weights, num_comps, pWeights, subset_total_colors[subset], &subset_colors[subset][0], best_err, weights);
  547. if (total_subset_err < best_err)
  548. {
  549. best_err = total_subset_err;
  550. best_common_pattern = common_pattern;
  551. }
  552. }
  553. return best_common_pattern;
  554. }
  555. // MODE 2
  556. // 2-subset, 3-bit indices, 4-bit endpoints, BC7 mode 1
  557. // DualPlane: 0, WeightRange: 5 (8), Subsets: 2, CEM: 8 (RGB Direct ), EndpointRange: 8 (16) MODE1
  558. static void astc_mode2(const color_rgba block[4][4], uastc_encode_results* pResults, uint32_t& total_results, bc7enc_compress_block_params& comp_params, bool estimate_partition)
  559. {
  560. uint32_t first_common_pattern = 0;
  561. uint32_t last_common_pattern = TOTAL_ASTC_BC7_COMMON_PARTITIONS2;
  562. if (estimate_partition)
  563. {
  564. const uint32_t weights[4] = { 1, 1, 1, 1 };
  565. first_common_pattern = estimate_partition2(8, 3, g_bc7_weights3, block, weights);
  566. last_common_pattern = first_common_pattern + 1;
  567. }
  568. for (uint32_t common_pattern = first_common_pattern; common_pattern < last_common_pattern; common_pattern++)
  569. {
  570. const uint32_t bc7_pattern = g_astc_bc7_common_partitions2[common_pattern].m_bc7;
  571. color_rgba part_pixels[2][16];
  572. uint32_t part_pixel_index[4][4];
  573. uint32_t num_part_pixels[2] = { 0, 0 };
  574. for (uint32_t y = 0; y < 4; y++)
  575. {
  576. for (uint32_t x = 0; x < 4; x++)
  577. {
  578. const uint32_t part = g_bc7_partition2[16 * bc7_pattern + x + y * 4];
  579. part_pixel_index[y][x] = num_part_pixels[part];
  580. part_pixels[part][num_part_pixels[part]++] = block[y][x];
  581. }
  582. }
  583. color_cell_compressor_params ccell_params[2];
  584. color_cell_compressor_results ccell_results[2];
  585. uint8_t ccell_result_selectors[2][16];
  586. uint8_t ccell_result_selectors_temp[2][16];
  587. uint64_t total_part_err = 0;
  588. for (uint32_t part = 0; part < 2; part++)
  589. {
  590. memset(&ccell_params[part], 0, sizeof(ccell_params[part]));
  591. ccell_params[part].m_num_pixels = num_part_pixels[part];
  592. ccell_params[part].m_pPixels = (color_quad_u8*)&part_pixels[part][0];
  593. ccell_params[part].m_num_selector_weights = 8;
  594. ccell_params[part].m_pSelector_weights = g_bc7_weights3;
  595. ccell_params[part].m_pSelector_weightsx = (const bc7enc_vec4F*)g_bc7_weights3x;
  596. ccell_params[part].m_astc_endpoint_range = 8;
  597. ccell_params[part].m_weights[0] = 1;
  598. ccell_params[part].m_weights[1] = 1;
  599. ccell_params[part].m_weights[2] = 1;
  600. ccell_params[part].m_weights[3] = 1;
  601. memset(&ccell_results[part], 0, sizeof(ccell_results[part]));
  602. ccell_results[part].m_pSelectors = &ccell_result_selectors[part][0];
  603. ccell_results[part].m_pSelectors_temp = &ccell_result_selectors_temp[part][0];
  604. uint64_t part_err = color_cell_compression(255, &ccell_params[part], &ccell_results[part], &comp_params);
  605. total_part_err += part_err;
  606. } // part
  607. {
  608. // ASTC
  609. astc_block_desc astc_results;
  610. memset(&astc_results, 0, sizeof(astc_results));
  611. astc_results.m_dual_plane = false;
  612. astc_results.m_weight_range = 5;
  613. astc_results.m_ccs = 0;
  614. astc_results.m_subsets = 2;
  615. astc_results.m_partition_seed = g_astc_bc7_common_partitions2[common_pattern].m_astc;
  616. astc_results.m_cem = 8;
  617. uint32_t p0 = 0;
  618. uint32_t p1 = 1;
  619. if (g_astc_bc7_common_partitions2[common_pattern].m_invert)
  620. std::swap(p0, p1);
  621. astc_results.m_endpoints[0] = ccell_results[p0].m_astc_low_endpoint.m_c[0];
  622. astc_results.m_endpoints[1] = ccell_results[p0].m_astc_high_endpoint.m_c[0];
  623. astc_results.m_endpoints[2] = ccell_results[p0].m_astc_low_endpoint.m_c[1];
  624. astc_results.m_endpoints[3] = ccell_results[p0].m_astc_high_endpoint.m_c[1];
  625. astc_results.m_endpoints[4] = ccell_results[p0].m_astc_low_endpoint.m_c[2];
  626. astc_results.m_endpoints[5] = ccell_results[p0].m_astc_high_endpoint.m_c[2];
  627. const uint32_t range = 8;
  628. bool invert[2] = { false, false };
  629. int s0 = g_astc_unquant[range][astc_results.m_endpoints[0]].m_unquant + g_astc_unquant[range][astc_results.m_endpoints[2]].m_unquant + g_astc_unquant[range][astc_results.m_endpoints[4]].m_unquant;
  630. int s1 = g_astc_unquant[range][astc_results.m_endpoints[1]].m_unquant + g_astc_unquant[range][astc_results.m_endpoints[3]].m_unquant + g_astc_unquant[range][astc_results.m_endpoints[5]].m_unquant;
  631. if (s1 < s0)
  632. {
  633. std::swap(astc_results.m_endpoints[0], astc_results.m_endpoints[1]);
  634. std::swap(astc_results.m_endpoints[2], astc_results.m_endpoints[3]);
  635. std::swap(astc_results.m_endpoints[4], astc_results.m_endpoints[5]);
  636. invert[0] = true;
  637. }
  638. astc_results.m_endpoints[6] = ccell_results[p1].m_astc_low_endpoint.m_c[0];
  639. astc_results.m_endpoints[7] = ccell_results[p1].m_astc_high_endpoint.m_c[0];
  640. astc_results.m_endpoints[8] = ccell_results[p1].m_astc_low_endpoint.m_c[1];
  641. astc_results.m_endpoints[9] = ccell_results[p1].m_astc_high_endpoint.m_c[1];
  642. astc_results.m_endpoints[10] = ccell_results[p1].m_astc_low_endpoint.m_c[2];
  643. astc_results.m_endpoints[11] = ccell_results[p1].m_astc_high_endpoint.m_c[2];
  644. s0 = g_astc_unquant[range][astc_results.m_endpoints[0 + 6]].m_unquant + g_astc_unquant[range][astc_results.m_endpoints[2 + 6]].m_unquant + g_astc_unquant[range][astc_results.m_endpoints[4 + 6]].m_unquant;
  645. s1 = g_astc_unquant[range][astc_results.m_endpoints[1 + 6]].m_unquant + g_astc_unquant[range][astc_results.m_endpoints[3 + 6]].m_unquant + g_astc_unquant[range][astc_results.m_endpoints[5 + 6]].m_unquant;
  646. if (s1 < s0)
  647. {
  648. std::swap(astc_results.m_endpoints[0 + 6], astc_results.m_endpoints[1 + 6]);
  649. std::swap(astc_results.m_endpoints[2 + 6], astc_results.m_endpoints[3 + 6]);
  650. std::swap(astc_results.m_endpoints[4 + 6], astc_results.m_endpoints[5 + 6]);
  651. invert[1] = true;
  652. }
  653. for (uint32_t y = 0; y < 4; y++)
  654. {
  655. for (uint32_t x = 0; x < 4; x++)
  656. {
  657. const uint32_t bc7_part = g_bc7_partition2[16 * bc7_pattern + x + y * 4];
  658. astc_results.m_weights[x + y * 4] = ccell_result_selectors[bc7_part][part_pixel_index[y][x]];
  659. uint32_t astc_part = bc7_part;
  660. if (g_astc_bc7_common_partitions2[common_pattern].m_invert)
  661. astc_part = 1 - astc_part;
  662. if (invert[astc_part])
  663. astc_results.m_weights[x + y * 4] = 7 - astc_results.m_weights[x + y * 4];
  664. }
  665. }
  666. assert(total_results < MAX_ENCODE_RESULTS);
  667. if (total_results < MAX_ENCODE_RESULTS)
  668. {
  669. pResults[total_results].m_uastc_mode = 2;
  670. pResults[total_results].m_common_pattern = common_pattern;
  671. pResults[total_results].m_astc = astc_results;
  672. pResults[total_results].m_astc_err = total_part_err;
  673. total_results++;
  674. }
  675. }
  676. } // common_pattern
  677. }
  678. // MODE 3
  679. // 3-subsets, 2-bit indices, [0,11] endpoints, BC7 mode 2
  680. // DualPlane: 0, WeightRange: 2 (4), Subsets: 3, CEM: 8 (RGB Direct ), EndpointRange: 7 (12) MODE2
  681. static void astc_mode3(const color_rgba block[4][4], uastc_encode_results* pResults, uint32_t& total_results, bc7enc_compress_block_params& comp_params, bool estimate_partition)
  682. {
  683. uint32_t first_common_pattern = 0;
  684. uint32_t last_common_pattern = TOTAL_ASTC_BC7_COMMON_PARTITIONS3;
  685. if (estimate_partition)
  686. {
  687. uint64_t best_err = UINT64_MAX;
  688. uint32_t best_common_pattern = 0;
  689. const uint32_t weights[4] = { 1, 1, 1, 1 };
  690. for (uint32_t common_pattern = 0; common_pattern < TOTAL_ASTC_BC7_COMMON_PARTITIONS3; common_pattern++)
  691. {
  692. const uint32_t bc7_pattern = g_astc_bc7_common_partitions3[common_pattern].m_bc7;
  693. const uint8_t* pPartition = &g_bc7_partition3[bc7_pattern * 16];
  694. color_quad_u8 subset_colors[3][16];
  695. uint32_t subset_total_colors[3] = { 0, 0 };
  696. for (uint32_t index = 0; index < 16; index++)
  697. subset_colors[pPartition[index]][subset_total_colors[pPartition[index]]++] = ((const color_quad_u8*)block)[index];
  698. uint64_t total_subset_err = 0;
  699. for (uint32_t subset = 0; (subset < 3) && (total_subset_err < best_err); subset++)
  700. total_subset_err += color_cell_compression_est_astc(4, 3, g_bc7_weights2, subset_total_colors[subset], &subset_colors[subset][0], best_err, weights);
  701. if (total_subset_err < best_err)
  702. {
  703. best_err = total_subset_err;
  704. best_common_pattern = common_pattern;
  705. }
  706. }
  707. first_common_pattern = best_common_pattern;
  708. last_common_pattern = best_common_pattern + 1;
  709. }
  710. for (uint32_t common_pattern = first_common_pattern; common_pattern < last_common_pattern; common_pattern++)
  711. {
  712. const uint32_t endpoint_range = 7;
  713. const uint32_t bc7_pattern = g_astc_bc7_common_partitions3[common_pattern].m_bc7;
  714. color_rgba part_pixels[3][16];
  715. uint32_t part_pixel_index[4][4];
  716. uint32_t num_part_pixels[3] = { 0, 0, 0 };
  717. for (uint32_t y = 0; y < 4; y++)
  718. {
  719. for (uint32_t x = 0; x < 4; x++)
  720. {
  721. const uint32_t bc7_part = g_bc7_partition3[16 * bc7_pattern + x + y * 4];
  722. part_pixel_index[y][x] = num_part_pixels[bc7_part];
  723. part_pixels[bc7_part][num_part_pixels[bc7_part]++] = block[y][x];
  724. }
  725. }
  726. color_cell_compressor_params ccell_params[3];
  727. color_cell_compressor_results ccell_results[3];
  728. uint8_t ccell_result_selectors[3][16];
  729. uint8_t ccell_result_selectors_temp[3][16];
  730. uint64_t total_part_err = 0;
  731. for (uint32_t bc7_part = 0; bc7_part < 3; bc7_part++)
  732. {
  733. memset(&ccell_params[bc7_part], 0, sizeof(ccell_params[bc7_part]));
  734. ccell_params[bc7_part].m_num_pixels = num_part_pixels[bc7_part];
  735. ccell_params[bc7_part].m_pPixels = (color_quad_u8*)&part_pixels[bc7_part][0];
  736. ccell_params[bc7_part].m_num_selector_weights = 4;
  737. ccell_params[bc7_part].m_pSelector_weights = g_bc7_weights2;
  738. ccell_params[bc7_part].m_pSelector_weightsx = (const bc7enc_vec4F*)g_bc7_weights2x;
  739. ccell_params[bc7_part].m_astc_endpoint_range = endpoint_range;
  740. ccell_params[bc7_part].m_weights[0] = 1;
  741. ccell_params[bc7_part].m_weights[1] = 1;
  742. ccell_params[bc7_part].m_weights[2] = 1;
  743. ccell_params[bc7_part].m_weights[3] = 1;
  744. memset(&ccell_results[bc7_part], 0, sizeof(ccell_results[bc7_part]));
  745. ccell_results[bc7_part].m_pSelectors = &ccell_result_selectors[bc7_part][0];
  746. ccell_results[bc7_part].m_pSelectors_temp = &ccell_result_selectors_temp[bc7_part][0];
  747. uint64_t part_err = color_cell_compression(255, &ccell_params[bc7_part], &ccell_results[bc7_part], &comp_params);
  748. total_part_err += part_err;
  749. } // part
  750. {
  751. // ASTC
  752. astc_block_desc astc_results;
  753. memset(&astc_results, 0, sizeof(astc_results));
  754. astc_results.m_dual_plane = false;
  755. astc_results.m_weight_range = 2;
  756. astc_results.m_ccs = 0;
  757. astc_results.m_subsets = 3;
  758. astc_results.m_partition_seed = g_astc_bc7_common_partitions3[common_pattern].m_astc;
  759. astc_results.m_cem = 8;
  760. uint32_t astc_to_bc7_part[3]; // converts ASTC to BC7 partition index
  761. const uint32_t perm = g_astc_bc7_common_partitions3[common_pattern].m_astc_to_bc7_perm;
  762. astc_to_bc7_part[0] = g_astc_to_bc7_partition_index_perm_tables[perm][0];
  763. astc_to_bc7_part[1] = g_astc_to_bc7_partition_index_perm_tables[perm][1];
  764. astc_to_bc7_part[2] = g_astc_to_bc7_partition_index_perm_tables[perm][2];
  765. bool invert_astc_part[3] = { false, false, false };
  766. for (uint32_t astc_part = 0; astc_part < 3; astc_part++)
  767. {
  768. uint8_t* pEndpoints = &astc_results.m_endpoints[6 * astc_part];
  769. pEndpoints[0] = ccell_results[astc_to_bc7_part[astc_part]].m_astc_low_endpoint.m_c[0];
  770. pEndpoints[1] = ccell_results[astc_to_bc7_part[astc_part]].m_astc_high_endpoint.m_c[0];
  771. pEndpoints[2] = ccell_results[astc_to_bc7_part[astc_part]].m_astc_low_endpoint.m_c[1];
  772. pEndpoints[3] = ccell_results[astc_to_bc7_part[astc_part]].m_astc_high_endpoint.m_c[1];
  773. pEndpoints[4] = ccell_results[astc_to_bc7_part[astc_part]].m_astc_low_endpoint.m_c[2];
  774. pEndpoints[5] = ccell_results[astc_to_bc7_part[astc_part]].m_astc_high_endpoint.m_c[2];
  775. int s0 = g_astc_unquant[endpoint_range][pEndpoints[0]].m_unquant + g_astc_unquant[endpoint_range][pEndpoints[2]].m_unquant + g_astc_unquant[endpoint_range][pEndpoints[4]].m_unquant;
  776. int s1 = g_astc_unquant[endpoint_range][pEndpoints[1]].m_unquant + g_astc_unquant[endpoint_range][pEndpoints[3]].m_unquant + g_astc_unquant[endpoint_range][pEndpoints[5]].m_unquant;
  777. if (s1 < s0)
  778. {
  779. std::swap(pEndpoints[0], pEndpoints[1]);
  780. std::swap(pEndpoints[2], pEndpoints[3]);
  781. std::swap(pEndpoints[4], pEndpoints[5]);
  782. invert_astc_part[astc_part] = true;
  783. }
  784. }
  785. for (uint32_t y = 0; y < 4; y++)
  786. {
  787. for (uint32_t x = 0; x < 4; x++)
  788. {
  789. const uint32_t bc7_part = g_bc7_partition3[16 * bc7_pattern + x + y * 4];
  790. astc_results.m_weights[x + y * 4] = ccell_result_selectors[bc7_part][part_pixel_index[y][x]];
  791. uint32_t astc_part = 0;
  792. for (uint32_t i = 0; i < 3; i++)
  793. {
  794. if (astc_to_bc7_part[i] == bc7_part)
  795. {
  796. astc_part = i;
  797. break;
  798. }
  799. }
  800. if (invert_astc_part[astc_part])
  801. astc_results.m_weights[x + y * 4] = 3 - astc_results.m_weights[x + y * 4];
  802. }
  803. }
  804. assert(total_results < MAX_ENCODE_RESULTS);
  805. if (total_results < MAX_ENCODE_RESULTS)
  806. {
  807. pResults[total_results].m_uastc_mode = 3;
  808. pResults[total_results].m_common_pattern = common_pattern;
  809. pResults[total_results].m_astc = astc_results;
  810. pResults[total_results].m_astc_err = total_part_err;
  811. total_results++;
  812. }
  813. }
  814. } // common_pattern
  815. }
  816. // MODE 4
  817. // DualPlane: 0, WeightRange: 2 (4), Subsets: 2, CEM: 8 (RGB Direct ), EndpointRange: 12 (40) MODE3
  818. static void astc_mode4(const color_rgba block[4][4], uastc_encode_results* pResults, uint32_t& total_results, bc7enc_compress_block_params& comp_params, bool estimate_partition)
  819. {
  820. //const uint32_t weight_range = 2;
  821. const uint32_t endpoint_range = 12;
  822. uint32_t first_common_pattern = 0;
  823. uint32_t last_common_pattern = TOTAL_ASTC_BC7_COMMON_PARTITIONS2;
  824. if (estimate_partition)
  825. {
  826. const uint32_t weights[4] = { 1, 1, 1, 1 };
  827. first_common_pattern = estimate_partition2(4, 3, g_bc7_weights2, block, weights);
  828. last_common_pattern = first_common_pattern + 1;
  829. }
  830. for (uint32_t common_pattern = first_common_pattern; common_pattern < last_common_pattern; common_pattern++)
  831. {
  832. const uint32_t bc7_pattern = g_astc_bc7_common_partitions2[common_pattern].m_bc7;
  833. color_rgba part_pixels[2][16];
  834. uint32_t part_pixel_index[4][4];
  835. uint32_t num_part_pixels[2] = { 0, 0 };
  836. for (uint32_t y = 0; y < 4; y++)
  837. {
  838. for (uint32_t x = 0; x < 4; x++)
  839. {
  840. const uint32_t part = g_bc7_partition2[16 * bc7_pattern + x + y * 4];
  841. part_pixel_index[y][x] = num_part_pixels[part];
  842. part_pixels[part][num_part_pixels[part]++] = block[y][x];
  843. }
  844. }
  845. color_cell_compressor_params ccell_params[2];
  846. color_cell_compressor_results ccell_results[2];
  847. uint8_t ccell_result_selectors[2][16];
  848. uint8_t ccell_result_selectors_temp[2][16];
  849. uint64_t total_part_err = 0;
  850. for (uint32_t part = 0; part < 2; part++)
  851. {
  852. memset(&ccell_params[part], 0, sizeof(ccell_params[part]));
  853. ccell_params[part].m_num_pixels = num_part_pixels[part];
  854. ccell_params[part].m_pPixels = (color_quad_u8*)&part_pixels[part][0];
  855. ccell_params[part].m_num_selector_weights = 4;
  856. ccell_params[part].m_pSelector_weights = g_bc7_weights2;
  857. ccell_params[part].m_pSelector_weightsx = (const bc7enc_vec4F*)g_bc7_weights2x;
  858. ccell_params[part].m_astc_endpoint_range = endpoint_range;
  859. ccell_params[part].m_weights[0] = 1;
  860. ccell_params[part].m_weights[1] = 1;
  861. ccell_params[part].m_weights[2] = 1;
  862. ccell_params[part].m_weights[3] = 1;
  863. memset(&ccell_results[part], 0, sizeof(ccell_results[part]));
  864. ccell_results[part].m_pSelectors = &ccell_result_selectors[part][0];
  865. ccell_results[part].m_pSelectors_temp = &ccell_result_selectors_temp[part][0];
  866. uint64_t part_err = color_cell_compression(255, &ccell_params[part], &ccell_results[part], &comp_params);
  867. total_part_err += part_err;
  868. } // part
  869. // ASTC
  870. astc_block_desc astc_results;
  871. memset(&astc_results, 0, sizeof(astc_results));
  872. astc_results.m_dual_plane = false;
  873. astc_results.m_weight_range = 2;
  874. astc_results.m_ccs = 0;
  875. astc_results.m_subsets = 2;
  876. astc_results.m_partition_seed = g_astc_bc7_common_partitions2[common_pattern].m_astc;
  877. astc_results.m_cem = 8;
  878. uint32_t p0 = 0;
  879. uint32_t p1 = 1;
  880. if (g_astc_bc7_common_partitions2[common_pattern].m_invert)
  881. std::swap(p0, p1);
  882. astc_results.m_endpoints[0] = ccell_results[p0].m_astc_low_endpoint.m_c[0];
  883. astc_results.m_endpoints[1] = ccell_results[p0].m_astc_high_endpoint.m_c[0];
  884. astc_results.m_endpoints[2] = ccell_results[p0].m_astc_low_endpoint.m_c[1];
  885. astc_results.m_endpoints[3] = ccell_results[p0].m_astc_high_endpoint.m_c[1];
  886. astc_results.m_endpoints[4] = ccell_results[p0].m_astc_low_endpoint.m_c[2];
  887. astc_results.m_endpoints[5] = ccell_results[p0].m_astc_high_endpoint.m_c[2];
  888. bool invert[2] = { false, false };
  889. int s0 = g_astc_unquant[endpoint_range][astc_results.m_endpoints[0]].m_unquant + g_astc_unquant[endpoint_range][astc_results.m_endpoints[2]].m_unquant + g_astc_unquant[endpoint_range][astc_results.m_endpoints[4]].m_unquant;
  890. int s1 = g_astc_unquant[endpoint_range][astc_results.m_endpoints[1]].m_unquant + g_astc_unquant[endpoint_range][astc_results.m_endpoints[3]].m_unquant + g_astc_unquant[endpoint_range][astc_results.m_endpoints[5]].m_unquant;
  891. if (s1 < s0)
  892. {
  893. std::swap(astc_results.m_endpoints[0], astc_results.m_endpoints[1]);
  894. std::swap(astc_results.m_endpoints[2], astc_results.m_endpoints[3]);
  895. std::swap(astc_results.m_endpoints[4], astc_results.m_endpoints[5]);
  896. invert[0] = true;
  897. }
  898. astc_results.m_endpoints[6] = ccell_results[p1].m_astc_low_endpoint.m_c[0];
  899. astc_results.m_endpoints[7] = ccell_results[p1].m_astc_high_endpoint.m_c[0];
  900. astc_results.m_endpoints[8] = ccell_results[p1].m_astc_low_endpoint.m_c[1];
  901. astc_results.m_endpoints[9] = ccell_results[p1].m_astc_high_endpoint.m_c[1];
  902. astc_results.m_endpoints[10] = ccell_results[p1].m_astc_low_endpoint.m_c[2];
  903. astc_results.m_endpoints[11] = ccell_results[p1].m_astc_high_endpoint.m_c[2];
  904. s0 = g_astc_unquant[endpoint_range][astc_results.m_endpoints[0 + 6]].m_unquant + g_astc_unquant[endpoint_range][astc_results.m_endpoints[2 + 6]].m_unquant + g_astc_unquant[endpoint_range][astc_results.m_endpoints[4 + 6]].m_unquant;
  905. s1 = g_astc_unquant[endpoint_range][astc_results.m_endpoints[1 + 6]].m_unquant + g_astc_unquant[endpoint_range][astc_results.m_endpoints[3 + 6]].m_unquant + g_astc_unquant[endpoint_range][astc_results.m_endpoints[5 + 6]].m_unquant;
  906. if (s1 < s0)
  907. {
  908. std::swap(astc_results.m_endpoints[0 + 6], astc_results.m_endpoints[1 + 6]);
  909. std::swap(astc_results.m_endpoints[2 + 6], astc_results.m_endpoints[3 + 6]);
  910. std::swap(astc_results.m_endpoints[4 + 6], astc_results.m_endpoints[5 + 6]);
  911. invert[1] = true;
  912. }
  913. for (uint32_t y = 0; y < 4; y++)
  914. {
  915. for (uint32_t x = 0; x < 4; x++)
  916. {
  917. const uint32_t bc7_part = g_bc7_partition2[16 * bc7_pattern + x + y * 4];
  918. astc_results.m_weights[x + y * 4] = ccell_result_selectors[bc7_part][part_pixel_index[y][x]];
  919. uint32_t astc_part = bc7_part;
  920. if (g_astc_bc7_common_partitions2[common_pattern].m_invert)
  921. astc_part = 1 - astc_part;
  922. if (invert[astc_part])
  923. astc_results.m_weights[x + y * 4] = 3 - astc_results.m_weights[x + y * 4];
  924. }
  925. }
  926. assert(total_results < MAX_ENCODE_RESULTS);
  927. if (total_results < MAX_ENCODE_RESULTS)
  928. {
  929. pResults[total_results].m_uastc_mode = 4;
  930. pResults[total_results].m_common_pattern = common_pattern;
  931. pResults[total_results].m_astc = astc_results;
  932. pResults[total_results].m_astc_err = total_part_err;
  933. total_results++;
  934. }
  935. } // common_pattern
  936. }
  937. // MODE 5
  938. // DualPlane: 0, WeightRange: 5 (8), Subsets: 1, CEM: 8 (RGB Direct ), EndpointRange: 20 (256) BC7 MODE 6 (or MODE 1 1-subset)
  939. static void astc_mode5(const color_rgba block[4][4], uastc_encode_results* pResults, uint32_t& total_results, bc7enc_compress_block_params& comp_params)
  940. {
  941. const uint32_t weight_range = 5;
  942. const uint32_t endpoint_range = 20;
  943. color_cell_compressor_params ccell_params;
  944. memset(&ccell_params, 0, sizeof(ccell_params));
  945. ccell_params.m_num_pixels = 16;
  946. ccell_params.m_pPixels = (color_quad_u8*)&block[0][0];
  947. ccell_params.m_num_selector_weights = 8;
  948. ccell_params.m_pSelector_weights = g_bc7_weights3;
  949. ccell_params.m_pSelector_weightsx = (const bc7enc_vec4F*)g_bc7_weights3x;
  950. ccell_params.m_astc_endpoint_range = endpoint_range;
  951. ccell_params.m_weights[0] = 1;
  952. ccell_params.m_weights[1] = 1;
  953. ccell_params.m_weights[2] = 1;
  954. ccell_params.m_weights[3] = 1;
  955. color_cell_compressor_results ccell_results;
  956. uint8_t ccell_result_selectors[16];
  957. uint8_t ccell_result_selectors_temp[16];
  958. memset(&ccell_results, 0, sizeof(ccell_results));
  959. ccell_results.m_pSelectors = &ccell_result_selectors[0];
  960. ccell_results.m_pSelectors_temp = &ccell_result_selectors_temp[0];
  961. uint64_t part_err = color_cell_compression(255, &ccell_params, &ccell_results, &comp_params);
  962. // ASTC
  963. astc_block_desc blk;
  964. memset(&blk, 0, sizeof(blk));
  965. blk.m_dual_plane = false;
  966. blk.m_weight_range = weight_range;
  967. blk.m_ccs = 0;
  968. blk.m_subsets = 1;
  969. blk.m_partition_seed = 0;
  970. blk.m_cem = 8;
  971. blk.m_endpoints[0] = ccell_results.m_astc_low_endpoint.m_c[0];
  972. blk.m_endpoints[1] = ccell_results.m_astc_high_endpoint.m_c[0];
  973. blk.m_endpoints[2] = ccell_results.m_astc_low_endpoint.m_c[1];
  974. blk.m_endpoints[3] = ccell_results.m_astc_high_endpoint.m_c[1];
  975. blk.m_endpoints[4] = ccell_results.m_astc_low_endpoint.m_c[2];
  976. blk.m_endpoints[5] = ccell_results.m_astc_high_endpoint.m_c[2];
  977. bool invert = false;
  978. int s0 = g_astc_unquant[endpoint_range][blk.m_endpoints[0]].m_unquant + g_astc_unquant[endpoint_range][blk.m_endpoints[2]].m_unquant + g_astc_unquant[endpoint_range][blk.m_endpoints[4]].m_unquant;
  979. int s1 = g_astc_unquant[endpoint_range][blk.m_endpoints[1]].m_unquant + g_astc_unquant[endpoint_range][blk.m_endpoints[3]].m_unquant + g_astc_unquant[endpoint_range][blk.m_endpoints[5]].m_unquant;
  980. if (s1 < s0)
  981. {
  982. std::swap(blk.m_endpoints[0], blk.m_endpoints[1]);
  983. std::swap(blk.m_endpoints[2], blk.m_endpoints[3]);
  984. std::swap(blk.m_endpoints[4], blk.m_endpoints[5]);
  985. invert = true;
  986. }
  987. for (uint32_t y = 0; y < 4; y++)
  988. {
  989. for (uint32_t x = 0; x < 4; x++)
  990. {
  991. blk.m_weights[x + y * 4] = ccell_result_selectors[x + y * 4];
  992. if (invert)
  993. blk.m_weights[x + y * 4] = 7 - blk.m_weights[x + y * 4];
  994. }
  995. }
  996. assert(total_results < MAX_ENCODE_RESULTS);
  997. if (total_results < MAX_ENCODE_RESULTS)
  998. {
  999. pResults[total_results].m_uastc_mode = 5;
  1000. pResults[total_results].m_common_pattern = 0;
  1001. pResults[total_results].m_astc = blk;
  1002. pResults[total_results].m_astc_err = part_err;
  1003. total_results++;
  1004. }
  1005. }
  1006. // MODE 6
  1007. // DualPlane: 1, WeightRange: 2 (4), Subsets: 1, CEM: 8 (RGB Direct ), EndpointRange: 18 (160) BC7 MODE5
  1008. static void astc_mode6(const color_rgba block[4][4], uastc_encode_results* pResults, uint32_t& total_results, bc7enc_compress_block_params& comp_params)
  1009. {
  1010. for (uint32_t rot_comp = 0; rot_comp < 3; rot_comp++)
  1011. {
  1012. const uint32_t weight_range = 2;
  1013. const uint32_t endpoint_range = 18;
  1014. color_quad_u8 block_rgb[16];
  1015. color_quad_u8 block_a[16];
  1016. for (uint32_t i = 0; i < 16; i++)
  1017. {
  1018. block_rgb[i] = ((color_quad_u8*)&block[0][0])[i];
  1019. block_a[i] = block_rgb[i];
  1020. uint8_t c = block_a[i].m_c[rot_comp];
  1021. block_a[i].m_c[0] = c;
  1022. block_a[i].m_c[1] = c;
  1023. block_a[i].m_c[2] = c;
  1024. block_a[i].m_c[3] = 255;
  1025. block_rgb[i].m_c[rot_comp] = 255;
  1026. }
  1027. uint8_t ccell_result_selectors_temp[16];
  1028. color_cell_compressor_params ccell_params_rgb;
  1029. memset(&ccell_params_rgb, 0, sizeof(ccell_params_rgb));
  1030. ccell_params_rgb.m_num_pixels = 16;
  1031. ccell_params_rgb.m_pPixels = block_rgb;
  1032. ccell_params_rgb.m_num_selector_weights = 4;
  1033. ccell_params_rgb.m_pSelector_weights = g_bc7_weights2;
  1034. ccell_params_rgb.m_pSelector_weightsx = (const bc7enc_vec4F*)g_bc7_weights2x;
  1035. ccell_params_rgb.m_astc_endpoint_range = endpoint_range;
  1036. ccell_params_rgb.m_weights[0] = 1;
  1037. ccell_params_rgb.m_weights[1] = 1;
  1038. ccell_params_rgb.m_weights[2] = 1;
  1039. ccell_params_rgb.m_weights[3] = 1;
  1040. color_cell_compressor_results ccell_results_rgb;
  1041. uint8_t ccell_result_selectors_rgb[16];
  1042. memset(&ccell_results_rgb, 0, sizeof(ccell_results_rgb));
  1043. ccell_results_rgb.m_pSelectors = &ccell_result_selectors_rgb[0];
  1044. ccell_results_rgb.m_pSelectors_temp = &ccell_result_selectors_temp[0];
  1045. uint64_t part_err_rgb = color_cell_compression(255, &ccell_params_rgb, &ccell_results_rgb, &comp_params);
  1046. color_cell_compressor_params ccell_params_a;
  1047. memset(&ccell_params_a, 0, sizeof(ccell_params_a));
  1048. ccell_params_a.m_num_pixels = 16;
  1049. ccell_params_a.m_pPixels = block_a;
  1050. ccell_params_a.m_num_selector_weights = 4;
  1051. ccell_params_a.m_pSelector_weights = g_bc7_weights2;
  1052. ccell_params_a.m_pSelector_weightsx = (const bc7enc_vec4F*)g_bc7_weights2x;
  1053. ccell_params_a.m_astc_endpoint_range = endpoint_range;
  1054. ccell_params_a.m_weights[0] = 1;
  1055. ccell_params_a.m_weights[1] = 1;
  1056. ccell_params_a.m_weights[2] = 1;
  1057. ccell_params_a.m_weights[3] = 1;
  1058. color_cell_compressor_results ccell_results_a;
  1059. uint8_t ccell_result_selectors_a[16];
  1060. memset(&ccell_results_a, 0, sizeof(ccell_results_a));
  1061. ccell_results_a.m_pSelectors = &ccell_result_selectors_a[0];
  1062. ccell_results_a.m_pSelectors_temp = &ccell_result_selectors_temp[0];
  1063. uint64_t part_err_a = color_cell_compression(255, &ccell_params_a, &ccell_results_a, &comp_params) / 3;
  1064. uint64_t total_err = part_err_rgb + part_err_a;
  1065. // ASTC
  1066. astc_block_desc blk;
  1067. memset(&blk, 0, sizeof(blk));
  1068. blk.m_dual_plane = true;
  1069. blk.m_weight_range = weight_range;
  1070. blk.m_ccs = rot_comp;
  1071. blk.m_subsets = 1;
  1072. blk.m_partition_seed = 0;
  1073. blk.m_cem = 8;
  1074. blk.m_endpoints[0] = (rot_comp == 0 ? ccell_results_a : ccell_results_rgb).m_astc_low_endpoint.m_c[0];
  1075. blk.m_endpoints[1] = (rot_comp == 0 ? ccell_results_a : ccell_results_rgb).m_astc_high_endpoint.m_c[0];
  1076. blk.m_endpoints[2] = (rot_comp == 1 ? ccell_results_a : ccell_results_rgb).m_astc_low_endpoint.m_c[1];
  1077. blk.m_endpoints[3] = (rot_comp == 1 ? ccell_results_a : ccell_results_rgb).m_astc_high_endpoint.m_c[1];
  1078. blk.m_endpoints[4] = (rot_comp == 2 ? ccell_results_a : ccell_results_rgb).m_astc_low_endpoint.m_c[2];
  1079. blk.m_endpoints[5] = (rot_comp == 2 ? ccell_results_a : ccell_results_rgb).m_astc_high_endpoint.m_c[2];
  1080. bool invert = false;
  1081. int s0 = g_astc_unquant[endpoint_range][blk.m_endpoints[0]].m_unquant + g_astc_unquant[endpoint_range][blk.m_endpoints[2]].m_unquant + g_astc_unquant[endpoint_range][blk.m_endpoints[4]].m_unquant;
  1082. int s1 = g_astc_unquant[endpoint_range][blk.m_endpoints[1]].m_unquant + g_astc_unquant[endpoint_range][blk.m_endpoints[3]].m_unquant + g_astc_unquant[endpoint_range][blk.m_endpoints[5]].m_unquant;
  1083. if (s1 < s0)
  1084. {
  1085. std::swap(blk.m_endpoints[0], blk.m_endpoints[1]);
  1086. std::swap(blk.m_endpoints[2], blk.m_endpoints[3]);
  1087. std::swap(blk.m_endpoints[4], blk.m_endpoints[5]);
  1088. invert = true;
  1089. }
  1090. for (uint32_t y = 0; y < 4; y++)
  1091. {
  1092. for (uint32_t x = 0; x < 4; x++)
  1093. {
  1094. uint32_t rgb_index = ccell_result_selectors_rgb[x + y * 4];
  1095. uint32_t a_index = ccell_result_selectors_a[x + y * 4];
  1096. if (invert)
  1097. {
  1098. rgb_index = 3 - rgb_index;
  1099. a_index = 3 - a_index;
  1100. }
  1101. blk.m_weights[(x + y * 4) * 2 + 0] = (uint8_t)rgb_index;
  1102. blk.m_weights[(x + y * 4) * 2 + 1] = (uint8_t)a_index;
  1103. }
  1104. }
  1105. assert(total_results < MAX_ENCODE_RESULTS);
  1106. if (total_results < MAX_ENCODE_RESULTS)
  1107. {
  1108. pResults[total_results].m_uastc_mode = 6;
  1109. pResults[total_results].m_common_pattern = 0;
  1110. pResults[total_results].m_astc = blk;
  1111. pResults[total_results].m_astc_err = total_err;
  1112. total_results++;
  1113. }
  1114. } // rot_comp
  1115. }
  1116. // MODE 7 - 2 subset ASTC, 3 subset BC7
  1117. // DualPlane: 0, WeightRange: 2 (4), Subsets: 2, CEM: 8 (RGB Direct ), EndpointRange: 12 (40) MODE2
  1118. static void astc_mode7(const color_rgba block[4][4], uastc_encode_results* pResults, uint32_t& total_results, bc7enc_compress_block_params& comp_params, bool estimate_partition)
  1119. {
  1120. uint32_t first_common_pattern = 0;
  1121. uint32_t last_common_pattern = TOTAL_BC7_3_ASTC2_COMMON_PARTITIONS;
  1122. if (estimate_partition)
  1123. {
  1124. uint64_t best_err = UINT64_MAX;
  1125. uint32_t best_common_pattern = 0;
  1126. const uint32_t weights[4] = { 1, 1, 1, 1 };
  1127. for (uint32_t common_pattern = 0; common_pattern < TOTAL_BC7_3_ASTC2_COMMON_PARTITIONS; common_pattern++)
  1128. {
  1129. const uint8_t* pPartition = &g_bc7_3_astc2_patterns2[common_pattern][0];
  1130. #ifdef _DEBUG
  1131. const uint32_t astc_pattern = g_bc7_3_astc2_common_partitions[common_pattern].m_astc2;
  1132. const uint32_t bc7_pattern = g_bc7_3_astc2_common_partitions[common_pattern].m_bc73;
  1133. const uint32_t common_pattern_k = g_bc7_3_astc2_common_partitions[common_pattern].k;
  1134. for (uint32_t y = 0; y < 4; y++)
  1135. {
  1136. for (uint32_t x = 0; x < 4; x++)
  1137. {
  1138. const uint32_t astc_part = bc7_convert_partition_index_3_to_2(g_bc7_partition3[16 * bc7_pattern + x + y * 4], common_pattern_k);
  1139. assert((int)astc_part == astc_compute_texel_partition(astc_pattern, x, y, 0, 2, true));
  1140. assert(astc_part == pPartition[x + y * 4]);
  1141. }
  1142. }
  1143. #endif
  1144. color_quad_u8 subset_colors[2][16];
  1145. uint32_t subset_total_colors[2] = { 0, 0 };
  1146. for (uint32_t index = 0; index < 16; index++)
  1147. subset_colors[pPartition[index]][subset_total_colors[pPartition[index]]++] = ((const color_quad_u8*)block)[index];
  1148. uint64_t total_subset_err = 0;
  1149. for (uint32_t subset = 0; (subset < 2) && (total_subset_err < best_err); subset++)
  1150. total_subset_err += color_cell_compression_est_astc(4, 3, g_bc7_weights2, subset_total_colors[subset], &subset_colors[subset][0], best_err, weights);
  1151. if (total_subset_err < best_err)
  1152. {
  1153. best_err = total_subset_err;
  1154. best_common_pattern = common_pattern;
  1155. }
  1156. }
  1157. first_common_pattern = best_common_pattern;
  1158. last_common_pattern = best_common_pattern + 1;
  1159. }
  1160. //const uint32_t weight_range = 2;
  1161. const uint32_t endpoint_range = 12;
  1162. for (uint32_t common_pattern = first_common_pattern; common_pattern < last_common_pattern; common_pattern++)
  1163. {
  1164. const uint32_t astc_pattern = g_bc7_3_astc2_common_partitions[common_pattern].m_astc2;
  1165. const uint32_t bc7_pattern = g_bc7_3_astc2_common_partitions[common_pattern].m_bc73;
  1166. const uint32_t common_pattern_k = g_bc7_3_astc2_common_partitions[common_pattern].k;
  1167. color_rgba part_pixels[2][16];
  1168. uint32_t part_pixel_index[4][4];
  1169. uint32_t num_part_pixels[2] = { 0, 0 };
  1170. for (uint32_t y = 0; y < 4; y++)
  1171. {
  1172. for (uint32_t x = 0; x < 4; x++)
  1173. {
  1174. const uint32_t astc_part = bc7_convert_partition_index_3_to_2(g_bc7_partition3[16 * bc7_pattern + x + y * 4], common_pattern_k);
  1175. #ifdef _DEBUG
  1176. assert((int)astc_part == astc_compute_texel_partition(astc_pattern, x, y, 0, 2, true));
  1177. #endif
  1178. part_pixel_index[y][x] = num_part_pixels[astc_part];
  1179. part_pixels[astc_part][num_part_pixels[astc_part]++] = block[y][x];
  1180. }
  1181. }
  1182. color_cell_compressor_params ccell_params[2];
  1183. color_cell_compressor_results ccell_results[2];
  1184. uint8_t ccell_result_selectors[2][16];
  1185. uint8_t ccell_result_selectors_temp[2][16];
  1186. uint64_t total_part_err = 0;
  1187. for (uint32_t part = 0; part < 2; part++)
  1188. {
  1189. memset(&ccell_params[part], 0, sizeof(ccell_params[part]));
  1190. ccell_params[part].m_num_pixels = num_part_pixels[part];
  1191. ccell_params[part].m_pPixels = (color_quad_u8*)&part_pixels[part][0];
  1192. ccell_params[part].m_num_selector_weights = 4;
  1193. ccell_params[part].m_pSelector_weights = g_bc7_weights2;
  1194. ccell_params[part].m_pSelector_weightsx = (const bc7enc_vec4F*)g_bc7_weights2x;
  1195. ccell_params[part].m_astc_endpoint_range = endpoint_range;
  1196. ccell_params[part].m_weights[0] = 1;
  1197. ccell_params[part].m_weights[1] = 1;
  1198. ccell_params[part].m_weights[2] = 1;
  1199. ccell_params[part].m_weights[3] = 1;
  1200. memset(&ccell_results[part], 0, sizeof(ccell_results[part]));
  1201. ccell_results[part].m_pSelectors = &ccell_result_selectors[part][0];
  1202. ccell_results[part].m_pSelectors_temp = &ccell_result_selectors_temp[part][0];
  1203. uint64_t part_err = color_cell_compression(255, &ccell_params[part], &ccell_results[part], &comp_params);
  1204. total_part_err += part_err;
  1205. } // part
  1206. // ASTC
  1207. astc_block_desc blk;
  1208. memset(&blk, 0, sizeof(blk));
  1209. blk.m_dual_plane = false;
  1210. blk.m_weight_range = 2;
  1211. blk.m_ccs = 0;
  1212. blk.m_subsets = 2;
  1213. blk.m_partition_seed = astc_pattern;
  1214. blk.m_cem = 8;
  1215. const uint32_t p0 = 0;
  1216. const uint32_t p1 = 1;
  1217. blk.m_endpoints[0] = ccell_results[p0].m_astc_low_endpoint.m_c[0];
  1218. blk.m_endpoints[1] = ccell_results[p0].m_astc_high_endpoint.m_c[0];
  1219. blk.m_endpoints[2] = ccell_results[p0].m_astc_low_endpoint.m_c[1];
  1220. blk.m_endpoints[3] = ccell_results[p0].m_astc_high_endpoint.m_c[1];
  1221. blk.m_endpoints[4] = ccell_results[p0].m_astc_low_endpoint.m_c[2];
  1222. blk.m_endpoints[5] = ccell_results[p0].m_astc_high_endpoint.m_c[2];
  1223. bool invert[2] = { false, false };
  1224. int s0 = g_astc_unquant[endpoint_range][blk.m_endpoints[0]].m_unquant + g_astc_unquant[endpoint_range][blk.m_endpoints[2]].m_unquant + g_astc_unquant[endpoint_range][blk.m_endpoints[4]].m_unquant;
  1225. int s1 = g_astc_unquant[endpoint_range][blk.m_endpoints[1]].m_unquant + g_astc_unquant[endpoint_range][blk.m_endpoints[3]].m_unquant + g_astc_unquant[endpoint_range][blk.m_endpoints[5]].m_unquant;
  1226. if (s1 < s0)
  1227. {
  1228. std::swap(blk.m_endpoints[0], blk.m_endpoints[1]);
  1229. std::swap(blk.m_endpoints[2], blk.m_endpoints[3]);
  1230. std::swap(blk.m_endpoints[4], blk.m_endpoints[5]);
  1231. invert[0] = true;
  1232. }
  1233. blk.m_endpoints[6] = ccell_results[p1].m_astc_low_endpoint.m_c[0];
  1234. blk.m_endpoints[7] = ccell_results[p1].m_astc_high_endpoint.m_c[0];
  1235. blk.m_endpoints[8] = ccell_results[p1].m_astc_low_endpoint.m_c[1];
  1236. blk.m_endpoints[9] = ccell_results[p1].m_astc_high_endpoint.m_c[1];
  1237. blk.m_endpoints[10] = ccell_results[p1].m_astc_low_endpoint.m_c[2];
  1238. blk.m_endpoints[11] = ccell_results[p1].m_astc_high_endpoint.m_c[2];
  1239. s0 = g_astc_unquant[endpoint_range][blk.m_endpoints[0 + 6]].m_unquant + g_astc_unquant[endpoint_range][blk.m_endpoints[2 + 6]].m_unquant + g_astc_unquant[endpoint_range][blk.m_endpoints[4 + 6]].m_unquant;
  1240. s1 = g_astc_unquant[endpoint_range][blk.m_endpoints[1 + 6]].m_unquant + g_astc_unquant[endpoint_range][blk.m_endpoints[3 + 6]].m_unquant + g_astc_unquant[endpoint_range][blk.m_endpoints[5 + 6]].m_unquant;
  1241. if (s1 < s0)
  1242. {
  1243. std::swap(blk.m_endpoints[0 + 6], blk.m_endpoints[1 + 6]);
  1244. std::swap(blk.m_endpoints[2 + 6], blk.m_endpoints[3 + 6]);
  1245. std::swap(blk.m_endpoints[4 + 6], blk.m_endpoints[5 + 6]);
  1246. invert[1] = true;
  1247. }
  1248. for (uint32_t y = 0; y < 4; y++)
  1249. {
  1250. for (uint32_t x = 0; x < 4; x++)
  1251. {
  1252. const uint32_t astc_part = bc7_convert_partition_index_3_to_2(g_bc7_partition3[16 * bc7_pattern + x + y * 4], common_pattern_k);
  1253. blk.m_weights[x + y * 4] = ccell_result_selectors[astc_part][part_pixel_index[y][x]];
  1254. if (invert[astc_part])
  1255. blk.m_weights[x + y * 4] = 3 - blk.m_weights[x + y * 4];
  1256. }
  1257. }
  1258. assert(total_results < MAX_ENCODE_RESULTS);
  1259. if (total_results < MAX_ENCODE_RESULTS)
  1260. {
  1261. pResults[total_results].m_uastc_mode = 7;
  1262. pResults[total_results].m_common_pattern = common_pattern;
  1263. pResults[total_results].m_astc = blk;
  1264. pResults[total_results].m_astc_err = total_part_err;
  1265. total_results++;
  1266. }
  1267. } // common_pattern
  1268. }
  1269. static void estimate_partition2_list(uint32_t num_weights, uint32_t num_comps, const uint32_t* pWeights, const color_rgba block[4][4], uint32_t* pParts, uint32_t max_parts, const uint32_t weights[4])
  1270. {
  1271. assert(pWeights[0] == 0 && pWeights[num_weights - 1] == 64);
  1272. const uint32_t MAX_PARTS = 8;
  1273. assert(max_parts <= MAX_PARTS);
  1274. uint64_t part_error[MAX_PARTS];
  1275. memset(part_error, 0xFF, sizeof(part_error));
  1276. memset(pParts, 0, sizeof(pParts[0]) * max_parts);
  1277. for (uint32_t common_pattern = 0; common_pattern < TOTAL_ASTC_BC7_COMMON_PARTITIONS2; common_pattern++)
  1278. {
  1279. const uint32_t bc7_pattern = g_astc_bc7_common_partitions2[common_pattern].m_bc7;
  1280. const uint8_t* pPartition = &g_bc7_partition2[bc7_pattern * 16];
  1281. color_quad_u8 subset_colors[2][16];
  1282. uint32_t subset_total_colors[2] = { 0, 0 };
  1283. for (uint32_t index = 0; index < 16; index++)
  1284. subset_colors[pPartition[index]][subset_total_colors[pPartition[index]]++] = ((const color_quad_u8*)block)[index];
  1285. uint64_t total_subset_err = 0;
  1286. for (uint32_t subset = 0; subset < 2; subset++)
  1287. total_subset_err += color_cell_compression_est_astc(num_weights, num_comps, pWeights, subset_total_colors[subset], &subset_colors[subset][0], UINT64_MAX, weights);
  1288. for (int i = 0; i < (int)max_parts; i++)
  1289. {
  1290. if (total_subset_err < part_error[i])
  1291. {
  1292. for (int j = max_parts - 1; j > i; --j)
  1293. {
  1294. pParts[j] = pParts[j - 1];
  1295. part_error[j] = part_error[j - 1];
  1296. }
  1297. pParts[i] = common_pattern;
  1298. part_error[i] = total_subset_err;
  1299. break;
  1300. }
  1301. }
  1302. }
  1303. #ifdef _DEBUG
  1304. for (uint32_t i = 0; i < max_parts - 1; i++)
  1305. {
  1306. assert(part_error[i] <= part_error[i + 1]);
  1307. }
  1308. #endif
  1309. }
  1310. // 9. DualPlane: 0, WeightRange: 2 (4), Subsets: 2, CEM: 12 (RGBA Direct), EndpointRange: 8 (16) - BC7 MODE 7
  1311. // 16. DualPlane: 0, WeightRange : 2 (4), Subsets : 2, CEM: 4 (LA Direct), EndpointRange : 20 (256) - BC7 MODE 7
  1312. static void astc_mode9_or_16(uint32_t mode, const color_rgba source_block[4][4], uastc_encode_results* pResults, uint32_t& total_results, bc7enc_compress_block_params& comp_params, uint32_t estimate_partition_list_size)
  1313. {
  1314. assert(mode == 9 || mode == 16);
  1315. const color_rgba* pBlock = &source_block[0][0];
  1316. color_rgba temp_block[16];
  1317. if (mode == 16)
  1318. {
  1319. for (uint32_t i = 0; i < 16; i++)
  1320. {
  1321. if (mode == 16)
  1322. {
  1323. assert(pBlock[i].r == pBlock[i].g);
  1324. assert(pBlock[i].r == pBlock[i].b);
  1325. }
  1326. const uint32_t l = pBlock[i].r;
  1327. const uint32_t a = pBlock[i].a;
  1328. // Use (l,0,0,a) not (l,l,l,a) so both components are treated equally.
  1329. temp_block[i].set_noclamp_rgba(l, 0, 0, a);
  1330. }
  1331. pBlock = temp_block;
  1332. }
  1333. const uint32_t weights[4] = { 1, 1, 1, 1 };
  1334. //const uint32_t weight_range = 2;
  1335. const uint32_t endpoint_range = (mode == 16) ? 20 : 8;
  1336. uint32_t first_common_pattern = 0;
  1337. uint32_t last_common_pattern = TOTAL_ASTC_BC7_COMMON_PARTITIONS2;
  1338. bool use_part_list = false;
  1339. const uint32_t MAX_PARTS = 8;
  1340. uint32_t parts[MAX_PARTS];
  1341. if (estimate_partition_list_size == 1)
  1342. {
  1343. first_common_pattern = estimate_partition2(4, 4, g_bc7_weights2, (const color_rgba(*)[4])pBlock, weights);
  1344. last_common_pattern = first_common_pattern + 1;
  1345. }
  1346. else if (estimate_partition_list_size > 0)
  1347. {
  1348. assert(estimate_partition_list_size <= MAX_PARTS);
  1349. estimate_partition_list_size = basisu::minimum(estimate_partition_list_size, MAX_PARTS);
  1350. estimate_partition2_list(4, 4, g_bc7_weights2, (const color_rgba(*)[4])pBlock, parts, estimate_partition_list_size, weights);
  1351. first_common_pattern = 0;
  1352. last_common_pattern = estimate_partition_list_size;
  1353. use_part_list = true;
  1354. #ifdef _DEBUG
  1355. assert(parts[0] == estimate_partition2(4, 4, g_bc7_weights2, (const color_rgba(*)[4])pBlock, weights));
  1356. #endif
  1357. }
  1358. for (uint32_t common_pattern_iter = first_common_pattern; common_pattern_iter < last_common_pattern; common_pattern_iter++)
  1359. {
  1360. const uint32_t common_pattern = use_part_list ? parts[common_pattern_iter] : common_pattern_iter;
  1361. const uint32_t bc7_pattern = g_astc_bc7_common_partitions2[common_pattern].m_bc7;
  1362. color_rgba part_pixels[2][16];
  1363. uint32_t part_pixel_index[4][4];
  1364. uint32_t num_part_pixels[2] = { 0, 0 };
  1365. for (uint32_t y = 0; y < 4; y++)
  1366. {
  1367. for (uint32_t x = 0; x < 4; x++)
  1368. {
  1369. const uint32_t part = g_bc7_partition2[16 * bc7_pattern + x + y * 4];
  1370. part_pixel_index[y][x] = num_part_pixels[part];
  1371. part_pixels[part][num_part_pixels[part]++] = pBlock[y * 4 + x];
  1372. }
  1373. }
  1374. color_cell_compressor_params ccell_params[2];
  1375. color_cell_compressor_results ccell_results[2];
  1376. uint8_t ccell_result_selectors[2][16];
  1377. uint8_t ccell_result_selectors_temp[2][16];
  1378. uint64_t total_err = 0;
  1379. for (uint32_t subset = 0; subset < 2; subset++)
  1380. {
  1381. memset(&ccell_params[subset], 0, sizeof(ccell_params[subset]));
  1382. ccell_params[subset].m_num_pixels = num_part_pixels[subset];
  1383. ccell_params[subset].m_pPixels = (color_quad_u8*)&part_pixels[subset][0];
  1384. ccell_params[subset].m_num_selector_weights = 4;
  1385. ccell_params[subset].m_pSelector_weights = g_bc7_weights2;
  1386. ccell_params[subset].m_pSelector_weightsx = (const bc7enc_vec4F*)g_bc7_weights2x;
  1387. ccell_params[subset].m_astc_endpoint_range = endpoint_range;
  1388. ccell_params[subset].m_weights[0] = weights[0];
  1389. ccell_params[subset].m_weights[1] = weights[1];
  1390. ccell_params[subset].m_weights[2] = weights[2];
  1391. ccell_params[subset].m_weights[3] = weights[3];
  1392. ccell_params[subset].m_has_alpha = true;
  1393. memset(&ccell_results[subset], 0, sizeof(ccell_results[subset]));
  1394. ccell_results[subset].m_pSelectors = &ccell_result_selectors[subset][0];
  1395. ccell_results[subset].m_pSelectors_temp = &ccell_result_selectors_temp[subset][0];
  1396. uint64_t subset_err = color_cell_compression(255, &ccell_params[subset], &ccell_results[subset], &comp_params);
  1397. if (mode == 16)
  1398. {
  1399. color_rgba colors[4];
  1400. for (uint32_t c = 0; c < 4; c++)
  1401. {
  1402. colors[0].m_comps[c] = g_astc_unquant[endpoint_range][ccell_results[subset].m_astc_low_endpoint.m_c[(c < 3) ? 0 : 3]].m_unquant;
  1403. colors[3].m_comps[c] = g_astc_unquant[endpoint_range][ccell_results[subset].m_astc_high_endpoint.m_c[(c < 3) ? 0 : 3]].m_unquant;
  1404. }
  1405. for (uint32_t i = 1; i < 4 - 1; i++)
  1406. for (uint32_t c = 0; c < 4; c++)
  1407. colors[i].m_comps[c] = (uint8_t)astc_interpolate(colors[0].m_comps[c], colors[3].m_comps[c], g_bc7_weights2[i], false);
  1408. for (uint32_t p = 0; p < ccell_params[subset].m_num_pixels; p++)
  1409. {
  1410. color_rgba orig_pix(part_pixels[subset][p]);
  1411. orig_pix.g = orig_pix.r;
  1412. orig_pix.b = orig_pix.r;
  1413. total_err += color_distance_la(orig_pix, colors[ccell_result_selectors[subset][p]]);
  1414. }
  1415. }
  1416. else
  1417. {
  1418. total_err += subset_err;
  1419. }
  1420. } // subset
  1421. // ASTC
  1422. astc_block_desc astc_results;
  1423. memset(&astc_results, 0, sizeof(astc_results));
  1424. astc_results.m_dual_plane = false;
  1425. astc_results.m_weight_range = 2;
  1426. astc_results.m_ccs = 0;
  1427. astc_results.m_subsets = 2;
  1428. astc_results.m_partition_seed = g_astc_bc7_common_partitions2[common_pattern].m_astc;
  1429. astc_results.m_cem = (mode == 16) ? 4 : 12;
  1430. uint32_t part[2] = { 0, 1 };
  1431. if (g_astc_bc7_common_partitions2[common_pattern].m_invert)
  1432. std::swap(part[0], part[1]);
  1433. bool invert[2] = { false, false };
  1434. for (uint32_t p = 0; p < 2; p++)
  1435. {
  1436. if (mode == 16)
  1437. {
  1438. astc_results.m_endpoints[p * 4 + 0] = ccell_results[part[p]].m_astc_low_endpoint.m_c[0];
  1439. astc_results.m_endpoints[p * 4 + 1] = ccell_results[part[p]].m_astc_high_endpoint.m_c[0];
  1440. astc_results.m_endpoints[p * 4 + 2] = ccell_results[part[p]].m_astc_low_endpoint.m_c[3];
  1441. astc_results.m_endpoints[p * 4 + 3] = ccell_results[part[p]].m_astc_high_endpoint.m_c[3];
  1442. }
  1443. else
  1444. {
  1445. for (uint32_t c = 0; c < 4; c++)
  1446. {
  1447. astc_results.m_endpoints[p * 8 + c * 2] = ccell_results[part[p]].m_astc_low_endpoint.m_c[c];
  1448. astc_results.m_endpoints[p * 8 + c * 2 + 1] = ccell_results[part[p]].m_astc_high_endpoint.m_c[c];
  1449. }
  1450. int s0 = g_astc_unquant[endpoint_range][astc_results.m_endpoints[p * 8 + 0]].m_unquant +
  1451. g_astc_unquant[endpoint_range][astc_results.m_endpoints[p * 8 + 2]].m_unquant +
  1452. g_astc_unquant[endpoint_range][astc_results.m_endpoints[p * 8 + 4]].m_unquant;
  1453. int s1 = g_astc_unquant[endpoint_range][astc_results.m_endpoints[p * 8 + 1]].m_unquant +
  1454. g_astc_unquant[endpoint_range][astc_results.m_endpoints[p * 8 + 3]].m_unquant +
  1455. g_astc_unquant[endpoint_range][astc_results.m_endpoints[p * 8 + 5]].m_unquant;
  1456. if (s1 < s0)
  1457. {
  1458. std::swap(astc_results.m_endpoints[p * 8 + 0], astc_results.m_endpoints[p * 8 + 1]);
  1459. std::swap(astc_results.m_endpoints[p * 8 + 2], astc_results.m_endpoints[p * 8 + 3]);
  1460. std::swap(astc_results.m_endpoints[p * 8 + 4], astc_results.m_endpoints[p * 8 + 5]);
  1461. std::swap(astc_results.m_endpoints[p * 8 + 6], astc_results.m_endpoints[p * 8 + 7]);
  1462. invert[p] = true;
  1463. }
  1464. }
  1465. }
  1466. for (uint32_t y = 0; y < 4; y++)
  1467. {
  1468. for (uint32_t x = 0; x < 4; x++)
  1469. {
  1470. const uint32_t bc7_part = g_bc7_partition2[16 * bc7_pattern + x + y * 4];
  1471. astc_results.m_weights[x + y * 4] = ccell_result_selectors[bc7_part][part_pixel_index[y][x]];
  1472. uint32_t astc_part = bc7_part;
  1473. if (g_astc_bc7_common_partitions2[common_pattern].m_invert)
  1474. astc_part = 1 - astc_part;
  1475. if (invert[astc_part])
  1476. astc_results.m_weights[x + y * 4] = 3 - astc_results.m_weights[x + y * 4];
  1477. }
  1478. }
  1479. assert(total_results < MAX_ENCODE_RESULTS);
  1480. if (total_results < MAX_ENCODE_RESULTS)
  1481. {
  1482. pResults[total_results].m_uastc_mode = mode;
  1483. pResults[total_results].m_common_pattern = common_pattern;
  1484. pResults[total_results].m_astc = astc_results;
  1485. pResults[total_results].m_astc_err = total_err;
  1486. total_results++;
  1487. }
  1488. } // common_pattern
  1489. }
  1490. // MODE 10
  1491. // DualPlane: 0, WeightRange: 8 (16), Subsets: 1, CEM: 12 (RGBA Direct ), EndpointRange: 13 (48) MODE6
  1492. static void astc_mode10(const color_rgba block[4][4], uastc_encode_results* pResults, uint32_t& total_results, bc7enc_compress_block_params& comp_params)
  1493. {
  1494. const uint32_t weight_range = 8;
  1495. const uint32_t endpoint_range = 13;
  1496. color_cell_compressor_params ccell_params;
  1497. memset(&ccell_params, 0, sizeof(ccell_params));
  1498. ccell_params.m_num_pixels = 16;
  1499. ccell_params.m_pPixels = (color_quad_u8*)&block[0][0];
  1500. ccell_params.m_num_selector_weights = 16;
  1501. ccell_params.m_pSelector_weights = g_astc_weights4;
  1502. ccell_params.m_pSelector_weightsx = (const bc7enc_vec4F*)g_astc_weights4x;
  1503. ccell_params.m_astc_endpoint_range = endpoint_range;
  1504. ccell_params.m_weights[0] = 1;
  1505. ccell_params.m_weights[1] = 1;
  1506. ccell_params.m_weights[2] = 1;
  1507. ccell_params.m_weights[3] = 1;
  1508. ccell_params.m_has_alpha = true;
  1509. color_cell_compressor_results ccell_results;
  1510. uint8_t ccell_result_selectors[16];
  1511. uint8_t ccell_result_selectors_temp[16];
  1512. memset(&ccell_results, 0, sizeof(ccell_results));
  1513. ccell_results.m_pSelectors = &ccell_result_selectors[0];
  1514. ccell_results.m_pSelectors_temp = &ccell_result_selectors_temp[0];
  1515. uint64_t part_err = color_cell_compression(255, &ccell_params, &ccell_results, &comp_params);
  1516. // ASTC
  1517. astc_block_desc astc_results;
  1518. memset(&astc_results, 0, sizeof(astc_results));
  1519. astc_results.m_dual_plane = false;
  1520. astc_results.m_weight_range = weight_range;
  1521. astc_results.m_ccs = 0;
  1522. astc_results.m_subsets = 1;
  1523. astc_results.m_partition_seed = 0;
  1524. astc_results.m_cem = 12;
  1525. astc_results.m_endpoints[0] = ccell_results.m_astc_low_endpoint.m_c[0];
  1526. astc_results.m_endpoints[1] = ccell_results.m_astc_high_endpoint.m_c[0];
  1527. astc_results.m_endpoints[2] = ccell_results.m_astc_low_endpoint.m_c[1];
  1528. astc_results.m_endpoints[3] = ccell_results.m_astc_high_endpoint.m_c[1];
  1529. astc_results.m_endpoints[4] = ccell_results.m_astc_low_endpoint.m_c[2];
  1530. astc_results.m_endpoints[5] = ccell_results.m_astc_high_endpoint.m_c[2];
  1531. astc_results.m_endpoints[6] = ccell_results.m_astc_low_endpoint.m_c[3];
  1532. astc_results.m_endpoints[7] = ccell_results.m_astc_high_endpoint.m_c[3];
  1533. bool invert = false;
  1534. int s0 = g_astc_unquant[endpoint_range][astc_results.m_endpoints[0]].m_unquant + g_astc_unquant[endpoint_range][astc_results.m_endpoints[2]].m_unquant + g_astc_unquant[endpoint_range][astc_results.m_endpoints[4]].m_unquant;
  1535. int s1 = g_astc_unquant[endpoint_range][astc_results.m_endpoints[1]].m_unquant + g_astc_unquant[endpoint_range][astc_results.m_endpoints[3]].m_unquant + g_astc_unquant[endpoint_range][astc_results.m_endpoints[5]].m_unquant;
  1536. if (s1 < s0)
  1537. {
  1538. std::swap(astc_results.m_endpoints[0], astc_results.m_endpoints[1]);
  1539. std::swap(astc_results.m_endpoints[2], astc_results.m_endpoints[3]);
  1540. std::swap(astc_results.m_endpoints[4], astc_results.m_endpoints[5]);
  1541. std::swap(astc_results.m_endpoints[6], astc_results.m_endpoints[7]);
  1542. invert = true;
  1543. }
  1544. for (uint32_t y = 0; y < 4; y++)
  1545. {
  1546. for (uint32_t x = 0; x < 4; x++)
  1547. {
  1548. astc_results.m_weights[x + y * 4] = ccell_result_selectors[x + y * 4];
  1549. if (invert)
  1550. astc_results.m_weights[x + y * 4] = 15 - astc_results.m_weights[x + y * 4];
  1551. }
  1552. }
  1553. assert(total_results < MAX_ENCODE_RESULTS);
  1554. if (total_results < MAX_ENCODE_RESULTS)
  1555. {
  1556. pResults[total_results].m_uastc_mode = 10;
  1557. pResults[total_results].m_common_pattern = 0;
  1558. pResults[total_results].m_astc = astc_results;
  1559. pResults[total_results].m_astc_err = part_err;
  1560. total_results++;
  1561. }
  1562. }
  1563. // 11. DualPlane: 1, WeightRange: 2 (4), Subsets: 1, CEM: 12 (RGBA Direct), EndpointRange: 13 (48) MODE5
  1564. // 17. DualPlane: 1, WeightRange : 2 (4), Subsets : 1, CEM : 4 (LA Direct), EndpointRange : 20 (256) BC7 MODE5
  1565. static void astc_mode11_or_17(uint32_t mode, const color_rgba block[4][4], uastc_encode_results* pResults, uint32_t& total_results, bc7enc_compress_block_params& comp_params)
  1566. {
  1567. assert((mode == 11) || (mode == 17));
  1568. const uint32_t weight_range = 2;
  1569. const uint32_t endpoint_range = (mode == 17) ? 20 : 13;
  1570. bc7enc_compress_block_params local_comp_params(comp_params);
  1571. local_comp_params.m_perceptual = false;
  1572. local_comp_params.m_weights[0] = 1;
  1573. local_comp_params.m_weights[1] = 1;
  1574. local_comp_params.m_weights[2] = 1;
  1575. local_comp_params.m_weights[3] = 1;
  1576. const uint32_t last_rot_comp = (mode == 17) ? 1 : 4;
  1577. for (uint32_t rot_comp = 0; rot_comp < last_rot_comp; rot_comp++)
  1578. {
  1579. color_quad_u8 block_rgb[16];
  1580. color_quad_u8 block_a[16];
  1581. for (uint32_t i = 0; i < 16; i++)
  1582. {
  1583. block_rgb[i] = ((color_quad_u8*)&block[0][0])[i];
  1584. block_a[i] = block_rgb[i];
  1585. if (mode == 17)
  1586. {
  1587. assert(block_rgb[i].m_c[0] == block_rgb[i].m_c[1]);
  1588. assert(block_rgb[i].m_c[0] == block_rgb[i].m_c[2]);
  1589. block_a[i].m_c[0] = block_rgb[i].m_c[3];
  1590. block_a[i].m_c[1] = block_rgb[i].m_c[3];
  1591. block_a[i].m_c[2] = block_rgb[i].m_c[3];
  1592. block_a[i].m_c[3] = 255;
  1593. block_rgb[i].m_c[1] = block_rgb[i].m_c[0];
  1594. block_rgb[i].m_c[2] = block_rgb[i].m_c[0];
  1595. block_rgb[i].m_c[3] = 255;
  1596. }
  1597. else
  1598. {
  1599. uint8_t c = block_a[i].m_c[rot_comp];
  1600. block_a[i].m_c[0] = c;
  1601. block_a[i].m_c[1] = c;
  1602. block_a[i].m_c[2] = c;
  1603. block_a[i].m_c[3] = 255;
  1604. block_rgb[i].m_c[rot_comp] = block_rgb[i].m_c[3];
  1605. block_rgb[i].m_c[3] = 255;
  1606. }
  1607. }
  1608. uint8_t ccell_result_selectors_temp[16];
  1609. color_cell_compressor_params ccell_params_rgb;
  1610. memset(&ccell_params_rgb, 0, sizeof(ccell_params_rgb));
  1611. ccell_params_rgb.m_num_pixels = 16;
  1612. ccell_params_rgb.m_pPixels = block_rgb;
  1613. ccell_params_rgb.m_num_selector_weights = 4;
  1614. ccell_params_rgb.m_pSelector_weights = g_bc7_weights2;
  1615. ccell_params_rgb.m_pSelector_weightsx = (const bc7enc_vec4F*)g_bc7_weights2x;
  1616. ccell_params_rgb.m_astc_endpoint_range = endpoint_range;
  1617. ccell_params_rgb.m_weights[0] = 1;
  1618. ccell_params_rgb.m_weights[1] = 1;
  1619. ccell_params_rgb.m_weights[2] = 1;
  1620. ccell_params_rgb.m_weights[3] = 1;
  1621. color_cell_compressor_results ccell_results_rgb;
  1622. uint8_t ccell_result_selectors_rgb[16];
  1623. memset(&ccell_results_rgb, 0, sizeof(ccell_results_rgb));
  1624. ccell_results_rgb.m_pSelectors = &ccell_result_selectors_rgb[0];
  1625. ccell_results_rgb.m_pSelectors_temp = &ccell_result_selectors_temp[0];
  1626. uint64_t part_err_rgb = color_cell_compression(255, &ccell_params_rgb, &ccell_results_rgb, &local_comp_params);
  1627. color_cell_compressor_params ccell_params_a;
  1628. memset(&ccell_params_a, 0, sizeof(ccell_params_a));
  1629. ccell_params_a.m_num_pixels = 16;
  1630. ccell_params_a.m_pPixels = block_a;
  1631. ccell_params_a.m_num_selector_weights = 4;
  1632. ccell_params_a.m_pSelector_weights = g_bc7_weights2;
  1633. ccell_params_a.m_pSelector_weightsx = (const bc7enc_vec4F*)g_bc7_weights2x;
  1634. ccell_params_a.m_astc_endpoint_range = endpoint_range;
  1635. ccell_params_a.m_weights[0] = 1;
  1636. ccell_params_a.m_weights[1] = 1;
  1637. ccell_params_a.m_weights[2] = 1;
  1638. ccell_params_a.m_weights[3] = 1;
  1639. color_cell_compressor_results ccell_results_a;
  1640. uint8_t ccell_result_selectors_a[16];
  1641. memset(&ccell_results_a, 0, sizeof(ccell_results_a));
  1642. ccell_results_a.m_pSelectors = &ccell_result_selectors_a[0];
  1643. ccell_results_a.m_pSelectors_temp = &ccell_result_selectors_temp[0];
  1644. uint64_t part_err_a = color_cell_compression(255, &ccell_params_a, &ccell_results_a, &local_comp_params) / 3;
  1645. uint64_t total_err = (mode == 17) ? ((part_err_rgb / 3) + part_err_a) : (part_err_rgb + part_err_a);
  1646. // ASTC
  1647. astc_block_desc blk;
  1648. memset(&blk, 0, sizeof(blk));
  1649. blk.m_dual_plane = true;
  1650. blk.m_weight_range = weight_range;
  1651. blk.m_ccs = (mode == 17) ? 3 : rot_comp;
  1652. blk.m_subsets = 1;
  1653. blk.m_partition_seed = 0;
  1654. blk.m_cem = (mode == 17) ? 4 : 12;
  1655. bool invert = false;
  1656. if (mode == 17)
  1657. {
  1658. assert(ccell_results_rgb.m_astc_low_endpoint.m_c[0] == ccell_results_rgb.m_astc_low_endpoint.m_c[1]);
  1659. assert(ccell_results_rgb.m_astc_low_endpoint.m_c[0] == ccell_results_rgb.m_astc_low_endpoint.m_c[2]);
  1660. assert(ccell_results_rgb.m_astc_high_endpoint.m_c[0] == ccell_results_rgb.m_astc_high_endpoint.m_c[1]);
  1661. assert(ccell_results_rgb.m_astc_high_endpoint.m_c[0] == ccell_results_rgb.m_astc_high_endpoint.m_c[2]);
  1662. blk.m_endpoints[0] = ccell_results_rgb.m_astc_low_endpoint.m_c[0];
  1663. blk.m_endpoints[1] = ccell_results_rgb.m_astc_high_endpoint.m_c[0];
  1664. blk.m_endpoints[2] = ccell_results_a.m_astc_low_endpoint.m_c[0];
  1665. blk.m_endpoints[3] = ccell_results_a.m_astc_high_endpoint.m_c[0];
  1666. }
  1667. else
  1668. {
  1669. blk.m_endpoints[0] = (rot_comp == 0 ? ccell_results_a : ccell_results_rgb).m_astc_low_endpoint.m_c[0];
  1670. blk.m_endpoints[1] = (rot_comp == 0 ? ccell_results_a : ccell_results_rgb).m_astc_high_endpoint.m_c[0];
  1671. blk.m_endpoints[2] = (rot_comp == 1 ? ccell_results_a : ccell_results_rgb).m_astc_low_endpoint.m_c[1];
  1672. blk.m_endpoints[3] = (rot_comp == 1 ? ccell_results_a : ccell_results_rgb).m_astc_high_endpoint.m_c[1];
  1673. blk.m_endpoints[4] = (rot_comp == 2 ? ccell_results_a : ccell_results_rgb).m_astc_low_endpoint.m_c[2];
  1674. blk.m_endpoints[5] = (rot_comp == 2 ? ccell_results_a : ccell_results_rgb).m_astc_high_endpoint.m_c[2];
  1675. if (rot_comp == 3)
  1676. {
  1677. blk.m_endpoints[6] = ccell_results_a.m_astc_low_endpoint.m_c[0];
  1678. blk.m_endpoints[7] = ccell_results_a.m_astc_high_endpoint.m_c[0];
  1679. }
  1680. else
  1681. {
  1682. blk.m_endpoints[6] = ccell_results_rgb.m_astc_low_endpoint.m_c[rot_comp];
  1683. blk.m_endpoints[7] = ccell_results_rgb.m_astc_high_endpoint.m_c[rot_comp];
  1684. }
  1685. int s0 = g_astc_unquant[endpoint_range][blk.m_endpoints[0]].m_unquant + g_astc_unquant[endpoint_range][blk.m_endpoints[2]].m_unquant + g_astc_unquant[endpoint_range][blk.m_endpoints[4]].m_unquant;
  1686. int s1 = g_astc_unquant[endpoint_range][blk.m_endpoints[1]].m_unquant + g_astc_unquant[endpoint_range][blk.m_endpoints[3]].m_unquant + g_astc_unquant[endpoint_range][blk.m_endpoints[5]].m_unquant;
  1687. if (s1 < s0)
  1688. {
  1689. std::swap(blk.m_endpoints[0], blk.m_endpoints[1]);
  1690. std::swap(blk.m_endpoints[2], blk.m_endpoints[3]);
  1691. std::swap(blk.m_endpoints[4], blk.m_endpoints[5]);
  1692. std::swap(blk.m_endpoints[6], blk.m_endpoints[7]);
  1693. invert = true;
  1694. }
  1695. }
  1696. for (uint32_t y = 0; y < 4; y++)
  1697. {
  1698. for (uint32_t x = 0; x < 4; x++)
  1699. {
  1700. uint32_t rgb_index = ccell_result_selectors_rgb[x + y * 4];
  1701. uint32_t a_index = ccell_result_selectors_a[x + y * 4];
  1702. if (invert)
  1703. {
  1704. rgb_index = 3 - rgb_index;
  1705. a_index = 3 - a_index;
  1706. }
  1707. blk.m_weights[(x + y * 4) * 2 + 0] = (uint8_t)rgb_index;
  1708. blk.m_weights[(x + y * 4) * 2 + 1] = (uint8_t)a_index;
  1709. }
  1710. }
  1711. assert(total_results < MAX_ENCODE_RESULTS);
  1712. if (total_results < MAX_ENCODE_RESULTS)
  1713. {
  1714. pResults[total_results].m_uastc_mode = mode;
  1715. pResults[total_results].m_common_pattern = 0;
  1716. pResults[total_results].m_astc = blk;
  1717. pResults[total_results].m_astc_err = total_err;
  1718. total_results++;
  1719. }
  1720. } // rot_comp
  1721. }
  1722. // MODE 12
  1723. // DualPlane: 0, WeightRange: 5 (8), Subsets: 1, CEM: 12 (RGBA Direct ), EndpointRange: 19 (192) MODE6
  1724. static void astc_mode12(const color_rgba block[4][4], uastc_encode_results* pResults, uint32_t& total_results, bc7enc_compress_block_params& comp_params)
  1725. {
  1726. const uint32_t weight_range = 5;
  1727. const uint32_t endpoint_range = 19;
  1728. color_cell_compressor_params ccell_params;
  1729. memset(&ccell_params, 0, sizeof(ccell_params));
  1730. ccell_params.m_num_pixels = 16;
  1731. ccell_params.m_pPixels = (color_quad_u8*)&block[0][0];
  1732. ccell_params.m_num_selector_weights = 8;
  1733. ccell_params.m_pSelector_weights = g_bc7_weights3;
  1734. ccell_params.m_pSelector_weightsx = (const bc7enc_vec4F*)g_bc7_weights3x;
  1735. ccell_params.m_astc_endpoint_range = endpoint_range;
  1736. ccell_params.m_weights[0] = 1;
  1737. ccell_params.m_weights[1] = 1;
  1738. ccell_params.m_weights[2] = 1;
  1739. ccell_params.m_weights[3] = 1;
  1740. ccell_params.m_has_alpha = true;
  1741. color_cell_compressor_results ccell_results;
  1742. uint8_t ccell_result_selectors[16];
  1743. uint8_t ccell_result_selectors_temp[16];
  1744. memset(&ccell_results, 0, sizeof(ccell_results));
  1745. ccell_results.m_pSelectors = &ccell_result_selectors[0];
  1746. ccell_results.m_pSelectors_temp = &ccell_result_selectors_temp[0];
  1747. uint64_t part_err = color_cell_compression(255, &ccell_params, &ccell_results, &comp_params);
  1748. // ASTC
  1749. astc_block_desc astc_results;
  1750. memset(&astc_results, 0, sizeof(astc_results));
  1751. astc_results.m_dual_plane = false;
  1752. astc_results.m_weight_range = weight_range;
  1753. astc_results.m_ccs = 0;
  1754. astc_results.m_subsets = 1;
  1755. astc_results.m_partition_seed = 0;
  1756. astc_results.m_cem = 12;
  1757. astc_results.m_endpoints[0] = ccell_results.m_astc_low_endpoint.m_c[0];
  1758. astc_results.m_endpoints[1] = ccell_results.m_astc_high_endpoint.m_c[0];
  1759. astc_results.m_endpoints[2] = ccell_results.m_astc_low_endpoint.m_c[1];
  1760. astc_results.m_endpoints[3] = ccell_results.m_astc_high_endpoint.m_c[1];
  1761. astc_results.m_endpoints[4] = ccell_results.m_astc_low_endpoint.m_c[2];
  1762. astc_results.m_endpoints[5] = ccell_results.m_astc_high_endpoint.m_c[2];
  1763. astc_results.m_endpoints[6] = ccell_results.m_astc_low_endpoint.m_c[3];
  1764. astc_results.m_endpoints[7] = ccell_results.m_astc_high_endpoint.m_c[3];
  1765. bool invert = false;
  1766. int s0 = g_astc_unquant[endpoint_range][astc_results.m_endpoints[0]].m_unquant + g_astc_unquant[endpoint_range][astc_results.m_endpoints[2]].m_unquant + g_astc_unquant[endpoint_range][astc_results.m_endpoints[4]].m_unquant;
  1767. int s1 = g_astc_unquant[endpoint_range][astc_results.m_endpoints[1]].m_unquant + g_astc_unquant[endpoint_range][astc_results.m_endpoints[3]].m_unquant + g_astc_unquant[endpoint_range][astc_results.m_endpoints[5]].m_unquant;
  1768. if (s1 < s0)
  1769. {
  1770. std::swap(astc_results.m_endpoints[0], astc_results.m_endpoints[1]);
  1771. std::swap(astc_results.m_endpoints[2], astc_results.m_endpoints[3]);
  1772. std::swap(astc_results.m_endpoints[4], astc_results.m_endpoints[5]);
  1773. std::swap(astc_results.m_endpoints[6], astc_results.m_endpoints[7]);
  1774. invert = true;
  1775. }
  1776. for (uint32_t y = 0; y < 4; y++)
  1777. {
  1778. for (uint32_t x = 0; x < 4; x++)
  1779. {
  1780. astc_results.m_weights[x + y * 4] = ccell_result_selectors[x + y * 4];
  1781. if (invert)
  1782. astc_results.m_weights[x + y * 4] = 7 - astc_results.m_weights[x + y * 4];
  1783. }
  1784. }
  1785. assert(total_results < MAX_ENCODE_RESULTS);
  1786. if (total_results < MAX_ENCODE_RESULTS)
  1787. {
  1788. pResults[total_results].m_uastc_mode = 12;
  1789. pResults[total_results].m_common_pattern = 0;
  1790. pResults[total_results].m_astc = astc_results;
  1791. pResults[total_results].m_astc_err = part_err;
  1792. total_results++;
  1793. }
  1794. }
  1795. // 13. DualPlane: 1, WeightRange: 0 (2), Subsets: 1, CEM: 12 (RGBA Direct ), EndpointRange: 20 (256) MODE5
  1796. static void astc_mode13(const color_rgba block[4][4], uastc_encode_results* pResults, uint32_t& total_results, bc7enc_compress_block_params& comp_params)
  1797. {
  1798. bc7enc_compress_block_params local_comp_params(comp_params);
  1799. local_comp_params.m_perceptual = false;
  1800. local_comp_params.m_weights[0] = 1;
  1801. local_comp_params.m_weights[1] = 1;
  1802. local_comp_params.m_weights[2] = 1;
  1803. local_comp_params.m_weights[3] = 1;
  1804. for (uint32_t rot_comp = 0; rot_comp < 4; rot_comp++)
  1805. {
  1806. const uint32_t weight_range = 0;
  1807. const uint32_t endpoint_range = 20;
  1808. color_quad_u8 block_rgb[16];
  1809. color_quad_u8 block_a[16];
  1810. for (uint32_t i = 0; i < 16; i++)
  1811. {
  1812. block_rgb[i] = ((color_quad_u8*)&block[0][0])[i];
  1813. block_a[i] = block_rgb[i];
  1814. uint8_t c = block_a[i].m_c[rot_comp];
  1815. block_a[i].m_c[0] = c;
  1816. block_a[i].m_c[1] = c;
  1817. block_a[i].m_c[2] = c;
  1818. block_a[i].m_c[3] = 255;
  1819. block_rgb[i].m_c[rot_comp] = block_rgb[i].m_c[3];
  1820. block_rgb[i].m_c[3] = 255;
  1821. }
  1822. uint8_t ccell_result_selectors_temp[16];
  1823. color_cell_compressor_params ccell_params_rgb;
  1824. memset(&ccell_params_rgb, 0, sizeof(ccell_params_rgb));
  1825. ccell_params_rgb.m_num_pixels = 16;
  1826. ccell_params_rgb.m_pPixels = block_rgb;
  1827. ccell_params_rgb.m_num_selector_weights = 2;
  1828. ccell_params_rgb.m_pSelector_weights = g_bc7_weights1;
  1829. ccell_params_rgb.m_pSelector_weightsx = (const bc7enc_vec4F*)g_bc7_weights1x;
  1830. ccell_params_rgb.m_astc_endpoint_range = endpoint_range;
  1831. ccell_params_rgb.m_weights[0] = 1;
  1832. ccell_params_rgb.m_weights[1] = 1;
  1833. ccell_params_rgb.m_weights[2] = 1;
  1834. ccell_params_rgb.m_weights[3] = 1;
  1835. color_cell_compressor_results ccell_results_rgb;
  1836. uint8_t ccell_result_selectors_rgb[16];
  1837. memset(&ccell_results_rgb, 0, sizeof(ccell_results_rgb));
  1838. ccell_results_rgb.m_pSelectors = &ccell_result_selectors_rgb[0];
  1839. ccell_results_rgb.m_pSelectors_temp = &ccell_result_selectors_temp[0];
  1840. uint64_t part_err_rgb = color_cell_compression(255, &ccell_params_rgb, &ccell_results_rgb, &local_comp_params);
  1841. color_cell_compressor_params ccell_params_a;
  1842. memset(&ccell_params_a, 0, sizeof(ccell_params_a));
  1843. ccell_params_a.m_num_pixels = 16;
  1844. ccell_params_a.m_pPixels = block_a;
  1845. ccell_params_a.m_num_selector_weights = 2;
  1846. ccell_params_a.m_pSelector_weights = g_bc7_weights1;
  1847. ccell_params_a.m_pSelector_weightsx = (const bc7enc_vec4F*)g_bc7_weights1x;
  1848. ccell_params_a.m_astc_endpoint_range = endpoint_range;
  1849. ccell_params_a.m_weights[0] = 1;
  1850. ccell_params_a.m_weights[1] = 1;
  1851. ccell_params_a.m_weights[2] = 1;
  1852. ccell_params_a.m_weights[3] = 1;
  1853. color_cell_compressor_results ccell_results_a;
  1854. uint8_t ccell_result_selectors_a[16];
  1855. memset(&ccell_results_a, 0, sizeof(ccell_results_a));
  1856. ccell_results_a.m_pSelectors = &ccell_result_selectors_a[0];
  1857. ccell_results_a.m_pSelectors_temp = &ccell_result_selectors_temp[0];
  1858. uint64_t part_err_a = color_cell_compression(255, &ccell_params_a, &ccell_results_a, &local_comp_params) / 3;
  1859. uint64_t total_err = part_err_rgb + part_err_a;
  1860. // ASTC
  1861. astc_block_desc blk;
  1862. memset(&blk, 0, sizeof(blk));
  1863. blk.m_dual_plane = true;
  1864. blk.m_weight_range = weight_range;
  1865. blk.m_ccs = rot_comp;
  1866. blk.m_subsets = 1;
  1867. blk.m_partition_seed = 0;
  1868. blk.m_cem = 12;
  1869. blk.m_endpoints[0] = (rot_comp == 0 ? ccell_results_a : ccell_results_rgb).m_astc_low_endpoint.m_c[0];
  1870. blk.m_endpoints[1] = (rot_comp == 0 ? ccell_results_a : ccell_results_rgb).m_astc_high_endpoint.m_c[0];
  1871. blk.m_endpoints[2] = (rot_comp == 1 ? ccell_results_a : ccell_results_rgb).m_astc_low_endpoint.m_c[1];
  1872. blk.m_endpoints[3] = (rot_comp == 1 ? ccell_results_a : ccell_results_rgb).m_astc_high_endpoint.m_c[1];
  1873. blk.m_endpoints[4] = (rot_comp == 2 ? ccell_results_a : ccell_results_rgb).m_astc_low_endpoint.m_c[2];
  1874. blk.m_endpoints[5] = (rot_comp == 2 ? ccell_results_a : ccell_results_rgb).m_astc_high_endpoint.m_c[2];
  1875. if (rot_comp == 3)
  1876. {
  1877. blk.m_endpoints[6] = ccell_results_a.m_astc_low_endpoint.m_c[0];
  1878. blk.m_endpoints[7] = ccell_results_a.m_astc_high_endpoint.m_c[0];
  1879. }
  1880. else
  1881. {
  1882. blk.m_endpoints[6] = ccell_results_rgb.m_astc_low_endpoint.m_c[rot_comp];
  1883. blk.m_endpoints[7] = ccell_results_rgb.m_astc_high_endpoint.m_c[rot_comp];
  1884. }
  1885. bool invert = false;
  1886. int s0 = g_astc_unquant[endpoint_range][blk.m_endpoints[0]].m_unquant + g_astc_unquant[endpoint_range][blk.m_endpoints[2]].m_unquant + g_astc_unquant[endpoint_range][blk.m_endpoints[4]].m_unquant;
  1887. int s1 = g_astc_unquant[endpoint_range][blk.m_endpoints[1]].m_unquant + g_astc_unquant[endpoint_range][blk.m_endpoints[3]].m_unquant + g_astc_unquant[endpoint_range][blk.m_endpoints[5]].m_unquant;
  1888. if (s1 < s0)
  1889. {
  1890. std::swap(blk.m_endpoints[0], blk.m_endpoints[1]);
  1891. std::swap(blk.m_endpoints[2], blk.m_endpoints[3]);
  1892. std::swap(blk.m_endpoints[4], blk.m_endpoints[5]);
  1893. std::swap(blk.m_endpoints[6], blk.m_endpoints[7]);
  1894. invert = true;
  1895. }
  1896. for (uint32_t y = 0; y < 4; y++)
  1897. {
  1898. for (uint32_t x = 0; x < 4; x++)
  1899. {
  1900. uint32_t rgb_index = ccell_result_selectors_rgb[x + y * 4];
  1901. uint32_t a_index = ccell_result_selectors_a[x + y * 4];
  1902. if (invert)
  1903. {
  1904. rgb_index = 1 - rgb_index;
  1905. a_index = 1 - a_index;
  1906. }
  1907. blk.m_weights[(x + y * 4) * 2 + 0] = (uint8_t)rgb_index;
  1908. blk.m_weights[(x + y * 4) * 2 + 1] = (uint8_t)a_index;
  1909. }
  1910. }
  1911. assert(total_results < MAX_ENCODE_RESULTS);
  1912. if (total_results < MAX_ENCODE_RESULTS)
  1913. {
  1914. pResults[total_results].m_uastc_mode = 13;
  1915. pResults[total_results].m_common_pattern = 0;
  1916. pResults[total_results].m_astc = blk;
  1917. pResults[total_results].m_astc_err = total_err;
  1918. total_results++;
  1919. }
  1920. } // rot_comp
  1921. }
  1922. // MODE14
  1923. // DualPlane: 0, WeightRange: 2 (4), Subsets: 1, CEM: 12 (RGBA Direct ), EndpointRange: 20 (256) MODE6
  1924. static void astc_mode14(const color_rgba block[4][4], uastc_encode_results* pResults, uint32_t& total_results, bc7enc_compress_block_params& comp_params)
  1925. {
  1926. const uint32_t weight_range = 2;
  1927. const uint32_t endpoint_range = 20;
  1928. color_cell_compressor_params ccell_params;
  1929. memset(&ccell_params, 0, sizeof(ccell_params));
  1930. ccell_params.m_num_pixels = 16;
  1931. ccell_params.m_pPixels = (color_quad_u8*)&block[0][0];
  1932. ccell_params.m_num_selector_weights = 4;
  1933. ccell_params.m_pSelector_weights = g_bc7_weights2;
  1934. ccell_params.m_pSelector_weightsx = (const bc7enc_vec4F*)g_bc7_weights2x;
  1935. ccell_params.m_astc_endpoint_range = endpoint_range;
  1936. ccell_params.m_weights[0] = 1;
  1937. ccell_params.m_weights[1] = 1;
  1938. ccell_params.m_weights[2] = 1;
  1939. ccell_params.m_weights[3] = 1;
  1940. ccell_params.m_has_alpha = true;
  1941. color_cell_compressor_results ccell_results;
  1942. uint8_t ccell_result_selectors[16];
  1943. uint8_t ccell_result_selectors_temp[16];
  1944. memset(&ccell_results, 0, sizeof(ccell_results));
  1945. ccell_results.m_pSelectors = &ccell_result_selectors[0];
  1946. ccell_results.m_pSelectors_temp = &ccell_result_selectors_temp[0];
  1947. uint64_t part_err = color_cell_compression(255, &ccell_params, &ccell_results, &comp_params);
  1948. // ASTC
  1949. astc_block_desc astc_results;
  1950. memset(&astc_results, 0, sizeof(astc_results));
  1951. astc_results.m_dual_plane = false;
  1952. astc_results.m_weight_range = weight_range;
  1953. astc_results.m_ccs = 0;
  1954. astc_results.m_subsets = 1;
  1955. astc_results.m_partition_seed = 0;
  1956. astc_results.m_cem = 12;
  1957. astc_results.m_endpoints[0] = ccell_results.m_astc_low_endpoint.m_c[0];
  1958. astc_results.m_endpoints[1] = ccell_results.m_astc_high_endpoint.m_c[0];
  1959. astc_results.m_endpoints[2] = ccell_results.m_astc_low_endpoint.m_c[1];
  1960. astc_results.m_endpoints[3] = ccell_results.m_astc_high_endpoint.m_c[1];
  1961. astc_results.m_endpoints[4] = ccell_results.m_astc_low_endpoint.m_c[2];
  1962. astc_results.m_endpoints[5] = ccell_results.m_astc_high_endpoint.m_c[2];
  1963. astc_results.m_endpoints[6] = ccell_results.m_astc_low_endpoint.m_c[3];
  1964. astc_results.m_endpoints[7] = ccell_results.m_astc_high_endpoint.m_c[3];
  1965. bool invert = false;
  1966. int s0 = g_astc_unquant[endpoint_range][astc_results.m_endpoints[0]].m_unquant + g_astc_unquant[endpoint_range][astc_results.m_endpoints[2]].m_unquant + g_astc_unquant[endpoint_range][astc_results.m_endpoints[4]].m_unquant;
  1967. int s1 = g_astc_unquant[endpoint_range][astc_results.m_endpoints[1]].m_unquant + g_astc_unquant[endpoint_range][astc_results.m_endpoints[3]].m_unquant + g_astc_unquant[endpoint_range][astc_results.m_endpoints[5]].m_unquant;
  1968. if (s1 < s0)
  1969. {
  1970. std::swap(astc_results.m_endpoints[0], astc_results.m_endpoints[1]);
  1971. std::swap(astc_results.m_endpoints[2], astc_results.m_endpoints[3]);
  1972. std::swap(astc_results.m_endpoints[4], astc_results.m_endpoints[5]);
  1973. std::swap(astc_results.m_endpoints[6], astc_results.m_endpoints[7]);
  1974. invert = true;
  1975. }
  1976. for (uint32_t y = 0; y < 4; y++)
  1977. {
  1978. for (uint32_t x = 0; x < 4; x++)
  1979. {
  1980. astc_results.m_weights[x + y * 4] = ccell_result_selectors[x + y * 4];
  1981. if (invert)
  1982. astc_results.m_weights[x + y * 4] = 3 - astc_results.m_weights[x + y * 4];
  1983. }
  1984. }
  1985. assert(total_results < MAX_ENCODE_RESULTS);
  1986. if (total_results < MAX_ENCODE_RESULTS)
  1987. {
  1988. pResults[total_results].m_uastc_mode = 14;
  1989. pResults[total_results].m_common_pattern = 0;
  1990. pResults[total_results].m_astc = astc_results;
  1991. pResults[total_results].m_astc_err = part_err;
  1992. total_results++;
  1993. }
  1994. }
  1995. // MODE 15
  1996. // DualPlane: 0, WeightRange : 8 (16), Subsets : 1, CEM : 4 (LA Direct), EndpointRange : 20 (256) BC7 MODE6
  1997. static void astc_mode15(const color_rgba block[4][4], uastc_encode_results* pResults, uint32_t& total_results, bc7enc_compress_block_params& comp_params)
  1998. {
  1999. const uint32_t weight_range = 8;
  2000. const uint32_t endpoint_range = 20;
  2001. color_cell_compressor_params ccell_params;
  2002. memset(&ccell_params, 0, sizeof(ccell_params));
  2003. color_rgba temp_block[16];
  2004. for (uint32_t i = 0; i < 16; i++)
  2005. {
  2006. const uint32_t l = ((const color_rgba*)block)[i].r;
  2007. const uint32_t a = ((const color_rgba*)block)[i].a;
  2008. // Use (l,0,0,a) not (l,l,l,a) so both components are treated equally.
  2009. temp_block[i].set_noclamp_rgba(l, 0, 0, a);
  2010. }
  2011. ccell_params.m_num_pixels = 16;
  2012. //ccell_params.m_pPixels = (color_quad_u8*)&block[0][0];
  2013. ccell_params.m_pPixels = (color_quad_u8*)temp_block;
  2014. ccell_params.m_num_selector_weights = 16;
  2015. ccell_params.m_pSelector_weights = g_astc_weights4;
  2016. ccell_params.m_pSelector_weightsx = (const bc7enc_vec4F*)g_astc_weights4x;
  2017. ccell_params.m_astc_endpoint_range = endpoint_range;
  2018. ccell_params.m_weights[0] = 1;
  2019. ccell_params.m_weights[1] = 1;
  2020. ccell_params.m_weights[2] = 1;
  2021. ccell_params.m_weights[3] = 1;
  2022. ccell_params.m_has_alpha = true;
  2023. color_cell_compressor_results ccell_results;
  2024. uint8_t ccell_result_selectors[16];
  2025. uint8_t ccell_result_selectors_temp[16];
  2026. memset(&ccell_results, 0, sizeof(ccell_results));
  2027. ccell_results.m_pSelectors = &ccell_result_selectors[0];
  2028. ccell_results.m_pSelectors_temp = &ccell_result_selectors_temp[0];
  2029. color_cell_compression(255, &ccell_params, &ccell_results, &comp_params);
  2030. // ASTC
  2031. astc_block_desc astc_results;
  2032. memset(&astc_results, 0, sizeof(astc_results));
  2033. astc_results.m_dual_plane = false;
  2034. astc_results.m_weight_range = weight_range;
  2035. astc_results.m_ccs = 0;
  2036. astc_results.m_subsets = 1;
  2037. astc_results.m_partition_seed = 0;
  2038. astc_results.m_cem = 4;
  2039. astc_results.m_endpoints[0] = ccell_results.m_astc_low_endpoint.m_c[0];
  2040. astc_results.m_endpoints[1] = ccell_results.m_astc_high_endpoint.m_c[0];
  2041. astc_results.m_endpoints[2] = ccell_results.m_astc_low_endpoint.m_c[3];
  2042. astc_results.m_endpoints[3] = ccell_results.m_astc_high_endpoint.m_c[3];
  2043. for (uint32_t y = 0; y < 4; y++)
  2044. for (uint32_t x = 0; x < 4; x++)
  2045. astc_results.m_weights[x + y * 4] = ccell_result_selectors[x + y * 4];
  2046. color_rgba colors[16];
  2047. for (uint32_t c = 0; c < 4; c++)
  2048. {
  2049. colors[0].m_comps[c] = g_astc_unquant[endpoint_range][ccell_results.m_astc_low_endpoint.m_c[(c < 3) ? 0 : 3]].m_unquant;
  2050. colors[15].m_comps[c] = g_astc_unquant[endpoint_range][ccell_results.m_astc_high_endpoint.m_c[(c < 3) ? 0 : 3]].m_unquant;
  2051. }
  2052. for (uint32_t i = 1; i < 16 - 1; i++)
  2053. for (uint32_t c = 0; c < 4; c++)
  2054. colors[i].m_comps[c] = (uint8_t)astc_interpolate(colors[0].m_comps[c], colors[15].m_comps[c], g_astc_weights4[i], false);
  2055. uint64_t total_err = 0;
  2056. for (uint32_t p = 0; p < 16; p++)
  2057. total_err += color_distance_la(((const color_rgba*)block)[p], colors[ccell_result_selectors[p]]);
  2058. assert(total_results < MAX_ENCODE_RESULTS);
  2059. if (total_results < MAX_ENCODE_RESULTS)
  2060. {
  2061. pResults[total_results].m_uastc_mode = 15;
  2062. pResults[total_results].m_common_pattern = 0;
  2063. pResults[total_results].m_astc = astc_results;
  2064. pResults[total_results].m_astc_err = total_err;
  2065. total_results++;
  2066. }
  2067. }
  2068. static void compute_block_error(const color_rgba block[4][4], const color_rgba decoded_block[4][4], uint64_t &total_rgb_err, uint64_t &total_rgba_err, uint64_t &total_la_err)
  2069. {
  2070. uint64_t total_err_r = 0, total_err_g = 0, total_err_b = 0, total_err_a = 0;
  2071. for (uint32_t y = 0; y < 4; y++)
  2072. {
  2073. for (uint32_t x = 0; x < 4; x++)
  2074. {
  2075. const int dr = (int)block[y][x].m_comps[0] - (int)decoded_block[y][x].m_comps[0];
  2076. const int dg = (int)block[y][x].m_comps[1] - (int)decoded_block[y][x].m_comps[1];
  2077. const int db = (int)block[y][x].m_comps[2] - (int)decoded_block[y][x].m_comps[2];
  2078. const int da = (int)block[y][x].m_comps[3] - (int)decoded_block[y][x].m_comps[3];
  2079. total_err_r += dr * dr;
  2080. total_err_g += dg * dg;
  2081. total_err_b += db * db;
  2082. total_err_a += da * da;
  2083. }
  2084. }
  2085. total_la_err = total_err_r + total_err_a;
  2086. total_rgb_err = total_err_r + total_err_g + total_err_b;
  2087. total_rgba_err = total_rgb_err + total_err_a;
  2088. }
  2089. static void compute_bc1_hints(bool &bc1_hint0, bool &bc1_hint1, const uastc_encode_results &best_results, const color_rgba block[4][4], const color_rgba decoded_uastc_block[4][4])
  2090. {
  2091. const uint32_t best_mode = best_results.m_uastc_mode;
  2092. const bool perceptual = false;
  2093. bc1_hint0 = false;
  2094. bc1_hint1 = false;
  2095. if (best_mode == UASTC_MODE_INDEX_SOLID_COLOR)
  2096. return;
  2097. if (!g_uastc_mode_has_bc1_hint0[best_mode] && !g_uastc_mode_has_bc1_hint1[best_mode])
  2098. return;
  2099. color_rgba tblock_bc1[4][4];
  2100. dxt1_block tbc1_block[8];
  2101. basist::encode_bc1(tbc1_block, (const uint8_t*)&decoded_uastc_block[0][0], 0);
  2102. unpack_block(texture_format::cBC1, tbc1_block, &tblock_bc1[0][0]);
  2103. color_rgba tblock_hint0_bc1[4][4];
  2104. color_rgba tblock_hint1_bc1[4][4];
  2105. etc_block etc1_blk;
  2106. memset(&etc1_blk, 0, sizeof(etc1_blk));
  2107. eac_a8_block etc2_blk;
  2108. memset(&etc2_blk, 0, sizeof(etc2_blk));
  2109. etc2_blk.m_multiplier = 1;
  2110. // Pack to UASTC, then unpack, because the endpoints may be swapped.
  2111. uastc_block temp_ublock;
  2112. pack_uastc(temp_ublock, best_results, etc1_blk, 0, etc2_blk, false, false);
  2113. unpacked_uastc_block temp_ublock_unpacked;
  2114. unpack_uastc(temp_ublock, temp_ublock_unpacked, false);
  2115. unpacked_uastc_block ublock;
  2116. memset(&ublock, 0, sizeof(ublock));
  2117. ublock.m_mode = best_results.m_uastc_mode;
  2118. ublock.m_common_pattern = best_results.m_common_pattern;
  2119. ublock.m_astc = temp_ublock_unpacked.m_astc;
  2120. dxt1_block b;
  2121. // HINT1
  2122. if (!g_uastc_mode_has_bc1_hint1[best_mode])
  2123. {
  2124. memset(tblock_hint1_bc1, 0, sizeof(tblock_hint1_bc1));
  2125. }
  2126. else
  2127. {
  2128. transcode_uastc_to_bc1_hint1(ublock, (color32 (*)[4]) decoded_uastc_block, &b, false);
  2129. unpack_block(texture_format::cBC1, &b, &tblock_hint1_bc1[0][0]);
  2130. }
  2131. // HINT0
  2132. if (!g_uastc_mode_has_bc1_hint0[best_mode])
  2133. {
  2134. memset(tblock_hint0_bc1, 0, sizeof(tblock_hint0_bc1));
  2135. }
  2136. else
  2137. {
  2138. transcode_uastc_to_bc1_hint0(ublock, &b);
  2139. unpack_block(texture_format::cBC1, &b, &tblock_hint0_bc1[0][0]);
  2140. }
  2141. // Compute block errors
  2142. uint64_t total_t_err = 0, total_hint0_err = 0, total_hint1_err = 0;
  2143. for (uint32_t y = 0; y < 4; y++)
  2144. {
  2145. for (uint32_t x = 0; x < 4; x++)
  2146. {
  2147. total_t_err += color_distance(perceptual, block[y][x], tblock_bc1[y][x], false);
  2148. total_hint0_err += color_distance(perceptual, block[y][x], tblock_hint0_bc1[y][x], false);
  2149. total_hint1_err += color_distance(perceptual, block[y][x], tblock_hint1_bc1[y][x], false);
  2150. }
  2151. }
  2152. const float t_err = sqrtf((float)total_t_err);
  2153. const float t_err_hint0 = sqrtf((float)total_hint0_err);
  2154. const float t_err_hint1 = sqrtf((float)total_hint1_err);
  2155. const float err_thresh0 = 1.075f;
  2156. const float err_thresh1 = 1.075f;
  2157. if ((g_uastc_mode_has_bc1_hint0[best_mode]) && (t_err_hint0 <= t_err * err_thresh0))
  2158. bc1_hint0 = true;
  2159. if ((g_uastc_mode_has_bc1_hint1[best_mode]) && (t_err_hint1 <= t_err * err_thresh1))
  2160. bc1_hint1 = true;
  2161. }
  2162. struct ycbcr
  2163. {
  2164. int32_t m_y;
  2165. int32_t m_cb;
  2166. int32_t m_cr;
  2167. };
  2168. static inline void rgb_to_y_cb_cr(const color_rgba& c, ycbcr& dst)
  2169. {
  2170. const int y = c.r * 54 + c.g * 183 + c.b * 19;
  2171. dst.m_y = y;
  2172. dst.m_cb = (c.b << 8) - y;
  2173. dst.m_cr = (c.r << 8) - y;
  2174. }
  2175. static inline uint64_t color_diff(const ycbcr& a, const ycbcr& b)
  2176. {
  2177. const int y_delta = a.m_y - b.m_y;
  2178. const int cb_delta = a.m_cb - b.m_cb;
  2179. const int cr_delta = a.m_cr - b.m_cr;
  2180. return ((int64_t)y_delta * y_delta * 4) + ((int64_t)cr_delta * cr_delta) + ((int64_t)cb_delta * cb_delta);
  2181. }
  2182. static inline int gray_distance2(const color_rgba& c, int r, int g, int b)
  2183. {
  2184. int gray_dist = (((int)c[0] - r) + ((int)c[1] - g) + ((int)c[2] - b) + 1) / 3;
  2185. int gray_point_r = clamp255(r + gray_dist);
  2186. int gray_point_g = clamp255(g + gray_dist);
  2187. int gray_point_b = clamp255(b + gray_dist);
  2188. int dist_to_gray_point_r = c[0] - gray_point_r;
  2189. int dist_to_gray_point_g = c[1] - gray_point_g;
  2190. int dist_to_gray_point_b = c[2] - gray_point_b;
  2191. return (dist_to_gray_point_r * dist_to_gray_point_r) + (dist_to_gray_point_g * dist_to_gray_point_g) + (dist_to_gray_point_b * dist_to_gray_point_b);
  2192. }
  2193. static bool pack_etc1_estimate_flipped(const color_rgba* pSrc_pixels)
  2194. {
  2195. int sums[3][2][2];
  2196. #define GET_XY(x, y, c) pSrc_pixels[(x) + ((y) * 4)][c]
  2197. for (uint32_t c = 0; c < 3; c++)
  2198. {
  2199. sums[c][0][0] = GET_XY(0, 0, c) + GET_XY(0, 1, c) + GET_XY(1, 0, c) + GET_XY(1, 1, c);
  2200. sums[c][1][0] = GET_XY(2, 0, c) + GET_XY(2, 1, c) + GET_XY(3, 0, c) + GET_XY(3, 1, c);
  2201. sums[c][0][1] = GET_XY(0, 2, c) + GET_XY(0, 3, c) + GET_XY(1, 2, c) + GET_XY(1, 3, c);
  2202. sums[c][1][1] = GET_XY(2, 2, c) + GET_XY(2, 3, c) + GET_XY(3, 2, c) + GET_XY(3, 3, c);
  2203. }
  2204. int upper_avg[3], lower_avg[3], left_avg[3], right_avg[3];
  2205. for (uint32_t c = 0; c < 3; c++)
  2206. {
  2207. upper_avg[c] = (sums[c][0][0] + sums[c][1][0] + 4) / 8;
  2208. lower_avg[c] = (sums[c][0][1] + sums[c][1][1] + 4) / 8;
  2209. left_avg[c] = (sums[c][0][0] + sums[c][0][1] + 4) / 8;
  2210. right_avg[c] = (sums[c][1][0] + sums[c][1][1] + 4) / 8;
  2211. }
  2212. #undef GET_XY
  2213. #define GET_XY(x, y, a) gray_distance2(pSrc_pixels[(x) + ((y) * 4)], a[0], a[1], a[2])
  2214. int upper_gray_dist = 0, lower_gray_dist = 0, left_gray_dist = 0, right_gray_dist = 0;
  2215. for (uint32_t i = 0; i < 4; i++)
  2216. {
  2217. for (uint32_t j = 0; j < 2; j++)
  2218. {
  2219. upper_gray_dist += GET_XY(i, j, upper_avg);
  2220. lower_gray_dist += GET_XY(i, 2 + j, lower_avg);
  2221. left_gray_dist += GET_XY(j, i, left_avg);
  2222. right_gray_dist += GET_XY(2 + j, i, right_avg);
  2223. }
  2224. }
  2225. #undef GET_XY
  2226. int upper_lower_sum = upper_gray_dist + lower_gray_dist;
  2227. int left_right_sum = left_gray_dist + right_gray_dist;
  2228. return upper_lower_sum < left_right_sum;
  2229. }
  2230. static void compute_etc1_hints(etc_block& best_etc1_blk, uint32_t& best_etc1_bias, const uastc_encode_results& best_results, const color_rgba block[4][4], const color_rgba decoded_uastc_block[4][4], int level, uint32_t flags)
  2231. {
  2232. best_etc1_bias = 0;
  2233. if (best_results.m_uastc_mode == UASTC_MODE_INDEX_SOLID_COLOR)
  2234. {
  2235. pack_etc1_block_solid_color(best_etc1_blk, &best_results.m_solid_color.m_comps[0]);
  2236. return;
  2237. }
  2238. const bool faster_etc1 = (flags & cPackUASTCETC1FasterHints) != 0;
  2239. const bool fastest_etc1 = (flags & cPackUASTCETC1FastestHints) != 0;
  2240. const bool has_bias = g_uastc_mode_has_etc1_bias[best_results.m_uastc_mode];
  2241. // 0 should be at the top, but we need 13 first because it represents bias (0,0,0).
  2242. const uint8_t s_sorted_bias_modes[32] = { 13, 0, 22, 29, 27, 12, 26, 9, 30, 31, 8, 10, 25, 2, 23, 5, 15, 7, 3, 11, 6, 17, 28, 18, 1, 19, 20, 21, 24, 4, 14, 16 };
  2243. uint32_t last_bias = 1;
  2244. bool use_faster_bias_mode_table = false;
  2245. const bool flip_estimate = (level <= cPackUASTCLevelFaster) || (faster_etc1) || (fastest_etc1);
  2246. if (has_bias)
  2247. {
  2248. switch (level)
  2249. {
  2250. case cPackUASTCLevelFastest:
  2251. {
  2252. last_bias = fastest_etc1 ? 1 : (faster_etc1 ? 1 : 2);
  2253. use_faster_bias_mode_table = true;
  2254. break;
  2255. }
  2256. case cPackUASTCLevelFaster:
  2257. {
  2258. last_bias = fastest_etc1 ? 1 : (faster_etc1 ? 3 : 5);
  2259. use_faster_bias_mode_table = true;
  2260. break;
  2261. }
  2262. case cPackUASTCLevelDefault:
  2263. {
  2264. last_bias = fastest_etc1 ? 1 : (faster_etc1 ? 10 : 20);
  2265. use_faster_bias_mode_table = true;
  2266. break;
  2267. }
  2268. case cPackUASTCLevelSlower:
  2269. {
  2270. last_bias = fastest_etc1 ? 1 : (faster_etc1 ? 16 : 32);
  2271. use_faster_bias_mode_table = true;
  2272. break;
  2273. }
  2274. default:
  2275. {
  2276. last_bias = 32;
  2277. break;
  2278. }
  2279. }
  2280. }
  2281. memset(&best_etc1_blk, 0, sizeof(best_etc1_blk));
  2282. uint64_t best_err = UINT64_MAX;
  2283. etc_block trial_block;
  2284. memset(&trial_block, 0, sizeof(trial_block));
  2285. ycbcr block_ycbcr[4][4], decoded_uastc_block_ycbcr[4][4];
  2286. for (uint32_t y = 0; y < 4; y++)
  2287. {
  2288. for (uint32_t x = 0; x < 4; x++)
  2289. {
  2290. rgb_to_y_cb_cr(block[y][x], block_ycbcr[y][x]);
  2291. rgb_to_y_cb_cr(decoded_uastc_block[y][x], decoded_uastc_block_ycbcr[y][x]);
  2292. }
  2293. }
  2294. uint32_t first_flip = 0, last_flip = 2;
  2295. uint32_t first_individ = 0, last_individ = 2;
  2296. if (flags & cPackUASTCETC1DisableFlipAndIndividual)
  2297. {
  2298. last_flip = 1;
  2299. last_individ = 1;
  2300. }
  2301. else if (flip_estimate)
  2302. {
  2303. if (pack_etc1_estimate_flipped(&decoded_uastc_block[0][0]))
  2304. first_flip = 1;
  2305. last_flip = first_flip + 1;
  2306. }
  2307. for (uint32_t flip = first_flip; flip < last_flip; flip++)
  2308. {
  2309. trial_block.set_flip_bit(flip != 0);
  2310. for (uint32_t individ = first_individ; individ < last_individ; individ++)
  2311. {
  2312. const uint32_t mul = individ ? 15 : 31;
  2313. trial_block.set_diff_bit(individ == 0);
  2314. color_rgba unbiased_block_colors[2];
  2315. int min_r[2] = { 255, 255 }, min_g[2] = { 255, 255 }, min_b[2] = { 255, 255 }, max_r[2] = { 0, 0 }, max_g[2] = { 0, 0 }, max_b[2] = { 0, 0 };
  2316. for (uint32_t subset = 0; subset < 2; subset++)
  2317. {
  2318. uint32_t avg_color[3];
  2319. memset(avg_color, 0, sizeof(avg_color));
  2320. for (uint32_t j = 0; j < 8; j++)
  2321. {
  2322. const etc_coord2 &c = g_etc1_pixel_coords[flip][subset][j];
  2323. const color_rgba& p = decoded_uastc_block[c.m_y][c.m_x];
  2324. avg_color[0] += p.r;
  2325. avg_color[1] += p.g;
  2326. avg_color[2] += p.b;
  2327. min_r[subset] = basisu::minimum<uint32_t>(min_r[subset], p.r);
  2328. min_g[subset] = basisu::minimum<uint32_t>(min_g[subset], p.g);
  2329. min_b[subset] = basisu::minimum<uint32_t>(min_b[subset], p.b);
  2330. max_r[subset] = basisu::maximum<uint32_t>(max_r[subset], p.r);
  2331. max_g[subset] = basisu::maximum<uint32_t>(max_g[subset], p.g);
  2332. max_b[subset] = basisu::maximum<uint32_t>(max_b[subset], p.b);
  2333. } // j
  2334. unbiased_block_colors[subset][0] = (uint8_t)((avg_color[0] * mul + 1020) / (8 * 255));
  2335. unbiased_block_colors[subset][1] = (uint8_t)((avg_color[1] * mul + 1020) / (8 * 255));
  2336. unbiased_block_colors[subset][2] = (uint8_t)((avg_color[2] * mul + 1020) / (8 * 255));
  2337. unbiased_block_colors[subset][3] = 0;
  2338. } // subset
  2339. for (uint32_t bias_iter = 0; bias_iter < last_bias; bias_iter++)
  2340. {
  2341. const uint32_t bias = use_faster_bias_mode_table ? s_sorted_bias_modes[bias_iter] : bias_iter;
  2342. color_rgba block_colors[2];
  2343. for (uint32_t subset = 0; subset < 2; subset++)
  2344. block_colors[subset] = has_bias ? apply_etc1_bias((color32&)unbiased_block_colors[subset], bias, mul, subset) : unbiased_block_colors[subset];
  2345. if (individ)
  2346. trial_block.set_block_color4(block_colors[0], block_colors[1]);
  2347. else
  2348. trial_block.set_block_color5_clamp(block_colors[0], block_colors[1]);
  2349. uint32_t range[2];
  2350. for (uint32_t subset = 0; subset < 2; subset++)
  2351. {
  2352. const color_rgba base_c(trial_block.get_block_color(subset, true));
  2353. const int pos_r = iabs(max_r[subset] - base_c.r);
  2354. const int neg_r = iabs(base_c.r - min_r[subset]);
  2355. const int pos_g = iabs(max_g[subset] - base_c.g);
  2356. const int neg_g = iabs(base_c.g - min_g[subset]);
  2357. const int pos_b = iabs(max_b[subset] - base_c.b);
  2358. const int neg_b = iabs(base_c.b - min_b[subset]);
  2359. range[subset] = maximum(maximum(pos_r, neg_r, pos_g, neg_g), pos_b, neg_b);
  2360. }
  2361. uint32_t best_inten_table[2] = { 0, 0 };
  2362. for (uint32_t subset = 0; subset < 2; subset++)
  2363. {
  2364. uint64_t best_subset_err = UINT64_MAX;
  2365. const uint32_t inten_table_limit = (level == cPackUASTCLevelVerySlow) ? 8 : ((range[subset] > 51) ? 8 : (range[subset] >= 7 ? 4 : 2));
  2366. for (uint32_t inten_table = 0; inten_table < inten_table_limit; inten_table++)
  2367. {
  2368. trial_block.set_inten_table(subset, inten_table);
  2369. color_rgba color_table[4];
  2370. trial_block.get_block_colors(color_table, subset);
  2371. ycbcr color_table_ycbcr[4];
  2372. for (uint32_t i = 0; i < 4; i++)
  2373. rgb_to_y_cb_cr(color_table[i], color_table_ycbcr[i]);
  2374. uint64_t total_error = 0;
  2375. if (flip)
  2376. {
  2377. for (uint32_t y = 0; y < 2; y++)
  2378. {
  2379. {
  2380. const ycbcr& c = decoded_uastc_block_ycbcr[subset * 2 + y][0];
  2381. total_error += minimum(color_diff(color_table_ycbcr[0], c), color_diff(color_table_ycbcr[1], c), color_diff(color_table_ycbcr[2], c), color_diff(color_table_ycbcr[3], c));
  2382. }
  2383. {
  2384. const ycbcr& c = decoded_uastc_block_ycbcr[subset * 2 + y][1];
  2385. total_error += minimum(color_diff(color_table_ycbcr[0], c), color_diff(color_table_ycbcr[1], c), color_diff(color_table_ycbcr[2], c), color_diff(color_table_ycbcr[3], c));
  2386. }
  2387. {
  2388. const ycbcr& c = decoded_uastc_block_ycbcr[subset * 2 + y][2];
  2389. total_error += minimum(color_diff(color_table_ycbcr[0], c), color_diff(color_table_ycbcr[1], c), color_diff(color_table_ycbcr[2], c), color_diff(color_table_ycbcr[3], c));
  2390. }
  2391. {
  2392. const ycbcr& c = decoded_uastc_block_ycbcr[subset * 2 + y][3];
  2393. total_error += minimum(color_diff(color_table_ycbcr[0], c), color_diff(color_table_ycbcr[1], c), color_diff(color_table_ycbcr[2], c), color_diff(color_table_ycbcr[3], c));
  2394. }
  2395. if (total_error >= best_subset_err)
  2396. break;
  2397. }
  2398. }
  2399. else
  2400. {
  2401. for (uint32_t y = 0; y < 4; y++)
  2402. {
  2403. {
  2404. const ycbcr& c = decoded_uastc_block_ycbcr[y][subset * 2 + 0];
  2405. total_error += minimum(color_diff(color_table_ycbcr[0], c), color_diff(color_table_ycbcr[1], c), color_diff(color_table_ycbcr[2], c), color_diff(color_table_ycbcr[3], c));
  2406. }
  2407. {
  2408. const ycbcr& c = decoded_uastc_block_ycbcr[y][subset * 2 + 1];
  2409. total_error += minimum(color_diff(color_table_ycbcr[0], c), color_diff(color_table_ycbcr[1], c), color_diff(color_table_ycbcr[2], c), color_diff(color_table_ycbcr[3], c));
  2410. }
  2411. }
  2412. if (total_error >= best_subset_err)
  2413. break;
  2414. }
  2415. if (total_error < best_subset_err)
  2416. {
  2417. best_subset_err = total_error;
  2418. best_inten_table[subset] = inten_table;
  2419. }
  2420. } // inten_table
  2421. } // subset
  2422. trial_block.set_inten_table(0, best_inten_table[0]);
  2423. trial_block.set_inten_table(1, best_inten_table[1]);
  2424. // Compute error against the ORIGINAL block.
  2425. uint64_t err = 0;
  2426. for (uint32_t subset = 0; subset < 2; subset++)
  2427. {
  2428. color_rgba color_table[4];
  2429. trial_block.get_block_colors(color_table, subset);
  2430. ycbcr color_table_ycbcr[4];
  2431. for (uint32_t i = 0; i < 4; i++)
  2432. rgb_to_y_cb_cr(color_table[i], color_table_ycbcr[i]);
  2433. if (flip)
  2434. {
  2435. for (uint32_t y = 0; y < 2; y++)
  2436. {
  2437. for (uint32_t x = 0; x < 4; x++)
  2438. {
  2439. const ycbcr& c = decoded_uastc_block_ycbcr[subset * 2 + y][x];
  2440. const uint64_t best_index_err = minimum(color_diff(color_table_ycbcr[0], c) << 2, (color_diff(color_table_ycbcr[1], c) << 2) + 1, (color_diff(color_table_ycbcr[2], c) << 2) + 2, (color_diff(color_table_ycbcr[3], c) << 2) + 3);
  2441. const uint32_t best_index = (uint32_t)best_index_err & 3;
  2442. err += color_diff(block_ycbcr[subset * 2 + y][x], color_table_ycbcr[best_index]);
  2443. }
  2444. if (err >= best_err)
  2445. break;
  2446. }
  2447. }
  2448. else
  2449. {
  2450. for (uint32_t y = 0; y < 4; y++)
  2451. {
  2452. for (uint32_t x = 0; x < 2; x++)
  2453. {
  2454. const ycbcr& c = decoded_uastc_block_ycbcr[y][subset * 2 + x];
  2455. const uint64_t best_index_err = minimum(color_diff(color_table_ycbcr[0], c) << 2, (color_diff(color_table_ycbcr[1], c) << 2) + 1, (color_diff(color_table_ycbcr[2], c) << 2) + 2, (color_diff(color_table_ycbcr[3], c) << 2) + 3);
  2456. const uint32_t best_index = (uint32_t)best_index_err & 3;
  2457. err += color_diff(block_ycbcr[y][subset * 2 + x], color_table_ycbcr[best_index]);
  2458. }
  2459. if (err >= best_err)
  2460. break;
  2461. }
  2462. }
  2463. } // subset
  2464. if (err < best_err)
  2465. {
  2466. best_err = err;
  2467. best_etc1_blk = trial_block;
  2468. best_etc1_bias = bias;
  2469. }
  2470. } // bias_iter
  2471. } // individ
  2472. } // flip
  2473. }
  2474. struct uastc_pack_eac_a8_results
  2475. {
  2476. uint32_t m_base;
  2477. uint32_t m_table;
  2478. uint32_t m_multiplier;
  2479. };
  2480. static uint64_t uastc_pack_eac_a8(uastc_pack_eac_a8_results& results, const uint8_t* pPixels, uint32_t num_pixels, uint32_t base_search_rad, uint32_t mul_search_rad, uint32_t table_mask)
  2481. {
  2482. assert(num_pixels <= 16);
  2483. uint32_t min_alpha = 255, max_alpha = 0;
  2484. for (uint32_t i = 0; i < num_pixels; i++)
  2485. {
  2486. const uint32_t a = pPixels[i];
  2487. if (a < min_alpha) min_alpha = a;
  2488. if (a > max_alpha) max_alpha = a;
  2489. }
  2490. if (min_alpha == max_alpha)
  2491. {
  2492. results.m_base = min_alpha;
  2493. results.m_table = 13;
  2494. results.m_multiplier = 1;
  2495. return 0;
  2496. }
  2497. const uint32_t alpha_range = max_alpha - min_alpha;
  2498. uint64_t best_err = UINT64_MAX;
  2499. for (uint32_t table = 0; table < 16; table++)
  2500. {
  2501. if ((table_mask & (1U << table)) == 0)
  2502. continue;
  2503. const float range = (float)(g_etc2_eac_tables[table][ETC2_EAC_MAX_VALUE_SELECTOR] - g_etc2_eac_tables[table][ETC2_EAC_MIN_VALUE_SELECTOR]);
  2504. const int center = (int)roundf(lerp((float)min_alpha, (float)max_alpha, (float)(0 - g_etc2_eac_tables[table][ETC2_EAC_MIN_VALUE_SELECTOR]) / range));
  2505. const int base_min = clamp255(center - base_search_rad);
  2506. const int base_max = clamp255(center + base_search_rad);
  2507. const int mul = (int)roundf(alpha_range / range);
  2508. const int mul_low = clamp<int>(mul - mul_search_rad, 1, 15);
  2509. const int mul_high = clamp<int>(mul + mul_search_rad, 1, 15);
  2510. for (int base = base_min; base <= base_max; base++)
  2511. {
  2512. for (int multiplier = mul_low; multiplier <= mul_high; multiplier++)
  2513. {
  2514. uint64_t total_err = 0;
  2515. for (uint32_t i = 0; i < num_pixels; i++)
  2516. {
  2517. const int a = pPixels[i];
  2518. uint32_t best_s_err = UINT32_MAX;
  2519. //uint32_t best_s = 0;
  2520. for (uint32_t s = 0; s < 8; s++)
  2521. {
  2522. const int v = clamp255((int)multiplier * g_etc2_eac_tables[table][s] + (int)base);
  2523. uint32_t err = iabs(a - v);
  2524. if (err < best_s_err)
  2525. {
  2526. best_s_err = err;
  2527. //best_s = s;
  2528. }
  2529. }
  2530. total_err += best_s_err * best_s_err;
  2531. if (total_err >= best_err)
  2532. break;
  2533. }
  2534. if (total_err < best_err)
  2535. {
  2536. best_err = total_err;
  2537. results.m_base = base;
  2538. results.m_multiplier = multiplier;
  2539. results.m_table = table;
  2540. if (!best_err)
  2541. return best_err;
  2542. }
  2543. } // table
  2544. } // multiplier
  2545. } // base
  2546. return best_err;
  2547. }
  2548. const int32_t DEFAULT_BC7_ERROR_WEIGHT = 50;
  2549. const float UASTC_ERROR_THRESH = 1.3f;
  2550. // TODO: This is a quick hack to favor certain modes when we know we'll be followed up with an RDO postprocess.
  2551. static inline float get_uastc_mode_weight(uint32_t mode)
  2552. {
  2553. const float FAVORED_MODE_WEIGHT = .8f;
  2554. switch (mode)
  2555. {
  2556. case 0:
  2557. case 10:
  2558. return FAVORED_MODE_WEIGHT;
  2559. default:
  2560. break;
  2561. }
  2562. return 1.0f;
  2563. }
  2564. void encode_uastc(const uint8_t* pRGBAPixels, uastc_block& output_block, uint32_t flags)
  2565. {
  2566. // printf("encode_uastc: \n");
  2567. // for (int i = 0; i < 16; i++)
  2568. // printf("[%u %u %u %u] ", pRGBAPixels[i * 4 + 0], pRGBAPixels[i * 4 + 1], pRGBAPixels[i * 4 + 2], pRGBAPixels[i * 4 + 3]);
  2569. // printf("\n");
  2570. const color_rgba(*block)[4] = reinterpret_cast<const color_rgba(*)[4]>(pRGBAPixels);
  2571. bool solid_color = true, has_alpha = false, is_la = true;
  2572. const color_rgba first_color(block[0][0]);
  2573. for (uint32_t y = 0; y < 4; y++)
  2574. {
  2575. for (uint32_t x = 0; x < 4; x++)
  2576. {
  2577. if (block[y][x].a < 255)
  2578. has_alpha = true;
  2579. if (block[y][x] != first_color)
  2580. solid_color = false;
  2581. if ((block[y][x].r != block[y][x].g) || (block[y][x].r != block[y][x].b))
  2582. is_la = false;
  2583. }
  2584. }
  2585. if (solid_color)
  2586. {
  2587. // Solid color blocks are so common that we handle them specially and as quickly as we can.
  2588. uastc_encode_results solid_results;
  2589. solid_results.m_uastc_mode = UASTC_MODE_INDEX_SOLID_COLOR;
  2590. solid_results.m_astc_err = 0;
  2591. solid_results.m_common_pattern = 0;
  2592. solid_results.m_solid_color = first_color;
  2593. memset(&solid_results.m_astc, 0, sizeof(solid_results.m_astc));
  2594. etc_block etc1_blk;
  2595. uint32_t etc1_bias = 0;
  2596. pack_etc1_block_solid_color(etc1_blk, &first_color.m_comps[0]);
  2597. eac_a8_block eac_a8_blk;
  2598. eac_a8_blk.m_table = 0;
  2599. eac_a8_blk.m_multiplier = 1;
  2600. pack_uastc(output_block, solid_results, etc1_blk, etc1_bias, eac_a8_blk, false, false);
  2601. // printf(" Solid\n");
  2602. return;
  2603. }
  2604. int level = flags & 7;
  2605. const bool favor_uastc_error = (flags & cPackUASTCFavorUASTCError) != 0;
  2606. const bool favor_bc7_error = !favor_uastc_error && ((flags & cPackUASTCFavorBC7Error) != 0);
  2607. //const bool etc1_perceptual = true;
  2608. // TODO: This uses 64KB of stack space!
  2609. uastc_encode_results results[MAX_ENCODE_RESULTS];
  2610. level = clampi(level, cPackUASTCLevelFastest, cPackUASTCLevelVerySlow);
  2611. // Set all options to slowest, then configure from there depending on the selected level.
  2612. uint32_t mode_mask = UINT32_MAX;
  2613. uint32_t uber_level = 6;
  2614. bool estimate_partition = false;
  2615. bool always_try_alpha_modes = true;
  2616. uint32_t eac_a8_mul_search_rad = 3;
  2617. uint32_t eac_a8_table_mask = UINT32_MAX;
  2618. uint32_t least_squares_passes = 2;
  2619. bool bc1_hints = true;
  2620. bool only_use_la_on_transparent_blocks = false;
  2621. switch (level)
  2622. {
  2623. case cPackUASTCLevelFastest:
  2624. {
  2625. mode_mask = (1 << 0) | (1 << 8) |
  2626. (1 << 11) | (1 << 12) |
  2627. (1 << 15);
  2628. always_try_alpha_modes = false;
  2629. eac_a8_mul_search_rad = 0;
  2630. eac_a8_table_mask = (1 << 2) | (1 << 8) | (1 << 11) | (1 << 13);
  2631. uber_level = 0;
  2632. least_squares_passes = 1;
  2633. bc1_hints = false;
  2634. estimate_partition = true;
  2635. only_use_la_on_transparent_blocks = true;
  2636. break;
  2637. }
  2638. case cPackUASTCLevelFaster:
  2639. {
  2640. mode_mask = (1 << 0) | (1 << 4) | (1 << 6) | (1 << 8) |
  2641. (1 << 9) | (1 << 11) | (1 << 12) |
  2642. (1 << 15) | (1 << 17);
  2643. always_try_alpha_modes = false;
  2644. eac_a8_mul_search_rad = 0;
  2645. eac_a8_table_mask = (1 << 2) | (1 << 8) | (1 << 11) | (1 << 13);
  2646. uber_level = 0;
  2647. least_squares_passes = 1;
  2648. estimate_partition = true;
  2649. break;
  2650. }
  2651. case cPackUASTCLevelDefault:
  2652. {
  2653. mode_mask = (1 << 0) | (1 << 1) | (1 << 4) | (1 << 5) | (1 << 6) | (1 << 8) |
  2654. (1 << 9) | (1 << 10) | (1 << 11) | (1 << 12) | (1 << 13) |
  2655. (1 << 15) | (1 << 16) | (1 << 17);
  2656. always_try_alpha_modes = false;
  2657. eac_a8_mul_search_rad = 1;
  2658. eac_a8_table_mask = (1 << 0) | (1 << 2) | (1 << 6) | (1 << 7) | (1 << 8) | (1 << 10) | (1 << 11) | (1 << 13);
  2659. uber_level = 1;
  2660. least_squares_passes = 1;
  2661. estimate_partition = true;
  2662. break;
  2663. }
  2664. case cPackUASTCLevelSlower:
  2665. {
  2666. always_try_alpha_modes = false;
  2667. eac_a8_mul_search_rad = 2;
  2668. uber_level = 3;
  2669. estimate_partition = true;
  2670. break;
  2671. }
  2672. case cPackUASTCLevelVerySlow:
  2673. {
  2674. break;
  2675. }
  2676. }
  2677. #if BASISU_SUPPORT_FORCE_MODE
  2678. static int force_mode = -1;
  2679. force_mode = (force_mode + 1) % TOTAL_UASTC_MODES;
  2680. mode_mask = UINT32_MAX;
  2681. always_try_alpha_modes = true;
  2682. only_use_la_on_transparent_blocks = false;
  2683. #endif
  2684. // HACK HACK
  2685. //mode_mask &= ~(1 << 18);
  2686. //mode_mask = (1 << 18)| (1 << 10);
  2687. uint32_t total_results = 0;
  2688. if (only_use_la_on_transparent_blocks)
  2689. {
  2690. if ((is_la) && (!has_alpha))
  2691. is_la = false;
  2692. }
  2693. const bool try_alpha_modes = has_alpha || always_try_alpha_modes;
  2694. bc7enc_compress_block_params comp_params;
  2695. memset(&comp_params, 0, sizeof(comp_params));
  2696. comp_params.m_max_partitions_mode1 = 64;
  2697. comp_params.m_least_squares_passes = least_squares_passes;
  2698. comp_params.m_weights[0] = 1;
  2699. comp_params.m_weights[1] = 1;
  2700. comp_params.m_weights[2] = 1;
  2701. comp_params.m_weights[3] = 1;
  2702. comp_params.m_uber_level = uber_level;
  2703. if (is_la)
  2704. {
  2705. if (mode_mask & (1U << 15))
  2706. astc_mode15(block, results, total_results, comp_params);
  2707. if (mode_mask & (1U << 16))
  2708. astc_mode9_or_16(16, block, results, total_results, comp_params, estimate_partition ? 4 : 0);
  2709. if (mode_mask & (1U << 17))
  2710. astc_mode11_or_17(17, block, results, total_results, comp_params);
  2711. }
  2712. if (!has_alpha)
  2713. {
  2714. if (mode_mask & (1U << 0))
  2715. astc_mode0_or_18(0, block, results, total_results, comp_params);
  2716. if (mode_mask & (1U << 1))
  2717. astc_mode1(block, results, total_results, comp_params);
  2718. if (mode_mask & (1U << 2))
  2719. astc_mode2(block, results, total_results, comp_params, estimate_partition);
  2720. if (mode_mask & (1U << 3))
  2721. astc_mode3(block, results, total_results, comp_params, estimate_partition);
  2722. if (mode_mask & (1U << 4))
  2723. astc_mode4(block, results, total_results, comp_params, estimate_partition);
  2724. if (mode_mask & (1U << 5))
  2725. astc_mode5(block, results, total_results, comp_params);
  2726. if (mode_mask & (1U << 6))
  2727. astc_mode6(block, results, total_results, comp_params);
  2728. if (mode_mask & (1U << 7))
  2729. astc_mode7(block, results, total_results, comp_params, estimate_partition);
  2730. if (mode_mask & (1U << 18))
  2731. astc_mode0_or_18(18, block, results, total_results, comp_params);
  2732. }
  2733. if (try_alpha_modes)
  2734. {
  2735. if (mode_mask & (1U << 9))
  2736. astc_mode9_or_16(9, block, results, total_results, comp_params, estimate_partition ? 4 : 0);
  2737. if (mode_mask & (1U << 10))
  2738. astc_mode10(block, results, total_results, comp_params);
  2739. if (mode_mask & (1U << 11))
  2740. astc_mode11_or_17(11, block, results, total_results, comp_params);
  2741. if (mode_mask & (1U << 12))
  2742. astc_mode12(block, results, total_results, comp_params);
  2743. if (mode_mask & (1U << 13))
  2744. astc_mode13(block, results, total_results, comp_params);
  2745. if (mode_mask & (1U << 14))
  2746. astc_mode14(block, results, total_results, comp_params);
  2747. }
  2748. assert(total_results);
  2749. // Fix up the errors so we consistently have LA, RGB, or RGBA error.
  2750. for (uint32_t i = 0; i < total_results; i++)
  2751. {
  2752. uastc_encode_results& r = results[i];
  2753. if (!is_la)
  2754. {
  2755. if (g_uastc_mode_is_la[r.m_uastc_mode])
  2756. {
  2757. color_rgba unpacked_block[16];
  2758. unpack_uastc(r.m_uastc_mode, r.m_common_pattern, r.m_solid_color.get_color32(), r.m_astc, (basist::color32 *)unpacked_block, false);
  2759. uint64_t total_err = 0;
  2760. for (uint32_t j = 0; j < 16; j++)
  2761. total_err += color_distance(unpacked_block[j], ((const color_rgba*)block)[j], true);
  2762. r.m_astc_err = total_err;
  2763. }
  2764. }
  2765. else
  2766. {
  2767. if (!g_uastc_mode_is_la[r.m_uastc_mode])
  2768. {
  2769. color_rgba unpacked_block[16];
  2770. unpack_uastc(r.m_uastc_mode, r.m_common_pattern, r.m_solid_color.get_color32(), r.m_astc, (basist::color32 *)unpacked_block, false);
  2771. uint64_t total_err = 0;
  2772. for (uint32_t j = 0; j < 16; j++)
  2773. total_err += color_distance_la(unpacked_block[j], ((const color_rgba*)block)[j]);
  2774. r.m_astc_err = total_err;
  2775. }
  2776. }
  2777. }
  2778. unpacked_uastc_block unpacked_ublock;
  2779. memset(&unpacked_ublock, 0, sizeof(unpacked_ublock));
  2780. uint64_t total_overall_err[MAX_ENCODE_RESULTS];
  2781. float uastc_err_f[MAX_ENCODE_RESULTS];
  2782. double best_uastc_err_f = 1e+20f;
  2783. int best_index = -1;
  2784. if (total_results == 1)
  2785. {
  2786. best_index = 0;
  2787. }
  2788. else
  2789. {
  2790. const uint32_t bc7_err_weight = favor_bc7_error ? 100 : ((favor_uastc_error ? 0 : DEFAULT_BC7_ERROR_WEIGHT));
  2791. const uint32_t uastc_err_weight = favor_bc7_error ? 0 : 100;
  2792. // Find best overall results, balancing UASTC and UASTC->BC7 error.
  2793. // We purposely allow UASTC error to increase a little, if doing so lowers the BC7 error.
  2794. for (uint32_t i = 0; i < total_results; i++)
  2795. {
  2796. #if BASISU_SUPPORT_FORCE_MODE
  2797. if (results[i].m_uastc_mode == force_mode)
  2798. {
  2799. best_index = i;
  2800. break;
  2801. }
  2802. #endif
  2803. unpacked_ublock.m_mode = results[i].m_uastc_mode;
  2804. unpacked_ublock.m_astc = results[i].m_astc;
  2805. unpacked_ublock.m_common_pattern = results[i].m_common_pattern;
  2806. unpacked_ublock.m_solid_color = results[i].m_solid_color.get_color32();
  2807. color_rgba decoded_uastc_block[4][4];
  2808. bool success = unpack_uastc(results[i].m_uastc_mode, results[i].m_common_pattern, results[i].m_solid_color.get_color32(), results[i].m_astc, (basist::color32 *)&decoded_uastc_block[0][0], false);
  2809. (void)success;
  2810. VALIDATE(success);
  2811. uint64_t total_uastc_rgb_err, total_uastc_rgba_err, total_uastc_la_err;
  2812. compute_block_error(block, decoded_uastc_block, total_uastc_rgb_err, total_uastc_rgba_err, total_uastc_la_err);
  2813. // Validate the computed error, or we're go mad if it's inaccurate.
  2814. if (results[i].m_uastc_mode == UASTC_MODE_INDEX_SOLID_COLOR)
  2815. {
  2816. VALIDATE(total_uastc_rgba_err == 0);
  2817. }
  2818. else if (is_la)
  2819. {
  2820. VALIDATE(total_uastc_la_err == results[i].m_astc_err);
  2821. }
  2822. else if (g_uastc_mode_has_alpha[results[i].m_uastc_mode])
  2823. {
  2824. VALIDATE(total_uastc_rgba_err == results[i].m_astc_err);
  2825. }
  2826. else
  2827. {
  2828. VALIDATE(total_uastc_rgb_err == results[i].m_astc_err);
  2829. }
  2830. // Transcode to BC7
  2831. bc7_optimization_results bc7_results;
  2832. transcode_uastc_to_bc7(unpacked_ublock, bc7_results);
  2833. bc7_block bc7_data;
  2834. encode_bc7_block(&bc7_data, &bc7_results);
  2835. color_rgba decoded_bc7_block[4][4];
  2836. unpack_block(texture_format::cBC7, &bc7_data, &decoded_bc7_block[0][0]);
  2837. // Compute BC7 error
  2838. uint64_t total_bc7_la_err, total_bc7_rgb_err, total_bc7_rgba_err;
  2839. compute_block_error(block, decoded_bc7_block, total_bc7_rgb_err, total_bc7_rgba_err, total_bc7_la_err);
  2840. if (results[i].m_uastc_mode == UASTC_MODE_INDEX_SOLID_COLOR)
  2841. {
  2842. VALIDATE(total_bc7_rgba_err == 0);
  2843. best_index = i;
  2844. break;
  2845. }
  2846. uint64_t total_uastc_err = 0, total_bc7_err = 0;
  2847. if (is_la)
  2848. {
  2849. total_bc7_err = total_bc7_la_err;
  2850. total_uastc_err = total_uastc_la_err;
  2851. }
  2852. else if (has_alpha)
  2853. {
  2854. total_bc7_err = total_bc7_rgba_err;
  2855. total_uastc_err = total_uastc_rgba_err;
  2856. }
  2857. else
  2858. {
  2859. total_bc7_err = total_bc7_rgb_err;
  2860. total_uastc_err = total_uastc_rgb_err;
  2861. }
  2862. total_overall_err[i] = ((total_bc7_err * bc7_err_weight) / 100) + ((total_uastc_err * uastc_err_weight) / 100);
  2863. if (!total_overall_err[i])
  2864. {
  2865. best_index = i;
  2866. break;
  2867. }
  2868. uastc_err_f[i] = sqrtf((float)total_uastc_err);
  2869. if (uastc_err_f[i] < best_uastc_err_f)
  2870. {
  2871. best_uastc_err_f = uastc_err_f[i];
  2872. }
  2873. } // total_results
  2874. if (best_index < 0)
  2875. {
  2876. uint64_t best_err = UINT64_MAX;
  2877. if ((best_uastc_err_f == 0.0f) || (favor_bc7_error))
  2878. {
  2879. for (uint32_t i = 0; i < total_results; i++)
  2880. {
  2881. // TODO: This is a quick hack to favor modes 0 or 10 for better RDO compression.
  2882. const float err_weight = (flags & cPackUASTCFavorSimplerModes) ? get_uastc_mode_weight(results[i].m_uastc_mode) : 1.0f;
  2883. const uint64_t w = (uint64_t)(total_overall_err[i] * err_weight);
  2884. if (w < best_err)
  2885. {
  2886. best_err = w;
  2887. best_index = i;
  2888. if (!best_err)
  2889. break;
  2890. }
  2891. } // i
  2892. }
  2893. else
  2894. {
  2895. // Scan the UASTC results, and consider all results within a window that has the best UASTC+BC7 error.
  2896. for (uint32_t i = 0; i < total_results; i++)
  2897. {
  2898. double err_delta = uastc_err_f[i] / best_uastc_err_f;
  2899. if (err_delta <= UASTC_ERROR_THRESH)
  2900. {
  2901. // TODO: This is a quick hack to favor modes 0 or 10 for better RDO compression.
  2902. const float err_weight = (flags & cPackUASTCFavorSimplerModes) ? get_uastc_mode_weight(results[i].m_uastc_mode) : 1.0f;
  2903. const uint64_t w = (uint64_t)(total_overall_err[i] * err_weight);
  2904. if (w < best_err)
  2905. {
  2906. best_err = w;
  2907. best_index = i;
  2908. if (!best_err)
  2909. break;
  2910. }
  2911. }
  2912. } // i
  2913. }
  2914. }
  2915. }
  2916. const uastc_encode_results& best_results = results[best_index];
  2917. const uint32_t best_mode = best_results.m_uastc_mode;
  2918. const astc_block_desc& best_astc_results = best_results.m_astc;
  2919. color_rgba decoded_uastc_block[4][4];
  2920. bool success = unpack_uastc(best_mode, best_results.m_common_pattern, best_results.m_solid_color.get_color32(), best_astc_results, (basist::color32 *)&decoded_uastc_block[0][0], false);
  2921. (void)success;
  2922. VALIDATE(success);
  2923. #if BASISU_VALIDATE_UASTC_ENC
  2924. // Make sure that the UASTC block unpacks to the same exact pixels as the ASTC block does, using two different decoders.
  2925. {
  2926. // Round trip to packed UASTC and back, then decode to pixels.
  2927. etc_block etc1_blk;
  2928. memset(&etc1_blk, 0, sizeof(etc1_blk));
  2929. eac_a8_block etc_eac_a8_blk;
  2930. memset(&etc_eac_a8_blk, 0, sizeof(etc_eac_a8_blk));
  2931. etc_eac_a8_blk.m_multiplier = 1;
  2932. basist::uastc_block temp_block;
  2933. pack_uastc(temp_block, best_results, etc1_blk, 0, etc_eac_a8_blk, false, false);
  2934. basist::color32 temp_block_unpacked[4][4];
  2935. success = basist::unpack_uastc(temp_block, (basist::color32 *)temp_block_unpacked, false);
  2936. VALIDATE(success);
  2937. // Now round trip to packed ASTC and back, then decode to pixels.
  2938. uint32_t astc_data[4];
  2939. if (best_results.m_uastc_mode == UASTC_MODE_INDEX_SOLID_COLOR)
  2940. pack_astc_solid_block(astc_data, (color32 &)best_results.m_solid_color);
  2941. else
  2942. {
  2943. success = pack_astc_block(astc_data, &best_astc_results, best_results.m_uastc_mode);
  2944. VALIDATE(success);
  2945. }
  2946. color_rgba decoded_astc_block[4][4];
  2947. success = basisu_astc::astc::decompress_ldr((uint8_t*)decoded_astc_block, (uint8_t*)&astc_data, false, 4, 4);
  2948. VALIDATE(success);
  2949. for (uint32_t y = 0; y < 4; y++)
  2950. {
  2951. for (uint32_t x = 0; x < 4; x++)
  2952. {
  2953. VALIDATE(decoded_astc_block[y][x] == decoded_uastc_block[y][x]);
  2954. VALIDATE(temp_block_unpacked[y][x].c[0] == decoded_uastc_block[y][x].r);
  2955. VALIDATE(temp_block_unpacked[y][x].c[1] == decoded_uastc_block[y][x].g);
  2956. VALIDATE(temp_block_unpacked[y][x].c[2] == decoded_uastc_block[y][x].b);
  2957. VALIDATE(temp_block_unpacked[y][x].c[3] == decoded_uastc_block[y][x].a);
  2958. }
  2959. }
  2960. }
  2961. #endif
  2962. // Compute BC1 hints
  2963. bool bc1_hint0 = false, bc1_hint1 = false;
  2964. if (bc1_hints)
  2965. compute_bc1_hints(bc1_hint0, bc1_hint1, best_results, block, decoded_uastc_block);
  2966. eac_a8_block eac_a8_blk;
  2967. if ((g_uastc_mode_has_alpha[best_mode]) && (best_mode != UASTC_MODE_INDEX_SOLID_COLOR))
  2968. {
  2969. // Compute ETC2 hints
  2970. uint8_t decoded_uastc_block_alpha[16];
  2971. for (uint32_t i = 0; i < 16; i++)
  2972. decoded_uastc_block_alpha[i] = decoded_uastc_block[i >> 2][i & 3].a;
  2973. uastc_pack_eac_a8_results eac8_a8_results;
  2974. memset(&eac8_a8_results, 0, sizeof(eac8_a8_results));
  2975. uastc_pack_eac_a8(eac8_a8_results, decoded_uastc_block_alpha, 16, 0, eac_a8_mul_search_rad, eac_a8_table_mask);
  2976. // All we care about for hinting is the table and multiplier.
  2977. eac_a8_blk.m_table = eac8_a8_results.m_table;
  2978. eac_a8_blk.m_multiplier = eac8_a8_results.m_multiplier;
  2979. }
  2980. else
  2981. {
  2982. memset(&eac_a8_blk, 0, sizeof(eac_a8_blk));
  2983. }
  2984. // Compute ETC1 hints
  2985. etc_block etc1_blk;
  2986. uint32_t etc1_bias = 0;
  2987. compute_etc1_hints(etc1_blk, etc1_bias, best_results, block, decoded_uastc_block, level, flags);
  2988. // Finally, pack the UASTC block with its hints and we're done.
  2989. pack_uastc(output_block, best_results, etc1_blk, etc1_bias, eac_a8_blk, bc1_hint0, bc1_hint1);
  2990. // printf(" Packed: ");
  2991. // for (int i = 0; i < 16; i++)
  2992. // printf("%X ", output_block.m_bytes[i]);
  2993. // printf("\n");
  2994. }
  2995. static bool uastc_recompute_hints(basist::uastc_block* pBlock, const color_rgba* pBlock_pixels, uint32_t flags, const unpacked_uastc_block *pUnpacked_blk)
  2996. {
  2997. unpacked_uastc_block unpacked_blk;
  2998. if (pUnpacked_blk)
  2999. unpacked_blk = *pUnpacked_blk;
  3000. else
  3001. {
  3002. if (!unpack_uastc(*pBlock, unpacked_blk, false, true))
  3003. return false;
  3004. }
  3005. color_rgba decoded_uastc_block[4][4];
  3006. if (!unpack_uastc(unpacked_blk, (basist::color32 *)decoded_uastc_block, false))
  3007. return false;
  3008. uastc_encode_results results;
  3009. results.m_uastc_mode = unpacked_blk.m_mode;
  3010. results.m_common_pattern = unpacked_blk.m_common_pattern;
  3011. results.m_astc = unpacked_blk.m_astc;
  3012. results.m_solid_color = unpacked_blk.m_solid_color;
  3013. results.m_astc_err = 0;
  3014. bool bc1_hints = true;
  3015. uint32_t eac_a8_mul_search_rad = 3;
  3016. uint32_t eac_a8_table_mask = UINT32_MAX;
  3017. const uint32_t level = flags & cPackUASTCLevelMask;
  3018. switch (level)
  3019. {
  3020. case cPackUASTCLevelFastest:
  3021. {
  3022. eac_a8_mul_search_rad = 0;
  3023. eac_a8_table_mask = (1 << 2) | (1 << 8) | (1 << 11) | (1 << 13);
  3024. bc1_hints = false;
  3025. break;
  3026. }
  3027. case cPackUASTCLevelFaster:
  3028. {
  3029. eac_a8_mul_search_rad = 0;
  3030. eac_a8_table_mask = (1 << 2) | (1 << 8) | (1 << 11) | (1 << 13);
  3031. break;
  3032. }
  3033. case cPackUASTCLevelDefault:
  3034. {
  3035. eac_a8_mul_search_rad = 1;
  3036. eac_a8_table_mask = (1 << 0) | (1 << 2) | (1 << 6) | (1 << 7) | (1 << 8) | (1 << 10) | (1 << 11) | (1 << 13);
  3037. break;
  3038. }
  3039. case cPackUASTCLevelSlower:
  3040. {
  3041. eac_a8_mul_search_rad = 2;
  3042. break;
  3043. }
  3044. case cPackUASTCLevelVerySlow:
  3045. {
  3046. break;
  3047. }
  3048. }
  3049. bool bc1_hint0 = false, bc1_hint1 = false;
  3050. if (bc1_hints)
  3051. compute_bc1_hints(bc1_hint0, bc1_hint1, results, (color_rgba (*)[4])pBlock_pixels, decoded_uastc_block);
  3052. const uint32_t best_mode = unpacked_blk.m_mode;
  3053. eac_a8_block eac_a8_blk;
  3054. if ((g_uastc_mode_has_alpha[best_mode]) && (best_mode != UASTC_MODE_INDEX_SOLID_COLOR))
  3055. {
  3056. uint8_t decoded_uastc_block_alpha[16];
  3057. for (uint32_t i = 0; i < 16; i++)
  3058. decoded_uastc_block_alpha[i] = decoded_uastc_block[i >> 2][i & 3].a;
  3059. uastc_pack_eac_a8_results eac8_a8_results;
  3060. memset(&eac8_a8_results, 0, sizeof(eac8_a8_results));
  3061. uastc_pack_eac_a8(eac8_a8_results, decoded_uastc_block_alpha, 16, 0, eac_a8_mul_search_rad, eac_a8_table_mask);
  3062. eac_a8_blk.m_table = eac8_a8_results.m_table;
  3063. eac_a8_blk.m_multiplier = eac8_a8_results.m_multiplier;
  3064. }
  3065. else
  3066. {
  3067. memset(&eac_a8_blk, 0, sizeof(eac_a8_blk));
  3068. }
  3069. etc_block etc1_blk;
  3070. uint32_t etc1_bias = 0;
  3071. compute_etc1_hints(etc1_blk, etc1_bias, results, (color_rgba (*)[4])pBlock_pixels, decoded_uastc_block, level, flags);
  3072. pack_uastc(*pBlock, results, etc1_blk, etc1_bias, eac_a8_blk, bc1_hint0, bc1_hint1);
  3073. return true;
  3074. }
  3075. static const uint8_t g_uastc_mode_selector_bits[TOTAL_UASTC_MODES][2] =
  3076. {
  3077. { 65, 63 }, { 69, 31 }, { 73, 46 }, { 89, 29 },
  3078. { 89, 30 }, { 68, 47 }, { 66, 62 }, { 89, 30 },
  3079. { 0, 0 }, { 97, 30 }, { 65, 63 }, { 66, 62 },
  3080. { 81, 47 }, { 94, 30 }, { 92, 31 }, { 62, 63 },
  3081. { 98, 30 }, { 61, 62 }, { 49, 79 }
  3082. };
  3083. static inline uint32_t set_block_bits(uint8_t* pBytes, uint64_t val, uint32_t num_bits, uint32_t cur_ofs)
  3084. {
  3085. assert(num_bits <= 64);
  3086. assert((num_bits == 64) || (val < (1ULL << num_bits)));
  3087. uint64_t mask = (num_bits == 64) ? UINT64_MAX : ((1ULL << num_bits) - 1);
  3088. while (num_bits)
  3089. {
  3090. const uint32_t n = basisu::minimum<uint32_t>(8U - (cur_ofs & 7U), num_bits);
  3091. pBytes[cur_ofs >> 3] &= ~static_cast<uint8_t>(mask << (cur_ofs & 7U));
  3092. pBytes[cur_ofs >> 3] |= static_cast<uint8_t>(val << (cur_ofs & 7U));
  3093. val >>= n;
  3094. mask >>= n;
  3095. num_bits -= n;
  3096. cur_ofs += n;
  3097. }
  3098. return cur_ofs;
  3099. }
  3100. static const uint8_t g_tdefl_small_dist_extra[512] =
  3101. {
  3102. 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5,
  3103. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  3104. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  3105. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  3106. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  3107. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  3108. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  3109. 7, 7, 7, 7, 7, 7, 7, 7
  3110. };
  3111. static const uint8_t g_tdefl_large_dist_extra[128] =
  3112. {
  3113. 0, 0, 8, 8, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
  3114. 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13,
  3115. 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13
  3116. };
  3117. static inline uint32_t compute_match_cost_estimate(uint32_t dist)
  3118. {
  3119. uint32_t len_cost = 7;
  3120. uint32_t dist_cost = 5;
  3121. if (dist < 512)
  3122. dist_cost += g_tdefl_small_dist_extra[dist & 511];
  3123. else
  3124. {
  3125. dist_cost += g_tdefl_large_dist_extra[basisu::minimum<uint32_t>(dist, 32767) >> 8];
  3126. while (dist >= 32768)
  3127. {
  3128. dist_cost++;
  3129. dist >>= 1;
  3130. }
  3131. }
  3132. return len_cost + dist_cost;
  3133. }
  3134. struct selector_bitsequence
  3135. {
  3136. uint64_t m_sel;
  3137. uint32_t m_ofs;
  3138. uint32_t m_pad; // avoid implicit padding for selector_bitsequence_hash
  3139. selector_bitsequence() { }
  3140. selector_bitsequence(uint32_t bit_ofs, uint64_t sel) : m_sel(sel), m_ofs(bit_ofs), m_pad(0) { }
  3141. bool operator== (const selector_bitsequence& other) const
  3142. {
  3143. return (m_ofs == other.m_ofs) && (m_sel == other.m_sel);
  3144. }
  3145. bool operator< (const selector_bitsequence& other) const
  3146. {
  3147. if (m_ofs < other.m_ofs)
  3148. return true;
  3149. else if (m_ofs == other.m_ofs)
  3150. return m_sel < other.m_sel;
  3151. return false;
  3152. }
  3153. };
  3154. struct selector_bitsequence_hash
  3155. {
  3156. std::size_t operator()(selector_bitsequence const& s) const noexcept
  3157. {
  3158. return hash_hsieh((const uint8_t*)&s, sizeof(s));
  3159. }
  3160. };
  3161. class tracked_stat
  3162. {
  3163. public:
  3164. tracked_stat() { clear(); }
  3165. void clear() { m_num = 0; m_total = 0; m_total2 = 0; }
  3166. void update(uint32_t val) { m_num++; m_total += val; m_total2 += val * val; }
  3167. tracked_stat& operator += (uint32_t val) { update(val); return *this; }
  3168. uint32_t get_number_of_values() { return m_num; }
  3169. uint64_t get_total() const { return m_total; }
  3170. uint64_t get_total2() const { return m_total2; }
  3171. float get_average() const { return m_num ? (float)m_total / m_num : 0.0f; };
  3172. float get_std_dev() const { return m_num ? sqrtf((float)(m_num * m_total2 - m_total * m_total)) / m_num : 0.0f; }
  3173. float get_variance() const { float s = get_std_dev(); return s * s; }
  3174. private:
  3175. uint32_t m_num;
  3176. uint64_t m_total;
  3177. uint64_t m_total2;
  3178. };
  3179. static bool uastc_rdo_blocks(uint32_t first_index, uint32_t last_index, basist::uastc_block* pBlocks, const color_rgba* pBlock_pixels, const uastc_rdo_params& params, uint32_t flags,
  3180. uint32_t &total_skipped, uint32_t &total_refined, uint32_t &total_modified, uint32_t &total_smooth)
  3181. {
  3182. debug_printf("uastc_rdo_blocks: Processing blocks %u to %u\n", first_index, last_index);
  3183. const int total_blocks_to_check = basisu::maximum<uint32_t>(1U, params.m_lz_dict_size / sizeof(basist::uastc_block));
  3184. const bool perceptual = false;
  3185. std::unordered_map<selector_bitsequence, uint32_t, selector_bitsequence_hash> selector_history;
  3186. for (uint32_t block_index = first_index; block_index < last_index; block_index++)
  3187. {
  3188. const basist::uastc_block& blk = pBlocks[block_index];
  3189. const color_rgba* pPixels = &pBlock_pixels[16 * block_index];
  3190. unpacked_uastc_block unpacked_blk;
  3191. if (!unpack_uastc(blk, unpacked_blk, false, true))
  3192. return false;
  3193. const uint32_t block_mode = unpacked_blk.m_mode;
  3194. if (block_mode == UASTC_MODE_INDEX_SOLID_COLOR)
  3195. continue;
  3196. tracked_stat r_stats, g_stats, b_stats, a_stats;
  3197. for (uint32_t i = 0; i < 16; i++)
  3198. {
  3199. r_stats.update(pPixels[i].r);
  3200. g_stats.update(pPixels[i].g);
  3201. b_stats.update(pPixels[i].b);
  3202. a_stats.update(pPixels[i].a);
  3203. }
  3204. const float max_std_dev = basisu::maximum<float>(basisu::maximum<float>(basisu::maximum(r_stats.get_std_dev(), g_stats.get_std_dev()), b_stats.get_std_dev()), a_stats.get_std_dev());
  3205. float yl = clamp<float>(max_std_dev / params.m_max_smooth_block_std_dev, 0.0f, 1.0f);
  3206. yl = yl * yl;
  3207. const float smooth_block_error_scale = lerp<float>(params.m_smooth_block_max_error_scale, 1.0f, yl);
  3208. if (smooth_block_error_scale > 1.0f)
  3209. total_smooth++;
  3210. color_rgba decoded_uastc_block[4][4];
  3211. if (!unpack_uastc(unpacked_blk, (basist::color32*)decoded_uastc_block, false))
  3212. return false;
  3213. uint64_t uastc_err = 0;
  3214. for (uint32_t i = 0; i < 16; i++)
  3215. uastc_err += color_distance(perceptual, pPixels[i], ((color_rgba*)decoded_uastc_block)[i], true);
  3216. // Transcode to BC7
  3217. bc7_optimization_results b7_results;
  3218. if (!transcode_uastc_to_bc7(unpacked_blk, b7_results))
  3219. return false;
  3220. basist::bc7_block b7_block;
  3221. basist::encode_bc7_block(&b7_block, &b7_results);
  3222. color_rgba decoded_b7_blk[4][4];
  3223. unpack_block(texture_format::cBC7, &b7_block, &decoded_b7_blk[0][0]);
  3224. uint64_t bc7_err = 0;
  3225. for (uint32_t i = 0; i < 16; i++)
  3226. bc7_err += color_distance(perceptual, pPixels[i], ((color_rgba*)decoded_b7_blk)[i], true);
  3227. uint64_t cur_err = (uastc_err + bc7_err) / 2;
  3228. // Divide by 16*4 to compute RMS error
  3229. const float cur_ms_err = (float)cur_err * (1.0f / 64.0f);
  3230. const float cur_rms_err = sqrt(cur_ms_err);
  3231. const uint32_t first_sel_bit = g_uastc_mode_selector_bits[block_mode][0];
  3232. const uint32_t total_sel_bits = g_uastc_mode_selector_bits[block_mode][1];
  3233. assert(first_sel_bit + total_sel_bits <= 128);
  3234. assert(total_sel_bits > 0);
  3235. uint32_t cur_bit_offset = first_sel_bit;
  3236. uint64_t cur_sel_bits = read_bits((const uint8_t*)&blk, cur_bit_offset, basisu::minimum(64U, total_sel_bits));
  3237. if (cur_rms_err >= params.m_skip_block_rms_thresh)
  3238. {
  3239. auto cur_search_res = selector_history.insert(std::make_pair(selector_bitsequence(first_sel_bit, cur_sel_bits), block_index));
  3240. // Block already has too much error, so don't mess with it.
  3241. if (!cur_search_res.second)
  3242. (*cur_search_res.first).second = block_index;
  3243. total_skipped++;
  3244. continue;
  3245. }
  3246. int cur_bits;
  3247. auto cur_find_res = selector_history.find(selector_bitsequence(first_sel_bit, cur_sel_bits));
  3248. if (cur_find_res == selector_history.end())
  3249. {
  3250. // Wasn't found - wildly estimate literal cost
  3251. //cur_bits = (total_sel_bits * 5) / 4;
  3252. cur_bits = (total_sel_bits * params.m_lz_literal_cost) / 100;
  3253. }
  3254. else
  3255. {
  3256. // Was found - wildly estimate match cost
  3257. uint32_t match_block_index = cur_find_res->second;
  3258. const int block_dist_in_bytes = (block_index - match_block_index) * 16;
  3259. cur_bits = compute_match_cost_estimate(block_dist_in_bytes);
  3260. }
  3261. int first_block_to_check = basisu::maximum<int>(first_index, block_index - total_blocks_to_check);
  3262. int last_block_to_check = block_index - 1;
  3263. basist::uastc_block best_block(blk);
  3264. uint32_t best_block_index = block_index;
  3265. float best_t = cur_ms_err * smooth_block_error_scale + cur_bits * params.m_lambda;
  3266. // Now scan through previous blocks, insert their selector bit patterns into the current block, and find
  3267. // selector bit patterns which don't increase the overall block error too much.
  3268. for (int prev_block_index = last_block_to_check; prev_block_index >= first_block_to_check; --prev_block_index)
  3269. {
  3270. const basist::uastc_block& prev_blk = pBlocks[prev_block_index];
  3271. uint32_t bit_offset = first_sel_bit;
  3272. uint64_t sel_bits = read_bits((const uint8_t*)&prev_blk, bit_offset, basisu::minimum(64U, total_sel_bits));
  3273. int match_block_index = prev_block_index;
  3274. auto res = selector_history.find(selector_bitsequence(first_sel_bit, sel_bits));
  3275. if (res != selector_history.end())
  3276. match_block_index = res->second;
  3277. // Have we already checked this bit pattern? If so then skip this block.
  3278. if (match_block_index > prev_block_index)
  3279. continue;
  3280. unpacked_uastc_block unpacked_prev_blk;
  3281. if (!unpack_uastc(prev_blk, unpacked_prev_blk, false, true))
  3282. return false;
  3283. basist::uastc_block trial_blk(blk);
  3284. set_block_bits((uint8_t*)&trial_blk, sel_bits, basisu::minimum(64U, total_sel_bits), first_sel_bit);
  3285. if (total_sel_bits > 64)
  3286. {
  3287. sel_bits = read_bits((const uint8_t*)&prev_blk, bit_offset, total_sel_bits - 64U);
  3288. set_block_bits((uint8_t*)&trial_blk, sel_bits, total_sel_bits - 64U, first_sel_bit + basisu::minimum(64U, total_sel_bits));
  3289. }
  3290. unpacked_uastc_block unpacked_trial_blk;
  3291. if (!unpack_uastc(trial_blk, unpacked_trial_blk, false, true))
  3292. continue;
  3293. color_rgba decoded_trial_uastc_block[4][4];
  3294. if (!unpack_uastc(unpacked_trial_blk, (basist::color32*)decoded_trial_uastc_block, false))
  3295. continue;
  3296. uint64_t trial_uastc_err = 0;
  3297. for (uint32_t i = 0; i < 16; i++)
  3298. trial_uastc_err += color_distance(perceptual, pPixels[i], ((color_rgba*)decoded_trial_uastc_block)[i], true);
  3299. // Transcode trial to BC7, compute error
  3300. bc7_optimization_results trial_b7_results;
  3301. if (!transcode_uastc_to_bc7(unpacked_trial_blk, trial_b7_results))
  3302. return false;
  3303. basist::bc7_block trial_b7_block;
  3304. basist::encode_bc7_block(&trial_b7_block, &trial_b7_results);
  3305. color_rgba decoded_trial_b7_blk[4][4];
  3306. unpack_block(texture_format::cBC7, &trial_b7_block, &decoded_trial_b7_blk[0][0]);
  3307. uint64_t trial_bc7_err = 0;
  3308. for (uint32_t i = 0; i < 16; i++)
  3309. trial_bc7_err += color_distance(perceptual, pPixels[i], ((color_rgba*)decoded_trial_b7_blk)[i], true);
  3310. uint64_t trial_err = (trial_uastc_err + trial_bc7_err) / 2;
  3311. const float trial_ms_err = (float)trial_err * (1.0f / 64.0f);
  3312. const float trial_rms_err = sqrtf(trial_ms_err);
  3313. if (trial_rms_err > cur_rms_err * params.m_max_allowed_rms_increase_ratio)
  3314. continue;
  3315. const int block_dist_in_bytes = (block_index - match_block_index) * 16;
  3316. const int match_bits = compute_match_cost_estimate(block_dist_in_bytes);
  3317. float t = trial_ms_err * smooth_block_error_scale + match_bits * params.m_lambda;
  3318. if (t < best_t)
  3319. {
  3320. best_t = t;
  3321. best_block_index = prev_block_index;
  3322. best_block = trial_blk;
  3323. }
  3324. } // prev_block_index
  3325. if (best_block_index != block_index)
  3326. {
  3327. total_modified++;
  3328. unpacked_uastc_block unpacked_best_blk;
  3329. if (!unpack_uastc(best_block, unpacked_best_blk, false, false))
  3330. return false;
  3331. if ((params.m_endpoint_refinement) && (block_mode == 0))
  3332. {
  3333. // Attempt to refine mode 0 block's endpoints, using the new selectors. This doesn't help much, but it does help.
  3334. // TODO: We could do this with the other modes too.
  3335. color_rgba decoded_best_uastc_block[4][4];
  3336. if (!unpack_uastc(unpacked_best_blk, (basist::color32*)decoded_best_uastc_block, false))
  3337. return false;
  3338. // Compute the block's current error (with the modified selectors).
  3339. uint64_t best_uastc_err = 0;
  3340. for (uint32_t i = 0; i < 16; i++)
  3341. best_uastc_err += color_distance(perceptual, pPixels[i], ((color_rgba*)decoded_best_uastc_block)[i], true);
  3342. bc7enc_compress_block_params comp_params;
  3343. memset(&comp_params, 0, sizeof(comp_params));
  3344. comp_params.m_max_partitions_mode1 = 64;
  3345. comp_params.m_least_squares_passes = 1;
  3346. comp_params.m_weights[0] = 1;
  3347. comp_params.m_weights[1] = 1;
  3348. comp_params.m_weights[2] = 1;
  3349. comp_params.m_weights[3] = 1;
  3350. comp_params.m_uber_level = 0;
  3351. uastc_encode_results results;
  3352. uint32_t total_results = 0;
  3353. astc_mode0_or_18(0, (color_rgba(*)[4])pPixels, &results, total_results, comp_params, unpacked_best_blk.m_astc.m_weights);
  3354. assert(total_results == 1);
  3355. // See if the overall error has actually gone done.
  3356. color_rgba decoded_trial_uastc_block[4][4];
  3357. bool success = unpack_uastc(results.m_uastc_mode, results.m_common_pattern, results.m_solid_color.get_color32(), results.m_astc, (basist::color32*) & decoded_trial_uastc_block[0][0], false);
  3358. assert(success);
  3359. BASISU_NOTE_UNUSED(success);
  3360. uint64_t trial_uastc_err = 0;
  3361. for (uint32_t i = 0; i < 16; i++)
  3362. trial_uastc_err += color_distance(perceptual, pPixels[i], ((color_rgba*)decoded_trial_uastc_block)[i], true);
  3363. if (trial_uastc_err < best_uastc_err)
  3364. {
  3365. // The error went down, so accept the new endpoints.
  3366. // Ensure the selectors haven't changed, otherwise we'll invalidate the LZ matches.
  3367. for (uint32_t i = 0; i < 16; i++)
  3368. assert(unpacked_best_blk.m_astc.m_weights[i] == results.m_astc.m_weights[i]);
  3369. unpacked_best_blk.m_astc = results.m_astc;
  3370. total_refined++;
  3371. }
  3372. } // if ((params.m_endpoint_refinement) && (block_mode == 0))
  3373. // The selectors have changed, so go recompute the block hints.
  3374. if (!uastc_recompute_hints(&best_block, pPixels, flags, &unpacked_best_blk))
  3375. return false;
  3376. // Write the modified block
  3377. pBlocks[block_index] = best_block;
  3378. } // if (best_block_index != block_index)
  3379. {
  3380. uint32_t bit_offset = first_sel_bit;
  3381. uint64_t sel_bits = read_bits((const uint8_t*)&best_block, bit_offset, basisu::minimum(64U, total_sel_bits));
  3382. auto res = selector_history.insert(std::make_pair(selector_bitsequence(first_sel_bit, sel_bits), block_index));
  3383. if (!res.second)
  3384. (*res.first).second = block_index;
  3385. }
  3386. } // block_index
  3387. return true;
  3388. }
  3389. // This function implements a basic form of rate distortion optimization (RDO) for UASTC.
  3390. // It only changes selectors and then updates the hints. It uses very approximate LZ bitprice estimation.
  3391. // There's A LOT that can be done better in here, but it's a start.
  3392. // One nice advantage of the method used here is that it works for any input, no matter which or how many modes it uses.
  3393. bool uastc_rdo(uint32_t num_blocks, basist::uastc_block* pBlocks, const color_rgba* pBlock_pixels, const uastc_rdo_params& params, uint32_t flags, job_pool* pJob_pool, uint32_t total_jobs)
  3394. {
  3395. assert(params.m_max_allowed_rms_increase_ratio > 1.0f);
  3396. assert(params.m_lz_dict_size > 0);
  3397. assert(params.m_lambda > 0.0f);
  3398. uint32_t total_skipped = 0, total_modified = 0, total_refined = 0, total_smooth = 0;
  3399. uint32_t blocks_per_job = total_jobs ? (num_blocks / total_jobs) : 0;
  3400. std::mutex stat_mutex;
  3401. bool status = false;
  3402. if ((!pJob_pool) || (total_jobs <= 1) || (blocks_per_job <= 8))
  3403. {
  3404. status = uastc_rdo_blocks(0, num_blocks, pBlocks, pBlock_pixels, params, flags, total_skipped, total_refined, total_modified, total_smooth);
  3405. }
  3406. else
  3407. {
  3408. bool all_succeeded = true;
  3409. for (uint32_t block_index_iter = 0; block_index_iter < num_blocks; block_index_iter += blocks_per_job)
  3410. {
  3411. const uint32_t first_index = block_index_iter;
  3412. const uint32_t last_index = minimum<uint32_t>(num_blocks, block_index_iter + blocks_per_job);
  3413. #ifndef __EMSCRIPTEN__
  3414. pJob_pool->add_job([first_index, last_index, pBlocks, pBlock_pixels, &params, flags, &total_skipped, &total_modified, &total_refined, &total_smooth, &all_succeeded, &stat_mutex] {
  3415. #endif
  3416. uint32_t job_skipped = 0, job_modified = 0, job_refined = 0, job_smooth = 0;
  3417. bool status = uastc_rdo_blocks(first_index, last_index, pBlocks, pBlock_pixels, params, flags, job_skipped, job_refined, job_modified, job_smooth);
  3418. {
  3419. std::lock_guard<std::mutex> lck(stat_mutex);
  3420. all_succeeded = all_succeeded && status;
  3421. total_skipped += job_skipped;
  3422. total_modified += job_modified;
  3423. total_refined += job_refined;
  3424. total_smooth += job_smooth;
  3425. }
  3426. #ifndef __EMSCRIPTEN__
  3427. }
  3428. );
  3429. #endif
  3430. } // block_index_iter
  3431. #ifndef __EMSCRIPTEN__
  3432. pJob_pool->wait_for_all();
  3433. #endif
  3434. status = all_succeeded;
  3435. }
  3436. debug_printf("uastc_rdo: Total modified: %3.2f%%, total skipped: %3.2f%%, total refined: %3.2f%%, total smooth: %3.2f%%\n", total_modified * 100.0f / num_blocks, total_skipped * 100.0f / num_blocks, total_refined * 100.0f / num_blocks, total_smooth * 100.0f / num_blocks);
  3437. return status;
  3438. }
  3439. } // namespace basisu