basisu_etc.cpp 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610
  1. // basis_etc.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_etc.h"
  16. #if BASISU_SUPPORT_SSE
  17. #define CPPSPMD_NAME(a) a##_sse41
  18. #include "basisu_kernels_declares.h"
  19. #endif
  20. #define BASISU_DEBUG_ETC_ENCODER 0
  21. #define BASISU_DEBUG_ETC_ENCODER_DEEPER 0
  22. namespace basisu
  23. {
  24. const int8_t g_etc2_eac_tables[16][8] =
  25. {
  26. { -3, -6, -9, -15, 2, 5, 8, 14 }, { -3, -7, -10, -13, 2, 6, 9, 12 }, { -2, -5, -8, -13, 1, 4, 7, 12 }, { -2, -4, -6, -13, 1, 3, 5, 12 },
  27. { -3, -6, -8, -12, 2, 5, 7, 11 }, { -3, -7, -9, -11, 2, 6, 8, 10 }, { -4, -7, -8, -11, 3, 6, 7, 10 }, { -3, -5, -8, -11, 2, 4, 7, 10 },
  28. { -2, -6, -8, -10, 1, 5, 7, 9 }, { -2, -5, -8, -10, 1, 4, 7, 9 }, { -2, -4, -8, -10, 1, 3, 7, 9 }, { -2, -5, -7, -10, 1, 4, 6, 9 },
  29. { -3, -4, -7, -10, 2, 3, 6, 9 }, { -1, -2, -3, -10, 0, 1, 2, 9 }, { -4, -6, -8, -9, 3, 5, 7, 8 }, { -3, -5, -7, -9, 2, 4, 6, 8 }
  30. };
  31. const int8_t g_etc2_eac_tables8[16][8] =
  32. {
  33. { -24, -48, -72, -120, 16, 40, 64, 112 }, { -24,-56,-80,-104,16,48,72,96 }, { -16,-40,-64,-104,8,32,56,96 }, { -16,-32,-48,-104,8,24,40,96 },
  34. { -24,-48,-64,-96,16,40,56,88 }, { -24,-56,-72,-88,16,48,64,80 }, { -32,-56,-64,-88,24,48,56,80 }, { -24,-40,-64,-88,16,32,56,80 },
  35. { -16,-48,-64,-80,8,40,56,72 }, { -16,-40,-64,-80,8,32,56,72 }, { -16,-32,-64,-80,8,24,56,72 }, { -16,-40,-56,-80,8,32,48,72 },
  36. { -24,-32,-56,-80,16,24,48,72 }, { -8,-16,-24,-80,0,8,16,72 }, { -32,-48,-64,-72,24,40,56,64 }, { -24,-40,-56,-72,16,32,48,64 }
  37. };
  38. // Given an ETC1 diff/inten_table/selector, and an 8-bit desired color, this table encodes the best packed_color in the low byte, and the abs error in the high byte.
  39. static uint16_t g_etc1_inverse_lookup[2 * 8 * 4][256]; // [ diff/inten_table/selector][desired_color ]
  40. // g_color8_to_etc_block_config[color][table_index] = Supplies for each 8-bit color value a list of packed ETC1 diff/intensity table/selectors/packed_colors that map to that color.
  41. // To pack: diff | (inten << 1) | (selector << 4) | (packed_c << 8)
  42. static const uint16_t g_etc1_color8_to_etc_block_config_0_255[2][33] =
  43. {
  44. { 0x0000, 0x0010, 0x0002, 0x0012, 0x0004, 0x0014, 0x0006, 0x0016, 0x0008, 0x0018, 0x000A, 0x001A, 0x000C, 0x001C, 0x000E, 0x001E, 0x0001, 0x0011, 0x0003, 0x0013, 0x0005, 0x0015, 0x0007, 0x0017, 0x0009, 0x0019, 0x000B, 0x001B, 0x000D, 0x001D, 0x000F, 0x001F, 0xFFFF },
  45. { 0x0F20, 0x0F30, 0x0E32, 0x0F22, 0x0E34, 0x0F24, 0x0D36, 0x0F26, 0x0C38, 0x0E28, 0x0B3A, 0x0E2A, 0x093C, 0x0E2C, 0x053E, 0x0D2E, 0x1E31, 0x1F21, 0x1D33, 0x1F23, 0x1C35, 0x1E25, 0x1A37, 0x1E27, 0x1839, 0x1D29, 0x163B, 0x1C2B, 0x133D, 0x1B2D, 0x093F, 0x1A2F, 0xFFFF },
  46. };
  47. // Really only [254][11].
  48. static const uint16_t g_etc1_color8_to_etc_block_config_1_to_254[254][12] =
  49. {
  50. { 0x021C, 0x0D0D, 0xFFFF }, { 0x0020, 0x0021, 0x0A0B, 0x061F, 0xFFFF }, { 0x0113, 0x0217, 0xFFFF }, { 0x0116, 0x031E, 0x0B0E, 0x0405, 0xFFFF }, { 0x0022, 0x0204, 0x050A, 0x0023, 0xFFFF }, { 0x0111, 0x0319, 0x0809, 0x170F, 0xFFFF }, {
  51. 0x0303, 0x0215, 0x0607, 0xFFFF }, { 0x0030, 0x0114, 0x0408, 0x0031, 0x0201, 0x051D, 0xFFFF }, { 0x0100, 0x0024, 0x0306, 0x0025, 0x041B, 0x0E0D, 0xFFFF }, { 0x021A, 0x0121, 0x0B0B, 0x071F, 0xFFFF }, { 0x0213, 0x0317, 0xFFFF }, { 0x0112,
  52. 0x0505, 0xFFFF }, { 0x0026, 0x070C, 0x0123, 0x0027, 0xFFFF }, { 0x0211, 0x0909, 0xFFFF }, { 0x0110, 0x0315, 0x0707, 0x0419, 0x180F, 0xFFFF }, { 0x0218, 0x0131, 0x0301, 0x0403, 0x061D, 0xFFFF }, { 0x0032, 0x0202, 0x0033, 0x0125, 0x051B,
  53. 0x0F0D, 0xFFFF }, { 0x0028, 0x031C, 0x0221, 0x0029, 0xFFFF }, { 0x0120, 0x0313, 0x0C0B, 0x081F, 0xFFFF }, { 0x0605, 0x0417, 0xFFFF }, { 0x0216, 0x041E, 0x0C0E, 0x0223, 0x0127, 0xFFFF }, { 0x0122, 0x0304, 0x060A, 0x0311, 0x0A09, 0xFFFF
  54. }, { 0x0519, 0x190F, 0xFFFF }, { 0x002A, 0x0231, 0x0503, 0x0415, 0x0807, 0x002B, 0x071D, 0xFFFF }, { 0x0130, 0x0214, 0x0508, 0x0401, 0x0133, 0x0225, 0x061B, 0xFFFF }, { 0x0200, 0x0124, 0x0406, 0x0321, 0x0129, 0x100D, 0xFFFF }, { 0x031A,
  55. 0x0D0B, 0x091F, 0xFFFF }, { 0x0413, 0x0705, 0x0517, 0xFFFF }, { 0x0212, 0x0034, 0x0323, 0x0035, 0x0227, 0xFFFF }, { 0x0126, 0x080C, 0x0B09, 0xFFFF }, { 0x0411, 0x0619, 0x1A0F, 0xFFFF }, { 0x0210, 0x0331, 0x0603, 0x0515, 0x0907, 0x012B,
  56. 0xFFFF }, { 0x0318, 0x002C, 0x0501, 0x0233, 0x0325, 0x071B, 0x002D, 0x081D, 0xFFFF }, { 0x0132, 0x0302, 0x0229, 0x110D, 0xFFFF }, { 0x0128, 0x041C, 0x0421, 0x0E0B, 0x0A1F, 0xFFFF }, { 0x0220, 0x0513, 0x0617, 0xFFFF }, { 0x0135, 0x0805,
  57. 0x0327, 0xFFFF }, { 0x0316, 0x051E, 0x0D0E, 0x0423, 0xFFFF }, { 0x0222, 0x0404, 0x070A, 0x0511, 0x0719, 0x0C09, 0x1B0F, 0xFFFF }, { 0x0703, 0x0615, 0x0A07, 0x022B, 0xFFFF }, { 0x012A, 0x0431, 0x0601, 0x0333, 0x012D, 0x091D, 0xFFFF }, {
  58. 0x0230, 0x0314, 0x0036, 0x0608, 0x0425, 0x0037, 0x0329, 0x081B, 0x120D, 0xFFFF }, { 0x0300, 0x0224, 0x0506, 0x0521, 0x0F0B, 0x0B1F, 0xFFFF }, { 0x041A, 0x0613, 0x0717, 0xFFFF }, { 0x0235, 0x0905, 0xFFFF }, { 0x0312, 0x0134, 0x0523,
  59. 0x0427, 0xFFFF }, { 0x0226, 0x090C, 0x002E, 0x0611, 0x0D09, 0x002F, 0xFFFF }, { 0x0715, 0x0B07, 0x0819, 0x032B, 0x1C0F, 0xFFFF }, { 0x0310, 0x0531, 0x0701, 0x0803, 0x022D, 0x0A1D, 0xFFFF }, { 0x0418, 0x012C, 0x0433, 0x0525, 0x0137, 0x091B,
  60. 0x130D, 0xFFFF }, { 0x0232, 0x0402, 0x0621, 0x0429, 0xFFFF }, { 0x0228, 0x051C, 0x0713, 0x100B, 0x0C1F, 0xFFFF }, { 0x0320, 0x0335, 0x0A05, 0x0817, 0xFFFF }, { 0x0623, 0x0527, 0xFFFF }, { 0x0416, 0x061E, 0x0E0E, 0x0711, 0x0E09, 0x012F,
  61. 0xFFFF }, { 0x0322, 0x0504, 0x080A, 0x0919, 0x1D0F, 0xFFFF }, { 0x0631, 0x0903, 0x0815, 0x0C07, 0x042B, 0x032D, 0x0B1D, 0xFFFF }, { 0x022A, 0x0801, 0x0533, 0x0625, 0x0237, 0x0A1B, 0xFFFF }, { 0x0330, 0x0414, 0x0136, 0x0708, 0x0721, 0x0529,
  62. 0x140D, 0xFFFF }, { 0x0400, 0x0324, 0x0606, 0x0038, 0x0039, 0x110B, 0x0D1F, 0xFFFF }, { 0x051A, 0x0813, 0x0B05, 0x0917, 0xFFFF }, { 0x0723, 0x0435, 0x0627, 0xFFFF }, { 0x0412, 0x0234, 0x0F09, 0x022F, 0xFFFF }, { 0x0326, 0x0A0C, 0x012E,
  63. 0x0811, 0x0A19, 0x1E0F, 0xFFFF }, { 0x0731, 0x0A03, 0x0915, 0x0D07, 0x052B, 0xFFFF }, { 0x0410, 0x0901, 0x0633, 0x0725, 0x0337, 0x0B1B, 0x042D, 0x0C1D, 0xFFFF }, { 0x0518, 0x022C, 0x0629, 0x150D, 0xFFFF }, { 0x0332, 0x0502, 0x0821, 0x0139,
  64. 0x120B, 0x0E1F, 0xFFFF }, { 0x0328, 0x061C, 0x0913, 0x0A17, 0xFFFF }, { 0x0420, 0x0535, 0x0C05, 0x0727, 0xFFFF }, { 0x0823, 0x032F, 0xFFFF }, { 0x0516, 0x071E, 0x0F0E, 0x0911, 0x0B19, 0x1009, 0x1F0F, 0xFFFF }, { 0x0422, 0x0604, 0x090A,
  65. 0x0B03, 0x0A15, 0x0E07, 0x062B, 0xFFFF }, { 0x0831, 0x0A01, 0x0733, 0x052D, 0x0D1D, 0xFFFF }, { 0x032A, 0x0825, 0x0437, 0x0729, 0x0C1B, 0x160D, 0xFFFF }, { 0x0430, 0x0514, 0x0236, 0x0808, 0x0921, 0x0239, 0x130B, 0x0F1F, 0xFFFF }, { 0x0500,
  66. 0x0424, 0x0706, 0x0138, 0x0A13, 0x0B17, 0xFFFF }, { 0x061A, 0x0635, 0x0D05, 0xFFFF }, { 0x0923, 0x0827, 0xFFFF }, { 0x0512, 0x0334, 0x003A, 0x0A11, 0x1109, 0x003B, 0x042F, 0xFFFF }, { 0x0426, 0x0B0C, 0x022E, 0x0B15, 0x0F07, 0x0C19,
  67. 0x072B, 0xFFFF }, { 0x0931, 0x0B01, 0x0C03, 0x062D, 0x0E1D, 0xFFFF }, { 0x0510, 0x0833, 0x0925, 0x0537, 0x0D1B, 0x170D, 0xFFFF }, { 0x0618, 0x032C, 0x0A21, 0x0339, 0x0829, 0xFFFF }, { 0x0432, 0x0602, 0x0B13, 0x140B, 0x101F, 0xFFFF }, {
  68. 0x0428, 0x071C, 0x0735, 0x0E05, 0x0C17, 0xFFFF }, { 0x0520, 0x0A23, 0x0927, 0xFFFF }, { 0x0B11, 0x1209, 0x013B, 0x052F, 0xFFFF }, { 0x0616, 0x081E, 0x0D19, 0xFFFF }, { 0x0522, 0x0704, 0x0A0A, 0x0A31, 0x0D03, 0x0C15, 0x1007, 0x082B, 0x072D,
  69. 0x0F1D, 0xFFFF }, { 0x0C01, 0x0933, 0x0A25, 0x0637, 0x0E1B, 0xFFFF }, { 0x042A, 0x0B21, 0x0929, 0x180D, 0xFFFF }, { 0x0530, 0x0614, 0x0336, 0x0908, 0x0439, 0x150B, 0x111F, 0xFFFF }, { 0x0600, 0x0524, 0x0806, 0x0238, 0x0C13, 0x0F05,
  70. 0x0D17, 0xFFFF }, { 0x071A, 0x0B23, 0x0835, 0x0A27, 0xFFFF }, { 0x1309, 0x023B, 0x062F, 0xFFFF }, { 0x0612, 0x0434, 0x013A, 0x0C11, 0x0E19, 0xFFFF }, { 0x0526, 0x0C0C, 0x032E, 0x0B31, 0x0E03, 0x0D15, 0x1107, 0x092B, 0xFFFF }, { 0x0D01,
  71. 0x0A33, 0x0B25, 0x0737, 0x0F1B, 0x082D, 0x101D, 0xFFFF }, { 0x0610, 0x0A29, 0x190D, 0xFFFF }, { 0x0718, 0x042C, 0x0C21, 0x0539, 0x160B, 0x121F, 0xFFFF }, { 0x0532, 0x0702, 0x0D13, 0x0E17, 0xFFFF }, { 0x0528, 0x081C, 0x0935, 0x1005, 0x0B27,
  72. 0xFFFF }, { 0x0620, 0x0C23, 0x033B, 0x072F, 0xFFFF }, { 0x0D11, 0x0F19, 0x1409, 0xFFFF }, { 0x0716, 0x003C, 0x091E, 0x0F03, 0x0E15, 0x1207, 0x0A2B, 0x003D, 0xFFFF }, { 0x0622, 0x0804, 0x0B0A, 0x0C31, 0x0E01, 0x0B33, 0x092D, 0x111D,
  73. 0xFFFF }, { 0x0C25, 0x0837, 0x0B29, 0x101B, 0x1A0D, 0xFFFF }, { 0x052A, 0x0D21, 0x0639, 0x170B, 0x131F, 0xFFFF }, { 0x0630, 0x0714, 0x0436, 0x0A08, 0x0E13, 0x0F17, 0xFFFF }, { 0x0700, 0x0624, 0x0906, 0x0338, 0x0A35, 0x1105, 0xFFFF }, {
  74. 0x081A, 0x0D23, 0x0C27, 0xFFFF }, { 0x0E11, 0x1509, 0x043B, 0x082F, 0xFFFF }, { 0x0712, 0x0534, 0x023A, 0x0F15, 0x1307, 0x1019, 0x0B2B, 0x013D, 0xFFFF }, { 0x0626, 0x0D0C, 0x042E, 0x0D31, 0x0F01, 0x1003, 0x0A2D, 0x121D, 0xFFFF }, { 0x0C33,
  75. 0x0D25, 0x0937, 0x111B, 0x1B0D, 0xFFFF }, { 0x0710, 0x0E21, 0x0739, 0x0C29, 0xFFFF }, { 0x0818, 0x052C, 0x0F13, 0x180B, 0x141F, 0xFFFF }, { 0x0632, 0x0802, 0x0B35, 0x1205, 0x1017, 0xFFFF }, { 0x0628, 0x091C, 0x0E23, 0x0D27, 0xFFFF }, {
  76. 0x0720, 0x0F11, 0x1609, 0x053B, 0x092F, 0xFFFF }, { 0x1119, 0x023D, 0xFFFF }, { 0x0816, 0x013C, 0x0A1E, 0x0E31, 0x1103, 0x1015, 0x1407, 0x0C2B, 0x0B2D, 0x131D, 0xFFFF }, { 0x0722, 0x0904, 0x0C0A, 0x1001, 0x0D33, 0x0E25, 0x0A37, 0x121B,
  77. 0xFFFF }, { 0x0F21, 0x0D29, 0x1C0D, 0xFFFF }, { 0x062A, 0x0839, 0x190B, 0x151F, 0xFFFF }, { 0x0730, 0x0814, 0x0536, 0x0B08, 0x1013, 0x1305, 0x1117, 0xFFFF }, { 0x0800, 0x0724, 0x0A06, 0x0438, 0x0F23, 0x0C35, 0x0E27, 0xFFFF }, { 0x091A,
  78. 0x1709, 0x063B, 0x0A2F, 0xFFFF }, { 0x1011, 0x1219, 0x033D, 0xFFFF }, { 0x0812, 0x0634, 0x033A, 0x0F31, 0x1203, 0x1115, 0x1507, 0x0D2B, 0xFFFF }, { 0x0726, 0x0E0C, 0x052E, 0x1101, 0x0E33, 0x0F25, 0x0B37, 0x131B, 0x0C2D, 0x141D, 0xFFFF }, {
  79. 0x0E29, 0x1D0D, 0xFFFF }, { 0x0810, 0x1021, 0x0939, 0x1A0B, 0x161F, 0xFFFF }, { 0x0918, 0x062C, 0x1113, 0x1217, 0xFFFF }, { 0x0732, 0x0902, 0x0D35, 0x1405, 0x0F27, 0xFFFF }, { 0x0728, 0x0A1C, 0x1023, 0x073B, 0x0B2F, 0xFFFF }, { 0x0820,
  80. 0x1111, 0x1319, 0x1809, 0xFFFF }, { 0x1303, 0x1215, 0x1607, 0x0E2B, 0x043D, 0xFFFF }, { 0x0916, 0x023C, 0x0B1E, 0x1031, 0x1201, 0x0F33, 0x0D2D, 0x151D, 0xFFFF }, { 0x0822, 0x0A04, 0x0D0A, 0x1025, 0x0C37, 0x0F29, 0x141B, 0x1E0D, 0xFFFF }, {
  81. 0x1121, 0x0A39, 0x1B0B, 0x171F, 0xFFFF }, { 0x072A, 0x1213, 0x1317, 0xFFFF }, { 0x0830, 0x0914, 0x0636, 0x0C08, 0x0E35, 0x1505, 0xFFFF }, { 0x0900, 0x0824, 0x0B06, 0x0538, 0x1123, 0x1027, 0xFFFF }, { 0x0A1A, 0x1211, 0x1909, 0x083B, 0x0C2F,
  82. 0xFFFF }, { 0x1315, 0x1707, 0x1419, 0x0F2B, 0x053D, 0xFFFF }, { 0x0912, 0x0734, 0x043A, 0x1131, 0x1301, 0x1403, 0x0E2D, 0x161D, 0xFFFF }, { 0x0826, 0x0F0C, 0x062E, 0x1033, 0x1125, 0x0D37, 0x151B, 0x1F0D, 0xFFFF }, { 0x1221, 0x0B39, 0x1029,
  83. 0xFFFF }, { 0x0910, 0x1313, 0x1C0B, 0x181F, 0xFFFF }, { 0x0A18, 0x072C, 0x0F35, 0x1605, 0x1417, 0xFFFF }, { 0x0832, 0x0A02, 0x1223, 0x1127, 0xFFFF }, { 0x0828, 0x0B1C, 0x1311, 0x1A09, 0x093B, 0x0D2F, 0xFFFF }, { 0x0920, 0x1519, 0x063D,
  84. 0xFFFF }, { 0x1231, 0x1503, 0x1415, 0x1807, 0x102B, 0x0F2D, 0x171D, 0xFFFF }, { 0x0A16, 0x033C, 0x0C1E, 0x1401, 0x1133, 0x1225, 0x0E37, 0x161B, 0xFFFF }, { 0x0922, 0x0B04, 0x0E0A, 0x1321, 0x1129, 0xFFFF }, { 0x0C39, 0x1D0B, 0x191F, 0xFFFF
  85. }, { 0x082A, 0x1413, 0x1705, 0x1517, 0xFFFF }, { 0x0930, 0x0A14, 0x0736, 0x0D08, 0x1323, 0x1035, 0x1227, 0xFFFF }, { 0x0A00, 0x0924, 0x0C06, 0x0638, 0x1B09, 0x0A3B, 0x0E2F, 0xFFFF }, { 0x0B1A, 0x1411, 0x1619, 0x073D, 0xFFFF }, { 0x1331,
  86. 0x1603, 0x1515, 0x1907, 0x112B, 0xFFFF }, { 0x0A12, 0x0834, 0x053A, 0x1501, 0x1233, 0x1325, 0x0F37, 0x171B, 0x102D, 0x181D, 0xFFFF }, { 0x0926, 0x072E, 0x1229, 0xFFFF }, { 0x1421, 0x0D39, 0x1E0B, 0x1A1F, 0xFFFF }, { 0x0A10, 0x1513,
  87. 0x1617, 0xFFFF }, { 0x0B18, 0x082C, 0x1135, 0x1805, 0x1327, 0xFFFF }, { 0x0932, 0x0B02, 0x1423, 0x0B3B, 0x0F2F, 0xFFFF }, { 0x0928, 0x0C1C, 0x1511, 0x1719, 0x1C09, 0xFFFF }, { 0x0A20, 0x1703, 0x1615, 0x1A07, 0x122B, 0x083D, 0xFFFF }, {
  88. 0x1431, 0x1601, 0x1333, 0x112D, 0x191D, 0xFFFF }, { 0x0B16, 0x043C, 0x0D1E, 0x1425, 0x1037, 0x1329, 0x181B, 0xFFFF }, { 0x0A22, 0x0C04, 0x0F0A, 0x1521, 0x0E39, 0x1F0B, 0x1B1F, 0xFFFF }, { 0x1613, 0x1717, 0xFFFF }, { 0x092A, 0x1235, 0x1905,
  89. 0xFFFF }, { 0x0A30, 0x0B14, 0x0836, 0x0E08, 0x1523, 0x1427, 0xFFFF }, { 0x0B00, 0x0A24, 0x0D06, 0x0738, 0x1611, 0x1D09, 0x0C3B, 0x102F, 0xFFFF }, { 0x0C1A, 0x1715, 0x1B07, 0x1819, 0x132B, 0x093D, 0xFFFF }, { 0x1531, 0x1701, 0x1803, 0x122D,
  90. 0x1A1D, 0xFFFF }, { 0x0B12, 0x0934, 0x063A, 0x1433, 0x1525, 0x1137, 0x191B, 0xFFFF }, { 0x0A26, 0x003E, 0x082E, 0x1621, 0x0F39, 0x1429, 0x003F, 0xFFFF }, { 0x1713, 0x1C1F, 0xFFFF }, { 0x0B10, 0x1335, 0x1A05, 0x1817, 0xFFFF }, { 0x0C18,
  91. 0x092C, 0x1623, 0x1527, 0xFFFF }, { 0x0A32, 0x0C02, 0x1711, 0x1E09, 0x0D3B, 0x112F, 0xFFFF }, { 0x0A28, 0x0D1C, 0x1919, 0x0A3D, 0xFFFF }, { 0x0B20, 0x1631, 0x1903, 0x1815, 0x1C07, 0x142B, 0x132D, 0x1B1D, 0xFFFF }, { 0x1801, 0x1533, 0x1625,
  92. 0x1237, 0x1A1B, 0xFFFF }, { 0x0C16, 0x053C, 0x0E1E, 0x1721, 0x1529, 0x013F, 0xFFFF }, { 0x0B22, 0x0D04, 0x1039, 0x1D1F, 0xFFFF }, { 0x1813, 0x1B05, 0x1917, 0xFFFF }, { 0x0A2A, 0x1723, 0x1435, 0x1627, 0xFFFF }, { 0x0B30, 0x0C14, 0x0936,
  93. 0x0F08, 0x1F09, 0x0E3B, 0x122F, 0xFFFF }, { 0x0C00, 0x0B24, 0x0E06, 0x0838, 0x1811, 0x1A19, 0x0B3D, 0xFFFF }, { 0x0D1A, 0x1731, 0x1A03, 0x1915, 0x1D07, 0x152B, 0xFFFF }, { 0x1901, 0x1633, 0x1725, 0x1337, 0x1B1B, 0x142D, 0x1C1D, 0xFFFF }, {
  94. 0x0C12, 0x0A34, 0x073A, 0x1629, 0x023F, 0xFFFF }, { 0x0B26, 0x013E, 0x092E, 0x1821, 0x1139, 0x1E1F, 0xFFFF }, { 0x1913, 0x1A17, 0xFFFF }, { 0x0C10, 0x1535, 0x1C05, 0x1727, 0xFFFF }, { 0x0D18, 0x0A2C, 0x1823, 0x0F3B, 0x132F, 0xFFFF }, {
  95. 0x0B32, 0x0D02, 0x1911, 0x1B19, 0xFFFF }, { 0x0B28, 0x0E1C, 0x1B03, 0x1A15, 0x1E07, 0x162B, 0x0C3D, 0xFFFF }, { 0x0C20, 0x1831, 0x1A01, 0x1733, 0x152D, 0x1D1D, 0xFFFF }, { 0x1825, 0x1437, 0x1729, 0x1C1B, 0x033F, 0xFFFF }, { 0x0D16, 0x063C,
  96. 0x0F1E, 0x1921, 0x1239, 0x1F1F, 0xFFFF }, { 0x0C22, 0x0E04, 0x1A13, 0x1B17, 0xFFFF }, { 0x1635, 0x1D05, 0xFFFF }, { 0x0B2A, 0x1923, 0x1827, 0xFFFF }, { 0x0C30, 0x0D14, 0x0A36, 0x1A11, 0x103B, 0x142F, 0xFFFF }, { 0x0D00, 0x0C24, 0x0F06,
  97. 0x0938, 0x1B15, 0x1F07, 0x1C19, 0x172B, 0x0D3D, 0xFFFF }, { 0x0E1A, 0x1931, 0x1B01, 0x1C03, 0x162D, 0x1E1D, 0xFFFF }, { 0x1833, 0x1925, 0x1537, 0x1D1B, 0xFFFF }, { 0x0D12, 0x0B34, 0x083A, 0x1A21, 0x1339, 0x1829, 0x043F, 0xFFFF }, { 0x0C26,
  98. 0x023E, 0x0A2E, 0x1B13, 0xFFFF }, { 0x1735, 0x1E05, 0x1C17, 0xFFFF }, { 0x0D10, 0x1A23, 0x1927, 0xFFFF }, { 0x0E18, 0x0B2C, 0x1B11, 0x113B, 0x152F, 0xFFFF }, { 0x0C32, 0x0E02, 0x1D19, 0x0E3D, 0xFFFF }, { 0x0C28, 0x0F1C, 0x1A31, 0x1D03,
  99. 0x1C15, 0x182B, 0x172D, 0x1F1D, 0xFFFF }, { 0x0D20, 0x1C01, 0x1933, 0x1A25, 0x1637, 0x1E1B, 0xFFFF }, { 0x1B21, 0x1929, 0x053F, 0xFFFF }, { 0x0E16, 0x073C, 0x1439, 0xFFFF }, { 0x0D22, 0x0F04, 0x1C13, 0x1F05, 0x1D17, 0xFFFF }, { 0x1B23,
  100. 0x1835, 0x1A27, 0xFFFF }, { 0x0C2A, 0x123B, 0x162F, 0xFFFF }, { 0x0D30, 0x0E14, 0x0B36, 0x1C11, 0x1E19, 0x0F3D, 0xFFFF }, { 0x0E00, 0x0D24, 0x0A38, 0x1B31, 0x1E03, 0x1D15, 0x192B, 0xFFFF }, { 0x0F1A, 0x1D01, 0x1A33, 0x1B25, 0x1737, 0x1F1B,
  101. 0x182D, 0xFFFF }, { 0x1A29, 0x063F, 0xFFFF }, { 0x0E12, 0x0C34, 0x093A, 0x1C21, 0x1539, 0xFFFF }, { 0x0D26, 0x033E, 0x0B2E, 0x1D13, 0x1E17, 0xFFFF }, { 0x1935, 0x1B27, 0xFFFF }, { 0x0E10, 0x1C23, 0x133B, 0x172F, 0xFFFF }, { 0x0F18,
  102. 0x0C2C, 0x1D11, 0x1F19, 0xFFFF }, { 0x0D32, 0x0F02, 0x1F03, 0x1E15, 0x1A2B, 0x103D, 0xFFFF }, { 0x0D28, 0x1C31, 0x1E01, 0x1B33, 0x192D, 0xFFFF }, { 0x0E20, 0x1C25, 0x1837, 0x1B29, 0x073F, 0xFFFF }, { 0x1D21, 0x1639, 0xFFFF }, { 0x0F16,
  103. 0x083C, 0x1E13, 0x1F17, 0xFFFF }, { 0x0E22, 0x1A35, 0xFFFF }, { 0x1D23, 0x1C27, 0xFFFF }, { 0x0D2A, 0x1E11, 0x143B, 0x182F, 0xFFFF }, { 0x0E30, 0x0F14, 0x0C36, 0x1F15, 0x1B2B, 0x113D, 0xFFFF }, { 0x0F00, 0x0E24, 0x0B38, 0x1D31, 0x1F01,
  104. 0x1A2D, 0xFFFF }, { 0x1C33, 0x1D25, 0x1937, 0xFFFF }, { 0x1E21, 0x1739, 0x1C29, 0x083F, 0xFFFF }, { 0x0F12, 0x0D34, 0x0A3A, 0x1F13, 0xFFFF }, { 0x0E26, 0x043E, 0x0C2E, 0x1B35, 0xFFFF }, { 0x1E23, 0x1D27, 0xFFFF }, { 0x0F10, 0x1F11, 0x153B, 0x192F, 0xFFFF }, { 0x0D2C, 0x123D, 0xFFFF },
  105. };
  106. static uint32_t etc1_decode_value(uint32_t diff, uint32_t inten, uint32_t selector, uint32_t packed_c)
  107. {
  108. const uint32_t limit = diff ? 32 : 16;
  109. BASISU_NOTE_UNUSED(limit);
  110. assert((diff < 2) && (inten < 8) && (selector < 4) && (packed_c < limit));
  111. int c;
  112. if (diff)
  113. c = (packed_c >> 2) | (packed_c << 3);
  114. else
  115. c = packed_c | (packed_c << 4);
  116. c += g_etc1_inten_tables[inten][selector];
  117. c = clamp<int>(c, 0, 255);
  118. return c;
  119. }
  120. void pack_etc1_solid_color_init()
  121. {
  122. for (uint32_t diff = 0; diff < 2; diff++)
  123. {
  124. const uint32_t limit = diff ? 32 : 16;
  125. for (uint32_t inten = 0; inten < 8; inten++)
  126. {
  127. for (uint32_t selector = 0; selector < 4; selector++)
  128. {
  129. const uint32_t inverse_table_index = diff + (inten << 1) + (selector << 4);
  130. for (uint32_t color = 0; color < 256; color++)
  131. {
  132. uint32_t best_error = UINT32_MAX, best_packed_c = 0;
  133. for (uint32_t packed_c = 0; packed_c < limit; packed_c++)
  134. {
  135. int v = etc1_decode_value(diff, inten, selector, packed_c);
  136. uint32_t err = (uint32_t)labs(v - static_cast<int>(color));
  137. if (err < best_error)
  138. {
  139. best_error = err;
  140. best_packed_c = packed_c;
  141. if (!best_error)
  142. break;
  143. }
  144. }
  145. assert(best_error <= 255);
  146. g_etc1_inverse_lookup[inverse_table_index][color] = static_cast<uint16_t>(best_packed_c | (best_error << 8));
  147. }
  148. }
  149. }
  150. }
  151. #if 0
  152. for (uint32_t y = 0; y < 64; y++)
  153. {
  154. printf("{");
  155. for (uint32_t x = 0; x < 256; x++)
  156. {
  157. printf("0x%X", g_etc1_inverse_lookup[y][x]);
  158. if (x != 255)
  159. printf(",");
  160. if (((x & 63) == 63) && (x != 255))
  161. printf("\n");
  162. }
  163. printf("},\n");
  164. }
  165. #endif
  166. }
  167. // Packs solid color blocks efficiently using a set of small precomputed tables.
  168. // For random 888 inputs, MSE results are better than Erricson's ETC1 packer in "slow" mode ~9.5% of the time, is slightly worse only ~.01% of the time, and is equal the rest of the time.
  169. uint64_t pack_etc1_block_solid_color(etc_block& block, const uint8_t* pColor)
  170. {
  171. assert(g_etc1_inverse_lookup[0][255]);
  172. static uint32_t s_next_comp[4] = { 1, 2, 0, 1 };
  173. uint32_t best_error = UINT32_MAX, best_i = 0;
  174. int best_x = 0, best_packed_c1 = 0, best_packed_c2 = 0;
  175. // For each possible 8-bit value, there is a precomputed list of diff/inten/selector configurations that allow that 8-bit value to be encoded with no error.
  176. for (uint32_t i = 0; i < 3; i++)
  177. {
  178. const uint32_t c1 = pColor[s_next_comp[i]], c2 = pColor[s_next_comp[i + 1]];
  179. const int delta_range = 1;
  180. for (int delta = -delta_range; delta <= delta_range; delta++)
  181. {
  182. const int c_plus_delta = clamp<int>(pColor[i] + delta, 0, 255);
  183. const uint16_t* pTable;
  184. if (!c_plus_delta)
  185. pTable = g_etc1_color8_to_etc_block_config_0_255[0];
  186. else if (c_plus_delta == 255)
  187. pTable = g_etc1_color8_to_etc_block_config_0_255[1];
  188. else
  189. pTable = g_etc1_color8_to_etc_block_config_1_to_254[c_plus_delta - 1];
  190. do
  191. {
  192. const uint32_t x = *pTable++;
  193. #ifdef _DEBUG
  194. const uint32_t diff = x & 1;
  195. const uint32_t inten = (x >> 1) & 7;
  196. const uint32_t selector = (x >> 4) & 3;
  197. const uint32_t p0 = (x >> 8) & 255;
  198. assert(etc1_decode_value(diff, inten, selector, p0) == (uint32_t)c_plus_delta);
  199. #endif
  200. const uint16_t* pInverse_table = g_etc1_inverse_lookup[x & 0xFF];
  201. uint16_t p1 = pInverse_table[c1];
  202. uint16_t p2 = pInverse_table[c2];
  203. const uint32_t trial_error = square(c_plus_delta - pColor[i]) + square(p1 >> 8) + square(p2 >> 8);
  204. if (trial_error < best_error)
  205. {
  206. best_error = trial_error;
  207. best_x = x;
  208. best_packed_c1 = p1 & 0xFF;
  209. best_packed_c2 = p2 & 0xFF;
  210. best_i = i;
  211. if (!best_error)
  212. goto found_perfect_match;
  213. }
  214. } while (*pTable != 0xFFFF);
  215. }
  216. }
  217. found_perfect_match:
  218. const uint32_t diff = best_x & 1;
  219. const uint32_t inten = (best_x >> 1) & 7;
  220. block.m_bytes[3] = static_cast<uint8_t>(((inten | (inten << 3)) << 2) | (diff << 1));
  221. const uint32_t etc1_selector = g_selector_index_to_etc1[(best_x >> 4) & 3];
  222. *reinterpret_cast<uint16_t*>(&block.m_bytes[4]) = (etc1_selector & 2) ? 0xFFFF : 0;
  223. *reinterpret_cast<uint16_t*>(&block.m_bytes[6]) = (etc1_selector & 1) ? 0xFFFF : 0;
  224. const uint32_t best_packed_c0 = (best_x >> 8) & 255;
  225. if (diff)
  226. {
  227. block.m_bytes[best_i] = static_cast<uint8_t>(best_packed_c0 << 3);
  228. block.m_bytes[s_next_comp[best_i]] = static_cast<uint8_t>(best_packed_c1 << 3);
  229. block.m_bytes[s_next_comp[best_i + 1]] = static_cast<uint8_t>(best_packed_c2 << 3);
  230. }
  231. else
  232. {
  233. block.m_bytes[best_i] = static_cast<uint8_t>(best_packed_c0 | (best_packed_c0 << 4));
  234. block.m_bytes[s_next_comp[best_i]] = static_cast<uint8_t>(best_packed_c1 | (best_packed_c1 << 4));
  235. block.m_bytes[s_next_comp[best_i + 1]] = static_cast<uint8_t>(best_packed_c2 | (best_packed_c2 << 4));
  236. }
  237. return best_error;
  238. }
  239. const uint32_t BASISU_ETC1_CLUSTER_FIT_ORDER_TABLE_SIZE = 165;
  240. static const struct { uint8_t m_v[4]; } g_cluster_fit_order_tab[BASISU_ETC1_CLUSTER_FIT_ORDER_TABLE_SIZE] =
  241. {
  242. { { 0, 0, 0, 8 } },{ { 0, 5, 2, 1 } },{ { 0, 6, 1, 1 } },{ { 0, 7, 0, 1 } },{ { 0, 7, 1, 0 } },
  243. { { 0, 0, 8, 0 } },{ { 0, 0, 3, 5 } },{ { 0, 1, 7, 0 } },{ { 0, 0, 4, 4 } },{ { 0, 0, 2, 6 } },
  244. { { 0, 0, 7, 1 } },{ { 0, 0, 1, 7 } },{ { 0, 0, 5, 3 } },{ { 1, 6, 0, 1 } },{ { 0, 0, 6, 2 } },
  245. { { 0, 2, 6, 0 } },{ { 2, 4, 2, 0 } },{ { 0, 3, 5, 0 } },{ { 3, 3, 1, 1 } },{ { 4, 2, 0, 2 } },
  246. { { 1, 5, 2, 0 } },{ { 0, 5, 3, 0 } },{ { 0, 6, 2, 0 } },{ { 2, 4, 1, 1 } },{ { 5, 1, 0, 2 } },
  247. { { 6, 1, 1, 0 } },{ { 3, 3, 0, 2 } },{ { 6, 0, 0, 2 } },{ { 0, 8, 0, 0 } },{ { 6, 1, 0, 1 } },
  248. { { 0, 1, 6, 1 } },{ { 1, 6, 1, 0 } },{ { 4, 1, 3, 0 } },{ { 0, 2, 5, 1 } },{ { 5, 0, 3, 0 } },
  249. { { 5, 3, 0, 0 } },{ { 0, 1, 5, 2 } },{ { 0, 3, 4, 1 } },{ { 2, 5, 1, 0 } },{ { 1, 7, 0, 0 } },
  250. { { 0, 1, 4, 3 } },{ { 6, 0, 2, 0 } },{ { 0, 4, 4, 0 } },{ { 2, 6, 0, 0 } },{ { 0, 2, 4, 2 } },
  251. { { 0, 5, 1, 2 } },{ { 0, 6, 0, 2 } },{ { 3, 5, 0, 0 } },{ { 0, 4, 3, 1 } },{ { 3, 4, 1, 0 } },
  252. { { 4, 3, 1, 0 } },{ { 1, 5, 0, 2 } },{ { 0, 3, 3, 2 } },{ { 1, 4, 1, 2 } },{ { 0, 4, 2, 2 } },
  253. { { 2, 3, 3, 0 } },{ { 4, 4, 0, 0 } },{ { 1, 2, 4, 1 } },{ { 0, 5, 0, 3 } },{ { 0, 1, 3, 4 } },
  254. { { 1, 5, 1, 1 } },{ { 1, 4, 2, 1 } },{ { 1, 3, 2, 2 } },{ { 5, 2, 1, 0 } },{ { 1, 3, 3, 1 } },
  255. { { 0, 1, 2, 5 } },{ { 1, 1, 5, 1 } },{ { 0, 3, 2, 3 } },{ { 2, 5, 0, 1 } },{ { 3, 2, 2, 1 } },
  256. { { 2, 3, 0, 3 } },{ { 1, 4, 3, 0 } },{ { 2, 2, 1, 3 } },{ { 6, 2, 0, 0 } },{ { 1, 0, 6, 1 } },
  257. { { 3, 3, 2, 0 } },{ { 7, 1, 0, 0 } },{ { 3, 1, 4, 0 } },{ { 0, 2, 3, 3 } },{ { 0, 4, 1, 3 } },
  258. { { 0, 4, 0, 4 } },{ { 0, 1, 0, 7 } },{ { 2, 0, 5, 1 } },{ { 2, 0, 4, 2 } },{ { 3, 0, 2, 3 } },
  259. { { 2, 2, 4, 0 } },{ { 2, 2, 3, 1 } },{ { 4, 0, 3, 1 } },{ { 3, 2, 3, 0 } },{ { 2, 3, 2, 1 } },
  260. { { 1, 3, 4, 0 } },{ { 7, 0, 1, 0 } },{ { 3, 0, 4, 1 } },{ { 1, 0, 5, 2 } },{ { 8, 0, 0, 0 } },
  261. { { 3, 0, 1, 4 } },{ { 4, 1, 1, 2 } },{ { 4, 0, 2, 2 } },{ { 1, 2, 5, 0 } },{ { 4, 2, 1, 1 } },
  262. { { 3, 4, 0, 1 } },{ { 2, 0, 3, 3 } },{ { 5, 0, 1, 2 } },{ { 5, 0, 0, 3 } },{ { 2, 4, 0, 2 } },
  263. { { 2, 1, 4, 1 } },{ { 4, 0, 1, 3 } },{ { 2, 1, 5, 0 } },{ { 4, 2, 2, 0 } },{ { 4, 0, 4, 0 } },
  264. { { 1, 0, 4, 3 } },{ { 1, 4, 0, 3 } },{ { 3, 0, 3, 2 } },{ { 4, 3, 0, 1 } },{ { 0, 1, 1, 6 } },
  265. { { 1, 3, 1, 3 } },{ { 0, 2, 2, 4 } },{ { 2, 0, 2, 4 } },{ { 5, 1, 1, 1 } },{ { 3, 0, 5, 0 } },
  266. { { 2, 3, 1, 2 } },{ { 3, 0, 0, 5 } },{ { 0, 3, 1, 4 } },{ { 5, 0, 2, 1 } },{ { 2, 1, 3, 2 } },
  267. { { 2, 0, 6, 0 } },{ { 3, 1, 3, 1 } },{ { 5, 1, 2, 0 } },{ { 1, 0, 3, 4 } },{ { 1, 1, 6, 0 } },
  268. { { 4, 0, 0, 4 } },{ { 2, 0, 1, 5 } },{ { 0, 3, 0, 5 } },{ { 1, 3, 0, 4 } },{ { 4, 1, 2, 1 } },
  269. { { 1, 2, 3, 2 } },{ { 3, 1, 0, 4 } },{ { 5, 2, 0, 1 } },{ { 1, 2, 2, 3 } },{ { 3, 2, 1, 2 } },
  270. { { 2, 2, 2, 2 } },{ { 6, 0, 1, 1 } },{ { 1, 2, 1, 4 } },{ { 1, 1, 4, 2 } },{ { 3, 2, 0, 3 } },
  271. { { 1, 2, 0, 5 } },{ { 1, 0, 7, 0 } },{ { 3, 1, 2, 2 } },{ { 1, 0, 2, 5 } },{ { 2, 0, 0, 6 } },
  272. { { 2, 1, 1, 4 } },{ { 2, 2, 0, 4 } },{ { 1, 1, 3, 3 } },{ { 7, 0, 0, 1 } },{ { 1, 0, 0, 7 } },
  273. { { 2, 1, 2, 3 } },{ { 4, 1, 0, 3 } },{ { 3, 1, 1, 3 } },{ { 1, 1, 2, 4 } },{ { 2, 1, 0, 5 } },
  274. { { 1, 0, 1, 6 } },{ { 0, 2, 1, 5 } },{ { 0, 2, 0, 6 } },{ { 1, 1, 1, 5 } },{ { 1, 1, 0, 6 } }
  275. };
  276. const int g_etc1_inten_tables[cETC1IntenModifierValues][cETC1SelectorValues] =
  277. {
  278. { -8, -2, 2, 8 }, { -17, -5, 5, 17 }, { -29, -9, 9, 29 }, { -42, -13, 13, 42 },
  279. { -60, -18, 18, 60 }, { -80, -24, 24, 80 }, { -106, -33, 33, 106 }, { -183, -47, 47, 183 }
  280. };
  281. const uint8_t g_etc1_to_selector_index[cETC1SelectorValues] = { 2, 3, 1, 0 };
  282. const uint8_t g_selector_index_to_etc1[cETC1SelectorValues] = { 3, 2, 0, 1 };
  283. // [flip][subblock][pixel_index]
  284. const etc_coord2 g_etc1_pixel_coords[2][2][8] =
  285. {
  286. {
  287. {
  288. { 0, 0 }, { 0, 1 }, { 0, 2 }, { 0, 3 },
  289. { 1, 0 }, { 1, 1 }, { 1, 2 }, { 1, 3 }
  290. },
  291. {
  292. { 2, 0 }, { 2, 1 }, { 2, 2 }, { 2, 3 },
  293. { 3, 0 }, { 3, 1 }, { 3, 2 }, { 3, 3 }
  294. }
  295. },
  296. {
  297. {
  298. { 0, 0 }, { 1, 0 }, { 2, 0 }, { 3, 0 },
  299. { 0, 1 }, { 1, 1 }, { 2, 1 }, { 3, 1 }
  300. },
  301. {
  302. { 0, 2 }, { 1, 2 }, { 2, 2 }, { 3, 2 },
  303. { 0, 3 }, { 1, 3 }, { 2, 3 }, { 3, 3 }
  304. },
  305. }
  306. };
  307. // [flip][subblock][pixel_index]
  308. const uint32_t g_etc1_pixel_indices[2][2][8] =
  309. {
  310. {
  311. {
  312. 0 + 4 * 0, 0 + 4 * 1, 0 + 4 * 2, 0 + 4 * 3,
  313. 1 + 4 * 0, 1 + 4 * 1, 1 + 4 * 2, 1 + 4 * 3
  314. },
  315. {
  316. 2 + 4 * 0, 2 + 4 * 1, 2 + 4 * 2, 2 + 4 * 3,
  317. 3 + 4 * 0, 3 + 4 * 1, 3 + 4 * 2, 3 + 4 * 3
  318. }
  319. },
  320. {
  321. {
  322. 0 + 4 * 0, 1 + 4 * 0, 2 + 4 * 0, 3 + 4 * 0,
  323. 0 + 4 * 1, 1 + 4 * 1, 2 + 4 * 1, 3 + 4 * 1
  324. },
  325. {
  326. 0 + 4 * 2, 1 + 4 * 2, 2 + 4 * 2, 3 + 4 * 2,
  327. 0 + 4 * 3, 1 + 4 * 3, 2 + 4 * 3, 3 + 4 * 3
  328. },
  329. }
  330. };
  331. uint16_t etc_block::pack_color5(const color_rgba& color, bool scaled, uint32_t bias)
  332. {
  333. return pack_color5(color.r, color.g, color.b, scaled, bias);
  334. }
  335. uint16_t etc_block::pack_color5(uint32_t r, uint32_t g, uint32_t b, bool scaled, uint32_t bias)
  336. {
  337. if (scaled)
  338. {
  339. r = (r * 31U + bias) / 255U;
  340. g = (g * 31U + bias) / 255U;
  341. b = (b * 31U + bias) / 255U;
  342. }
  343. r = minimum(r, 31U);
  344. g = minimum(g, 31U);
  345. b = minimum(b, 31U);
  346. return static_cast<uint16_t>(b | (g << 5U) | (r << 10U));
  347. }
  348. color_rgba etc_block::unpack_color5(uint16_t packed_color5, bool scaled, uint32_t alpha)
  349. {
  350. uint32_t b = packed_color5 & 31U;
  351. uint32_t g = (packed_color5 >> 5U) & 31U;
  352. uint32_t r = (packed_color5 >> 10U) & 31U;
  353. if (scaled)
  354. {
  355. b = (b << 3U) | (b >> 2U);
  356. g = (g << 3U) | (g >> 2U);
  357. r = (r << 3U) | (r >> 2U);
  358. }
  359. return color_rgba(cNoClamp, r, g, b, minimum(alpha, 255U));
  360. }
  361. void etc_block::unpack_color5(color_rgba& result, uint16_t packed_color5, bool scaled)
  362. {
  363. result = unpack_color5(packed_color5, scaled, 255);
  364. }
  365. void etc_block::unpack_color5(uint32_t& r, uint32_t& g, uint32_t& b, uint16_t packed_color5, bool scaled)
  366. {
  367. color_rgba c(unpack_color5(packed_color5, scaled, 0));
  368. r = c.r;
  369. g = c.g;
  370. b = c.b;
  371. }
  372. bool etc_block::unpack_color5(color_rgba& result, uint16_t packed_color5, uint16_t packed_delta3, bool scaled, uint32_t alpha)
  373. {
  374. color_rgba_i16 dc(unpack_delta3(packed_delta3));
  375. int b = (packed_color5 & 31U) + dc.b;
  376. int g = ((packed_color5 >> 5U) & 31U) + dc.g;
  377. int r = ((packed_color5 >> 10U) & 31U) + dc.r;
  378. bool success = true;
  379. if (static_cast<uint32_t>(r | g | b) > 31U)
  380. {
  381. success = false;
  382. r = clamp<int>(r, 0, 31);
  383. g = clamp<int>(g, 0, 31);
  384. b = clamp<int>(b, 0, 31);
  385. }
  386. if (scaled)
  387. {
  388. b = (b << 3U) | (b >> 2U);
  389. g = (g << 3U) | (g >> 2U);
  390. r = (r << 3U) | (r >> 2U);
  391. }
  392. result.set_noclamp_rgba(r, g, b, minimum(alpha, 255U));
  393. return success;
  394. }
  395. bool etc_block::unpack_color5(uint32_t& r, uint32_t& g, uint32_t& b, uint16_t packed_color5, uint16_t packed_delta3, bool scaled, uint32_t alpha)
  396. {
  397. color_rgba result;
  398. const bool success = unpack_color5(result, packed_color5, packed_delta3, scaled, alpha);
  399. r = result.r;
  400. g = result.g;
  401. b = result.b;
  402. return success;
  403. }
  404. uint16_t etc_block::pack_delta3(const color_rgba_i16& color)
  405. {
  406. return pack_delta3(color.r, color.g, color.b);
  407. }
  408. uint16_t etc_block::pack_delta3(int r, int g, int b)
  409. {
  410. assert((r >= cETC1ColorDeltaMin) && (r <= cETC1ColorDeltaMax));
  411. assert((g >= cETC1ColorDeltaMin) && (g <= cETC1ColorDeltaMax));
  412. assert((b >= cETC1ColorDeltaMin) && (b <= cETC1ColorDeltaMax));
  413. if (r < 0) r += 8;
  414. if (g < 0) g += 8;
  415. if (b < 0) b += 8;
  416. return static_cast<uint16_t>(b | (g << 3) | (r << 6));
  417. }
  418. color_rgba_i16 etc_block::unpack_delta3(uint16_t packed_delta3)
  419. {
  420. int r = (packed_delta3 >> 6) & 7;
  421. int g = (packed_delta3 >> 3) & 7;
  422. int b = packed_delta3 & 7;
  423. if (r >= 4) r -= 8;
  424. if (g >= 4) g -= 8;
  425. if (b >= 4) b -= 8;
  426. return color_rgba_i16(r, g, b, 255);
  427. }
  428. void etc_block::unpack_delta3(int& r, int& g, int& b, uint16_t packed_delta3)
  429. {
  430. r = (packed_delta3 >> 6) & 7;
  431. g = (packed_delta3 >> 3) & 7;
  432. b = packed_delta3 & 7;
  433. if (r >= 4) r -= 8;
  434. if (g >= 4) g -= 8;
  435. if (b >= 4) b -= 8;
  436. }
  437. uint16_t etc_block::pack_color4(const color_rgba& color, bool scaled, uint32_t bias)
  438. {
  439. return pack_color4(color.r, color.g, color.b, scaled, bias);
  440. }
  441. uint16_t etc_block::pack_color4(uint32_t r, uint32_t g, uint32_t b, bool scaled, uint32_t bias)
  442. {
  443. if (scaled)
  444. {
  445. r = (r * 15U + bias) / 255U;
  446. g = (g * 15U + bias) / 255U;
  447. b = (b * 15U + bias) / 255U;
  448. }
  449. r = minimum(r, 15U);
  450. g = minimum(g, 15U);
  451. b = minimum(b, 15U);
  452. return static_cast<uint16_t>(b | (g << 4U) | (r << 8U));
  453. }
  454. color_rgba etc_block::unpack_color4(uint16_t packed_color4, bool scaled, uint32_t alpha)
  455. {
  456. uint32_t b = packed_color4 & 15U;
  457. uint32_t g = (packed_color4 >> 4U) & 15U;
  458. uint32_t r = (packed_color4 >> 8U) & 15U;
  459. if (scaled)
  460. {
  461. b = (b << 4U) | b;
  462. g = (g << 4U) | g;
  463. r = (r << 4U) | r;
  464. }
  465. return color_rgba(cNoClamp, r, g, b, minimum(alpha, 255U));
  466. }
  467. void etc_block::unpack_color4(uint32_t& r, uint32_t& g, uint32_t& b, uint16_t packed_color4, bool scaled)
  468. {
  469. color_rgba c(unpack_color4(packed_color4, scaled, 0));
  470. r = c.r;
  471. g = c.g;
  472. b = c.b;
  473. }
  474. void etc_block::get_diff_subblock_colors(color_rgba* pDst, uint16_t packed_color5, uint32_t table_idx)
  475. {
  476. assert(table_idx < cETC1IntenModifierValues);
  477. const int *pInten_modifer_table = &g_etc1_inten_tables[table_idx][0];
  478. uint32_t r, g, b;
  479. unpack_color5(r, g, b, packed_color5, true);
  480. const int ir = static_cast<int>(r), ig = static_cast<int>(g), ib = static_cast<int>(b);
  481. const int y0 = pInten_modifer_table[0];
  482. pDst[0].set(ir + y0, ig + y0, ib + y0, 255);
  483. const int y1 = pInten_modifer_table[1];
  484. pDst[1].set(ir + y1, ig + y1, ib + y1, 255);
  485. const int y2 = pInten_modifer_table[2];
  486. pDst[2].set(ir + y2, ig + y2, ib + y2, 255);
  487. const int y3 = pInten_modifer_table[3];
  488. pDst[3].set(ir + y3, ig + y3, ib + y3, 255);
  489. }
  490. bool etc_block::get_diff_subblock_colors(color_rgba* pDst, uint16_t packed_color5, uint16_t packed_delta3, uint32_t table_idx)
  491. {
  492. assert(table_idx < cETC1IntenModifierValues);
  493. const int *pInten_modifer_table = &g_etc1_inten_tables[table_idx][0];
  494. uint32_t r, g, b;
  495. bool success = unpack_color5(r, g, b, packed_color5, packed_delta3, true);
  496. const int ir = static_cast<int>(r), ig = static_cast<int>(g), ib = static_cast<int>(b);
  497. const int y0 = pInten_modifer_table[0];
  498. pDst[0].set(ir + y0, ig + y0, ib + y0, 255);
  499. const int y1 = pInten_modifer_table[1];
  500. pDst[1].set(ir + y1, ig + y1, ib + y1, 255);
  501. const int y2 = pInten_modifer_table[2];
  502. pDst[2].set(ir + y2, ig + y2, ib + y2, 255);
  503. const int y3 = pInten_modifer_table[3];
  504. pDst[3].set(ir + y3, ig + y3, ib + y3, 255);
  505. return success;
  506. }
  507. void etc_block::get_abs_subblock_colors(color_rgba* pDst, uint16_t packed_color4, uint32_t table_idx)
  508. {
  509. assert(table_idx < cETC1IntenModifierValues);
  510. const int *pInten_modifer_table = &g_etc1_inten_tables[table_idx][0];
  511. uint32_t r, g, b;
  512. unpack_color4(r, g, b, packed_color4, true);
  513. const int ir = static_cast<int>(r), ig = static_cast<int>(g), ib = static_cast<int>(b);
  514. const int y0 = pInten_modifer_table[0];
  515. pDst[0].set(ir + y0, ig + y0, ib + y0, 255);
  516. const int y1 = pInten_modifer_table[1];
  517. pDst[1].set(ir + y1, ig + y1, ib + y1, 255);
  518. const int y2 = pInten_modifer_table[2];
  519. pDst[2].set(ir + y2, ig + y2, ib + y2, 255);
  520. const int y3 = pInten_modifer_table[3];
  521. pDst[3].set(ir + y3, ig + y3, ib + y3, 255);
  522. }
  523. bool unpack_etc1(const etc_block& block, color_rgba *pDst, bool preserve_alpha)
  524. {
  525. const bool diff_flag = block.get_diff_bit();
  526. const bool flip_flag = block.get_flip_bit();
  527. const uint32_t table_index0 = block.get_inten_table(0);
  528. const uint32_t table_index1 = block.get_inten_table(1);
  529. color_rgba subblock_colors0[4];
  530. color_rgba subblock_colors1[4];
  531. if (diff_flag)
  532. {
  533. const uint16_t base_color5 = block.get_base5_color();
  534. const uint16_t delta_color3 = block.get_delta3_color();
  535. etc_block::get_diff_subblock_colors(subblock_colors0, base_color5, table_index0);
  536. if (!etc_block::get_diff_subblock_colors(subblock_colors1, base_color5, delta_color3, table_index1))
  537. return false;
  538. }
  539. else
  540. {
  541. const uint16_t base_color4_0 = block.get_base4_color(0);
  542. etc_block::get_abs_subblock_colors(subblock_colors0, base_color4_0, table_index0);
  543. const uint16_t base_color4_1 = block.get_base4_color(1);
  544. etc_block::get_abs_subblock_colors(subblock_colors1, base_color4_1, table_index1);
  545. }
  546. if (preserve_alpha)
  547. {
  548. if (flip_flag)
  549. {
  550. for (uint32_t y = 0; y < 2; y++)
  551. {
  552. pDst[0].set_rgb(subblock_colors0[block.get_selector(0, y)]);
  553. pDst[1].set_rgb(subblock_colors0[block.get_selector(1, y)]);
  554. pDst[2].set_rgb(subblock_colors0[block.get_selector(2, y)]);
  555. pDst[3].set_rgb(subblock_colors0[block.get_selector(3, y)]);
  556. pDst += 4;
  557. }
  558. for (uint32_t y = 2; y < 4; y++)
  559. {
  560. pDst[0].set_rgb(subblock_colors1[block.get_selector(0, y)]);
  561. pDst[1].set_rgb(subblock_colors1[block.get_selector(1, y)]);
  562. pDst[2].set_rgb(subblock_colors1[block.get_selector(2, y)]);
  563. pDst[3].set_rgb(subblock_colors1[block.get_selector(3, y)]);
  564. pDst += 4;
  565. }
  566. }
  567. else
  568. {
  569. for (uint32_t y = 0; y < 4; y++)
  570. {
  571. pDst[0].set_rgb(subblock_colors0[block.get_selector(0, y)]);
  572. pDst[1].set_rgb(subblock_colors0[block.get_selector(1, y)]);
  573. pDst[2].set_rgb(subblock_colors1[block.get_selector(2, y)]);
  574. pDst[3].set_rgb(subblock_colors1[block.get_selector(3, y)]);
  575. pDst += 4;
  576. }
  577. }
  578. }
  579. else
  580. {
  581. if (flip_flag)
  582. {
  583. // 0000
  584. // 0000
  585. // 1111
  586. // 1111
  587. for (uint32_t y = 0; y < 2; y++)
  588. {
  589. pDst[0] = subblock_colors0[block.get_selector(0, y)];
  590. pDst[1] = subblock_colors0[block.get_selector(1, y)];
  591. pDst[2] = subblock_colors0[block.get_selector(2, y)];
  592. pDst[3] = subblock_colors0[block.get_selector(3, y)];
  593. pDst += 4;
  594. }
  595. for (uint32_t y = 2; y < 4; y++)
  596. {
  597. pDst[0] = subblock_colors1[block.get_selector(0, y)];
  598. pDst[1] = subblock_colors1[block.get_selector(1, y)];
  599. pDst[2] = subblock_colors1[block.get_selector(2, y)];
  600. pDst[3] = subblock_colors1[block.get_selector(3, y)];
  601. pDst += 4;
  602. }
  603. }
  604. else
  605. {
  606. // 0011
  607. // 0011
  608. // 0011
  609. // 0011
  610. for (uint32_t y = 0; y < 4; y++)
  611. {
  612. pDst[0] = subblock_colors0[block.get_selector(0, y)];
  613. pDst[1] = subblock_colors0[block.get_selector(1, y)];
  614. pDst[2] = subblock_colors1[block.get_selector(2, y)];
  615. pDst[3] = subblock_colors1[block.get_selector(3, y)];
  616. pDst += 4;
  617. }
  618. }
  619. }
  620. return true;
  621. }
  622. inline int extend_6_to_8(uint32_t n)
  623. {
  624. return (n << 2) | (n >> 4);
  625. }
  626. inline int extend_7_to_8(uint32_t n)
  627. {
  628. return (n << 1) | (n >> 6);
  629. }
  630. inline int extend_4_to_8(uint32_t n)
  631. {
  632. return (n << 4) | n;
  633. }
  634. uint64_t etc_block::evaluate_etc1_error(const color_rgba* pBlock_pixels, bool perceptual, int subblock_index) const
  635. {
  636. color_rgba unpacked_block[16];
  637. unpack_etc1(*this, unpacked_block);
  638. uint64_t total_error = 0;
  639. if (subblock_index < 0)
  640. {
  641. for (uint32_t i = 0; i < 16; i++)
  642. total_error += color_distance(perceptual, pBlock_pixels[i], unpacked_block[i], false);
  643. }
  644. else
  645. {
  646. const bool flip_bit = get_flip_bit();
  647. for (uint32_t i = 0; i < 8; i++)
  648. {
  649. const uint32_t idx = g_etc1_pixel_indices[flip_bit][subblock_index][i];
  650. total_error += color_distance(perceptual, pBlock_pixels[idx], unpacked_block[idx], false);
  651. }
  652. }
  653. return total_error;
  654. }
  655. void etc_block::get_subblock_pixels(color_rgba* pPixels, int subblock_index) const
  656. {
  657. if (subblock_index < 0)
  658. unpack_etc1(*this, pPixels);
  659. else
  660. {
  661. color_rgba unpacked_block[16];
  662. unpack_etc1(*this, unpacked_block);
  663. const bool flip_bit = get_flip_bit();
  664. for (uint32_t i = 0; i < 8; i++)
  665. {
  666. const uint32_t idx = g_etc1_pixel_indices[flip_bit][subblock_index][i];
  667. pPixels[i] = unpacked_block[idx];
  668. }
  669. }
  670. }
  671. bool etc1_optimizer::compute()
  672. {
  673. assert(m_pResult->m_pSelectors);
  674. if (m_pParams->m_pForce_selectors)
  675. {
  676. assert(m_pParams->m_quality >= cETCQualitySlow);
  677. if (m_pParams->m_quality < cETCQualitySlow)
  678. return false;
  679. }
  680. const uint32_t n = m_pParams->m_num_src_pixels;
  681. if (m_pParams->m_cluster_fit)
  682. {
  683. if (m_pParams->m_quality == cETCQualityFast)
  684. compute_internal_cluster_fit(4);
  685. else if (m_pParams->m_quality == cETCQualityMedium)
  686. compute_internal_cluster_fit(16);
  687. else if (m_pParams->m_quality == cETCQualitySlow)
  688. compute_internal_cluster_fit(64);
  689. else
  690. compute_internal_cluster_fit(BASISU_ETC1_CLUSTER_FIT_ORDER_TABLE_SIZE);
  691. }
  692. else
  693. compute_internal_neighborhood(m_br, m_bg, m_bb);
  694. if (!m_best_solution.m_valid)
  695. {
  696. m_pResult->m_error = UINT32_MAX;
  697. return false;
  698. }
  699. //const uint8_t* pSelectors = &m_best_solution.m_selectors[0];
  700. const uint8_t* pSelectors = m_pParams->m_pForce_selectors ? m_pParams->m_pForce_selectors : &m_best_solution.m_selectors[0];
  701. #if defined(DEBUG) || defined(_DEBUG)
  702. {
  703. // sanity check the returned error
  704. color_rgba block_colors[4];
  705. m_best_solution.m_coords.get_block_colors(block_colors);
  706. const color_rgba* pSrc_pixels = m_pParams->m_pSrc_pixels;
  707. uint64_t actual_error = 0;
  708. bool perceptual;
  709. if (m_pParams->m_quality >= cETCQualityMedium)
  710. perceptual = m_pParams->m_perceptual;
  711. else
  712. perceptual = (m_pParams->m_quality == cETCQualityFast) ? false : m_pParams->m_perceptual;
  713. for (uint32_t i = 0; i < n; i++)
  714. actual_error += color_distance(perceptual, pSrc_pixels[i], block_colors[pSelectors[i]], false);
  715. assert(actual_error == m_best_solution.m_error);
  716. }
  717. #endif
  718. m_pResult->m_error = m_best_solution.m_error;
  719. m_pResult->m_block_color_unscaled = m_best_solution.m_coords.m_unscaled_color;
  720. m_pResult->m_block_color4 = m_best_solution.m_coords.m_color4;
  721. m_pResult->m_block_inten_table = m_best_solution.m_coords.m_inten_table;
  722. memcpy(m_pResult->m_pSelectors, pSelectors, n);
  723. m_pResult->m_n = n;
  724. return true;
  725. }
  726. void etc1_optimizer::refine_solution(uint32_t max_refinement_trials)
  727. {
  728. // Now we have the input block, the avg. color of the input pixels, a set of trial selector indices, and the block color+intensity index.
  729. // Now, for each component, attempt to refine the current solution by solving a simple linear equation. For example, for 4 colors:
  730. // The goal is:
  731. // pixel0 - (block_color+inten_table[selector0]) + pixel1 - (block_color+inten_table[selector1]) + pixel2 - (block_color+inten_table[selector2]) + pixel3 - (block_color+inten_table[selector3]) = 0
  732. // Rearranging this:
  733. // (pixel0 + pixel1 + pixel2 + pixel3) - (block_color+inten_table[selector0]) - (block_color+inten_table[selector1]) - (block_color+inten_table[selector2]) - (block_color+inten_table[selector3]) = 0
  734. // (pixel0 + pixel1 + pixel2 + pixel3) - block_color - inten_table[selector0] - block_color-inten_table[selector1] - block_color-inten_table[selector2] - block_color-inten_table[selector3] = 0
  735. // (pixel0 + pixel1 + pixel2 + pixel3) - 4*block_color - inten_table[selector0] - inten_table[selector1] - inten_table[selector2] - inten_table[selector3] = 0
  736. // (pixel0 + pixel1 + pixel2 + pixel3) - 4*block_color - (inten_table[selector0] + inten_table[selector1] + inten_table[selector2] + inten_table[selector3]) = 0
  737. // (pixel0 + pixel1 + pixel2 + pixel3)/4 - block_color - (inten_table[selector0] + inten_table[selector1] + inten_table[selector2] + inten_table[selector3])/4 = 0
  738. // block_color = (pixel0 + pixel1 + pixel2 + pixel3)/4 - (inten_table[selector0] + inten_table[selector1] + inten_table[selector2] + inten_table[selector3])/4
  739. // So what this means:
  740. // optimal_block_color = avg_input - avg_inten_delta
  741. // So the optimal block color can be computed by taking the average block color and subtracting the current average of the intensity delta.
  742. // Unfortunately, optimal_block_color must then be quantized to 555 or 444 so it's not always possible to improve matters using this formula.
  743. // Also, the above formula is for unclamped intensity deltas. The actual implementation takes into account clamping.
  744. const uint32_t n = m_pParams->m_num_src_pixels;
  745. for (uint32_t refinement_trial = 0; refinement_trial < max_refinement_trials; refinement_trial++)
  746. {
  747. const uint8_t* pSelectors = &m_best_solution.m_selectors[0];
  748. const int* pInten_table = g_etc1_inten_tables[m_best_solution.m_coords.m_inten_table];
  749. int delta_sum_r = 0, delta_sum_g = 0, delta_sum_b = 0;
  750. const color_rgba base_color(m_best_solution.m_coords.get_scaled_color());
  751. for (uint32_t r = 0; r < n; r++)
  752. {
  753. const uint32_t s = *pSelectors++;
  754. const int yd_temp = pInten_table[s];
  755. // Compute actual delta being applied to each pixel, taking into account clamping.
  756. delta_sum_r += clamp<int>(base_color.r + yd_temp, 0, 255) - base_color.r;
  757. delta_sum_g += clamp<int>(base_color.g + yd_temp, 0, 255) - base_color.g;
  758. delta_sum_b += clamp<int>(base_color.b + yd_temp, 0, 255) - base_color.b;
  759. }
  760. if ((!delta_sum_r) && (!delta_sum_g) && (!delta_sum_b))
  761. break;
  762. const float avg_delta_r_f = static_cast<float>(delta_sum_r) / n;
  763. const float avg_delta_g_f = static_cast<float>(delta_sum_g) / n;
  764. const float avg_delta_b_f = static_cast<float>(delta_sum_b) / n;
  765. const int br1 = clamp<int>(static_cast<int32_t>((m_avg_color[0] - avg_delta_r_f) * m_limit / 255.0f + .5f), 0, m_limit);
  766. const int bg1 = clamp<int>(static_cast<int32_t>((m_avg_color[1] - avg_delta_g_f) * m_limit / 255.0f + .5f), 0, m_limit);
  767. const int bb1 = clamp<int>(static_cast<int32_t>((m_avg_color[2] - avg_delta_b_f) * m_limit / 255.0f + .5f), 0, m_limit);
  768. #if BASISU_DEBUG_ETC_ENCODER_DEEPER
  769. printf("Refinement trial %u, avg_delta %f %f %f\n", refinement_trial, avg_delta_r_f, avg_delta_g_f, avg_delta_b_f);
  770. #endif
  771. if (!evaluate_solution(etc1_solution_coordinates(br1, bg1, bb1, 0, m_pParams->m_use_color4), m_trial_solution, &m_best_solution))
  772. break;
  773. } // refinement_trial
  774. }
  775. void etc1_optimizer::compute_internal_neighborhood(int scan_r, int scan_g, int scan_b)
  776. {
  777. if (m_best_solution.m_error == 0)
  778. return;
  779. //const uint32_t n = m_pParams->m_num_src_pixels;
  780. const int scan_delta_size = m_pParams->m_scan_delta_size;
  781. // Scan through a subset of the 3D lattice centered around the avg block color trying each 3D (555 or 444) lattice point as a potential block color.
  782. // Each time a better solution is found try to refine the current solution's block color based of the current selectors and intensity table index.
  783. for (int zdi = 0; zdi < scan_delta_size; zdi++)
  784. {
  785. const int zd = m_pParams->m_pScan_deltas[zdi];
  786. const int mbb = scan_b + zd;
  787. if (mbb < 0) continue; else if (mbb > m_limit) break;
  788. for (int ydi = 0; ydi < scan_delta_size; ydi++)
  789. {
  790. const int yd = m_pParams->m_pScan_deltas[ydi];
  791. const int mbg = scan_g + yd;
  792. if (mbg < 0) continue; else if (mbg > m_limit) break;
  793. for (int xdi = 0; xdi < scan_delta_size; xdi++)
  794. {
  795. const int xd = m_pParams->m_pScan_deltas[xdi];
  796. const int mbr = scan_r + xd;
  797. if (mbr < 0) continue; else if (mbr > m_limit) break;
  798. etc1_solution_coordinates coords(mbr, mbg, mbb, 0, m_pParams->m_use_color4);
  799. if (!evaluate_solution(coords, m_trial_solution, &m_best_solution))
  800. continue;
  801. if (m_pParams->m_refinement)
  802. {
  803. refine_solution((m_pParams->m_quality == cETCQualityFast) ? 2 : (((xd | yd | zd) == 0) ? 4 : 2));
  804. }
  805. } // xdi
  806. } // ydi
  807. } // zdi
  808. }
  809. void etc1_optimizer::compute_internal_cluster_fit(uint32_t total_perms_to_try)
  810. {
  811. if ((!m_best_solution.m_valid) || ((m_br != m_best_solution.m_coords.m_unscaled_color.r) || (m_bg != m_best_solution.m_coords.m_unscaled_color.g) || (m_bb != m_best_solution.m_coords.m_unscaled_color.b)))
  812. {
  813. evaluate_solution(etc1_solution_coordinates(m_br, m_bg, m_bb, 0, m_pParams->m_use_color4), m_trial_solution, &m_best_solution);
  814. }
  815. if ((m_best_solution.m_error == 0) || (!m_best_solution.m_valid))
  816. return;
  817. for (uint32_t i = 0; i < total_perms_to_try; i++)
  818. {
  819. int delta_sum_r = 0, delta_sum_g = 0, delta_sum_b = 0;
  820. const int *pInten_table = g_etc1_inten_tables[m_best_solution.m_coords.m_inten_table];
  821. const color_rgba base_color(m_best_solution.m_coords.get_scaled_color());
  822. const uint8_t *pNum_selectors = g_cluster_fit_order_tab[i].m_v;
  823. for (uint32_t q = 0; q < 4; q++)
  824. {
  825. const int yd_temp = pInten_table[q];
  826. delta_sum_r += pNum_selectors[q] * (clamp<int>(base_color.r + yd_temp, 0, 255) - base_color.r);
  827. delta_sum_g += pNum_selectors[q] * (clamp<int>(base_color.g + yd_temp, 0, 255) - base_color.g);
  828. delta_sum_b += pNum_selectors[q] * (clamp<int>(base_color.b + yd_temp, 0, 255) - base_color.b);
  829. }
  830. if ((!delta_sum_r) && (!delta_sum_g) && (!delta_sum_b))
  831. continue;
  832. const float avg_delta_r_f = static_cast<float>(delta_sum_r) / 8;
  833. const float avg_delta_g_f = static_cast<float>(delta_sum_g) / 8;
  834. const float avg_delta_b_f = static_cast<float>(delta_sum_b) / 8;
  835. const int br1 = clamp<int>(static_cast<int32_t>((m_avg_color[0] - avg_delta_r_f) * m_limit / 255.0f + .5f), 0, m_limit);
  836. const int bg1 = clamp<int>(static_cast<int32_t>((m_avg_color[1] - avg_delta_g_f) * m_limit / 255.0f + .5f), 0, m_limit);
  837. const int bb1 = clamp<int>(static_cast<int32_t>((m_avg_color[2] - avg_delta_b_f) * m_limit / 255.0f + .5f), 0, m_limit);
  838. #if BASISU_DEBUG_ETC_ENCODER_DEEPER
  839. printf("Second refinement trial %u, avg_delta %f %f %f\n", i, avg_delta_r_f, avg_delta_g_f, avg_delta_b_f);
  840. #endif
  841. evaluate_solution(etc1_solution_coordinates(br1, bg1, bb1, 0, m_pParams->m_use_color4), m_trial_solution, &m_best_solution);
  842. if (m_best_solution.m_error == 0)
  843. break;
  844. }
  845. }
  846. void etc1_optimizer::init(const params& params, results& result)
  847. {
  848. m_pParams = &params;
  849. m_pResult = &result;
  850. const uint32_t n = m_pParams->m_num_src_pixels;
  851. m_selectors.resize(n);
  852. m_best_selectors.resize(n);
  853. m_temp_selectors.resize(n);
  854. m_trial_solution.m_selectors.resize(n);
  855. m_best_solution.m_selectors.resize(n);
  856. m_limit = m_pParams->m_use_color4 ? 15 : 31;
  857. vec3F avg_color(0.0f);
  858. m_luma.resize(n);
  859. m_sorted_luma_indices.resize(n);
  860. m_sorted_luma.resize(n);
  861. int min_r = 255, min_g = 255, min_b = 255;
  862. int max_r = 0, max_g = 0, max_b = 0;
  863. for (uint32_t i = 0; i < n; i++)
  864. {
  865. const color_rgba& c = m_pParams->m_pSrc_pixels[i];
  866. min_r = basisu::minimum<int>(min_r, c.r);
  867. min_g = basisu::minimum<int>(min_g, c.g);
  868. min_b = basisu::minimum<int>(min_b, c.b);
  869. max_r = basisu::maximum<int>(max_r, c.r);
  870. max_g = basisu::maximum<int>(max_g, c.g);
  871. max_b = basisu::maximum<int>(max_b, c.b);
  872. const vec3F fc(c.r, c.g, c.b);
  873. avg_color += fc;
  874. m_luma[i] = static_cast<uint16_t>(c.r + c.g + c.b);
  875. m_sorted_luma_indices[i] = i;
  876. }
  877. avg_color /= static_cast<float>(n);
  878. m_avg_color = avg_color;
  879. m_max_comp_spread = basisu::maximum(basisu::maximum(max_r - min_r, max_g - min_g), max_b - min_b);
  880. m_br = clamp<int>(static_cast<uint32_t>(m_avg_color[0] * m_limit / 255.0f + .5f), 0, m_limit);
  881. m_bg = clamp<int>(static_cast<uint32_t>(m_avg_color[1] * m_limit / 255.0f + .5f), 0, m_limit);
  882. m_bb = clamp<int>(static_cast<uint32_t>(m_avg_color[2] * m_limit / 255.0f + .5f), 0, m_limit);
  883. #if BASISU_DEBUG_ETC_ENCODER_DEEPER
  884. printf("Avg block color: %u %u %u\n", m_br, m_bg, m_bb);
  885. #endif
  886. if (m_pParams->m_quality == cETCQualityFast)
  887. {
  888. indirect_sort(n, &m_sorted_luma_indices[0], &m_luma[0]);
  889. m_pSorted_luma = &m_sorted_luma[0];
  890. m_pSorted_luma_indices = &m_sorted_luma_indices[0];
  891. for (uint32_t i = 0; i < n; i++)
  892. m_pSorted_luma[i] = m_luma[m_pSorted_luma_indices[i]];
  893. }
  894. m_best_solution.m_coords.clear();
  895. m_best_solution.m_valid = false;
  896. m_best_solution.m_error = UINT64_MAX;
  897. clear_obj(m_solutions_tried);
  898. }
  899. // Return false if we've probably already tried this solution, true if we have definitely not.
  900. bool etc1_optimizer::check_for_redundant_solution(const etc1_solution_coordinates& coords)
  901. {
  902. // Hash first 3 bytes of color (RGB)
  903. uint32_t kh = hash_hsieh((uint8_t*)&coords.m_unscaled_color.r, 3);
  904. uint32_t h0 = kh & cSolutionsTriedHashMask;
  905. uint32_t h1 = (kh >> cSolutionsTriedHashBits) & cSolutionsTriedHashMask;
  906. // Simple Bloom filter lookup with k=2
  907. if ( ((m_solutions_tried[h0 >> 3] & (1 << (h0 & 7))) != 0) &&
  908. ((m_solutions_tried[h1 >> 3] & (1 << (h1 & 7))) != 0) )
  909. return false;
  910. m_solutions_tried[h0 >> 3] |= (1 << (h0 & 7));
  911. m_solutions_tried[h1 >> 3] |= (1 << (h1 & 7));
  912. return true;
  913. }
  914. static uint8_t g_eval_dist_tables[8][256] =
  915. {
  916. // 99% threshold
  917. { 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,},
  918. { 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,},
  919. { 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,},
  920. { 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,1,1,0,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,0,1,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,},
  921. { 1,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,0,1,0,0,0,0,0,0,0,0,0,1,0,0,1,},
  922. { 1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,1,0,0,1,0,0,0,0,1,0,1,1,0,1,1,1,1,1,0,1,1,1,0,1,1,0,0,0,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,},
  923. { 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,1,1,0,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,},
  924. { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,1,1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,}
  925. };
  926. bool etc1_optimizer::evaluate_solution_slow(const etc1_solution_coordinates& coords, potential_solution& trial_solution, potential_solution* pBest_solution)
  927. {
  928. if (!check_for_redundant_solution(coords))
  929. return false;
  930. #if BASISU_DEBUG_ETC_ENCODER_DEEPER
  931. printf("Eval solution: %u %u %u\n", coords.m_unscaled_color.r, coords.m_unscaled_color.g, coords.m_unscaled_color.b);
  932. #endif
  933. trial_solution.m_valid = false;
  934. if (m_pParams->m_constrain_against_base_color5)
  935. {
  936. const int dr = (int)coords.m_unscaled_color.r - (int)m_pParams->m_base_color5.r;
  937. const int dg = (int)coords.m_unscaled_color.g - (int)m_pParams->m_base_color5.g;
  938. const int db = (int)coords.m_unscaled_color.b - (int)m_pParams->m_base_color5.b;
  939. if ((minimum(dr, dg, db) < cETC1ColorDeltaMin) || (maximum(dr, dg, db) > cETC1ColorDeltaMax))
  940. {
  941. #if BASISU_DEBUG_ETC_ENCODER_DEEPER
  942. printf("Eval failed due to constraint from %u %u %u\n", m_pParams->m_base_color5.r, m_pParams->m_base_color5.g, m_pParams->m_base_color5.b);
  943. #endif
  944. return false;
  945. }
  946. }
  947. const color_rgba base_color(coords.get_scaled_color());
  948. const uint32_t n = m_pParams->m_num_src_pixels;
  949. assert(trial_solution.m_selectors.size() == n);
  950. trial_solution.m_error = INT64_MAX;
  951. const uint8_t *pSelectors_to_use = m_pParams->m_pForce_selectors;
  952. for (uint32_t inten_table = 0; inten_table < cETC1IntenModifierValues; inten_table++)
  953. {
  954. if (m_pParams->m_quality <= cETCQualityMedium)
  955. {
  956. if (!g_eval_dist_tables[inten_table][m_max_comp_spread])
  957. continue;
  958. }
  959. #if 0
  960. if (m_pParams->m_quality <= cETCQualityMedium)
  961. {
  962. // For tables 5-7, if the max component spread falls within certain ranges, skip the inten table. Statistically they are extremely unlikely to result in lower error.
  963. if (inten_table == 7)
  964. {
  965. if (m_max_comp_spread < 42)
  966. continue;
  967. }
  968. else if (inten_table == 6)
  969. {
  970. if ((m_max_comp_spread >= 12) && (m_max_comp_spread <= 31))
  971. continue;
  972. }
  973. else if (inten_table == 5)
  974. {
  975. if ((m_max_comp_spread >= 13) && (m_max_comp_spread <= 21))
  976. continue;
  977. }
  978. }
  979. #endif
  980. const int* pInten_table = g_etc1_inten_tables[inten_table];
  981. color_rgba block_colors[4];
  982. for (uint32_t s = 0; s < 4; s++)
  983. {
  984. const int yd = pInten_table[s];
  985. block_colors[s].set(base_color.r + yd, base_color.g + yd, base_color.b + yd, 255);
  986. }
  987. uint64_t total_error = 0;
  988. const color_rgba* pSrc_pixels = m_pParams->m_pSrc_pixels;
  989. if (!g_cpu_supports_sse41)
  990. {
  991. for (uint32_t c = 0; c < n; c++)
  992. {
  993. const color_rgba& src_pixel = *pSrc_pixels++;
  994. uint32_t best_selector_index = 0;
  995. uint32_t best_error = 0;
  996. if (pSelectors_to_use)
  997. {
  998. best_selector_index = pSelectors_to_use[c];
  999. best_error = color_distance(m_pParams->m_perceptual, src_pixel, block_colors[best_selector_index], false);
  1000. }
  1001. else
  1002. {
  1003. best_error = color_distance(m_pParams->m_perceptual, src_pixel, block_colors[0], false);
  1004. uint32_t trial_error = color_distance(m_pParams->m_perceptual, src_pixel, block_colors[1], false);
  1005. if (trial_error < best_error)
  1006. {
  1007. best_error = trial_error;
  1008. best_selector_index = 1;
  1009. }
  1010. trial_error = color_distance(m_pParams->m_perceptual, src_pixel, block_colors[2], false);
  1011. if (trial_error < best_error)
  1012. {
  1013. best_error = trial_error;
  1014. best_selector_index = 2;
  1015. }
  1016. trial_error = color_distance(m_pParams->m_perceptual, src_pixel, block_colors[3], false);
  1017. if (trial_error < best_error)
  1018. {
  1019. best_error = trial_error;
  1020. best_selector_index = 3;
  1021. }
  1022. }
  1023. m_temp_selectors[c] = static_cast<uint8_t>(best_selector_index);
  1024. total_error += best_error;
  1025. if (total_error >= trial_solution.m_error)
  1026. break;
  1027. }
  1028. }
  1029. else
  1030. {
  1031. #if BASISU_SUPPORT_SSE
  1032. if (pSelectors_to_use)
  1033. {
  1034. if (m_pParams->m_perceptual)
  1035. perceptual_distance_rgb_4_N_sse41((int64_t*)&total_error, pSelectors_to_use, block_colors, pSrc_pixels, n, trial_solution.m_error);
  1036. else
  1037. linear_distance_rgb_4_N_sse41((int64_t*)&total_error, pSelectors_to_use, block_colors, pSrc_pixels, n, trial_solution.m_error);
  1038. }
  1039. else
  1040. {
  1041. if (m_pParams->m_perceptual)
  1042. find_selectors_perceptual_rgb_4_N_sse41((int64_t*)&total_error, &m_temp_selectors[0], block_colors, pSrc_pixels, n, trial_solution.m_error);
  1043. else
  1044. find_selectors_linear_rgb_4_N_sse41((int64_t*)&total_error, &m_temp_selectors[0], block_colors, pSrc_pixels, n, trial_solution.m_error);
  1045. }
  1046. #endif
  1047. }
  1048. if (total_error < trial_solution.m_error)
  1049. {
  1050. trial_solution.m_error = total_error;
  1051. trial_solution.m_coords.m_inten_table = inten_table;
  1052. trial_solution.m_selectors.swap(m_temp_selectors);
  1053. trial_solution.m_valid = true;
  1054. }
  1055. }
  1056. trial_solution.m_coords.m_unscaled_color = coords.m_unscaled_color;
  1057. trial_solution.m_coords.m_color4 = m_pParams->m_use_color4;
  1058. #if BASISU_DEBUG_ETC_ENCODER_DEEPER
  1059. printf("Eval done: %u error: %I64u best error so far: %I64u\n", (trial_solution.m_error < pBest_solution->m_error), trial_solution.m_error, pBest_solution->m_error);
  1060. #endif
  1061. bool success = false;
  1062. if (pBest_solution)
  1063. {
  1064. if (trial_solution.m_error < pBest_solution->m_error)
  1065. {
  1066. *pBest_solution = trial_solution;
  1067. success = true;
  1068. }
  1069. }
  1070. return success;
  1071. }
  1072. bool etc1_optimizer::evaluate_solution_fast(const etc1_solution_coordinates& coords, potential_solution& trial_solution, potential_solution* pBest_solution)
  1073. {
  1074. if (!check_for_redundant_solution(coords))
  1075. return false;
  1076. #if BASISU_DEBUG_ETC_ENCODER_DEEPER
  1077. printf("Eval solution fast: %u %u %u\n", coords.m_unscaled_color.r, coords.m_unscaled_color.g, coords.m_unscaled_color.b);
  1078. #endif
  1079. if (m_pParams->m_constrain_against_base_color5)
  1080. {
  1081. const int dr = (int)coords.m_unscaled_color.r - (int)m_pParams->m_base_color5.r;
  1082. const int dg = (int)coords.m_unscaled_color.g - (int)m_pParams->m_base_color5.g;
  1083. const int db = (int)coords.m_unscaled_color.b - (int)m_pParams->m_base_color5.b;
  1084. if ((minimum(dr, dg, db) < cETC1ColorDeltaMin) || (maximum(dr, dg, db) > cETC1ColorDeltaMax))
  1085. {
  1086. trial_solution.m_valid = false;
  1087. #if BASISU_DEBUG_ETC_ENCODER_DEEPER
  1088. printf("Eval failed due to constraint from %u %u %u\n", m_pParams->m_base_color5.r, m_pParams->m_base_color5.g, m_pParams->m_base_color5.b);
  1089. #endif
  1090. return false;
  1091. }
  1092. }
  1093. const color_rgba base_color(coords.get_scaled_color());
  1094. const uint32_t n = m_pParams->m_num_src_pixels;
  1095. assert(trial_solution.m_selectors.size() == n);
  1096. trial_solution.m_error = UINT64_MAX;
  1097. const bool perceptual = (m_pParams->m_quality == cETCQualityFast) ? false : m_pParams->m_perceptual;
  1098. for (int inten_table = cETC1IntenModifierValues - 1; inten_table >= 0; --inten_table)
  1099. {
  1100. const int* pInten_table = g_etc1_inten_tables[inten_table];
  1101. uint32_t block_inten[4];
  1102. color_rgba block_colors[4];
  1103. for (uint32_t s = 0; s < 4; s++)
  1104. {
  1105. const int yd = pInten_table[s];
  1106. color_rgba block_color(base_color.r + yd, base_color.g + yd, base_color.b + yd, 255);
  1107. block_colors[s] = block_color;
  1108. block_inten[s] = block_color.r + block_color.g + block_color.b;
  1109. }
  1110. // evaluate_solution_fast() enforces/assumes a total ordering of the input colors along the intensity (1,1,1) axis to more quickly classify the inputs to selectors.
  1111. // The inputs colors have been presorted along the projection onto this axis, and ETC1 block colors are always ordered along the intensity axis, so this classification is fast.
  1112. // 0 1 2 3
  1113. // 01 12 23
  1114. const uint32_t block_inten_midpoints[3] = { block_inten[0] + block_inten[1], block_inten[1] + block_inten[2], block_inten[2] + block_inten[3] };
  1115. uint64_t total_error = 0;
  1116. const color_rgba* pSrc_pixels = m_pParams->m_pSrc_pixels;
  1117. if (perceptual)
  1118. {
  1119. if ((m_pSorted_luma[n - 1] * 2) < block_inten_midpoints[0])
  1120. {
  1121. if (block_inten[0] > m_pSorted_luma[n - 1])
  1122. {
  1123. const uint32_t min_error = iabs((int)block_inten[0] - (int)m_pSorted_luma[n - 1]);
  1124. if (min_error >= trial_solution.m_error)
  1125. continue;
  1126. }
  1127. memset(&m_temp_selectors[0], 0, n);
  1128. for (uint32_t c = 0; c < n; c++)
  1129. total_error += color_distance(true, block_colors[0], pSrc_pixels[c], false);
  1130. }
  1131. else if ((m_pSorted_luma[0] * 2) >= block_inten_midpoints[2])
  1132. {
  1133. if (m_pSorted_luma[0] > block_inten[3])
  1134. {
  1135. const uint32_t min_error = iabs((int)m_pSorted_luma[0] - (int)block_inten[3]);
  1136. if (min_error >= trial_solution.m_error)
  1137. continue;
  1138. }
  1139. memset(&m_temp_selectors[0], 3, n);
  1140. for (uint32_t c = 0; c < n; c++)
  1141. total_error += color_distance(true, block_colors[3], pSrc_pixels[c], false);
  1142. }
  1143. else
  1144. {
  1145. if (!g_cpu_supports_sse41)
  1146. {
  1147. uint32_t cur_selector = 0, c;
  1148. for (c = 0; c < n; c++)
  1149. {
  1150. const uint32_t y = m_pSorted_luma[c];
  1151. while ((y * 2) >= block_inten_midpoints[cur_selector])
  1152. if (++cur_selector > 2)
  1153. goto done;
  1154. const uint32_t sorted_pixel_index = m_pSorted_luma_indices[c];
  1155. m_temp_selectors[sorted_pixel_index] = static_cast<uint8_t>(cur_selector);
  1156. total_error += color_distance(true, block_colors[cur_selector], pSrc_pixels[sorted_pixel_index], false);
  1157. }
  1158. done:
  1159. while (c < n)
  1160. {
  1161. const uint32_t sorted_pixel_index = m_pSorted_luma_indices[c];
  1162. m_temp_selectors[sorted_pixel_index] = 3;
  1163. total_error += color_distance(true, block_colors[3], pSrc_pixels[sorted_pixel_index], false);
  1164. ++c;
  1165. }
  1166. }
  1167. else
  1168. {
  1169. #if BASISU_SUPPORT_SSE
  1170. uint32_t cur_selector = 0, c;
  1171. for (c = 0; c < n; c++)
  1172. {
  1173. const uint32_t y = m_pSorted_luma[c];
  1174. while ((y * 2) >= block_inten_midpoints[cur_selector])
  1175. {
  1176. if (++cur_selector > 2)
  1177. goto done3;
  1178. }
  1179. const uint32_t sorted_pixel_index = m_pSorted_luma_indices[c];
  1180. m_temp_selectors[sorted_pixel_index] = static_cast<uint8_t>(cur_selector);
  1181. }
  1182. done3:
  1183. while (c < n)
  1184. {
  1185. const uint32_t sorted_pixel_index = m_pSorted_luma_indices[c];
  1186. m_temp_selectors[sorted_pixel_index] = 3;
  1187. ++c;
  1188. }
  1189. int64_t block_error;
  1190. perceptual_distance_rgb_4_N_sse41(&block_error, &m_temp_selectors[0], block_colors, pSrc_pixels, n, INT64_MAX);
  1191. total_error += block_error;
  1192. #endif
  1193. }
  1194. }
  1195. }
  1196. else
  1197. {
  1198. if ((m_pSorted_luma[n - 1] * 2) < block_inten_midpoints[0])
  1199. {
  1200. if (block_inten[0] > m_pSorted_luma[n - 1])
  1201. {
  1202. const uint32_t min_error = iabs((int)block_inten[0] - (int)m_pSorted_luma[n - 1]);
  1203. if (min_error >= trial_solution.m_error)
  1204. continue;
  1205. }
  1206. memset(&m_temp_selectors[0], 0, n);
  1207. for (uint32_t c = 0; c < n; c++)
  1208. total_error += color_distance(block_colors[0], pSrc_pixels[c], false);
  1209. }
  1210. else if ((m_pSorted_luma[0] * 2) >= block_inten_midpoints[2])
  1211. {
  1212. if (m_pSorted_luma[0] > block_inten[3])
  1213. {
  1214. const uint32_t min_error = iabs((int)m_pSorted_luma[0] - (int)block_inten[3]);
  1215. if (min_error >= trial_solution.m_error)
  1216. continue;
  1217. }
  1218. memset(&m_temp_selectors[0], 3, n);
  1219. for (uint32_t c = 0; c < n; c++)
  1220. total_error += color_distance(block_colors[3], pSrc_pixels[c], false);
  1221. }
  1222. else
  1223. {
  1224. uint32_t cur_selector = 0, c;
  1225. for (c = 0; c < n; c++)
  1226. {
  1227. const uint32_t y = m_pSorted_luma[c];
  1228. while ((y * 2) >= block_inten_midpoints[cur_selector])
  1229. if (++cur_selector > 2)
  1230. goto done2;
  1231. const uint32_t sorted_pixel_index = m_pSorted_luma_indices[c];
  1232. m_temp_selectors[sorted_pixel_index] = static_cast<uint8_t>(cur_selector);
  1233. total_error += color_distance(block_colors[cur_selector], pSrc_pixels[sorted_pixel_index], false);
  1234. }
  1235. done2:
  1236. while (c < n)
  1237. {
  1238. const uint32_t sorted_pixel_index = m_pSorted_luma_indices[c];
  1239. m_temp_selectors[sorted_pixel_index] = 3;
  1240. total_error += color_distance(block_colors[3], pSrc_pixels[sorted_pixel_index], false);
  1241. ++c;
  1242. }
  1243. }
  1244. }
  1245. if (total_error < trial_solution.m_error)
  1246. {
  1247. trial_solution.m_error = total_error;
  1248. trial_solution.m_coords.m_inten_table = inten_table;
  1249. trial_solution.m_selectors.swap(m_temp_selectors);
  1250. trial_solution.m_valid = true;
  1251. if (!total_error)
  1252. break;
  1253. }
  1254. }
  1255. trial_solution.m_coords.m_unscaled_color = coords.m_unscaled_color;
  1256. trial_solution.m_coords.m_color4 = m_pParams->m_use_color4;
  1257. #if BASISU_DEBUG_ETC_ENCODER_DEEPER
  1258. printf("Eval done: %u error: %I64u best error so far: %I64u\n", (trial_solution.m_error < pBest_solution->m_error), trial_solution.m_error, pBest_solution->m_error);
  1259. #endif
  1260. bool success = false;
  1261. if (pBest_solution)
  1262. {
  1263. if (trial_solution.m_error < pBest_solution->m_error)
  1264. {
  1265. *pBest_solution = trial_solution;
  1266. success = true;
  1267. }
  1268. }
  1269. return success;
  1270. }
  1271. uint64_t pack_eac_a8(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)
  1272. {
  1273. results.m_selectors.resize(num_pixels);
  1274. results.m_selectors_temp.resize(num_pixels);
  1275. uint32_t min_alpha = 255, max_alpha = 0;
  1276. for (uint32_t i = 0; i < num_pixels; i++)
  1277. {
  1278. const uint32_t a = pPixels[i];
  1279. if (a < min_alpha) min_alpha = a;
  1280. if (a > max_alpha) max_alpha = a;
  1281. }
  1282. if (min_alpha == max_alpha)
  1283. {
  1284. results.m_base = min_alpha;
  1285. results.m_table = 13;
  1286. results.m_multiplier = 1;
  1287. for (uint32_t i = 0; i < num_pixels; i++)
  1288. results.m_selectors[i] = 4;
  1289. return 0;
  1290. }
  1291. const uint32_t alpha_range = max_alpha - min_alpha;
  1292. uint64_t best_err = UINT64_MAX;
  1293. for (uint32_t table = 0; table < 16; table++)
  1294. {
  1295. if ((table_mask & (1U << table)) == 0)
  1296. continue;
  1297. const float range = (float)(g_etc2_eac_tables[table][ETC2_EAC_MAX_VALUE_SELECTOR] - g_etc2_eac_tables[table][ETC2_EAC_MIN_VALUE_SELECTOR]);
  1298. 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));
  1299. const int base_min = clamp255(center - base_search_rad);
  1300. const int base_max = clamp255(center + base_search_rad);
  1301. const int mul = (int)roundf(alpha_range / range);
  1302. const int mul_low = clamp<int>(mul - mul_search_rad, 1, 15);
  1303. const int mul_high = clamp<int>(mul + mul_search_rad, 1, 15);
  1304. for (int base = base_min; base <= base_max; base++)
  1305. {
  1306. for (int multiplier = mul_low; multiplier <= mul_high; multiplier++)
  1307. {
  1308. uint64_t total_err = 0;
  1309. for (uint32_t i = 0; i < num_pixels; i++)
  1310. {
  1311. const int a = pPixels[i];
  1312. uint32_t best_s_err = UINT32_MAX;
  1313. uint32_t best_s = 0;
  1314. for (uint32_t s = 0; s < 8; s++)
  1315. {
  1316. const int v = clamp255((int)multiplier * g_etc2_eac_tables[table][s] + (int)base);
  1317. uint32_t err = iabs(a - v);
  1318. if (err < best_s_err)
  1319. {
  1320. best_s_err = err;
  1321. best_s = s;
  1322. }
  1323. }
  1324. results.m_selectors_temp[i] = static_cast<uint8_t>(best_s);
  1325. total_err += best_s_err * best_s_err;
  1326. if (total_err >= best_err)
  1327. break;
  1328. }
  1329. if (total_err < best_err)
  1330. {
  1331. best_err = total_err;
  1332. results.m_base = base;
  1333. results.m_multiplier = multiplier;
  1334. results.m_table = table;
  1335. results.m_selectors.swap(results.m_selectors_temp);
  1336. if (!best_err)
  1337. return best_err;
  1338. }
  1339. } // table
  1340. } // multiplier
  1341. } // base
  1342. return best_err;
  1343. }
  1344. void pack_eac_a8(eac_a8_block* pBlock, const uint8_t* pPixels, uint32_t base_search_rad, uint32_t mul_search_rad, uint32_t table_mask)
  1345. {
  1346. pack_eac_a8_results results;
  1347. pack_eac_a8(results, pPixels, 16, base_search_rad, mul_search_rad, table_mask);
  1348. pBlock->m_base = results.m_base;
  1349. pBlock->m_multiplier = results.m_multiplier;
  1350. pBlock->m_table = results.m_table;
  1351. for (uint32_t y = 0; y < 4; y++)
  1352. for (uint32_t x = 0; x < 4; x++)
  1353. pBlock->set_selector(x, y, results.m_selectors[x + y * 4]);
  1354. }
  1355. } // namespace basisu