basisu_uastc_enc.cpp 149 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195
  1. // basisu_uastc_enc.cpp
  2. // Copyright (C) 2019-2021 Binomial LLC. All Rights Reserved.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #include "basisu_uastc_enc.h"
  16. #if BASISU_USE_ASTC_DECOMPRESS
  17. #include "basisu_astc_decomp.h"
  18. #endif
  19. #include "basisu_gpu_texture.h"
  20. #include "basisu_bc7enc.h"
  21. #ifdef _DEBUG
  22. // 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.
  23. #define BASISU_VALIDATE_UASTC_ENC 1
  24. #endif
  25. #define BASISU_SUPPORT_FORCE_MODE 0
  26. using namespace basist;
  27. namespace basisu
  28. {
  29. const uint32_t MAX_ENCODE_RESULTS = 512;
  30. #if BASISU_VALIDATE_UASTC_ENC
  31. static void validate_func(bool condition, int line)
  32. {
  33. if (!condition)
  34. {
  35. fprintf(stderr, "basisu_uastc_enc: Internal validation failed on line %u!\n", line);
  36. }
  37. }
  38. #define VALIDATE(c) validate_func(c, __LINE__);
  39. #else
  40. #define VALIDATE(c)
  41. #endif
  42. enum dxt_constants
  43. {
  44. cDXT1SelectorBits = 2U, cDXT1SelectorValues = 1U << cDXT1SelectorBits, cDXT1SelectorMask = cDXT1SelectorValues - 1U,
  45. cDXT5SelectorBits = 3U, cDXT5SelectorValues = 1U << cDXT5SelectorBits, cDXT5SelectorMask = cDXT5SelectorValues - 1U,
  46. };
  47. struct dxt1_block
  48. {
  49. enum { cTotalEndpointBytes = 2, cTotalSelectorBytes = 4 };
  50. uint8_t m_low_color[cTotalEndpointBytes];
  51. uint8_t m_high_color[cTotalEndpointBytes];
  52. uint8_t m_selectors[cTotalSelectorBytes];
  53. inline void clear() { basisu::clear_obj(*this); }
  54. inline uint32_t get_high_color() const { return m_high_color[0] | (m_high_color[1] << 8U); }
  55. inline uint32_t get_low_color() const { return m_low_color[0] | (m_low_color[1] << 8U); }
  56. 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); }
  57. 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); }
  58. inline uint32_t get_selector(uint32_t x, uint32_t y) const { assert((x < 4U) && (y < 4U)); return (m_selectors[y] >> (x * cDXT1SelectorBits))& cDXT1SelectorMask; }
  59. 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)); }
  60. static uint16_t pack_color(const color_rgba& color, bool scaled, uint32_t bias = 127U)
  61. {
  62. uint32_t r = color.r, g = color.g, b = color.b;
  63. if (scaled)
  64. {
  65. r = (r * 31U + bias) / 255U;
  66. g = (g * 63U + bias) / 255U;
  67. b = (b * 31U + bias) / 255U;
  68. }
  69. return static_cast<uint16_t>(basisu::minimum(b, 31U) | (basisu::minimum(g, 63U) << 5U) | (basisu::minimum(r, 31U) << 11U));
  70. }
  71. 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)); }
  72. };
  73. #define UASTC_WRITE_MODE_DESCS 0
  74. static inline void uastc_write_bits(uint8_t* pBuf, uint32_t& bit_offset, uint64_t code, uint32_t codesize, const char* pDesc)
  75. {
  76. (void)pDesc;
  77. #if UASTC_WRITE_MODE_DESCS
  78. if (pDesc)
  79. printf("%s: %u %u\n", pDesc, bit_offset, codesize);
  80. #endif
  81. assert((codesize == 64) || (code < (1ULL << codesize)));
  82. while (codesize)
  83. {
  84. uint32_t byte_bit_offset = bit_offset & 7;
  85. uint32_t bits_to_write = basisu::minimum<int>(codesize, 8 - byte_bit_offset);
  86. pBuf[bit_offset >> 3] |= (code << byte_bit_offset);
  87. code >>= bits_to_write;
  88. codesize -= bits_to_write;
  89. bit_offset += bits_to_write;
  90. }
  91. }
  92. 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)
  93. {
  94. if ((g_uastc_mode_has_alpha[result.m_uastc_mode]) && (result.m_uastc_mode != UASTC_MODE_INDEX_SOLID_COLOR))
  95. {
  96. assert(etc_eac_a8_blk.m_multiplier >= 1);
  97. }
  98. uint8_t buf[32];
  99. memset(buf, 0, sizeof(buf));
  100. uint32_t block_bit_offset = 0;
  101. #if UASTC_WRITE_MODE_DESCS
  102. printf("**** Mode: %u\n", result.m_uastc_mode);
  103. #endif
  104. 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");
  105. if (result.m_uastc_mode == UASTC_MODE_INDEX_SOLID_COLOR)
  106. {
  107. uastc_write_bits(buf, block_bit_offset, result.m_solid_color.r, 8, "R");
  108. uastc_write_bits(buf, block_bit_offset, result.m_solid_color.g, 8, "G");
  109. uastc_write_bits(buf, block_bit_offset, result.m_solid_color.b, 8, "B");
  110. uastc_write_bits(buf, block_bit_offset, result.m_solid_color.a, 8, "A");
  111. uastc_write_bits(buf, block_bit_offset, etc1_blk.get_diff_bit(), 1, "ETC1D");
  112. uastc_write_bits(buf, block_bit_offset, etc1_blk.get_inten_table(0), 3, "ETC1I");
  113. uastc_write_bits(buf, block_bit_offset, etc1_blk.get_selector(0, 0), 2, "ETC1S");
  114. uint32_t r, g, b;
  115. if (etc1_blk.get_diff_bit())
  116. etc_block::unpack_color5(r, g, b, etc1_blk.get_base5_color(), false);
  117. else
  118. etc_block::unpack_color4(r, g, b, etc1_blk.get_base4_color(0), false);
  119. uastc_write_bits(buf, block_bit_offset, r, 5, "ETC1R");
  120. uastc_write_bits(buf, block_bit_offset, g, 5, "ETC1G");
  121. uastc_write_bits(buf, block_bit_offset, b, 5, "ETC1B");
  122. memcpy(&blk, buf, sizeof(blk));
  123. return;
  124. }
  125. if (g_uastc_mode_has_bc1_hint0[result.m_uastc_mode])
  126. uastc_write_bits(buf, block_bit_offset, bc1_hint0, 1, "BC1H0");
  127. else
  128. {
  129. assert(bc1_hint0 == false);
  130. }
  131. if (g_uastc_mode_has_bc1_hint1[result.m_uastc_mode])
  132. uastc_write_bits(buf, block_bit_offset, bc1_hint1, 1, "BC1H1");
  133. else
  134. {
  135. assert(bc1_hint1 == false);
  136. }
  137. uastc_write_bits(buf, block_bit_offset, etc1_blk.get_flip_bit(), 1, "ETC1F");
  138. uastc_write_bits(buf, block_bit_offset, etc1_blk.get_diff_bit(), 1, "ETC1D");
  139. uastc_write_bits(buf, block_bit_offset, etc1_blk.get_inten_table(0), 3, "ETC1I0");
  140. uastc_write_bits(buf, block_bit_offset, etc1_blk.get_inten_table(1), 3, "ETC1I1");
  141. if (g_uastc_mode_has_etc1_bias[result.m_uastc_mode])
  142. uastc_write_bits(buf, block_bit_offset, etc1_bias, 5, "ETC1BIAS");
  143. else
  144. {
  145. assert(etc1_bias == 0);
  146. }
  147. if (g_uastc_mode_has_alpha[result.m_uastc_mode])
  148. {
  149. const uint32_t etc2_hints = etc_eac_a8_blk.m_table | (etc_eac_a8_blk.m_multiplier << 4);
  150. assert(etc2_hints > 0 && etc2_hints <= 0xFF);
  151. uastc_write_bits(buf, block_bit_offset, etc2_hints, 8, "ETC2TM");
  152. }
  153. uint32_t subsets = 1;
  154. switch (result.m_uastc_mode)
  155. {
  156. case 2:
  157. case 4:
  158. case 7:
  159. case 9:
  160. case 16:
  161. uastc_write_bits(buf, block_bit_offset, result.m_common_pattern, 5, "PAT");
  162. subsets = 2;
  163. break;
  164. case 3:
  165. uastc_write_bits(buf, block_bit_offset, result.m_common_pattern, 4, "PAT");
  166. subsets = 3;
  167. break;
  168. default:
  169. break;
  170. }
  171. #ifdef _DEBUG
  172. uint32_t part_seed = 0;
  173. switch (result.m_uastc_mode)
  174. {
  175. case 2:
  176. case 4:
  177. case 9:
  178. case 16:
  179. part_seed = g_astc_bc7_common_partitions2[result.m_common_pattern].m_astc;
  180. break;
  181. case 3:
  182. part_seed = g_astc_bc7_common_partitions3[result.m_common_pattern].m_astc;
  183. break;
  184. case 7:
  185. part_seed = g_bc7_3_astc2_common_partitions[result.m_common_pattern].m_astc2;
  186. break;
  187. default:
  188. break;
  189. }
  190. #endif
  191. uint32_t total_planes = 1;
  192. switch (result.m_uastc_mode)
  193. {
  194. case 6:
  195. case 11:
  196. case 13:
  197. uastc_write_bits(buf, block_bit_offset, result.m_astc.m_ccs, 2, "COMPSEL");
  198. total_planes = 2;
  199. break;
  200. case 17:
  201. // CCS field is always 3 for dual plane LA.
  202. assert(result.m_astc.m_ccs == 3);
  203. total_planes = 2;
  204. break;
  205. default:
  206. break;
  207. }
  208. uint8_t weights[32];
  209. memcpy(weights, result.m_astc.m_weights, 16 * total_planes);
  210. uint8_t endpoints[18];
  211. memcpy(endpoints, result.m_astc.m_endpoints, sizeof(endpoints));
  212. const uint32_t total_comps = g_uastc_mode_comps[result.m_uastc_mode];
  213. // LLAA
  214. // LLAA LLAA
  215. // LLAA LLAA LLAA
  216. // RRGGBB
  217. // RRGGBB RRGGBB
  218. // RRGGBB RRGGBB RRGGBB
  219. // RRGGBBAA
  220. // RRGGBBAA RRGGBBAA
  221. const uint32_t weight_bits = g_uastc_mode_weight_bits[result.m_uastc_mode];
  222. const uint8_t* pPartition_pattern;
  223. const uint8_t* pSubset_anchor_indices = basist::get_anchor_indices(subsets, result.m_uastc_mode, result.m_common_pattern, pPartition_pattern);
  224. for (uint32_t plane_index = 0; plane_index < total_planes; plane_index++)
  225. {
  226. for (uint32_t subset_index = 0; subset_index < subsets; subset_index++)
  227. {
  228. const uint32_t anchor_index = pSubset_anchor_indices[subset_index];
  229. #ifdef _DEBUG
  230. if (subsets >= 2)
  231. {
  232. for (uint32_t i = 0; i < 16; i++)
  233. {
  234. const uint32_t part_index = astc_compute_texel_partition(part_seed, i & 3, i >> 2, 0, subsets, true);
  235. if (part_index == subset_index)
  236. {
  237. assert(anchor_index == i);
  238. break;
  239. }
  240. }
  241. }
  242. else
  243. {
  244. assert(!anchor_index);
  245. }
  246. #endif
  247. // Check anchor weight's MSB - if it's set then invert this subset's weights and swap the endpoints
  248. if (weights[anchor_index * total_planes + plane_index] & (1 << (weight_bits - 1)))
  249. {
  250. for (uint32_t i = 0; i < 16; i++)
  251. {
  252. const uint32_t part_index = pPartition_pattern[i];
  253. #ifdef _DEBUG
  254. if (subsets >= 2)
  255. {
  256. assert(part_index == (uint32_t)astc_compute_texel_partition(part_seed, i & 3, i >> 2, 0, subsets, true));
  257. }
  258. else
  259. {
  260. assert(!part_index);
  261. }
  262. #endif
  263. if (part_index == subset_index)
  264. weights[i * total_planes + plane_index] = ((1 << weight_bits) - 1) - weights[i * total_planes + plane_index];
  265. }
  266. if (total_planes == 2)
  267. {
  268. for (int c = 0; c < (int)total_comps; c++)
  269. {
  270. const uint32_t comp_plane = (total_comps == 2) ? c : ((c == result.m_astc.m_ccs) ? 1 : 0);
  271. if (comp_plane == plane_index)
  272. std::swap(endpoints[c * 2 + 0], endpoints[c * 2 + 1]);
  273. }
  274. }
  275. else
  276. {
  277. for (uint32_t c = 0; c < total_comps; c++)
  278. std::swap(endpoints[subset_index * total_comps * 2 + c * 2 + 0], endpoints[subset_index * total_comps * 2 + c * 2 + 1]);
  279. }
  280. }
  281. } // subset_index
  282. } // plane_index
  283. const uint32_t total_values = total_comps * 2 * subsets;
  284. const uint32_t endpoint_range = g_uastc_mode_endpoint_ranges[result.m_uastc_mode];
  285. uint32_t bit_values[18];
  286. uint32_t tq_values[8];
  287. uint32_t total_tq_values = 0;
  288. uint32_t tq_accum = 0;
  289. uint32_t tq_mul = 1;
  290. const uint32_t ep_bits = g_astc_bise_range_table[endpoint_range][0];
  291. const uint32_t ep_trits = g_astc_bise_range_table[endpoint_range][1];
  292. const uint32_t ep_quints = g_astc_bise_range_table[endpoint_range][2];
  293. for (uint32_t i = 0; i < total_values; i++)
  294. {
  295. uint32_t val = endpoints[i];
  296. uint32_t bits = val & ((1 << ep_bits) - 1);
  297. uint32_t tq = val >> ep_bits;
  298. bit_values[i] = bits;
  299. if (ep_trits)
  300. {
  301. assert(tq < 3);
  302. tq_accum += tq * tq_mul;
  303. tq_mul *= 3;
  304. if (tq_mul == 243)
  305. {
  306. tq_values[total_tq_values++] = tq_accum;
  307. tq_accum = 0;
  308. tq_mul = 1;
  309. }
  310. }
  311. else if (ep_quints)
  312. {
  313. assert(tq < 5);
  314. tq_accum += tq * tq_mul;
  315. tq_mul *= 5;
  316. if (tq_mul == 125)
  317. {
  318. tq_values[total_tq_values++] = tq_accum;
  319. tq_accum = 0;
  320. tq_mul = 1;
  321. }
  322. }
  323. }
  324. uint32_t total_endpoint_bits = 0;
  325. for (uint32_t i = 0; i < total_tq_values; i++)
  326. {
  327. const uint32_t num_bits = ep_trits ? 8 : 7;
  328. uastc_write_bits(buf, block_bit_offset, tq_values[i], num_bits, "ETQ");
  329. total_endpoint_bits += num_bits;
  330. }
  331. if (tq_mul > 1)
  332. {
  333. uint32_t num_bits;
  334. if (ep_trits)
  335. {
  336. if (tq_mul == 3)
  337. num_bits = 2;
  338. else if (tq_mul == 9)
  339. num_bits = 4;
  340. else if (tq_mul == 27)
  341. num_bits = 5;
  342. else //if (tq_mul == 81)
  343. num_bits = 7;
  344. }
  345. else
  346. {
  347. if (tq_mul == 5)
  348. num_bits = 3;
  349. else //if (tq_mul == 25)
  350. num_bits = 5;
  351. }
  352. uastc_write_bits(buf, block_bit_offset, tq_accum, num_bits, "ETQ");
  353. total_endpoint_bits += num_bits;
  354. }
  355. for (uint32_t i = 0; i < total_values; i++)
  356. {
  357. uastc_write_bits(buf, block_bit_offset, bit_values[i], ep_bits, "EBITS");
  358. total_endpoint_bits += ep_bits;
  359. }
  360. #if UASTC_WRITE_MODE_DESCS
  361. uint32_t weight_start = block_bit_offset;
  362. #endif
  363. uint32_t total_weight_bits = 0;
  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. uastc_encode_results results[MAX_ENCODE_RESULTS];
  2609. level = clampi(level, cPackUASTCLevelFastest, cPackUASTCLevelVerySlow);
  2610. // Set all options to slowest, then configure from there depending on the selected level.
  2611. uint32_t mode_mask = UINT32_MAX;
  2612. uint32_t uber_level = 6;
  2613. bool estimate_partition = false;
  2614. bool always_try_alpha_modes = true;
  2615. uint32_t eac_a8_mul_search_rad = 3;
  2616. uint32_t eac_a8_table_mask = UINT32_MAX;
  2617. uint32_t least_squares_passes = 2;
  2618. bool bc1_hints = true;
  2619. bool only_use_la_on_transparent_blocks = false;
  2620. switch (level)
  2621. {
  2622. case cPackUASTCLevelFastest:
  2623. {
  2624. mode_mask = (1 << 0) | (1 << 8) |
  2625. (1 << 11) | (1 << 12) |
  2626. (1 << 15);
  2627. always_try_alpha_modes = false;
  2628. eac_a8_mul_search_rad = 0;
  2629. eac_a8_table_mask = (1 << 2) | (1 << 8) | (1 << 11) | (1 << 13);
  2630. uber_level = 0;
  2631. least_squares_passes = 1;
  2632. bc1_hints = false;
  2633. estimate_partition = true;
  2634. only_use_la_on_transparent_blocks = true;
  2635. break;
  2636. }
  2637. case cPackUASTCLevelFaster:
  2638. {
  2639. mode_mask = (1 << 0) | (1 << 4) | (1 << 6) | (1 << 8) |
  2640. (1 << 9) | (1 << 11) | (1 << 12) |
  2641. (1 << 15) | (1 << 17);
  2642. always_try_alpha_modes = false;
  2643. eac_a8_mul_search_rad = 0;
  2644. eac_a8_table_mask = (1 << 2) | (1 << 8) | (1 << 11) | (1 << 13);
  2645. uber_level = 0;
  2646. least_squares_passes = 1;
  2647. estimate_partition = true;
  2648. break;
  2649. }
  2650. case cPackUASTCLevelDefault:
  2651. {
  2652. mode_mask = (1 << 0) | (1 << 1) | (1 << 4) | (1 << 5) | (1 << 6) | (1 << 8) |
  2653. (1 << 9) | (1 << 10) | (1 << 11) | (1 << 12) | (1 << 13) |
  2654. (1 << 15) | (1 << 16) | (1 << 17);
  2655. always_try_alpha_modes = false;
  2656. eac_a8_mul_search_rad = 1;
  2657. eac_a8_table_mask = (1 << 0) | (1 << 2) | (1 << 6) | (1 << 7) | (1 << 8) | (1 << 10) | (1 << 11) | (1 << 13);
  2658. uber_level = 1;
  2659. least_squares_passes = 1;
  2660. estimate_partition = true;
  2661. break;
  2662. }
  2663. case cPackUASTCLevelSlower:
  2664. {
  2665. always_try_alpha_modes = false;
  2666. eac_a8_mul_search_rad = 2;
  2667. uber_level = 3;
  2668. estimate_partition = true;
  2669. break;
  2670. }
  2671. case cPackUASTCLevelVerySlow:
  2672. {
  2673. break;
  2674. }
  2675. }
  2676. #if BASISU_SUPPORT_FORCE_MODE
  2677. static int force_mode = -1;
  2678. force_mode = (force_mode + 1) % TOTAL_UASTC_MODES;
  2679. mode_mask = UINT32_MAX;
  2680. always_try_alpha_modes = true;
  2681. only_use_la_on_transparent_blocks = false;
  2682. #endif
  2683. // HACK HACK
  2684. //mode_mask &= ~(1 << 18);
  2685. //mode_mask = (1 << 18)| (1 << 10);
  2686. uint32_t total_results = 0;
  2687. if (only_use_la_on_transparent_blocks)
  2688. {
  2689. if ((is_la) && (!has_alpha))
  2690. is_la = false;
  2691. }
  2692. const bool try_alpha_modes = has_alpha || always_try_alpha_modes;
  2693. bc7enc_compress_block_params comp_params;
  2694. memset(&comp_params, 0, sizeof(comp_params));
  2695. comp_params.m_max_partitions_mode1 = 64;
  2696. comp_params.m_least_squares_passes = least_squares_passes;
  2697. comp_params.m_weights[0] = 1;
  2698. comp_params.m_weights[1] = 1;
  2699. comp_params.m_weights[2] = 1;
  2700. comp_params.m_weights[3] = 1;
  2701. comp_params.m_uber_level = uber_level;
  2702. if (is_la)
  2703. {
  2704. if (mode_mask & (1U << 15))
  2705. astc_mode15(block, results, total_results, comp_params);
  2706. if (mode_mask & (1U << 16))
  2707. astc_mode9_or_16(16, block, results, total_results, comp_params, estimate_partition ? 4 : 0);
  2708. if (mode_mask & (1U << 17))
  2709. astc_mode11_or_17(17, block, results, total_results, comp_params);
  2710. }
  2711. if (!has_alpha)
  2712. {
  2713. if (mode_mask & (1U << 0))
  2714. astc_mode0_or_18(0, block, results, total_results, comp_params);
  2715. if (mode_mask & (1U << 1))
  2716. astc_mode1(block, results, total_results, comp_params);
  2717. if (mode_mask & (1U << 2))
  2718. astc_mode2(block, results, total_results, comp_params, estimate_partition);
  2719. if (mode_mask & (1U << 3))
  2720. astc_mode3(block, results, total_results, comp_params, estimate_partition);
  2721. if (mode_mask & (1U << 4))
  2722. astc_mode4(block, results, total_results, comp_params, estimate_partition);
  2723. if (mode_mask & (1U << 5))
  2724. astc_mode5(block, results, total_results, comp_params);
  2725. if (mode_mask & (1U << 6))
  2726. astc_mode6(block, results, total_results, comp_params);
  2727. if (mode_mask & (1U << 7))
  2728. astc_mode7(block, results, total_results, comp_params, estimate_partition);
  2729. if (mode_mask & (1U << 18))
  2730. astc_mode0_or_18(18, block, results, total_results, comp_params);
  2731. }
  2732. if (try_alpha_modes)
  2733. {
  2734. if (mode_mask & (1U << 9))
  2735. astc_mode9_or_16(9, block, results, total_results, comp_params, estimate_partition ? 4 : 0);
  2736. if (mode_mask & (1U << 10))
  2737. astc_mode10(block, results, total_results, comp_params);
  2738. if (mode_mask & (1U << 11))
  2739. astc_mode11_or_17(11, block, results, total_results, comp_params);
  2740. if (mode_mask & (1U << 12))
  2741. astc_mode12(block, results, total_results, comp_params);
  2742. if (mode_mask & (1U << 13))
  2743. astc_mode13(block, results, total_results, comp_params);
  2744. if (mode_mask & (1U << 14))
  2745. astc_mode14(block, results, total_results, comp_params);
  2746. }
  2747. assert(total_results);
  2748. // Fix up the errors so we consistently have LA, RGB, or RGBA error.
  2749. for (uint32_t i = 0; i < total_results; i++)
  2750. {
  2751. uastc_encode_results& r = results[i];
  2752. if (!is_la)
  2753. {
  2754. if (g_uastc_mode_is_la[r.m_uastc_mode])
  2755. {
  2756. color_rgba unpacked_block[16];
  2757. unpack_uastc(r.m_uastc_mode, r.m_common_pattern, r.m_solid_color.get_color32(), r.m_astc, (basist::color32 *)unpacked_block, false);
  2758. uint64_t total_err = 0;
  2759. for (uint32_t j = 0; j < 16; j++)
  2760. total_err += color_distance(unpacked_block[j], ((const color_rgba*)block)[j], true);
  2761. r.m_astc_err = total_err;
  2762. }
  2763. }
  2764. else
  2765. {
  2766. if (!g_uastc_mode_is_la[r.m_uastc_mode])
  2767. {
  2768. color_rgba unpacked_block[16];
  2769. unpack_uastc(r.m_uastc_mode, r.m_common_pattern, r.m_solid_color.get_color32(), r.m_astc, (basist::color32 *)unpacked_block, false);
  2770. uint64_t total_err = 0;
  2771. for (uint32_t j = 0; j < 16; j++)
  2772. total_err += color_distance_la(unpacked_block[j], ((const color_rgba*)block)[j]);
  2773. r.m_astc_err = total_err;
  2774. }
  2775. }
  2776. }
  2777. unpacked_uastc_block unpacked_ublock;
  2778. memset(&unpacked_ublock, 0, sizeof(unpacked_ublock));
  2779. uint64_t total_overall_err[MAX_ENCODE_RESULTS];
  2780. float uastc_err_f[MAX_ENCODE_RESULTS];
  2781. double best_uastc_err_f = 1e+20f;
  2782. int best_index = -1;
  2783. if (total_results == 1)
  2784. {
  2785. best_index = 0;
  2786. }
  2787. else
  2788. {
  2789. const uint32_t bc7_err_weight = favor_bc7_error ? 100 : ((favor_uastc_error ? 0 : DEFAULT_BC7_ERROR_WEIGHT));
  2790. const uint32_t uastc_err_weight = favor_bc7_error ? 0 : 100;
  2791. // Find best overall results, balancing UASTC and UASTC->BC7 error.
  2792. // We purposely allow UASTC error to increase a little, if doing so lowers the BC7 error.
  2793. for (uint32_t i = 0; i < total_results; i++)
  2794. {
  2795. #if BASISU_SUPPORT_FORCE_MODE
  2796. if (results[i].m_uastc_mode == force_mode)
  2797. {
  2798. best_index = i;
  2799. break;
  2800. }
  2801. #endif
  2802. unpacked_ublock.m_mode = results[i].m_uastc_mode;
  2803. unpacked_ublock.m_astc = results[i].m_astc;
  2804. unpacked_ublock.m_common_pattern = results[i].m_common_pattern;
  2805. unpacked_ublock.m_solid_color = results[i].m_solid_color.get_color32();
  2806. color_rgba decoded_uastc_block[4][4];
  2807. 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);
  2808. (void)success;
  2809. VALIDATE(success);
  2810. uint64_t total_uastc_rgb_err, total_uastc_rgba_err, total_uastc_la_err;
  2811. compute_block_error(block, decoded_uastc_block, total_uastc_rgb_err, total_uastc_rgba_err, total_uastc_la_err);
  2812. // Validate the computed error, or we're go mad if it's inaccurate.
  2813. if (results[i].m_uastc_mode == UASTC_MODE_INDEX_SOLID_COLOR)
  2814. {
  2815. VALIDATE(total_uastc_rgba_err == 0);
  2816. }
  2817. else if (is_la)
  2818. {
  2819. VALIDATE(total_uastc_la_err == results[i].m_astc_err);
  2820. }
  2821. else if (g_uastc_mode_has_alpha[results[i].m_uastc_mode])
  2822. {
  2823. VALIDATE(total_uastc_rgba_err == results[i].m_astc_err);
  2824. }
  2825. else
  2826. {
  2827. VALIDATE(total_uastc_rgb_err == results[i].m_astc_err);
  2828. }
  2829. // Transcode to BC7
  2830. bc7_optimization_results bc7_results;
  2831. transcode_uastc_to_bc7(unpacked_ublock, bc7_results);
  2832. bc7_block bc7_data;
  2833. encode_bc7_block(&bc7_data, &bc7_results);
  2834. color_rgba decoded_bc7_block[4][4];
  2835. unpack_block(texture_format::cBC7, &bc7_data, &decoded_bc7_block[0][0]);
  2836. // Compute BC7 error
  2837. uint64_t total_bc7_la_err, total_bc7_rgb_err, total_bc7_rgba_err;
  2838. compute_block_error(block, decoded_bc7_block, total_bc7_rgb_err, total_bc7_rgba_err, total_bc7_la_err);
  2839. if (results[i].m_uastc_mode == UASTC_MODE_INDEX_SOLID_COLOR)
  2840. {
  2841. VALIDATE(total_bc7_rgba_err == 0);
  2842. best_index = i;
  2843. break;
  2844. }
  2845. uint64_t total_uastc_err = 0, total_bc7_err = 0;
  2846. if (is_la)
  2847. {
  2848. total_bc7_err = total_bc7_la_err;
  2849. total_uastc_err = total_uastc_la_err;
  2850. }
  2851. else if (has_alpha)
  2852. {
  2853. total_bc7_err = total_bc7_rgba_err;
  2854. total_uastc_err = total_uastc_rgba_err;
  2855. }
  2856. else
  2857. {
  2858. total_bc7_err = total_bc7_rgb_err;
  2859. total_uastc_err = total_uastc_rgb_err;
  2860. }
  2861. total_overall_err[i] = ((total_bc7_err * bc7_err_weight) / 100) + ((total_uastc_err * uastc_err_weight) / 100);
  2862. if (!total_overall_err[i])
  2863. {
  2864. best_index = i;
  2865. break;
  2866. }
  2867. uastc_err_f[i] = sqrtf((float)total_uastc_err);
  2868. if (uastc_err_f[i] < best_uastc_err_f)
  2869. {
  2870. best_uastc_err_f = uastc_err_f[i];
  2871. }
  2872. } // total_results
  2873. if (best_index < 0)
  2874. {
  2875. uint64_t best_err = UINT64_MAX;
  2876. if ((best_uastc_err_f == 0.0f) || (favor_bc7_error))
  2877. {
  2878. for (uint32_t i = 0; i < total_results; i++)
  2879. {
  2880. // TODO: This is a quick hack to favor modes 0 or 10 for better RDO compression.
  2881. const float err_weight = (flags & cPackUASTCFavorSimplerModes) ? get_uastc_mode_weight(results[i].m_uastc_mode) : 1.0f;
  2882. const uint64_t w = (uint64_t)(total_overall_err[i] * err_weight);
  2883. if (w < best_err)
  2884. {
  2885. best_err = w;
  2886. best_index = i;
  2887. if (!best_err)
  2888. break;
  2889. }
  2890. } // i
  2891. }
  2892. else
  2893. {
  2894. // Scan the UASTC results, and consider all results within a window that has the best UASTC+BC7 error.
  2895. for (uint32_t i = 0; i < total_results; i++)
  2896. {
  2897. double err_delta = uastc_err_f[i] / best_uastc_err_f;
  2898. if (err_delta <= UASTC_ERROR_THRESH)
  2899. {
  2900. // TODO: This is a quick hack to favor modes 0 or 10 for better RDO compression.
  2901. const float err_weight = (flags & cPackUASTCFavorSimplerModes) ? get_uastc_mode_weight(results[i].m_uastc_mode) : 1.0f;
  2902. const uint64_t w = (uint64_t)(total_overall_err[i] * err_weight);
  2903. if (w < best_err)
  2904. {
  2905. best_err = w;
  2906. best_index = i;
  2907. if (!best_err)
  2908. break;
  2909. }
  2910. }
  2911. } // i
  2912. }
  2913. }
  2914. }
  2915. const uastc_encode_results& best_results = results[best_index];
  2916. const uint32_t best_mode = best_results.m_uastc_mode;
  2917. const astc_block_desc& best_astc_results = best_results.m_astc;
  2918. color_rgba decoded_uastc_block[4][4];
  2919. 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);
  2920. (void)success;
  2921. VALIDATE(success);
  2922. #if BASISU_VALIDATE_UASTC_ENC
  2923. // Make sure that the UASTC block unpacks to the same exact pixels as the ASTC block does, using two different decoders.
  2924. {
  2925. // Round trip to packed UASTC and back, then decode to pixels.
  2926. etc_block etc1_blk;
  2927. memset(&etc1_blk, 0, sizeof(etc1_blk));
  2928. eac_a8_block etc_eac_a8_blk;
  2929. memset(&etc_eac_a8_blk, 0, sizeof(etc_eac_a8_blk));
  2930. etc_eac_a8_blk.m_multiplier = 1;
  2931. basist::uastc_block temp_block;
  2932. pack_uastc(temp_block, best_results, etc1_blk, 0, etc_eac_a8_blk, false, false);
  2933. basist::color32 temp_block_unpacked[4][4];
  2934. success = basist::unpack_uastc(temp_block, (basist::color32 *)temp_block_unpacked, false);
  2935. VALIDATE(success);
  2936. #if BASISU_USE_ASTC_DECOMPRESS
  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((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. #endif
  2961. }
  2962. #endif
  2963. // Compute BC1 hints
  2964. bool bc1_hint0 = false, bc1_hint1 = false;
  2965. if (bc1_hints)
  2966. compute_bc1_hints(bc1_hint0, bc1_hint1, best_results, block, decoded_uastc_block);
  2967. eac_a8_block eac_a8_blk;
  2968. if ((g_uastc_mode_has_alpha[best_mode]) && (best_mode != UASTC_MODE_INDEX_SOLID_COLOR))
  2969. {
  2970. // Compute ETC2 hints
  2971. uint8_t decoded_uastc_block_alpha[16];
  2972. for (uint32_t i = 0; i < 16; i++)
  2973. decoded_uastc_block_alpha[i] = decoded_uastc_block[i >> 2][i & 3].a;
  2974. uastc_pack_eac_a8_results eac8_a8_results;
  2975. memset(&eac8_a8_results, 0, sizeof(eac8_a8_results));
  2976. uastc_pack_eac_a8(eac8_a8_results, decoded_uastc_block_alpha, 16, 0, eac_a8_mul_search_rad, eac_a8_table_mask);
  2977. // All we care about for hinting is the table and multiplier.
  2978. eac_a8_blk.m_table = eac8_a8_results.m_table;
  2979. eac_a8_blk.m_multiplier = eac8_a8_results.m_multiplier;
  2980. }
  2981. else
  2982. {
  2983. memset(&eac_a8_blk, 0, sizeof(eac_a8_blk));
  2984. }
  2985. // Compute ETC1 hints
  2986. etc_block etc1_blk;
  2987. uint32_t etc1_bias = 0;
  2988. compute_etc1_hints(etc1_blk, etc1_bias, best_results, block, decoded_uastc_block, level, flags);
  2989. // Finally, pack the UASTC block with its hints and we're done.
  2990. pack_uastc(output_block, best_results, etc1_blk, etc1_bias, eac_a8_blk, bc1_hint0, bc1_hint1);
  2991. // printf(" Packed: ");
  2992. // for (int i = 0; i < 16; i++)
  2993. // printf("%X ", output_block.m_bytes[i]);
  2994. // printf("\n");
  2995. }
  2996. static bool uastc_recompute_hints(basist::uastc_block* pBlock, const color_rgba* pBlock_pixels, uint32_t flags, const unpacked_uastc_block *pUnpacked_blk)
  2997. {
  2998. unpacked_uastc_block unpacked_blk;
  2999. if (pUnpacked_blk)
  3000. unpacked_blk = *pUnpacked_blk;
  3001. else
  3002. {
  3003. if (!unpack_uastc(*pBlock, unpacked_blk, false, true))
  3004. return false;
  3005. }
  3006. color_rgba decoded_uastc_block[4][4];
  3007. if (!unpack_uastc(unpacked_blk, (basist::color32 *)decoded_uastc_block, false))
  3008. return false;
  3009. uastc_encode_results results;
  3010. results.m_uastc_mode = unpacked_blk.m_mode;
  3011. results.m_common_pattern = unpacked_blk.m_common_pattern;
  3012. results.m_astc = unpacked_blk.m_astc;
  3013. results.m_solid_color = unpacked_blk.m_solid_color;
  3014. results.m_astc_err = 0;
  3015. bool bc1_hints = true;
  3016. uint32_t eac_a8_mul_search_rad = 3;
  3017. uint32_t eac_a8_table_mask = UINT32_MAX;
  3018. const uint32_t level = flags & cPackUASTCLevelMask;
  3019. switch (level)
  3020. {
  3021. case cPackUASTCLevelFastest:
  3022. {
  3023. eac_a8_mul_search_rad = 0;
  3024. eac_a8_table_mask = (1 << 2) | (1 << 8) | (1 << 11) | (1 << 13);
  3025. bc1_hints = false;
  3026. break;
  3027. }
  3028. case cPackUASTCLevelFaster:
  3029. {
  3030. eac_a8_mul_search_rad = 0;
  3031. eac_a8_table_mask = (1 << 2) | (1 << 8) | (1 << 11) | (1 << 13);
  3032. break;
  3033. }
  3034. case cPackUASTCLevelDefault:
  3035. {
  3036. eac_a8_mul_search_rad = 1;
  3037. eac_a8_table_mask = (1 << 0) | (1 << 2) | (1 << 6) | (1 << 7) | (1 << 8) | (1 << 10) | (1 << 11) | (1 << 13);
  3038. break;
  3039. }
  3040. case cPackUASTCLevelSlower:
  3041. {
  3042. eac_a8_mul_search_rad = 2;
  3043. break;
  3044. }
  3045. case cPackUASTCLevelVerySlow:
  3046. {
  3047. break;
  3048. }
  3049. }
  3050. bool bc1_hint0 = false, bc1_hint1 = false;
  3051. if (bc1_hints)
  3052. compute_bc1_hints(bc1_hint0, bc1_hint1, results, (color_rgba (*)[4])pBlock_pixels, decoded_uastc_block);
  3053. const uint32_t best_mode = unpacked_blk.m_mode;
  3054. eac_a8_block eac_a8_blk;
  3055. if ((g_uastc_mode_has_alpha[best_mode]) && (best_mode != UASTC_MODE_INDEX_SOLID_COLOR))
  3056. {
  3057. uint8_t decoded_uastc_block_alpha[16];
  3058. for (uint32_t i = 0; i < 16; i++)
  3059. decoded_uastc_block_alpha[i] = decoded_uastc_block[i >> 2][i & 3].a;
  3060. uastc_pack_eac_a8_results eac8_a8_results;
  3061. memset(&eac8_a8_results, 0, sizeof(eac8_a8_results));
  3062. uastc_pack_eac_a8(eac8_a8_results, decoded_uastc_block_alpha, 16, 0, eac_a8_mul_search_rad, eac_a8_table_mask);
  3063. eac_a8_blk.m_table = eac8_a8_results.m_table;
  3064. eac_a8_blk.m_multiplier = eac8_a8_results.m_multiplier;
  3065. }
  3066. else
  3067. {
  3068. memset(&eac_a8_blk, 0, sizeof(eac_a8_blk));
  3069. }
  3070. etc_block etc1_blk;
  3071. uint32_t etc1_bias = 0;
  3072. compute_etc1_hints(etc1_blk, etc1_bias, results, (color_rgba (*)[4])pBlock_pixels, decoded_uastc_block, level, flags);
  3073. pack_uastc(*pBlock, results, etc1_blk, etc1_bias, eac_a8_blk, bc1_hint0, bc1_hint1);
  3074. return true;
  3075. }
  3076. static const uint8_t g_uastc_mode_selector_bits[TOTAL_UASTC_MODES][2] =
  3077. {
  3078. { 65, 63 }, { 69, 31 }, { 73, 46 }, { 89, 29 },
  3079. { 89, 30 }, { 68, 47 }, { 66, 62 }, { 89, 30 },
  3080. { 0, 0 }, { 97, 30 }, { 65, 63 }, { 66, 62 },
  3081. { 81, 47 }, { 94, 30 }, { 92, 31 }, { 62, 63 },
  3082. { 98, 30 }, { 61, 62 }, { 49, 79 }
  3083. };
  3084. static inline uint32_t set_block_bits(uint8_t* pBytes, uint64_t val, uint32_t num_bits, uint32_t cur_ofs)
  3085. {
  3086. assert(num_bits <= 64);
  3087. assert((num_bits == 64) || (val < (1ULL << num_bits)));
  3088. uint64_t mask = (num_bits == 64) ? UINT64_MAX : ((1ULL << num_bits) - 1);
  3089. while (num_bits)
  3090. {
  3091. const uint32_t n = basisu::minimum<uint32_t>(8U - (cur_ofs & 7U), num_bits);
  3092. pBytes[cur_ofs >> 3] &= ~static_cast<uint8_t>(mask << (cur_ofs & 7U));
  3093. pBytes[cur_ofs >> 3] |= static_cast<uint8_t>(val << (cur_ofs & 7U));
  3094. val >>= n;
  3095. mask >>= n;
  3096. num_bits -= n;
  3097. cur_ofs += n;
  3098. }
  3099. return cur_ofs;
  3100. }
  3101. static const uint8_t g_tdefl_small_dist_extra[512] =
  3102. {
  3103. 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,
  3104. 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,
  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, 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,
  3106. 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,
  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, 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,
  3110. 7, 7, 7, 7, 7, 7, 7, 7
  3111. };
  3112. static const uint8_t g_tdefl_large_dist_extra[128] =
  3113. {
  3114. 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,
  3115. 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,
  3116. 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
  3117. };
  3118. static inline uint32_t compute_match_cost_estimate(uint32_t dist)
  3119. {
  3120. uint32_t len_cost = 7;
  3121. uint32_t dist_cost = 5;
  3122. if (dist < 512)
  3123. dist_cost += g_tdefl_small_dist_extra[dist & 511];
  3124. else
  3125. {
  3126. dist_cost += g_tdefl_large_dist_extra[basisu::minimum<uint32_t>(dist, 32767) >> 8];
  3127. while (dist >= 32768)
  3128. {
  3129. dist_cost++;
  3130. dist >>= 1;
  3131. }
  3132. }
  3133. return len_cost + dist_cost;
  3134. }
  3135. struct selector_bitsequence
  3136. {
  3137. uint64_t m_sel;
  3138. uint32_t m_ofs;
  3139. selector_bitsequence() { }
  3140. selector_bitsequence(uint32_t bit_ofs, uint64_t sel) : m_sel(sel), m_ofs(bit_ofs) { }
  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 static_cast<std::size_t>(hash_hsieh((uint8_t *)&s, sizeof(s)) ^ s.m_sel);
  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