Path: blob/master/thirdparty/basis_universal/encoder/basisu_etc.cpp
9903 views
// basis_etc.cpp1// Copyright (C) 2019-2024 Binomial LLC. All Rights Reserved.2//3// Licensed under the Apache License, Version 2.0 (the "License");4// you may not use this file except in compliance with the License.5// You may obtain a copy of the License at6//7// http://www.apache.org/licenses/LICENSE-2.08//9// Unless required by applicable law or agreed to in writing, software10// distributed under the License is distributed on an "AS IS" BASIS,11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12// See the License for the specific language governing permissions and13// limitations under the License.14#include "basisu_etc.h"1516#if BASISU_SUPPORT_SSE17#define CPPSPMD_NAME(a) a##_sse4118#include "basisu_kernels_declares.h"19#endif2021#define BASISU_DEBUG_ETC_ENCODER 022#define BASISU_DEBUG_ETC_ENCODER_DEEPER 02324namespace basisu25{26const int8_t g_etc2_eac_tables[16][8] =27{28{ -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 },29{ -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 },30{ -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 },31{ -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 }32};3334const int8_t g_etc2_eac_tables8[16][8] =35{36{ -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 },37{ -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 },38{ -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 },39{ -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 }40};4142// 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.43static uint16_t g_etc1_inverse_lookup[2 * 8 * 4][256]; // [ diff/inten_table/selector][desired_color ]4445// 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.46// To pack: diff | (inten << 1) | (selector << 4) | (packed_c << 8)47static const uint16_t g_etc1_color8_to_etc_block_config_0_255[2][33] =48{49{ 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 },50{ 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 },51};5253// Really only [254][11].54static const uint16_t g_etc1_color8_to_etc_block_config_1_to_254[254][12] =55{56{ 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 }, {570x0303, 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,580x0505, 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,590x0F0D, 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, 0xFFFF60}, { 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,610x0D0B, 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,620xFFFF }, { 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,630x0327, 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 }, {640x0230, 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,650x0427, 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,660x130D, 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,670xFFFF }, { 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,680x140D, 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,690x0811, 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,700x120B, 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,710x0B03, 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,720x0424, 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,730x072B, 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 }, {740x0428, 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,750x0F1D, 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,760x0D17, 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,770x0A33, 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,780xFFFF }, { 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,790xFFFF }, { 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 }, {800x081A, 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,810x0D25, 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 }, {820x0720, 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,830xFFFF }, { 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,840x1709, 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 }, {850x0E29, 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,860x1111, 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 }, {870x1121, 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,880xFFFF }, { 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,890xFFFF }, { 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,900xFFFF }, { 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, 0xFFFF91}, { 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,920x1603, 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,930x1617, 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 }, {940x1431, 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,950xFFFF }, { 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,960x1A1D, 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,970x092C, 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,980x1237, 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,990x0F08, 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 }, {1000x0C12, 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 }, {1010x0B32, 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,1020x0F1E, 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,1030x0938, 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,1040x023E, 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,1050x1C15, 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,1060x1835, 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,1070x182D, 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,1080x0C2C, 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,1090x083C, 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,1100x1A2D, 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 },111};112113static uint32_t etc1_decode_value(uint32_t diff, uint32_t inten, uint32_t selector, uint32_t packed_c)114{115const uint32_t limit = diff ? 32 : 16;116BASISU_NOTE_UNUSED(limit);117assert((diff < 2) && (inten < 8) && (selector < 4) && (packed_c < limit));118int c;119if (diff)120c = (packed_c >> 2) | (packed_c << 3);121else122c = packed_c | (packed_c << 4);123c += g_etc1_inten_tables[inten][selector];124c = clamp<int>(c, 0, 255);125return c;126}127128void pack_etc1_solid_color_init()129{130for (uint32_t diff = 0; diff < 2; diff++)131{132const uint32_t limit = diff ? 32 : 16;133134for (uint32_t inten = 0; inten < 8; inten++)135{136for (uint32_t selector = 0; selector < 4; selector++)137{138const uint32_t inverse_table_index = diff + (inten << 1) + (selector << 4);139for (uint32_t color = 0; color < 256; color++)140{141uint32_t best_error = UINT32_MAX, best_packed_c = 0;142for (uint32_t packed_c = 0; packed_c < limit; packed_c++)143{144int v = etc1_decode_value(diff, inten, selector, packed_c);145uint32_t err = (uint32_t)labs(v - static_cast<int>(color));146if (err < best_error)147{148best_error = err;149best_packed_c = packed_c;150if (!best_error)151break;152}153}154assert(best_error <= 255);155g_etc1_inverse_lookup[inverse_table_index][color] = static_cast<uint16_t>(best_packed_c | (best_error << 8));156}157}158}159}160161#if 0162for (uint32_t y = 0; y < 64; y++)163{164printf("{");165for (uint32_t x = 0; x < 256; x++)166{167printf("0x%X", g_etc1_inverse_lookup[y][x]);168if (x != 255)169printf(",");170if (((x & 63) == 63) && (x != 255))171printf("\n");172}173printf("},\n");174}175#endif176}177178// Packs solid color blocks efficiently using a set of small precomputed tables.179// 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.180uint64_t pack_etc1_block_solid_color(etc_block& block, const uint8_t* pColor)181{182assert(g_etc1_inverse_lookup[0][255]);183184static uint32_t s_next_comp[4] = { 1, 2, 0, 1 };185186uint32_t best_error = UINT32_MAX, best_i = 0;187int best_x = 0, best_packed_c1 = 0, best_packed_c2 = 0;188189// 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.190for (uint32_t i = 0; i < 3; i++)191{192const uint32_t c1 = pColor[s_next_comp[i]], c2 = pColor[s_next_comp[i + 1]];193194const int delta_range = 1;195for (int delta = -delta_range; delta <= delta_range; delta++)196{197const int c_plus_delta = clamp<int>(pColor[i] + delta, 0, 255);198199const uint16_t* pTable;200if (!c_plus_delta)201pTable = g_etc1_color8_to_etc_block_config_0_255[0];202else if (c_plus_delta == 255)203pTable = g_etc1_color8_to_etc_block_config_0_255[1];204else205pTable = g_etc1_color8_to_etc_block_config_1_to_254[c_plus_delta - 1];206207do208{209const uint32_t x = *pTable++;210211#ifdef _DEBUG212const uint32_t diff = x & 1;213const uint32_t inten = (x >> 1) & 7;214const uint32_t selector = (x >> 4) & 3;215const uint32_t p0 = (x >> 8) & 255;216assert(etc1_decode_value(diff, inten, selector, p0) == (uint32_t)c_plus_delta);217#endif218219const uint16_t* pInverse_table = g_etc1_inverse_lookup[x & 0xFF];220uint16_t p1 = pInverse_table[c1];221uint16_t p2 = pInverse_table[c2];222const uint32_t trial_error = square(c_plus_delta - pColor[i]) + square(p1 >> 8) + square(p2 >> 8);223if (trial_error < best_error)224{225best_error = trial_error;226best_x = x;227best_packed_c1 = p1 & 0xFF;228best_packed_c2 = p2 & 0xFF;229best_i = i;230if (!best_error)231goto found_perfect_match;232}233} while (*pTable != 0xFFFF);234}235}236found_perfect_match:237238const uint32_t diff = best_x & 1;239const uint32_t inten = (best_x >> 1) & 7;240241block.m_bytes[3] = static_cast<uint8_t>(((inten | (inten << 3)) << 2) | (diff << 1));242243const uint32_t etc1_selector = g_selector_index_to_etc1[(best_x >> 4) & 3];244*reinterpret_cast<uint16_t*>(&block.m_bytes[4]) = (etc1_selector & 2) ? 0xFFFF : 0;245*reinterpret_cast<uint16_t*>(&block.m_bytes[6]) = (etc1_selector & 1) ? 0xFFFF : 0;246247const uint32_t best_packed_c0 = (best_x >> 8) & 255;248if (diff)249{250block.m_bytes[best_i] = static_cast<uint8_t>(best_packed_c0 << 3);251block.m_bytes[s_next_comp[best_i]] = static_cast<uint8_t>(best_packed_c1 << 3);252block.m_bytes[s_next_comp[best_i + 1]] = static_cast<uint8_t>(best_packed_c2 << 3);253}254else255{256block.m_bytes[best_i] = static_cast<uint8_t>(best_packed_c0 | (best_packed_c0 << 4));257block.m_bytes[s_next_comp[best_i]] = static_cast<uint8_t>(best_packed_c1 | (best_packed_c1 << 4));258block.m_bytes[s_next_comp[best_i + 1]] = static_cast<uint8_t>(best_packed_c2 | (best_packed_c2 << 4));259}260261return best_error;262}263264const uint32_t BASISU_ETC1_CLUSTER_FIT_ORDER_TABLE_SIZE = 165;265266static const struct { uint8_t m_v[4]; } g_cluster_fit_order_tab[BASISU_ETC1_CLUSTER_FIT_ORDER_TABLE_SIZE] =267{268{ { 0, 0, 0, 8 } },{ { 0, 5, 2, 1 } },{ { 0, 6, 1, 1 } },{ { 0, 7, 0, 1 } },{ { 0, 7, 1, 0 } },269{ { 0, 0, 8, 0 } },{ { 0, 0, 3, 5 } },{ { 0, 1, 7, 0 } },{ { 0, 0, 4, 4 } },{ { 0, 0, 2, 6 } },270{ { 0, 0, 7, 1 } },{ { 0, 0, 1, 7 } },{ { 0, 0, 5, 3 } },{ { 1, 6, 0, 1 } },{ { 0, 0, 6, 2 } },271{ { 0, 2, 6, 0 } },{ { 2, 4, 2, 0 } },{ { 0, 3, 5, 0 } },{ { 3, 3, 1, 1 } },{ { 4, 2, 0, 2 } },272{ { 1, 5, 2, 0 } },{ { 0, 5, 3, 0 } },{ { 0, 6, 2, 0 } },{ { 2, 4, 1, 1 } },{ { 5, 1, 0, 2 } },273{ { 6, 1, 1, 0 } },{ { 3, 3, 0, 2 } },{ { 6, 0, 0, 2 } },{ { 0, 8, 0, 0 } },{ { 6, 1, 0, 1 } },274{ { 0, 1, 6, 1 } },{ { 1, 6, 1, 0 } },{ { 4, 1, 3, 0 } },{ { 0, 2, 5, 1 } },{ { 5, 0, 3, 0 } },275{ { 5, 3, 0, 0 } },{ { 0, 1, 5, 2 } },{ { 0, 3, 4, 1 } },{ { 2, 5, 1, 0 } },{ { 1, 7, 0, 0 } },276{ { 0, 1, 4, 3 } },{ { 6, 0, 2, 0 } },{ { 0, 4, 4, 0 } },{ { 2, 6, 0, 0 } },{ { 0, 2, 4, 2 } },277{ { 0, 5, 1, 2 } },{ { 0, 6, 0, 2 } },{ { 3, 5, 0, 0 } },{ { 0, 4, 3, 1 } },{ { 3, 4, 1, 0 } },278{ { 4, 3, 1, 0 } },{ { 1, 5, 0, 2 } },{ { 0, 3, 3, 2 } },{ { 1, 4, 1, 2 } },{ { 0, 4, 2, 2 } },279{ { 2, 3, 3, 0 } },{ { 4, 4, 0, 0 } },{ { 1, 2, 4, 1 } },{ { 0, 5, 0, 3 } },{ { 0, 1, 3, 4 } },280{ { 1, 5, 1, 1 } },{ { 1, 4, 2, 1 } },{ { 1, 3, 2, 2 } },{ { 5, 2, 1, 0 } },{ { 1, 3, 3, 1 } },281{ { 0, 1, 2, 5 } },{ { 1, 1, 5, 1 } },{ { 0, 3, 2, 3 } },{ { 2, 5, 0, 1 } },{ { 3, 2, 2, 1 } },282{ { 2, 3, 0, 3 } },{ { 1, 4, 3, 0 } },{ { 2, 2, 1, 3 } },{ { 6, 2, 0, 0 } },{ { 1, 0, 6, 1 } },283{ { 3, 3, 2, 0 } },{ { 7, 1, 0, 0 } },{ { 3, 1, 4, 0 } },{ { 0, 2, 3, 3 } },{ { 0, 4, 1, 3 } },284{ { 0, 4, 0, 4 } },{ { 0, 1, 0, 7 } },{ { 2, 0, 5, 1 } },{ { 2, 0, 4, 2 } },{ { 3, 0, 2, 3 } },285{ { 2, 2, 4, 0 } },{ { 2, 2, 3, 1 } },{ { 4, 0, 3, 1 } },{ { 3, 2, 3, 0 } },{ { 2, 3, 2, 1 } },286{ { 1, 3, 4, 0 } },{ { 7, 0, 1, 0 } },{ { 3, 0, 4, 1 } },{ { 1, 0, 5, 2 } },{ { 8, 0, 0, 0 } },287{ { 3, 0, 1, 4 } },{ { 4, 1, 1, 2 } },{ { 4, 0, 2, 2 } },{ { 1, 2, 5, 0 } },{ { 4, 2, 1, 1 } },288{ { 3, 4, 0, 1 } },{ { 2, 0, 3, 3 } },{ { 5, 0, 1, 2 } },{ { 5, 0, 0, 3 } },{ { 2, 4, 0, 2 } },289{ { 2, 1, 4, 1 } },{ { 4, 0, 1, 3 } },{ { 2, 1, 5, 0 } },{ { 4, 2, 2, 0 } },{ { 4, 0, 4, 0 } },290{ { 1, 0, 4, 3 } },{ { 1, 4, 0, 3 } },{ { 3, 0, 3, 2 } },{ { 4, 3, 0, 1 } },{ { 0, 1, 1, 6 } },291{ { 1, 3, 1, 3 } },{ { 0, 2, 2, 4 } },{ { 2, 0, 2, 4 } },{ { 5, 1, 1, 1 } },{ { 3, 0, 5, 0 } },292{ { 2, 3, 1, 2 } },{ { 3, 0, 0, 5 } },{ { 0, 3, 1, 4 } },{ { 5, 0, 2, 1 } },{ { 2, 1, 3, 2 } },293{ { 2, 0, 6, 0 } },{ { 3, 1, 3, 1 } },{ { 5, 1, 2, 0 } },{ { 1, 0, 3, 4 } },{ { 1, 1, 6, 0 } },294{ { 4, 0, 0, 4 } },{ { 2, 0, 1, 5 } },{ { 0, 3, 0, 5 } },{ { 1, 3, 0, 4 } },{ { 4, 1, 2, 1 } },295{ { 1, 2, 3, 2 } },{ { 3, 1, 0, 4 } },{ { 5, 2, 0, 1 } },{ { 1, 2, 2, 3 } },{ { 3, 2, 1, 2 } },296{ { 2, 2, 2, 2 } },{ { 6, 0, 1, 1 } },{ { 1, 2, 1, 4 } },{ { 1, 1, 4, 2 } },{ { 3, 2, 0, 3 } },297{ { 1, 2, 0, 5 } },{ { 1, 0, 7, 0 } },{ { 3, 1, 2, 2 } },{ { 1, 0, 2, 5 } },{ { 2, 0, 0, 6 } },298{ { 2, 1, 1, 4 } },{ { 2, 2, 0, 4 } },{ { 1, 1, 3, 3 } },{ { 7, 0, 0, 1 } },{ { 1, 0, 0, 7 } },299{ { 2, 1, 2, 3 } },{ { 4, 1, 0, 3 } },{ { 3, 1, 1, 3 } },{ { 1, 1, 2, 4 } },{ { 2, 1, 0, 5 } },300{ { 1, 0, 1, 6 } },{ { 0, 2, 1, 5 } },{ { 0, 2, 0, 6 } },{ { 1, 1, 1, 5 } },{ { 1, 1, 0, 6 } }301};302303const int g_etc1_inten_tables[cETC1IntenModifierValues][cETC1SelectorValues] =304{305{ -8, -2, 2, 8 }, { -17, -5, 5, 17 }, { -29, -9, 9, 29 }, { -42, -13, 13, 42 },306{ -60, -18, 18, 60 }, { -80, -24, 24, 80 }, { -106, -33, 33, 106 }, { -183, -47, 47, 183 }307};308309const uint8_t g_etc1_to_selector_index[cETC1SelectorValues] = { 2, 3, 1, 0 };310const uint8_t g_selector_index_to_etc1[cETC1SelectorValues] = { 3, 2, 0, 1 };311312// [flip][subblock][pixel_index]313const etc_coord2 g_etc1_pixel_coords[2][2][8] =314{315{316{317{ 0, 0 }, { 0, 1 }, { 0, 2 }, { 0, 3 },318{ 1, 0 }, { 1, 1 }, { 1, 2 }, { 1, 3 }319},320{321{ 2, 0 }, { 2, 1 }, { 2, 2 }, { 2, 3 },322{ 3, 0 }, { 3, 1 }, { 3, 2 }, { 3, 3 }323}324},325{326{327{ 0, 0 }, { 1, 0 }, { 2, 0 }, { 3, 0 },328{ 0, 1 }, { 1, 1 }, { 2, 1 }, { 3, 1 }329},330{331{ 0, 2 }, { 1, 2 }, { 2, 2 }, { 3, 2 },332{ 0, 3 }, { 1, 3 }, { 2, 3 }, { 3, 3 }333},334}335};336337// [flip][subblock][pixel_index]338const uint32_t g_etc1_pixel_indices[2][2][8] =339{340{341{3420 + 4 * 0, 0 + 4 * 1, 0 + 4 * 2, 0 + 4 * 3,3431 + 4 * 0, 1 + 4 * 1, 1 + 4 * 2, 1 + 4 * 3344},345{3462 + 4 * 0, 2 + 4 * 1, 2 + 4 * 2, 2 + 4 * 3,3473 + 4 * 0, 3 + 4 * 1, 3 + 4 * 2, 3 + 4 * 3348}349},350{351{3520 + 4 * 0, 1 + 4 * 0, 2 + 4 * 0, 3 + 4 * 0,3530 + 4 * 1, 1 + 4 * 1, 2 + 4 * 1, 3 + 4 * 1354},355{3560 + 4 * 2, 1 + 4 * 2, 2 + 4 * 2, 3 + 4 * 2,3570 + 4 * 3, 1 + 4 * 3, 2 + 4 * 3, 3 + 4 * 3358},359}360};361362uint16_t etc_block::pack_color5(const color_rgba& color, bool scaled, uint32_t bias)363{364return pack_color5(color.r, color.g, color.b, scaled, bias);365}366367uint16_t etc_block::pack_color5(uint32_t r, uint32_t g, uint32_t b, bool scaled, uint32_t bias)368{369if (scaled)370{371r = (r * 31U + bias) / 255U;372g = (g * 31U + bias) / 255U;373b = (b * 31U + bias) / 255U;374}375376r = minimum(r, 31U);377g = minimum(g, 31U);378b = minimum(b, 31U);379380return static_cast<uint16_t>(b | (g << 5U) | (r << 10U));381}382383color_rgba etc_block::unpack_color5(uint16_t packed_color5, bool scaled, uint32_t alpha)384{385uint32_t b = packed_color5 & 31U;386uint32_t g = (packed_color5 >> 5U) & 31U;387uint32_t r = (packed_color5 >> 10U) & 31U;388389if (scaled)390{391b = (b << 3U) | (b >> 2U);392g = (g << 3U) | (g >> 2U);393r = (r << 3U) | (r >> 2U);394}395396return color_rgba(cNoClamp, r, g, b, minimum(alpha, 255U));397}398399void etc_block::unpack_color5(color_rgba& result, uint16_t packed_color5, bool scaled)400{401result = unpack_color5(packed_color5, scaled, 255);402}403404void etc_block::unpack_color5(uint32_t& r, uint32_t& g, uint32_t& b, uint16_t packed_color5, bool scaled)405{406color_rgba c(unpack_color5(packed_color5, scaled, 0));407r = c.r;408g = c.g;409b = c.b;410}411412bool etc_block::unpack_color5(color_rgba& result, uint16_t packed_color5, uint16_t packed_delta3, bool scaled, uint32_t alpha)413{414color_rgba_i16 dc(unpack_delta3(packed_delta3));415416int b = (packed_color5 & 31U) + dc.b;417int g = ((packed_color5 >> 5U) & 31U) + dc.g;418int r = ((packed_color5 >> 10U) & 31U) + dc.r;419420bool success = true;421if (static_cast<uint32_t>(r | g | b) > 31U)422{423success = false;424r = clamp<int>(r, 0, 31);425g = clamp<int>(g, 0, 31);426b = clamp<int>(b, 0, 31);427}428429if (scaled)430{431b = (b << 3U) | (b >> 2U);432g = (g << 3U) | (g >> 2U);433r = (r << 3U) | (r >> 2U);434}435436result.set_noclamp_rgba(r, g, b, minimum(alpha, 255U));437return success;438}439440bool 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)441{442color_rgba result;443const bool success = unpack_color5(result, packed_color5, packed_delta3, scaled, alpha);444r = result.r;445g = result.g;446b = result.b;447return success;448}449450uint16_t etc_block::pack_delta3(const color_rgba_i16& color)451{452return pack_delta3(color.r, color.g, color.b);453}454455uint16_t etc_block::pack_delta3(int r, int g, int b)456{457assert((r >= cETC1ColorDeltaMin) && (r <= cETC1ColorDeltaMax));458assert((g >= cETC1ColorDeltaMin) && (g <= cETC1ColorDeltaMax));459assert((b >= cETC1ColorDeltaMin) && (b <= cETC1ColorDeltaMax));460if (r < 0) r += 8;461if (g < 0) g += 8;462if (b < 0) b += 8;463return static_cast<uint16_t>(b | (g << 3) | (r << 6));464}465466color_rgba_i16 etc_block::unpack_delta3(uint16_t packed_delta3)467{468int r = (packed_delta3 >> 6) & 7;469int g = (packed_delta3 >> 3) & 7;470int b = packed_delta3 & 7;471if (r >= 4) r -= 8;472if (g >= 4) g -= 8;473if (b >= 4) b -= 8;474return color_rgba_i16(r, g, b, 255);475}476477void etc_block::unpack_delta3(int& r, int& g, int& b, uint16_t packed_delta3)478{479r = (packed_delta3 >> 6) & 7;480g = (packed_delta3 >> 3) & 7;481b = packed_delta3 & 7;482if (r >= 4) r -= 8;483if (g >= 4) g -= 8;484if (b >= 4) b -= 8;485}486487uint16_t etc_block::pack_color4(const color_rgba& color, bool scaled, uint32_t bias)488{489return pack_color4(color.r, color.g, color.b, scaled, bias);490}491492uint16_t etc_block::pack_color4(uint32_t r, uint32_t g, uint32_t b, bool scaled, uint32_t bias)493{494if (scaled)495{496r = (r * 15U + bias) / 255U;497g = (g * 15U + bias) / 255U;498b = (b * 15U + bias) / 255U;499}500501r = minimum(r, 15U);502g = minimum(g, 15U);503b = minimum(b, 15U);504505return static_cast<uint16_t>(b | (g << 4U) | (r << 8U));506}507508color_rgba etc_block::unpack_color4(uint16_t packed_color4, bool scaled, uint32_t alpha)509{510uint32_t b = packed_color4 & 15U;511uint32_t g = (packed_color4 >> 4U) & 15U;512uint32_t r = (packed_color4 >> 8U) & 15U;513514if (scaled)515{516b = (b << 4U) | b;517g = (g << 4U) | g;518r = (r << 4U) | r;519}520521return color_rgba(cNoClamp, r, g, b, minimum(alpha, 255U));522}523524void etc_block::unpack_color4(uint32_t& r, uint32_t& g, uint32_t& b, uint16_t packed_color4, bool scaled)525{526color_rgba c(unpack_color4(packed_color4, scaled, 0));527r = c.r;528g = c.g;529b = c.b;530}531532void etc_block::get_diff_subblock_colors(color_rgba* pDst, uint16_t packed_color5, uint32_t table_idx)533{534assert(table_idx < cETC1IntenModifierValues);535const int *pInten_modifer_table = &g_etc1_inten_tables[table_idx][0];536537uint32_t r, g, b;538unpack_color5(r, g, b, packed_color5, true);539540const int ir = static_cast<int>(r), ig = static_cast<int>(g), ib = static_cast<int>(b);541542const int y0 = pInten_modifer_table[0];543pDst[0].set(ir + y0, ig + y0, ib + y0, 255);544545const int y1 = pInten_modifer_table[1];546pDst[1].set(ir + y1, ig + y1, ib + y1, 255);547548const int y2 = pInten_modifer_table[2];549pDst[2].set(ir + y2, ig + y2, ib + y2, 255);550551const int y3 = pInten_modifer_table[3];552pDst[3].set(ir + y3, ig + y3, ib + y3, 255);553}554555bool etc_block::get_diff_subblock_colors(color_rgba* pDst, uint16_t packed_color5, uint16_t packed_delta3, uint32_t table_idx)556{557assert(table_idx < cETC1IntenModifierValues);558const int *pInten_modifer_table = &g_etc1_inten_tables[table_idx][0];559560uint32_t r, g, b;561bool success = unpack_color5(r, g, b, packed_color5, packed_delta3, true);562563const int ir = static_cast<int>(r), ig = static_cast<int>(g), ib = static_cast<int>(b);564565const int y0 = pInten_modifer_table[0];566pDst[0].set(ir + y0, ig + y0, ib + y0, 255);567568const int y1 = pInten_modifer_table[1];569pDst[1].set(ir + y1, ig + y1, ib + y1, 255);570571const int y2 = pInten_modifer_table[2];572pDst[2].set(ir + y2, ig + y2, ib + y2, 255);573574const int y3 = pInten_modifer_table[3];575pDst[3].set(ir + y3, ig + y3, ib + y3, 255);576577return success;578}579580void etc_block::get_abs_subblock_colors(color_rgba* pDst, uint16_t packed_color4, uint32_t table_idx)581{582assert(table_idx < cETC1IntenModifierValues);583const int *pInten_modifer_table = &g_etc1_inten_tables[table_idx][0];584585uint32_t r, g, b;586unpack_color4(r, g, b, packed_color4, true);587588const int ir = static_cast<int>(r), ig = static_cast<int>(g), ib = static_cast<int>(b);589590const int y0 = pInten_modifer_table[0];591pDst[0].set(ir + y0, ig + y0, ib + y0, 255);592593const int y1 = pInten_modifer_table[1];594pDst[1].set(ir + y1, ig + y1, ib + y1, 255);595596const int y2 = pInten_modifer_table[2];597pDst[2].set(ir + y2, ig + y2, ib + y2, 255);598599const int y3 = pInten_modifer_table[3];600pDst[3].set(ir + y3, ig + y3, ib + y3, 255);601}602603bool unpack_etc1(const etc_block& block, color_rgba *pDst, bool preserve_alpha)604{605const bool diff_flag = block.get_diff_bit();606const bool flip_flag = block.get_flip_bit();607const uint32_t table_index0 = block.get_inten_table(0);608const uint32_t table_index1 = block.get_inten_table(1);609610color_rgba subblock_colors0[4];611color_rgba subblock_colors1[4];612613if (diff_flag)614{615const uint16_t base_color5 = block.get_base5_color();616const uint16_t delta_color3 = block.get_delta3_color();617etc_block::get_diff_subblock_colors(subblock_colors0, base_color5, table_index0);618619if (!etc_block::get_diff_subblock_colors(subblock_colors1, base_color5, delta_color3, table_index1))620return false;621}622else623{624const uint16_t base_color4_0 = block.get_base4_color(0);625etc_block::get_abs_subblock_colors(subblock_colors0, base_color4_0, table_index0);626627const uint16_t base_color4_1 = block.get_base4_color(1);628etc_block::get_abs_subblock_colors(subblock_colors1, base_color4_1, table_index1);629}630631if (preserve_alpha)632{633if (flip_flag)634{635for (uint32_t y = 0; y < 2; y++)636{637pDst[0].set_rgb(subblock_colors0[block.get_selector(0, y)]);638pDst[1].set_rgb(subblock_colors0[block.get_selector(1, y)]);639pDst[2].set_rgb(subblock_colors0[block.get_selector(2, y)]);640pDst[3].set_rgb(subblock_colors0[block.get_selector(3, y)]);641pDst += 4;642}643644for (uint32_t y = 2; y < 4; y++)645{646pDst[0].set_rgb(subblock_colors1[block.get_selector(0, y)]);647pDst[1].set_rgb(subblock_colors1[block.get_selector(1, y)]);648pDst[2].set_rgb(subblock_colors1[block.get_selector(2, y)]);649pDst[3].set_rgb(subblock_colors1[block.get_selector(3, y)]);650pDst += 4;651}652}653else654{655for (uint32_t y = 0; y < 4; y++)656{657pDst[0].set_rgb(subblock_colors0[block.get_selector(0, y)]);658pDst[1].set_rgb(subblock_colors0[block.get_selector(1, y)]);659pDst[2].set_rgb(subblock_colors1[block.get_selector(2, y)]);660pDst[3].set_rgb(subblock_colors1[block.get_selector(3, y)]);661pDst += 4;662}663}664}665else666{667if (flip_flag)668{669// 0000670// 0000671// 1111672// 1111673for (uint32_t y = 0; y < 2; y++)674{675pDst[0] = subblock_colors0[block.get_selector(0, y)];676pDst[1] = subblock_colors0[block.get_selector(1, y)];677pDst[2] = subblock_colors0[block.get_selector(2, y)];678pDst[3] = subblock_colors0[block.get_selector(3, y)];679pDst += 4;680}681682for (uint32_t y = 2; y < 4; y++)683{684pDst[0] = subblock_colors1[block.get_selector(0, y)];685pDst[1] = subblock_colors1[block.get_selector(1, y)];686pDst[2] = subblock_colors1[block.get_selector(2, y)];687pDst[3] = subblock_colors1[block.get_selector(3, y)];688pDst += 4;689}690}691else692{693// 0011694// 0011695// 0011696// 0011697for (uint32_t y = 0; y < 4; y++)698{699pDst[0] = subblock_colors0[block.get_selector(0, y)];700pDst[1] = subblock_colors0[block.get_selector(1, y)];701pDst[2] = subblock_colors1[block.get_selector(2, y)];702pDst[3] = subblock_colors1[block.get_selector(3, y)];703pDst += 4;704}705}706}707708return true;709}710711inline int extend_6_to_8(uint32_t n)712{713return (n << 2) | (n >> 4);714}715716inline int extend_7_to_8(uint32_t n)717{718return (n << 1) | (n >> 6);719}720721inline int extend_4_to_8(uint32_t n)722{723return (n << 4) | n;724}725726uint64_t etc_block::evaluate_etc1_error(const color_rgba* pBlock_pixels, bool perceptual, int subblock_index) const727{728color_rgba unpacked_block[16];729730unpack_etc1(*this, unpacked_block);731732uint64_t total_error = 0;733734if (subblock_index < 0)735{736for (uint32_t i = 0; i < 16; i++)737total_error += color_distance(perceptual, pBlock_pixels[i], unpacked_block[i], false);738}739else740{741const bool flip_bit = get_flip_bit();742743for (uint32_t i = 0; i < 8; i++)744{745const uint32_t idx = g_etc1_pixel_indices[flip_bit][subblock_index][i];746747total_error += color_distance(perceptual, pBlock_pixels[idx], unpacked_block[idx], false);748}749}750751return total_error;752}753754void etc_block::get_subblock_pixels(color_rgba* pPixels, int subblock_index) const755{756if (subblock_index < 0)757unpack_etc1(*this, pPixels);758else759{760color_rgba unpacked_block[16];761762unpack_etc1(*this, unpacked_block);763764const bool flip_bit = get_flip_bit();765766for (uint32_t i = 0; i < 8; i++)767{768const uint32_t idx = g_etc1_pixel_indices[flip_bit][subblock_index][i];769770pPixels[i] = unpacked_block[idx];771}772}773}774775bool etc1_optimizer::compute()776{777assert(m_pResult->m_pSelectors);778779if (m_pParams->m_pForce_selectors)780{781assert(m_pParams->m_quality >= cETCQualitySlow);782if (m_pParams->m_quality < cETCQualitySlow)783return false;784}785786const uint32_t n = m_pParams->m_num_src_pixels;787788if (m_pParams->m_cluster_fit)789{790if (m_pParams->m_quality == cETCQualityFast)791compute_internal_cluster_fit(4);792else if (m_pParams->m_quality == cETCQualityMedium)793compute_internal_cluster_fit(16);794else if (m_pParams->m_quality == cETCQualitySlow)795compute_internal_cluster_fit(64);796else797compute_internal_cluster_fit(BASISU_ETC1_CLUSTER_FIT_ORDER_TABLE_SIZE);798}799else800compute_internal_neighborhood(m_br, m_bg, m_bb);801802if (!m_best_solution.m_valid)803{804m_pResult->m_error = UINT32_MAX;805return false;806}807808//const uint8_t* pSelectors = &m_best_solution.m_selectors[0];809const uint8_t* pSelectors = m_pParams->m_pForce_selectors ? m_pParams->m_pForce_selectors : &m_best_solution.m_selectors[0];810811#if defined(DEBUG) || defined(_DEBUG)812{813// sanity check the returned error814color_rgba block_colors[4];815m_best_solution.m_coords.get_block_colors(block_colors);816817const color_rgba* pSrc_pixels = m_pParams->m_pSrc_pixels;818uint64_t actual_error = 0;819820bool perceptual;821if (m_pParams->m_quality >= cETCQualityMedium)822perceptual = m_pParams->m_perceptual;823else824perceptual = (m_pParams->m_quality == cETCQualityFast) ? false : m_pParams->m_perceptual;825826for (uint32_t i = 0; i < n; i++)827actual_error += color_distance(perceptual, pSrc_pixels[i], block_colors[pSelectors[i]], false);828829assert(actual_error == m_best_solution.m_error);830}831#endif832833m_pResult->m_error = m_best_solution.m_error;834835m_pResult->m_block_color_unscaled = m_best_solution.m_coords.m_unscaled_color;836m_pResult->m_block_color4 = m_best_solution.m_coords.m_color4;837838m_pResult->m_block_inten_table = m_best_solution.m_coords.m_inten_table;839memcpy(m_pResult->m_pSelectors, pSelectors, n);840m_pResult->m_n = n;841842return true;843}844845void etc1_optimizer::refine_solution(uint32_t max_refinement_trials)846{847// 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.848// Now, for each component, attempt to refine the current solution by solving a simple linear equation. For example, for 4 colors:849// The goal is:850// pixel0 - (block_color+inten_table[selector0]) + pixel1 - (block_color+inten_table[selector1]) + pixel2 - (block_color+inten_table[selector2]) + pixel3 - (block_color+inten_table[selector3]) = 0851// Rearranging this:852// (pixel0 + pixel1 + pixel2 + pixel3) - (block_color+inten_table[selector0]) - (block_color+inten_table[selector1]) - (block_color+inten_table[selector2]) - (block_color+inten_table[selector3]) = 0853// (pixel0 + pixel1 + pixel2 + pixel3) - block_color - inten_table[selector0] - block_color-inten_table[selector1] - block_color-inten_table[selector2] - block_color-inten_table[selector3] = 0854// (pixel0 + pixel1 + pixel2 + pixel3) - 4*block_color - inten_table[selector0] - inten_table[selector1] - inten_table[selector2] - inten_table[selector3] = 0855// (pixel0 + pixel1 + pixel2 + pixel3) - 4*block_color - (inten_table[selector0] + inten_table[selector1] + inten_table[selector2] + inten_table[selector3]) = 0856// (pixel0 + pixel1 + pixel2 + pixel3)/4 - block_color - (inten_table[selector0] + inten_table[selector1] + inten_table[selector2] + inten_table[selector3])/4 = 0857// block_color = (pixel0 + pixel1 + pixel2 + pixel3)/4 - (inten_table[selector0] + inten_table[selector1] + inten_table[selector2] + inten_table[selector3])/4858// So what this means:859// optimal_block_color = avg_input - avg_inten_delta860// So the optimal block color can be computed by taking the average block color and subtracting the current average of the intensity delta.861// Unfortunately, optimal_block_color must then be quantized to 555 or 444 so it's not always possible to improve matters using this formula.862// Also, the above formula is for unclamped intensity deltas. The actual implementation takes into account clamping.863864const uint32_t n = m_pParams->m_num_src_pixels;865866for (uint32_t refinement_trial = 0; refinement_trial < max_refinement_trials; refinement_trial++)867{868const uint8_t* pSelectors = &m_best_solution.m_selectors[0];869const int* pInten_table = g_etc1_inten_tables[m_best_solution.m_coords.m_inten_table];870871int delta_sum_r = 0, delta_sum_g = 0, delta_sum_b = 0;872const color_rgba base_color(m_best_solution.m_coords.get_scaled_color());873for (uint32_t r = 0; r < n; r++)874{875const uint32_t s = *pSelectors++;876const int yd_temp = pInten_table[s];877// Compute actual delta being applied to each pixel, taking into account clamping.878delta_sum_r += clamp<int>(base_color.r + yd_temp, 0, 255) - base_color.r;879delta_sum_g += clamp<int>(base_color.g + yd_temp, 0, 255) - base_color.g;880delta_sum_b += clamp<int>(base_color.b + yd_temp, 0, 255) - base_color.b;881}882883if ((!delta_sum_r) && (!delta_sum_g) && (!delta_sum_b))884break;885886const float avg_delta_r_f = static_cast<float>(delta_sum_r) / n;887const float avg_delta_g_f = static_cast<float>(delta_sum_g) / n;888const float avg_delta_b_f = static_cast<float>(delta_sum_b) / n;889const int br1 = clamp<int>(static_cast<int32_t>((m_avg_color[0] - avg_delta_r_f) * m_limit / 255.0f + .5f), 0, m_limit);890const int bg1 = clamp<int>(static_cast<int32_t>((m_avg_color[1] - avg_delta_g_f) * m_limit / 255.0f + .5f), 0, m_limit);891const int bb1 = clamp<int>(static_cast<int32_t>((m_avg_color[2] - avg_delta_b_f) * m_limit / 255.0f + .5f), 0, m_limit);892893#if BASISU_DEBUG_ETC_ENCODER_DEEPER894printf("Refinement trial %u, avg_delta %f %f %f\n", refinement_trial, avg_delta_r_f, avg_delta_g_f, avg_delta_b_f);895#endif896897if (!evaluate_solution(etc1_solution_coordinates(br1, bg1, bb1, 0, m_pParams->m_use_color4), m_trial_solution, &m_best_solution))898break;899900} // refinement_trial901}902903void etc1_optimizer::compute_internal_neighborhood(int scan_r, int scan_g, int scan_b)904{905if (m_best_solution.m_error == 0)906return;907908//const uint32_t n = m_pParams->m_num_src_pixels;909const int scan_delta_size = m_pParams->m_scan_delta_size;910911// 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.912// 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.913for (int zdi = 0; zdi < scan_delta_size; zdi++)914{915const int zd = m_pParams->m_pScan_deltas[zdi];916const int mbb = scan_b + zd;917if (mbb < 0) continue; else if (mbb > m_limit) break;918919for (int ydi = 0; ydi < scan_delta_size; ydi++)920{921const int yd = m_pParams->m_pScan_deltas[ydi];922const int mbg = scan_g + yd;923if (mbg < 0) continue; else if (mbg > m_limit) break;924925for (int xdi = 0; xdi < scan_delta_size; xdi++)926{927const int xd = m_pParams->m_pScan_deltas[xdi];928const int mbr = scan_r + xd;929if (mbr < 0) continue; else if (mbr > m_limit) break;930931etc1_solution_coordinates coords(mbr, mbg, mbb, 0, m_pParams->m_use_color4);932933if (!evaluate_solution(coords, m_trial_solution, &m_best_solution))934continue;935936if (m_pParams->m_refinement)937{938refine_solution((m_pParams->m_quality == cETCQualityFast) ? 2 : (((xd | yd | zd) == 0) ? 4 : 2));939}940941} // xdi942} // ydi943} // zdi944}945946void etc1_optimizer::compute_internal_cluster_fit(uint32_t total_perms_to_try)947{948if ((!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)))949{950evaluate_solution(etc1_solution_coordinates(m_br, m_bg, m_bb, 0, m_pParams->m_use_color4), m_trial_solution, &m_best_solution);951}952953if ((m_best_solution.m_error == 0) || (!m_best_solution.m_valid))954return;955956for (uint32_t i = 0; i < total_perms_to_try; i++)957{958int delta_sum_r = 0, delta_sum_g = 0, delta_sum_b = 0;959960const int *pInten_table = g_etc1_inten_tables[m_best_solution.m_coords.m_inten_table];961const color_rgba base_color(m_best_solution.m_coords.get_scaled_color());962963const uint8_t *pNum_selectors = g_cluster_fit_order_tab[i].m_v;964965for (uint32_t q = 0; q < 4; q++)966{967const int yd_temp = pInten_table[q];968969delta_sum_r += pNum_selectors[q] * (clamp<int>(base_color.r + yd_temp, 0, 255) - base_color.r);970delta_sum_g += pNum_selectors[q] * (clamp<int>(base_color.g + yd_temp, 0, 255) - base_color.g);971delta_sum_b += pNum_selectors[q] * (clamp<int>(base_color.b + yd_temp, 0, 255) - base_color.b);972}973974if ((!delta_sum_r) && (!delta_sum_g) && (!delta_sum_b))975continue;976977const float avg_delta_r_f = static_cast<float>(delta_sum_r) / 8;978const float avg_delta_g_f = static_cast<float>(delta_sum_g) / 8;979const float avg_delta_b_f = static_cast<float>(delta_sum_b) / 8;980981const int br1 = clamp<int>(static_cast<int32_t>((m_avg_color[0] - avg_delta_r_f) * m_limit / 255.0f + .5f), 0, m_limit);982const int bg1 = clamp<int>(static_cast<int32_t>((m_avg_color[1] - avg_delta_g_f) * m_limit / 255.0f + .5f), 0, m_limit);983const int bb1 = clamp<int>(static_cast<int32_t>((m_avg_color[2] - avg_delta_b_f) * m_limit / 255.0f + .5f), 0, m_limit);984985#if BASISU_DEBUG_ETC_ENCODER_DEEPER986printf("Second refinement trial %u, avg_delta %f %f %f\n", i, avg_delta_r_f, avg_delta_g_f, avg_delta_b_f);987#endif988989evaluate_solution(etc1_solution_coordinates(br1, bg1, bb1, 0, m_pParams->m_use_color4), m_trial_solution, &m_best_solution);990991if (m_best_solution.m_error == 0)992break;993}994}995996void etc1_optimizer::init(const params& params, results& result)997{998m_pParams = ¶ms;999m_pResult = &result;10001001const uint32_t n = m_pParams->m_num_src_pixels;10021003m_selectors.resize(n);1004m_best_selectors.resize(n);1005m_temp_selectors.resize(n);1006m_trial_solution.m_selectors.resize(n);1007m_best_solution.m_selectors.resize(n);10081009m_limit = m_pParams->m_use_color4 ? 15 : 31;10101011vec3F avg_color(0.0f);10121013m_luma.resize(n);1014m_sorted_luma_indices.resize(n);1015m_sorted_luma.resize(n);10161017int min_r = 255, min_g = 255, min_b = 255;1018int max_r = 0, max_g = 0, max_b = 0;10191020for (uint32_t i = 0; i < n; i++)1021{1022const color_rgba& c = m_pParams->m_pSrc_pixels[i];10231024min_r = basisu::minimum<int>(min_r, c.r);1025min_g = basisu::minimum<int>(min_g, c.g);1026min_b = basisu::minimum<int>(min_b, c.b);10271028max_r = basisu::maximum<int>(max_r, c.r);1029max_g = basisu::maximum<int>(max_g, c.g);1030max_b = basisu::maximum<int>(max_b, c.b);10311032const vec3F fc(c.r, c.g, c.b);10331034avg_color += fc;10351036m_luma[i] = static_cast<uint16_t>(c.r + c.g + c.b);1037m_sorted_luma_indices[i] = i;1038}1039avg_color /= static_cast<float>(n);1040m_avg_color = avg_color;1041m_max_comp_spread = basisu::maximum(basisu::maximum(max_r - min_r, max_g - min_g), max_b - min_b);10421043m_br = clamp<int>(static_cast<uint32_t>(m_avg_color[0] * m_limit / 255.0f + .5f), 0, m_limit);1044m_bg = clamp<int>(static_cast<uint32_t>(m_avg_color[1] * m_limit / 255.0f + .5f), 0, m_limit);1045m_bb = clamp<int>(static_cast<uint32_t>(m_avg_color[2] * m_limit / 255.0f + .5f), 0, m_limit);10461047#if BASISU_DEBUG_ETC_ENCODER_DEEPER1048printf("Avg block color: %u %u %u\n", m_br, m_bg, m_bb);1049#endif10501051if (m_pParams->m_quality == cETCQualityFast)1052{1053indirect_sort(n, &m_sorted_luma_indices[0], &m_luma[0]);10541055m_pSorted_luma = &m_sorted_luma[0];1056m_pSorted_luma_indices = &m_sorted_luma_indices[0];10571058for (uint32_t i = 0; i < n; i++)1059m_pSorted_luma[i] = m_luma[m_pSorted_luma_indices[i]];1060}10611062m_best_solution.m_coords.clear();1063m_best_solution.m_valid = false;1064m_best_solution.m_error = UINT64_MAX;10651066clear_obj(m_solutions_tried);1067}10681069// Return false if we've probably already tried this solution, true if we have definitely not.1070bool etc1_optimizer::check_for_redundant_solution(const etc1_solution_coordinates& coords)1071{1072// Hash first 3 bytes of color (RGB)1073uint32_t kh = hash_hsieh((uint8_t*)&coords.m_unscaled_color.r, 3);10741075uint32_t h0 = kh & cSolutionsTriedHashMask;1076uint32_t h1 = (kh >> cSolutionsTriedHashBits) & cSolutionsTriedHashMask;10771078// Simple Bloom filter lookup with k=21079if ( ((m_solutions_tried[h0 >> 3] & (1 << (h0 & 7))) != 0) &&1080((m_solutions_tried[h1 >> 3] & (1 << (h1 & 7))) != 0) )1081return false;10821083m_solutions_tried[h0 >> 3] |= (1 << (h0 & 7));1084m_solutions_tried[h1 >> 3] |= (1 << (h1 & 7));10851086return true;1087}10881089static uint8_t g_eval_dist_tables[8][256] =1090{1091// 99% threshold1092{ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,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,},1093{ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,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,},1094{ 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,},1095{ 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,},1096{ 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,},1097{ 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,},1098{ 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,},1099{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,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,}1100};11011102bool etc1_optimizer::evaluate_solution_slow(const etc1_solution_coordinates& coords, potential_solution& trial_solution, potential_solution* pBest_solution)1103{1104if (!check_for_redundant_solution(coords))1105return false;11061107#if BASISU_DEBUG_ETC_ENCODER_DEEPER1108printf("Eval solution: %u %u %u\n", coords.m_unscaled_color.r, coords.m_unscaled_color.g, coords.m_unscaled_color.b);1109#endif11101111trial_solution.m_valid = false;11121113if (m_pParams->m_constrain_against_base_color5)1114{1115const int dr = (int)coords.m_unscaled_color.r - (int)m_pParams->m_base_color5.r;1116const int dg = (int)coords.m_unscaled_color.g - (int)m_pParams->m_base_color5.g;1117const int db = (int)coords.m_unscaled_color.b - (int)m_pParams->m_base_color5.b;11181119if ((minimum(dr, dg, db) < cETC1ColorDeltaMin) || (maximum(dr, dg, db) > cETC1ColorDeltaMax))1120{1121#if BASISU_DEBUG_ETC_ENCODER_DEEPER1122printf("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);1123#endif1124return false;1125}1126}11271128const color_rgba base_color(coords.get_scaled_color());11291130const uint32_t n = m_pParams->m_num_src_pixels;1131assert(trial_solution.m_selectors.size() == n);11321133trial_solution.m_error = INT64_MAX;11341135const uint8_t *pSelectors_to_use = m_pParams->m_pForce_selectors;11361137for (uint32_t inten_table = 0; inten_table < cETC1IntenModifierValues; inten_table++)1138{1139if (m_pParams->m_quality <= cETCQualityMedium)1140{1141if (!g_eval_dist_tables[inten_table][m_max_comp_spread])1142continue;1143}11441145#if 01146if (m_pParams->m_quality <= cETCQualityMedium)1147{1148// 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.1149if (inten_table == 7)1150{1151if (m_max_comp_spread < 42)1152continue;1153}1154else if (inten_table == 6)1155{1156if ((m_max_comp_spread >= 12) && (m_max_comp_spread <= 31))1157continue;1158}1159else if (inten_table == 5)1160{1161if ((m_max_comp_spread >= 13) && (m_max_comp_spread <= 21))1162continue;1163}1164}1165#endif11661167const int* pInten_table = g_etc1_inten_tables[inten_table];11681169color_rgba block_colors[4];1170for (uint32_t s = 0; s < 4; s++)1171{1172const int yd = pInten_table[s];1173block_colors[s].set(base_color.r + yd, base_color.g + yd, base_color.b + yd, 255);1174}11751176uint64_t total_error = 0;11771178const color_rgba* pSrc_pixels = m_pParams->m_pSrc_pixels;11791180if (!g_cpu_supports_sse41)1181{1182for (uint32_t c = 0; c < n; c++)1183{1184const color_rgba& src_pixel = *pSrc_pixels++;11851186uint32_t best_selector_index = 0;1187uint32_t best_error = 0;11881189if (pSelectors_to_use)1190{1191best_selector_index = pSelectors_to_use[c];1192best_error = color_distance(m_pParams->m_perceptual, src_pixel, block_colors[best_selector_index], false);1193}1194else1195{1196best_error = color_distance(m_pParams->m_perceptual, src_pixel, block_colors[0], false);11971198uint32_t trial_error = color_distance(m_pParams->m_perceptual, src_pixel, block_colors[1], false);1199if (trial_error < best_error)1200{1201best_error = trial_error;1202best_selector_index = 1;1203}12041205trial_error = color_distance(m_pParams->m_perceptual, src_pixel, block_colors[2], false);1206if (trial_error < best_error)1207{1208best_error = trial_error;1209best_selector_index = 2;1210}12111212trial_error = color_distance(m_pParams->m_perceptual, src_pixel, block_colors[3], false);1213if (trial_error < best_error)1214{1215best_error = trial_error;1216best_selector_index = 3;1217}1218}12191220m_temp_selectors[c] = static_cast<uint8_t>(best_selector_index);12211222total_error += best_error;1223if (total_error >= trial_solution.m_error)1224break;1225}1226}1227else1228{1229#if BASISU_SUPPORT_SSE1230if (pSelectors_to_use)1231{1232if (m_pParams->m_perceptual)1233perceptual_distance_rgb_4_N_sse41((int64_t*)&total_error, pSelectors_to_use, block_colors, pSrc_pixels, n, trial_solution.m_error);1234else1235linear_distance_rgb_4_N_sse41((int64_t*)&total_error, pSelectors_to_use, block_colors, pSrc_pixels, n, trial_solution.m_error);1236}1237else1238{1239if (m_pParams->m_perceptual)1240find_selectors_perceptual_rgb_4_N_sse41((int64_t*)&total_error, &m_temp_selectors[0], block_colors, pSrc_pixels, n, trial_solution.m_error);1241else1242find_selectors_linear_rgb_4_N_sse41((int64_t*)&total_error, &m_temp_selectors[0], block_colors, pSrc_pixels, n, trial_solution.m_error);1243}1244#endif1245}12461247if (total_error < trial_solution.m_error)1248{1249trial_solution.m_error = total_error;1250trial_solution.m_coords.m_inten_table = inten_table;1251trial_solution.m_selectors.swap(m_temp_selectors);1252trial_solution.m_valid = true;1253}1254}1255trial_solution.m_coords.m_unscaled_color = coords.m_unscaled_color;1256trial_solution.m_coords.m_color4 = m_pParams->m_use_color4;12571258#if BASISU_DEBUG_ETC_ENCODER_DEEPER1259printf("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);1260#endif12611262bool success = false;1263if (pBest_solution)1264{1265if (trial_solution.m_error < pBest_solution->m_error)1266{1267*pBest_solution = trial_solution;1268success = true;1269}1270}12711272return success;1273}12741275bool etc1_optimizer::evaluate_solution_fast(const etc1_solution_coordinates& coords, potential_solution& trial_solution, potential_solution* pBest_solution)1276{1277if (!check_for_redundant_solution(coords))1278return false;12791280#if BASISU_DEBUG_ETC_ENCODER_DEEPER1281printf("Eval solution fast: %u %u %u\n", coords.m_unscaled_color.r, coords.m_unscaled_color.g, coords.m_unscaled_color.b);1282#endif12831284if (m_pParams->m_constrain_against_base_color5)1285{1286const int dr = (int)coords.m_unscaled_color.r - (int)m_pParams->m_base_color5.r;1287const int dg = (int)coords.m_unscaled_color.g - (int)m_pParams->m_base_color5.g;1288const int db = (int)coords.m_unscaled_color.b - (int)m_pParams->m_base_color5.b;12891290if ((minimum(dr, dg, db) < cETC1ColorDeltaMin) || (maximum(dr, dg, db) > cETC1ColorDeltaMax))1291{1292trial_solution.m_valid = false;12931294#if BASISU_DEBUG_ETC_ENCODER_DEEPER1295printf("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);1296#endif1297return false;1298}1299}13001301const color_rgba base_color(coords.get_scaled_color());13021303const uint32_t n = m_pParams->m_num_src_pixels;1304assert(trial_solution.m_selectors.size() == n);13051306trial_solution.m_error = UINT64_MAX;13071308const bool perceptual = (m_pParams->m_quality == cETCQualityFast) ? false : m_pParams->m_perceptual;13091310for (int inten_table = cETC1IntenModifierValues - 1; inten_table >= 0; --inten_table)1311{1312const int* pInten_table = g_etc1_inten_tables[inten_table];13131314uint32_t block_inten[4];1315color_rgba block_colors[4];1316for (uint32_t s = 0; s < 4; s++)1317{1318const int yd = pInten_table[s];1319color_rgba block_color(base_color.r + yd, base_color.g + yd, base_color.b + yd, 255);1320block_colors[s] = block_color;1321block_inten[s] = block_color.r + block_color.g + block_color.b;1322}13231324// 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.1325// 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.1326// 0 1 2 31327// 01 12 231328const uint32_t block_inten_midpoints[3] = { block_inten[0] + block_inten[1], block_inten[1] + block_inten[2], block_inten[2] + block_inten[3] };13291330uint64_t total_error = 0;1331const color_rgba* pSrc_pixels = m_pParams->m_pSrc_pixels;13321333if (perceptual)1334{1335if ((m_pSorted_luma[n - 1] * 2) < block_inten_midpoints[0])1336{1337if (block_inten[0] > m_pSorted_luma[n - 1])1338{1339const uint32_t min_error = iabs((int)block_inten[0] - (int)m_pSorted_luma[n - 1]);1340if (min_error >= trial_solution.m_error)1341continue;1342}13431344memset(&m_temp_selectors[0], 0, n);13451346for (uint32_t c = 0; c < n; c++)1347total_error += color_distance(true, block_colors[0], pSrc_pixels[c], false);1348}1349else if ((m_pSorted_luma[0] * 2) >= block_inten_midpoints[2])1350{1351if (m_pSorted_luma[0] > block_inten[3])1352{1353const uint32_t min_error = iabs((int)m_pSorted_luma[0] - (int)block_inten[3]);1354if (min_error >= trial_solution.m_error)1355continue;1356}13571358memset(&m_temp_selectors[0], 3, n);13591360for (uint32_t c = 0; c < n; c++)1361total_error += color_distance(true, block_colors[3], pSrc_pixels[c], false);1362}1363else1364{1365if (!g_cpu_supports_sse41)1366{1367uint32_t cur_selector = 0, c;1368for (c = 0; c < n; c++)1369{1370const uint32_t y = m_pSorted_luma[c];1371while ((y * 2) >= block_inten_midpoints[cur_selector])1372if (++cur_selector > 2)1373goto done;1374const uint32_t sorted_pixel_index = m_pSorted_luma_indices[c];1375m_temp_selectors[sorted_pixel_index] = static_cast<uint8_t>(cur_selector);1376total_error += color_distance(true, block_colors[cur_selector], pSrc_pixels[sorted_pixel_index], false);1377}1378done:1379while (c < n)1380{1381const uint32_t sorted_pixel_index = m_pSorted_luma_indices[c];1382m_temp_selectors[sorted_pixel_index] = 3;1383total_error += color_distance(true, block_colors[3], pSrc_pixels[sorted_pixel_index], false);1384++c;1385}1386}1387else1388{1389#if BASISU_SUPPORT_SSE1390uint32_t cur_selector = 0, c;13911392for (c = 0; c < n; c++)1393{1394const uint32_t y = m_pSorted_luma[c];1395while ((y * 2) >= block_inten_midpoints[cur_selector])1396{1397if (++cur_selector > 2)1398goto done3;1399}1400const uint32_t sorted_pixel_index = m_pSorted_luma_indices[c];1401m_temp_selectors[sorted_pixel_index] = static_cast<uint8_t>(cur_selector);1402}1403done3:14041405while (c < n)1406{1407const uint32_t sorted_pixel_index = m_pSorted_luma_indices[c];1408m_temp_selectors[sorted_pixel_index] = 3;1409++c;1410}14111412int64_t block_error;1413perceptual_distance_rgb_4_N_sse41(&block_error, &m_temp_selectors[0], block_colors, pSrc_pixels, n, INT64_MAX);1414total_error += block_error;1415#endif1416}1417}1418}1419else1420{1421if ((m_pSorted_luma[n - 1] * 2) < block_inten_midpoints[0])1422{1423if (block_inten[0] > m_pSorted_luma[n - 1])1424{1425const uint32_t min_error = iabs((int)block_inten[0] - (int)m_pSorted_luma[n - 1]);1426if (min_error >= trial_solution.m_error)1427continue;1428}14291430memset(&m_temp_selectors[0], 0, n);14311432for (uint32_t c = 0; c < n; c++)1433total_error += color_distance(block_colors[0], pSrc_pixels[c], false);1434}1435else if ((m_pSorted_luma[0] * 2) >= block_inten_midpoints[2])1436{1437if (m_pSorted_luma[0] > block_inten[3])1438{1439const uint32_t min_error = iabs((int)m_pSorted_luma[0] - (int)block_inten[3]);1440if (min_error >= trial_solution.m_error)1441continue;1442}14431444memset(&m_temp_selectors[0], 3, n);14451446for (uint32_t c = 0; c < n; c++)1447total_error += color_distance(block_colors[3], pSrc_pixels[c], false);1448}1449else1450{1451uint32_t cur_selector = 0, c;1452for (c = 0; c < n; c++)1453{1454const uint32_t y = m_pSorted_luma[c];1455while ((y * 2) >= block_inten_midpoints[cur_selector])1456if (++cur_selector > 2)1457goto done2;1458const uint32_t sorted_pixel_index = m_pSorted_luma_indices[c];1459m_temp_selectors[sorted_pixel_index] = static_cast<uint8_t>(cur_selector);1460total_error += color_distance(block_colors[cur_selector], pSrc_pixels[sorted_pixel_index], false);1461}1462done2:1463while (c < n)1464{1465const uint32_t sorted_pixel_index = m_pSorted_luma_indices[c];1466m_temp_selectors[sorted_pixel_index] = 3;1467total_error += color_distance(block_colors[3], pSrc_pixels[sorted_pixel_index], false);1468++c;1469}1470}1471}14721473if (total_error < trial_solution.m_error)1474{1475trial_solution.m_error = total_error;1476trial_solution.m_coords.m_inten_table = inten_table;1477trial_solution.m_selectors.swap(m_temp_selectors);1478trial_solution.m_valid = true;1479if (!total_error)1480break;1481}1482}1483trial_solution.m_coords.m_unscaled_color = coords.m_unscaled_color;1484trial_solution.m_coords.m_color4 = m_pParams->m_use_color4;14851486#if BASISU_DEBUG_ETC_ENCODER_DEEPER1487printf("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);1488#endif14891490bool success = false;1491if (pBest_solution)1492{1493if (trial_solution.m_error < pBest_solution->m_error)1494{1495*pBest_solution = trial_solution;1496success = true;1497}1498}14991500return success;1501}15021503uint64_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)1504{1505results.m_selectors.resize(num_pixels);1506results.m_selectors_temp.resize(num_pixels);15071508uint32_t min_alpha = 255, max_alpha = 0;1509for (uint32_t i = 0; i < num_pixels; i++)1510{1511const uint32_t a = pPixels[i];1512if (a < min_alpha) min_alpha = a;1513if (a > max_alpha) max_alpha = a;1514}15151516if (min_alpha == max_alpha)1517{1518results.m_base = min_alpha;1519results.m_table = 13;1520results.m_multiplier = 1;1521for (uint32_t i = 0; i < num_pixels; i++)1522results.m_selectors[i] = 4;1523return 0;1524}15251526const uint32_t alpha_range = max_alpha - min_alpha;15271528uint64_t best_err = UINT64_MAX;15291530for (uint32_t table = 0; table < 16; table++)1531{1532if ((table_mask & (1U << table)) == 0)1533continue;15341535const float range = (float)(g_etc2_eac_tables[table][ETC2_EAC_MAX_VALUE_SELECTOR] - g_etc2_eac_tables[table][ETC2_EAC_MIN_VALUE_SELECTOR]);1536const int center = (int)roundf(lerp((float)min_alpha, (float)max_alpha, (float)(0 - g_etc2_eac_tables[table][ETC2_EAC_MIN_VALUE_SELECTOR]) / range));15371538const int base_min = clamp255(center - base_search_rad);1539const int base_max = clamp255(center + base_search_rad);15401541const int mul = (int)roundf(alpha_range / range);1542const int mul_low = clamp<int>(mul - mul_search_rad, 1, 15);1543const int mul_high = clamp<int>(mul + mul_search_rad, 1, 15);15441545for (int base = base_min; base <= base_max; base++)1546{1547for (int multiplier = mul_low; multiplier <= mul_high; multiplier++)1548{1549uint64_t total_err = 0;15501551for (uint32_t i = 0; i < num_pixels; i++)1552{1553const int a = pPixels[i];15541555uint32_t best_s_err = UINT32_MAX;1556uint32_t best_s = 0;1557for (uint32_t s = 0; s < 8; s++)1558{1559const int v = clamp255((int)multiplier * g_etc2_eac_tables[table][s] + (int)base);15601561uint32_t err = iabs(a - v);1562if (err < best_s_err)1563{1564best_s_err = err;1565best_s = s;1566}1567}15681569results.m_selectors_temp[i] = static_cast<uint8_t>(best_s);15701571total_err += best_s_err * best_s_err;1572if (total_err >= best_err)1573break;1574}15751576if (total_err < best_err)1577{1578best_err = total_err;1579results.m_base = base;1580results.m_multiplier = multiplier;1581results.m_table = table;1582results.m_selectors.swap(results.m_selectors_temp);1583if (!best_err)1584return best_err;1585}15861587} // table15881589} // multiplier15901591} // base15921593return best_err;1594}15951596void 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)1597{1598pack_eac_a8_results results;1599pack_eac_a8(results, pPixels, 16, base_search_rad, mul_search_rad, table_mask);16001601pBlock->m_base = results.m_base;1602pBlock->m_multiplier = results.m_multiplier;1603pBlock->m_table = results.m_table;1604for (uint32_t y = 0; y < 4; y++)1605for (uint32_t x = 0; x < 4; x++)1606pBlock->set_selector(x, y, results.m_selectors[x + y * 4]);1607}16081609} // namespace basisu161016111612