basisu_etc.h 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181
  1. // basis_etc.h
  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. #pragma once
  16. #include "../transcoder/basisu.h"
  17. #include "basisu_enc.h"
  18. namespace basisu
  19. {
  20. enum etc_constants
  21. {
  22. cETC1BytesPerBlock = 8U,
  23. cETC1SelectorBits = 2U,
  24. cETC1SelectorValues = 1U << cETC1SelectorBits,
  25. cETC1SelectorMask = cETC1SelectorValues - 1U,
  26. cETC1BlockShift = 2U,
  27. cETC1BlockSize = 1U << cETC1BlockShift,
  28. cETC1LSBSelectorIndicesBitOffset = 0,
  29. cETC1MSBSelectorIndicesBitOffset = 16,
  30. cETC1FlipBitOffset = 32,
  31. cETC1DiffBitOffset = 33,
  32. cETC1IntenModifierNumBits = 3,
  33. cETC1IntenModifierValues = 1 << cETC1IntenModifierNumBits,
  34. cETC1RightIntenModifierTableBitOffset = 34,
  35. cETC1LeftIntenModifierTableBitOffset = 37,
  36. // Base+Delta encoding (5 bit bases, 3 bit delta)
  37. cETC1BaseColorCompNumBits = 5,
  38. cETC1BaseColorCompMax = 1 << cETC1BaseColorCompNumBits,
  39. cETC1DeltaColorCompNumBits = 3,
  40. cETC1DeltaColorComp = 1 << cETC1DeltaColorCompNumBits,
  41. cETC1DeltaColorCompMax = 1 << cETC1DeltaColorCompNumBits,
  42. cETC1BaseColor5RBitOffset = 59,
  43. cETC1BaseColor5GBitOffset = 51,
  44. cETC1BaseColor5BBitOffset = 43,
  45. cETC1DeltaColor3RBitOffset = 56,
  46. cETC1DeltaColor3GBitOffset = 48,
  47. cETC1DeltaColor3BBitOffset = 40,
  48. // Absolute (non-delta) encoding (two 4-bit per component bases)
  49. cETC1AbsColorCompNumBits = 4,
  50. cETC1AbsColorCompMax = 1 << cETC1AbsColorCompNumBits,
  51. cETC1AbsColor4R1BitOffset = 60,
  52. cETC1AbsColor4G1BitOffset = 52,
  53. cETC1AbsColor4B1BitOffset = 44,
  54. cETC1AbsColor4R2BitOffset = 56,
  55. cETC1AbsColor4G2BitOffset = 48,
  56. cETC1AbsColor4B2BitOffset = 40,
  57. cETC1ColorDeltaMin = -4,
  58. cETC1ColorDeltaMax = 3,
  59. // Delta3:
  60. // 0 1 2 3 4 5 6 7
  61. // 000 001 010 011 100 101 110 111
  62. // 0 1 2 3 -4 -3 -2 -1
  63. };
  64. extern const int g_etc1_inten_tables[cETC1IntenModifierValues][cETC1SelectorValues];
  65. extern const uint8_t g_etc1_to_selector_index[cETC1SelectorValues];
  66. extern const uint8_t g_selector_index_to_etc1[cETC1SelectorValues];
  67. struct etc_coord2
  68. {
  69. uint8_t m_x, m_y;
  70. };
  71. extern const etc_coord2 g_etc1_pixel_coords[2][2][8]; // [flipped][subblock][subblock_pixel]
  72. extern const uint32_t g_etc1_pixel_indices[2][2][8]; // [flipped][subblock][subblock_pixel]
  73. struct etc_block
  74. {
  75. // big endian uint64:
  76. // bit ofs: 56 48 40 32 24 16 8 0
  77. // byte ofs: b0, b1, b2, b3, b4, b5, b6, b7
  78. union
  79. {
  80. uint64_t m_uint64;
  81. uint8_t m_bytes[8];
  82. };
  83. inline void clear()
  84. {
  85. assert(sizeof(*this) == 8);
  86. clear_obj(*this);
  87. }
  88. inline uint64_t get_all_bits() const
  89. {
  90. return read_be64(&m_uint64);
  91. }
  92. inline uint32_t get_general_bits(uint32_t ofs, uint32_t num) const
  93. {
  94. assert((ofs + num) <= 64U);
  95. assert(num && (num < 32U));
  96. return (uint32_t)(read_be64(&m_uint64) >> ofs) & ((1UL << num) - 1UL);
  97. }
  98. inline void set_general_bits(uint32_t ofs, uint32_t num, uint32_t bits)
  99. {
  100. assert((ofs + num) <= 64U);
  101. assert(num && (num < 32U));
  102. uint64_t x = read_be64(&m_uint64);
  103. uint64_t msk = ((1ULL << static_cast<uint64_t>(num)) - 1ULL) << static_cast<uint64_t>(ofs);
  104. x &= ~msk;
  105. x |= (static_cast<uint64_t>(bits) << static_cast<uint64_t>(ofs));
  106. write_be64(&m_uint64, x);
  107. }
  108. inline uint32_t get_byte_bits(uint32_t ofs, uint32_t num) const
  109. {
  110. assert((ofs + num) <= 64U);
  111. assert(num && (num <= 8U));
  112. assert((ofs >> 3) == ((ofs + num - 1) >> 3));
  113. const uint32_t byte_ofs = 7 - (ofs >> 3);
  114. const uint32_t byte_bit_ofs = ofs & 7;
  115. return (m_bytes[byte_ofs] >> byte_bit_ofs) & ((1 << num) - 1);
  116. }
  117. inline void set_byte_bits(uint32_t ofs, uint32_t num, uint32_t bits)
  118. {
  119. assert((ofs + num) <= 64U);
  120. assert(num && (num < 32U));
  121. assert((ofs >> 3) == ((ofs + num - 1) >> 3));
  122. assert(bits < (1U << num));
  123. const uint32_t byte_ofs = 7 - (ofs >> 3);
  124. const uint32_t byte_bit_ofs = ofs & 7;
  125. const uint32_t mask = (1 << num) - 1;
  126. m_bytes[byte_ofs] &= ~(mask << byte_bit_ofs);
  127. m_bytes[byte_ofs] |= (bits << byte_bit_ofs);
  128. }
  129. // false = left/right subblocks
  130. // true = upper/lower subblocks
  131. inline bool get_flip_bit() const
  132. {
  133. return (m_bytes[3] & 1) != 0;
  134. }
  135. inline void set_flip_bit(bool flip)
  136. {
  137. m_bytes[3] &= ~1;
  138. m_bytes[3] |= static_cast<uint8_t>(flip);
  139. }
  140. inline bool get_diff_bit() const
  141. {
  142. return (m_bytes[3] & 2) != 0;
  143. }
  144. inline void set_diff_bit(bool diff)
  145. {
  146. m_bytes[3] &= ~2;
  147. m_bytes[3] |= (static_cast<uint32_t>(diff) << 1);
  148. }
  149. // Returns intensity modifier table (0-7) used by subblock subblock_id.
  150. // subblock_id=0 left/top (CW 1), 1=right/bottom (CW 2)
  151. inline uint32_t get_inten_table(uint32_t subblock_id) const
  152. {
  153. assert(subblock_id < 2);
  154. const uint32_t ofs = subblock_id ? 2 : 5;
  155. return (m_bytes[3] >> ofs) & 7;
  156. }
  157. // Sets intensity modifier table (0-7) used by subblock subblock_id (0 or 1)
  158. inline void set_inten_table(uint32_t subblock_id, uint32_t t)
  159. {
  160. assert(subblock_id < 2);
  161. assert(t < 8);
  162. const uint32_t ofs = subblock_id ? 2 : 5;
  163. m_bytes[3] &= ~(7 << ofs);
  164. m_bytes[3] |= (t << ofs);
  165. }
  166. inline void set_inten_tables_etc1s(uint32_t t)
  167. {
  168. set_inten_table(0, t);
  169. set_inten_table(1, t);
  170. }
  171. inline bool is_etc1s() const
  172. {
  173. if (get_inten_table(0) != get_inten_table(1))
  174. return false;
  175. if (get_diff_bit())
  176. {
  177. if (get_delta3_color() != 0)
  178. return false;
  179. }
  180. else
  181. {
  182. if (get_base4_color(0) != get_base4_color(1))
  183. return false;
  184. }
  185. return true;
  186. }
  187. // Returned encoded selector value ranges from 0-3 (this is NOT a direct index into g_etc1_inten_tables, see get_selector())
  188. inline uint32_t get_raw_selector(uint32_t x, uint32_t y) const
  189. {
  190. assert((x | y) < 4);
  191. const uint32_t bit_index = x * 4 + y;
  192. const uint32_t byte_bit_ofs = bit_index & 7;
  193. const uint8_t *p = &m_bytes[7 - (bit_index >> 3)];
  194. const uint32_t lsb = (p[0] >> byte_bit_ofs) & 1;
  195. const uint32_t msb = (p[-2] >> byte_bit_ofs) & 1;
  196. const uint32_t val = lsb | (msb << 1);
  197. return val;
  198. }
  199. // Returned selector value ranges from 0-3 and is a direct index into g_etc1_inten_tables.
  200. inline uint32_t get_selector(uint32_t x, uint32_t y) const
  201. {
  202. return g_etc1_to_selector_index[get_raw_selector(x, y)];
  203. }
  204. // Selector "val" ranges from 0-3 and is a direct index into g_etc1_inten_tables.
  205. inline void set_selector(uint32_t x, uint32_t y, uint32_t val)
  206. {
  207. assert((x | y | val) < 4);
  208. const uint32_t bit_index = x * 4 + y;
  209. uint8_t *p = &m_bytes[7 - (bit_index >> 3)];
  210. const uint32_t byte_bit_ofs = bit_index & 7;
  211. const uint32_t mask = 1 << byte_bit_ofs;
  212. const uint32_t etc1_val = g_selector_index_to_etc1[val];
  213. const uint32_t lsb = etc1_val & 1;
  214. const uint32_t msb = etc1_val >> 1;
  215. p[0] &= ~mask;
  216. p[0] |= (lsb << byte_bit_ofs);
  217. p[-2] &= ~mask;
  218. p[-2] |= (msb << byte_bit_ofs);
  219. }
  220. // Selector "etc1_val" ranges from 0-3 and is a direct (raw) ETC1 selector.
  221. inline void set_raw_selector(uint32_t x, uint32_t y, uint32_t etc1_val)
  222. {
  223. assert((x | y | etc1_val) < 4);
  224. const uint32_t bit_index = x * 4 + y;
  225. uint8_t* p = &m_bytes[7 - (bit_index >> 3)];
  226. const uint32_t byte_bit_ofs = bit_index & 7;
  227. const uint32_t mask = 1 << byte_bit_ofs;
  228. const uint32_t lsb = etc1_val & 1;
  229. const uint32_t msb = etc1_val >> 1;
  230. p[0] &= ~mask;
  231. p[0] |= (lsb << byte_bit_ofs);
  232. p[-2] &= ~mask;
  233. p[-2] |= (msb << byte_bit_ofs);
  234. }
  235. inline uint32_t get_raw_selector_bits() const
  236. {
  237. return m_bytes[4] | (m_bytes[5] << 8) | (m_bytes[6] << 16) | (m_bytes[7] << 24);
  238. }
  239. inline void set_raw_selector_bits(uint32_t bits)
  240. {
  241. m_bytes[4] = static_cast<uint8_t>(bits);
  242. m_bytes[5] = static_cast<uint8_t>(bits >> 8);
  243. m_bytes[6] = static_cast<uint8_t>(bits >> 16);
  244. m_bytes[7] = static_cast<uint8_t>(bits >> 24);
  245. }
  246. inline void set_raw_selector_bits(uint8_t byte0, uint8_t byte1, uint8_t byte2, uint8_t byte3)
  247. {
  248. m_bytes[4] = byte0;
  249. m_bytes[5] = byte1;
  250. m_bytes[6] = byte2;
  251. m_bytes[7] = byte3;
  252. }
  253. inline void set_base4_color(uint32_t idx, uint16_t c)
  254. {
  255. if (idx)
  256. {
  257. set_byte_bits(cETC1AbsColor4R2BitOffset, 4, (c >> 8) & 15);
  258. set_byte_bits(cETC1AbsColor4G2BitOffset, 4, (c >> 4) & 15);
  259. set_byte_bits(cETC1AbsColor4B2BitOffset, 4, c & 15);
  260. }
  261. else
  262. {
  263. set_byte_bits(cETC1AbsColor4R1BitOffset, 4, (c >> 8) & 15);
  264. set_byte_bits(cETC1AbsColor4G1BitOffset, 4, (c >> 4) & 15);
  265. set_byte_bits(cETC1AbsColor4B1BitOffset, 4, c & 15);
  266. }
  267. }
  268. inline uint16_t get_base4_color(uint32_t idx) const
  269. {
  270. uint32_t r, g, b;
  271. if (idx)
  272. {
  273. r = get_byte_bits(cETC1AbsColor4R2BitOffset, 4);
  274. g = get_byte_bits(cETC1AbsColor4G2BitOffset, 4);
  275. b = get_byte_bits(cETC1AbsColor4B2BitOffset, 4);
  276. }
  277. else
  278. {
  279. r = get_byte_bits(cETC1AbsColor4R1BitOffset, 4);
  280. g = get_byte_bits(cETC1AbsColor4G1BitOffset, 4);
  281. b = get_byte_bits(cETC1AbsColor4B1BitOffset, 4);
  282. }
  283. return static_cast<uint16_t>(b | (g << 4U) | (r << 8U));
  284. }
  285. inline void set_base5_color(uint16_t c)
  286. {
  287. set_byte_bits(cETC1BaseColor5RBitOffset, 5, (c >> 10) & 31);
  288. set_byte_bits(cETC1BaseColor5GBitOffset, 5, (c >> 5) & 31);
  289. set_byte_bits(cETC1BaseColor5BBitOffset, 5, c & 31);
  290. }
  291. inline uint16_t get_base5_color() const
  292. {
  293. const uint32_t r = get_byte_bits(cETC1BaseColor5RBitOffset, 5);
  294. const uint32_t g = get_byte_bits(cETC1BaseColor5GBitOffset, 5);
  295. const uint32_t b = get_byte_bits(cETC1BaseColor5BBitOffset, 5);
  296. return static_cast<uint16_t>(b | (g << 5U) | (r << 10U));
  297. }
  298. void set_delta3_color(uint16_t c)
  299. {
  300. set_byte_bits(cETC1DeltaColor3RBitOffset, 3, (c >> 6) & 7);
  301. set_byte_bits(cETC1DeltaColor3GBitOffset, 3, (c >> 3) & 7);
  302. set_byte_bits(cETC1DeltaColor3BBitOffset, 3, c & 7);
  303. }
  304. inline uint16_t get_delta3_color() const
  305. {
  306. const uint32_t r = get_byte_bits(cETC1DeltaColor3RBitOffset, 3);
  307. const uint32_t g = get_byte_bits(cETC1DeltaColor3GBitOffset, 3);
  308. const uint32_t b = get_byte_bits(cETC1DeltaColor3BBitOffset, 3);
  309. return static_cast<uint16_t>(b | (g << 3U) | (r << 6U));
  310. }
  311. uint64_t determine_selectors(const color_rgba* pSource_pixels, bool perceptual, uint32_t begin_subblock = 0, uint32_t end_subblock = 2)
  312. {
  313. uint64_t total_error = 0;
  314. for (uint32_t subblock = begin_subblock; subblock < end_subblock; subblock++)
  315. {
  316. color_rgba block_colors[4];
  317. get_block_colors(block_colors, subblock);
  318. if (get_flip_bit())
  319. {
  320. for (uint32_t y = 0; y < 2; y++)
  321. {
  322. for (uint32_t x = 0; x < 4; x++)
  323. {
  324. uint32_t best_selector = 0;
  325. uint64_t best_error = UINT64_MAX;
  326. for (uint32_t s = 0; s < 4; s++)
  327. {
  328. uint64_t err = color_distance(perceptual, block_colors[s], pSource_pixels[x + (subblock * 2 + y) * 4], false);
  329. if (err < best_error)
  330. {
  331. best_error = err;
  332. best_selector = s;
  333. }
  334. }
  335. set_selector(x, subblock * 2 + y, best_selector);
  336. total_error += best_error;
  337. }
  338. }
  339. }
  340. else
  341. {
  342. for (uint32_t y = 0; y < 4; y++)
  343. {
  344. for (uint32_t x = 0; x < 2; x++)
  345. {
  346. uint32_t best_selector = 0;
  347. uint64_t best_error = UINT64_MAX;
  348. for (uint32_t s = 0; s < 4; s++)
  349. {
  350. uint64_t err = color_distance(perceptual, block_colors[s], pSource_pixels[(subblock * 2) + x + y * 4], false);
  351. if (err < best_error)
  352. {
  353. best_error = err;
  354. best_selector = s;
  355. }
  356. }
  357. set_selector(subblock * 2 + x, y, best_selector);
  358. total_error += best_error;
  359. }
  360. }
  361. }
  362. }
  363. return total_error;
  364. }
  365. color_rgba get_block_color(uint32_t subblock_index, bool scaled) const
  366. {
  367. color_rgba b;
  368. if (get_diff_bit())
  369. {
  370. if (subblock_index)
  371. unpack_color5(b, get_base5_color(), get_delta3_color(), scaled);
  372. else
  373. unpack_color5(b, get_base5_color(), scaled);
  374. }
  375. else
  376. {
  377. b = unpack_color4(get_base4_color(subblock_index), scaled);
  378. }
  379. return b;
  380. }
  381. uint32_t get_subblock_index(uint32_t x, uint32_t y) const
  382. {
  383. if (get_flip_bit())
  384. return y >= 2;
  385. else
  386. return x >= 2;
  387. }
  388. bool get_block_colors(color_rgba* pBlock_colors, uint32_t subblock_index) const
  389. {
  390. color_rgba b;
  391. if (get_diff_bit())
  392. {
  393. if (subblock_index)
  394. unpack_color5(b, get_base5_color(), get_delta3_color(), true);
  395. else
  396. unpack_color5(b, get_base5_color(), true);
  397. }
  398. else
  399. {
  400. b = unpack_color4(get_base4_color(subblock_index), true);
  401. }
  402. const int* pInten_table = g_etc1_inten_tables[get_inten_table(subblock_index)];
  403. bool dc = false;
  404. pBlock_colors[0].set(clamp255(b.r + pInten_table[0], dc), clamp255(b.g + pInten_table[0], dc), clamp255(b.b + pInten_table[0], dc), 255);
  405. pBlock_colors[1].set(clamp255(b.r + pInten_table[1], dc), clamp255(b.g + pInten_table[1], dc), clamp255(b.b + pInten_table[1], dc), 255);
  406. pBlock_colors[2].set(clamp255(b.r + pInten_table[2], dc), clamp255(b.g + pInten_table[2], dc), clamp255(b.b + pInten_table[2], dc), 255);
  407. pBlock_colors[3].set(clamp255(b.r + pInten_table[3], dc), clamp255(b.g + pInten_table[3], dc), clamp255(b.b + pInten_table[3], dc), 255);
  408. return dc;
  409. }
  410. void get_block_colors_etc1s(color_rgba* pBlock_colors) const
  411. {
  412. color_rgba b;
  413. unpack_color5(b, get_base5_color(), true);
  414. const int* pInten_table = g_etc1_inten_tables[get_inten_table(0)];
  415. pBlock_colors[0].set(clamp255(b.r + pInten_table[0]), clamp255(b.g + pInten_table[0]), clamp255(b.b + pInten_table[0]), 255);
  416. pBlock_colors[1].set(clamp255(b.r + pInten_table[1]), clamp255(b.g + pInten_table[1]), clamp255(b.b + pInten_table[1]), 255);
  417. pBlock_colors[2].set(clamp255(b.r + pInten_table[2]), clamp255(b.g + pInten_table[2]), clamp255(b.b + pInten_table[2]), 255);
  418. pBlock_colors[3].set(clamp255(b.r + pInten_table[3]), clamp255(b.g + pInten_table[3]), clamp255(b.b + pInten_table[3]), 255);
  419. }
  420. static void get_block_colors_etc1s(color_rgba* pBlock_colors, const color_rgba &base5_color, uint32_t inten_table)
  421. {
  422. color_rgba b;
  423. b.r = (base5_color.r << 3U) | (base5_color.r >> 2U);
  424. b.g = (base5_color.g << 3U) | (base5_color.g >> 2U);
  425. b.b = (base5_color.b << 3U) | (base5_color.b >> 2U);
  426. const int* pInten_table = g_etc1_inten_tables[inten_table];
  427. pBlock_colors[0].set(clamp255(b.r + pInten_table[0]), clamp255(b.g + pInten_table[0]), clamp255(b.b + pInten_table[0]), 255);
  428. pBlock_colors[1].set(clamp255(b.r + pInten_table[1]), clamp255(b.g + pInten_table[1]), clamp255(b.b + pInten_table[1]), 255);
  429. pBlock_colors[2].set(clamp255(b.r + pInten_table[2]), clamp255(b.g + pInten_table[2]), clamp255(b.b + pInten_table[2]), 255);
  430. pBlock_colors[3].set(clamp255(b.r + pInten_table[3]), clamp255(b.g + pInten_table[3]), clamp255(b.b + pInten_table[3]), 255);
  431. }
  432. void get_block_color(color_rgba& color, uint32_t subblock_index, uint32_t selector_index) const
  433. {
  434. color_rgba b;
  435. if (get_diff_bit())
  436. {
  437. if (subblock_index)
  438. unpack_color5(b, get_base5_color(), get_delta3_color(), true);
  439. else
  440. unpack_color5(b, get_base5_color(), true);
  441. }
  442. else
  443. {
  444. b = unpack_color4(get_base4_color(subblock_index), true);
  445. }
  446. const int* pInten_table = g_etc1_inten_tables[get_inten_table(subblock_index)];
  447. color.set(clamp255(b.r + pInten_table[selector_index]), clamp255(b.g + pInten_table[selector_index]), clamp255(b.b + pInten_table[selector_index]), 255);
  448. }
  449. bool get_block_low_high_colors(color_rgba* pBlock_colors, uint32_t subblock_index) const
  450. {
  451. color_rgba b;
  452. if (get_diff_bit())
  453. {
  454. if (subblock_index)
  455. unpack_color5(b, get_base5_color(), get_delta3_color(), true);
  456. else
  457. unpack_color5(b, get_base5_color(), true);
  458. }
  459. else
  460. {
  461. b = unpack_color4(get_base4_color(subblock_index), true);
  462. }
  463. const int* pInten_table = g_etc1_inten_tables[get_inten_table(subblock_index)];
  464. bool dc = false;
  465. pBlock_colors[0].set(clamp255(b.r + pInten_table[0], dc), clamp255(b.g + pInten_table[0], dc), clamp255(b.b + pInten_table[0], dc), 255);
  466. pBlock_colors[1].set(clamp255(b.r + pInten_table[3], dc), clamp255(b.g + pInten_table[3], dc), clamp255(b.b + pInten_table[3], dc), 255);
  467. return dc;
  468. }
  469. static void get_block_colors5(color_rgba *pBlock_colors, const color_rgba &base_color5, uint32_t inten_table, bool scaled = false)
  470. {
  471. color_rgba b(base_color5);
  472. if (!scaled)
  473. {
  474. b.r = (b.r << 3) | (b.r >> 2);
  475. b.g = (b.g << 3) | (b.g >> 2);
  476. b.b = (b.b << 3) | (b.b >> 2);
  477. }
  478. const int* pInten_table = g_etc1_inten_tables[inten_table];
  479. pBlock_colors[0].set(clamp255(b.r + pInten_table[0]), clamp255(b.g + pInten_table[0]), clamp255(b.b + pInten_table[0]), 255);
  480. pBlock_colors[1].set(clamp255(b.r + pInten_table[1]), clamp255(b.g + pInten_table[1]), clamp255(b.b + pInten_table[1]), 255);
  481. pBlock_colors[2].set(clamp255(b.r + pInten_table[2]), clamp255(b.g + pInten_table[2]), clamp255(b.b + pInten_table[2]), 255);
  482. pBlock_colors[3].set(clamp255(b.r + pInten_table[3]), clamp255(b.g + pInten_table[3]), clamp255(b.b + pInten_table[3]), 255);
  483. }
  484. static void get_block_colors4(color_rgba *pBlock_colors, const color_rgba &base_color4, uint32_t inten_table, bool scaled = false)
  485. {
  486. color_rgba b(base_color4);
  487. if (!scaled)
  488. {
  489. b.r = (b.r << 4) | b.r;
  490. b.g = (b.g << 4) | b.g;
  491. b.b = (b.b << 4) | b.b;
  492. }
  493. const int* pInten_table = g_etc1_inten_tables[inten_table];
  494. pBlock_colors[0].set(clamp255(b.r + pInten_table[0]), clamp255(b.g + pInten_table[0]), clamp255(b.b + pInten_table[0]), 255);
  495. pBlock_colors[1].set(clamp255(b.r + pInten_table[1]), clamp255(b.g + pInten_table[1]), clamp255(b.b + pInten_table[1]), 255);
  496. pBlock_colors[2].set(clamp255(b.r + pInten_table[2]), clamp255(b.g + pInten_table[2]), clamp255(b.b + pInten_table[2]), 255);
  497. pBlock_colors[3].set(clamp255(b.r + pInten_table[3]), clamp255(b.g + pInten_table[3]), clamp255(b.b + pInten_table[3]), 255);
  498. }
  499. uint64_t evaluate_etc1_error(const color_rgba* pBlock_pixels, bool perceptual, int subblock_index = -1) const;
  500. void get_subblock_pixels(color_rgba* pPixels, int subblock_index = -1) const;
  501. void get_selector_range(uint32_t& low, uint32_t& high) const
  502. {
  503. low = 3;
  504. high = 0;
  505. for (uint32_t y = 0; y < 4; y++)
  506. {
  507. for (uint32_t x = 0; x < 4; x++)
  508. {
  509. const uint32_t s = get_selector(x, y);
  510. low = minimum(low, s);
  511. high = maximum(high, s);
  512. }
  513. }
  514. }
  515. void set_block_color4(const color_rgba &c0_unscaled, const color_rgba &c1_unscaled)
  516. {
  517. set_diff_bit(false);
  518. set_base4_color(0, pack_color4(c0_unscaled, false));
  519. set_base4_color(1, pack_color4(c1_unscaled, false));
  520. }
  521. void set_block_color5(const color_rgba &c0_unscaled, const color_rgba &c1_unscaled)
  522. {
  523. set_diff_bit(true);
  524. set_base5_color(pack_color5(c0_unscaled, false));
  525. int dr = c1_unscaled.r - c0_unscaled.r;
  526. int dg = c1_unscaled.g - c0_unscaled.g;
  527. int db = c1_unscaled.b - c0_unscaled.b;
  528. set_delta3_color(pack_delta3(dr, dg, db));
  529. }
  530. void set_block_color5_etc1s(const color_rgba &c_unscaled)
  531. {
  532. set_diff_bit(true);
  533. set_base5_color(pack_color5(c_unscaled, false));
  534. set_delta3_color(pack_delta3(0, 0, 0));
  535. }
  536. bool set_block_color5_check(const color_rgba &c0_unscaled, const color_rgba &c1_unscaled)
  537. {
  538. set_diff_bit(true);
  539. set_base5_color(pack_color5(c0_unscaled, false));
  540. int dr = c1_unscaled.r - c0_unscaled.r;
  541. int dg = c1_unscaled.g - c0_unscaled.g;
  542. int db = c1_unscaled.b - c0_unscaled.b;
  543. if (((dr < cETC1ColorDeltaMin) || (dr > cETC1ColorDeltaMax)) ||
  544. ((dg < cETC1ColorDeltaMin) || (dg > cETC1ColorDeltaMax)) ||
  545. ((db < cETC1ColorDeltaMin) || (db > cETC1ColorDeltaMax)))
  546. return false;
  547. set_delta3_color(pack_delta3(dr, dg, db));
  548. return true;
  549. }
  550. bool set_block_color5_clamp(const color_rgba &c0_unscaled, const color_rgba &c1_unscaled)
  551. {
  552. set_diff_bit(true);
  553. set_base5_color(pack_color5(c0_unscaled, false));
  554. int dr = c1_unscaled.r - c0_unscaled.r;
  555. int dg = c1_unscaled.g - c0_unscaled.g;
  556. int db = c1_unscaled.b - c0_unscaled.b;
  557. dr = clamp<int>(dr, cETC1ColorDeltaMin, cETC1ColorDeltaMax);
  558. dg = clamp<int>(dg, cETC1ColorDeltaMin, cETC1ColorDeltaMax);
  559. db = clamp<int>(db, cETC1ColorDeltaMin, cETC1ColorDeltaMax);
  560. set_delta3_color(pack_delta3(dr, dg, db));
  561. return true;
  562. }
  563. color_rgba get_selector_color(uint32_t x, uint32_t y, uint32_t s) const
  564. {
  565. color_rgba block_colors[4];
  566. get_block_colors(block_colors, get_subblock_index(x, y));
  567. return block_colors[s];
  568. }
  569. // Base color 5
  570. static uint16_t pack_color5(const color_rgba& color, bool scaled, uint32_t bias = 127U);
  571. static uint16_t pack_color5(uint32_t r, uint32_t g, uint32_t b, bool scaled, uint32_t bias = 127U);
  572. static color_rgba unpack_color5(uint16_t packed_color5, bool scaled, uint32_t alpha = 255U);
  573. static void unpack_color5(uint32_t& r, uint32_t& g, uint32_t& b, uint16_t packed_color, bool scaled);
  574. static void unpack_color5(color_rgba& result, uint16_t packed_color5, bool scaled);
  575. static bool unpack_color5(color_rgba& result, uint16_t packed_color5, uint16_t packed_delta3, bool scaled, uint32_t alpha = 255U);
  576. static bool unpack_color5(uint32_t& r, uint32_t& g, uint32_t& b, uint16_t packed_color5, uint16_t packed_delta3, bool scaled, uint32_t alpha = 255U);
  577. // Delta color 3
  578. // Inputs range from -4 to 3 (cETC1ColorDeltaMin to cETC1ColorDeltaMax)
  579. static uint16_t pack_delta3(const color_rgba_i16& color);
  580. static uint16_t pack_delta3(int r, int g, int b);
  581. // Results range from -4 to 3 (cETC1ColorDeltaMin to cETC1ColorDeltaMax)
  582. static color_rgba_i16 unpack_delta3(uint16_t packed_delta3);
  583. static void unpack_delta3(int& r, int& g, int& b, uint16_t packed_delta3);
  584. static bool try_pack_color5_delta3(const color_rgba *pColor5_unscaled)
  585. {
  586. int dr = pColor5_unscaled[1].r - pColor5_unscaled[0].r;
  587. int dg = pColor5_unscaled[1].g - pColor5_unscaled[0].g;
  588. int db = pColor5_unscaled[1].b - pColor5_unscaled[0].b;
  589. if ((minimum(dr, dg, db) < cETC1ColorDeltaMin) || (maximum(dr, dg, db) > cETC1ColorDeltaMax))
  590. return false;
  591. return true;
  592. }
  593. // Abs color 4
  594. static uint16_t pack_color4(const color_rgba& color, bool scaled, uint32_t bias = 127U);
  595. static uint16_t pack_color4(uint32_t r, uint32_t g, uint32_t b, bool scaled, uint32_t bias = 127U);
  596. static color_rgba unpack_color4(uint16_t packed_color4, bool scaled, uint32_t alpha = 255U);
  597. static void unpack_color4(uint32_t& r, uint32_t& g, uint32_t& b, uint16_t packed_color4, bool scaled);
  598. // subblock colors
  599. static void get_diff_subblock_colors(color_rgba* pDst, uint16_t packed_color5, uint32_t table_idx);
  600. static bool get_diff_subblock_colors(color_rgba* pDst, uint16_t packed_color5, uint16_t packed_delta3, uint32_t table_idx);
  601. static void get_abs_subblock_colors(color_rgba* pDst, uint16_t packed_color4, uint32_t table_idx);
  602. static inline void unscaled_to_scaled_color(color_rgba& dst, const color_rgba& src, bool color4)
  603. {
  604. if (color4)
  605. {
  606. dst.r = src.r | (src.r << 4);
  607. dst.g = src.g | (src.g << 4);
  608. dst.b = src.b | (src.b << 4);
  609. }
  610. else
  611. {
  612. dst.r = (src.r >> 2) | (src.r << 3);
  613. dst.g = (src.g >> 2) | (src.g << 3);
  614. dst.b = (src.b >> 2) | (src.b << 3);
  615. }
  616. dst.a = src.a;
  617. }
  618. private:
  619. static uint8_t clamp255(int x, bool &did_clamp)
  620. {
  621. if (x < 0)
  622. {
  623. did_clamp = true;
  624. return 0;
  625. }
  626. else if (x > 255)
  627. {
  628. did_clamp = true;
  629. return 255;
  630. }
  631. return static_cast<uint8_t>(x);
  632. }
  633. static uint8_t clamp255(int x)
  634. {
  635. if (x < 0)
  636. return 0;
  637. else if (x > 255)
  638. return 255;
  639. return static_cast<uint8_t>(x);
  640. }
  641. };
  642. typedef basisu::vector<etc_block> etc_block_vec;
  643. // Returns false if the unpack fails (could be bogus data or ETC2)
  644. bool unpack_etc1(const etc_block& block, color_rgba *pDst, bool preserve_alpha = false);
  645. enum basis_etc_quality
  646. {
  647. cETCQualityFast,
  648. cETCQualityMedium,
  649. cETCQualitySlow,
  650. cETCQualityUber,
  651. cETCQualityTotal,
  652. };
  653. struct basis_etc1_pack_params
  654. {
  655. basis_etc_quality m_quality;
  656. bool m_perceptual;
  657. bool m_cluster_fit;
  658. bool m_force_etc1s;
  659. bool m_use_color4;
  660. float m_flip_bias;
  661. inline basis_etc1_pack_params()
  662. {
  663. clear();
  664. }
  665. void clear()
  666. {
  667. m_quality = cETCQualitySlow;
  668. m_perceptual = true;
  669. m_cluster_fit = true;
  670. m_force_etc1s = false;
  671. m_use_color4 = true;
  672. m_flip_bias = 0.0f;
  673. }
  674. };
  675. struct etc1_solution_coordinates
  676. {
  677. inline etc1_solution_coordinates() :
  678. m_unscaled_color(0, 0, 0, 0),
  679. m_inten_table(0),
  680. m_color4(false)
  681. {
  682. }
  683. inline etc1_solution_coordinates(uint32_t r, uint32_t g, uint32_t b, uint32_t inten_table, bool color4) :
  684. m_unscaled_color((uint8_t)r, (uint8_t)g, (uint8_t)b, 255),
  685. m_inten_table((uint8_t)inten_table),
  686. m_color4(color4)
  687. {
  688. }
  689. inline etc1_solution_coordinates(const color_rgba& c, uint32_t inten_table, bool color4) :
  690. m_unscaled_color(c),
  691. m_inten_table(inten_table),
  692. m_color4(color4)
  693. {
  694. }
  695. inline etc1_solution_coordinates(const etc1_solution_coordinates& other)
  696. {
  697. *this = other;
  698. }
  699. inline etc1_solution_coordinates& operator= (const etc1_solution_coordinates& rhs)
  700. {
  701. m_unscaled_color = rhs.m_unscaled_color;
  702. m_inten_table = rhs.m_inten_table;
  703. m_color4 = rhs.m_color4;
  704. return *this;
  705. }
  706. inline void clear()
  707. {
  708. m_unscaled_color.clear();
  709. m_inten_table = 0;
  710. m_color4 = false;
  711. }
  712. inline void init(const color_rgba& c, uint32_t inten_table, bool color4)
  713. {
  714. m_unscaled_color = c;
  715. m_inten_table = inten_table;
  716. m_color4 = color4;
  717. }
  718. inline color_rgba get_scaled_color() const
  719. {
  720. int br, bg, bb;
  721. if (m_color4)
  722. {
  723. br = m_unscaled_color.r | (m_unscaled_color.r << 4);
  724. bg = m_unscaled_color.g | (m_unscaled_color.g << 4);
  725. bb = m_unscaled_color.b | (m_unscaled_color.b << 4);
  726. }
  727. else
  728. {
  729. br = (m_unscaled_color.r >> 2) | (m_unscaled_color.r << 3);
  730. bg = (m_unscaled_color.g >> 2) | (m_unscaled_color.g << 3);
  731. bb = (m_unscaled_color.b >> 2) | (m_unscaled_color.b << 3);
  732. }
  733. return color_rgba((uint8_t)br, (uint8_t)bg, (uint8_t)bb, 255);
  734. }
  735. // returns true if anything was clamped
  736. inline void get_block_colors(color_rgba* pBlock_colors)
  737. {
  738. int br, bg, bb;
  739. if (m_color4)
  740. {
  741. br = m_unscaled_color.r | (m_unscaled_color.r << 4);
  742. bg = m_unscaled_color.g | (m_unscaled_color.g << 4);
  743. bb = m_unscaled_color.b | (m_unscaled_color.b << 4);
  744. }
  745. else
  746. {
  747. br = (m_unscaled_color.r >> 2) | (m_unscaled_color.r << 3);
  748. bg = (m_unscaled_color.g >> 2) | (m_unscaled_color.g << 3);
  749. bb = (m_unscaled_color.b >> 2) | (m_unscaled_color.b << 3);
  750. }
  751. const int* pInten_table = g_etc1_inten_tables[m_inten_table];
  752. pBlock_colors[0].set(br + pInten_table[0], bg + pInten_table[0], bb + pInten_table[0], 255);
  753. pBlock_colors[1].set(br + pInten_table[1], bg + pInten_table[1], bb + pInten_table[1], 255);
  754. pBlock_colors[2].set(br + pInten_table[2], bg + pInten_table[2], bb + pInten_table[2], 255);
  755. pBlock_colors[3].set(br + pInten_table[3], bg + pInten_table[3], bb + pInten_table[3], 255);
  756. }
  757. color_rgba m_unscaled_color;
  758. uint32_t m_inten_table;
  759. bool m_color4;
  760. };
  761. class etc1_optimizer
  762. {
  763. BASISU_NO_EQUALS_OR_COPY_CONSTRUCT(etc1_optimizer);
  764. public:
  765. etc1_optimizer()
  766. {
  767. clear();
  768. }
  769. void clear()
  770. {
  771. m_pParams = nullptr;
  772. m_pResult = nullptr;
  773. m_pSorted_luma = nullptr;
  774. m_pSorted_luma_indices = nullptr;
  775. }
  776. struct params;
  777. typedef bool(*evaluate_solution_override_func)(uint64_t &error, const params &p, const color_rgba* pBlock_colors, const uint8_t* pSelectors, const etc1_solution_coordinates& coords);
  778. struct params : basis_etc1_pack_params
  779. {
  780. params()
  781. {
  782. clear();
  783. }
  784. params(const basis_etc1_pack_params& base_params)
  785. {
  786. clear_optimizer_params();
  787. *static_cast<basis_etc1_pack_params *>(this) = base_params;
  788. }
  789. void clear()
  790. {
  791. clear_optimizer_params();
  792. }
  793. void clear_optimizer_params()
  794. {
  795. basis_etc1_pack_params::clear();
  796. m_num_src_pixels = 0;
  797. m_pSrc_pixels = 0;
  798. m_use_color4 = false;
  799. static const int s_default_scan_delta[] = { 0 };
  800. m_pScan_deltas = s_default_scan_delta;
  801. m_scan_delta_size = 1;
  802. m_base_color5.clear();
  803. m_constrain_against_base_color5 = false;
  804. m_refinement = true;
  805. m_pForce_selectors = nullptr;
  806. }
  807. uint32_t m_num_src_pixels;
  808. const color_rgba* m_pSrc_pixels;
  809. bool m_use_color4;
  810. const int* m_pScan_deltas;
  811. uint32_t m_scan_delta_size;
  812. color_rgba m_base_color5;
  813. bool m_constrain_against_base_color5;
  814. bool m_refinement;
  815. const uint8_t* m_pForce_selectors;
  816. };
  817. struct results
  818. {
  819. uint64_t m_error;
  820. color_rgba m_block_color_unscaled;
  821. uint32_t m_block_inten_table;
  822. uint32_t m_n;
  823. uint8_t* m_pSelectors;
  824. bool m_block_color4;
  825. inline results& operator= (const results& rhs)
  826. {
  827. m_block_color_unscaled = rhs.m_block_color_unscaled;
  828. m_block_color4 = rhs.m_block_color4;
  829. m_block_inten_table = rhs.m_block_inten_table;
  830. m_error = rhs.m_error;
  831. memcpy(m_pSelectors, rhs.m_pSelectors, minimum(rhs.m_n, m_n));
  832. return *this;
  833. }
  834. };
  835. void init(const params& params, results& result);
  836. bool compute();
  837. const params* get_params() const { return m_pParams; }
  838. private:
  839. struct potential_solution
  840. {
  841. potential_solution() : m_coords(), m_error(UINT64_MAX), m_valid(false)
  842. {
  843. }
  844. etc1_solution_coordinates m_coords;
  845. basisu::vector<uint8_t> m_selectors;
  846. uint64_t m_error;
  847. bool m_valid;
  848. void clear()
  849. {
  850. m_coords.clear();
  851. m_selectors.resize(0);
  852. m_error = UINT64_MAX;
  853. m_valid = false;
  854. }
  855. bool are_selectors_all_equal() const
  856. {
  857. if (!m_selectors.size())
  858. return false;
  859. const uint32_t s = m_selectors[0];
  860. for (uint32_t i = 1; i < m_selectors.size(); i++)
  861. if (m_selectors[i] != s)
  862. return false;
  863. return true;
  864. }
  865. };
  866. const params* m_pParams;
  867. results* m_pResult;
  868. int m_limit;
  869. vec3F m_avg_color;
  870. int m_br, m_bg, m_bb;
  871. int m_max_comp_spread;
  872. basisu::vector<uint16_t> m_luma;
  873. basisu::vector<uint32_t> m_sorted_luma;
  874. basisu::vector<uint32_t> m_sorted_luma_indices;
  875. const uint32_t* m_pSorted_luma_indices;
  876. uint32_t* m_pSorted_luma;
  877. basisu::vector<uint8_t> m_selectors;
  878. basisu::vector<uint8_t> m_best_selectors;
  879. potential_solution m_best_solution;
  880. potential_solution m_trial_solution;
  881. basisu::vector<uint8_t> m_temp_selectors;
  882. enum { cSolutionsTriedHashBits = 10, cTotalSolutionsTriedHashSize = 1 << cSolutionsTriedHashBits, cSolutionsTriedHashMask = cTotalSolutionsTriedHashSize - 1 };
  883. uint8_t m_solutions_tried[cTotalSolutionsTriedHashSize / 8];
  884. void get_nearby_inten_tables(uint32_t idx, int &first_inten_table, int &last_inten_table)
  885. {
  886. first_inten_table = maximum<int>(idx - 1, 0);
  887. last_inten_table = minimum<int>(cETC1IntenModifierValues, idx + 1);
  888. }
  889. bool check_for_redundant_solution(const etc1_solution_coordinates& coords);
  890. bool evaluate_solution_slow(const etc1_solution_coordinates& coords, potential_solution& trial_solution, potential_solution* pBest_solution);
  891. bool evaluate_solution_fast(const etc1_solution_coordinates& coords, potential_solution& trial_solution, potential_solution* pBest_solution);
  892. inline bool evaluate_solution(const etc1_solution_coordinates& coords, potential_solution& trial_solution, potential_solution* pBest_solution)
  893. {
  894. if (m_pParams->m_quality >= cETCQualityMedium)
  895. return evaluate_solution_slow(coords, trial_solution, pBest_solution);
  896. else
  897. return evaluate_solution_fast(coords, trial_solution, pBest_solution);
  898. }
  899. void refine_solution(uint32_t max_refinement_trials);
  900. void compute_internal_neighborhood(int scan_r, int scan_g, int scan_b);
  901. void compute_internal_cluster_fit(uint32_t total_perms_to_try);
  902. };
  903. struct pack_etc1_block_context
  904. {
  905. etc1_optimizer m_optimizer;
  906. };
  907. void pack_etc1_solid_color_init();
  908. uint64_t pack_etc1_block_solid_color(etc_block& block, const uint8_t* pColor);
  909. // ETC EAC
  910. extern const int8_t g_etc2_eac_tables[16][8];
  911. extern const int8_t g_etc2_eac_tables8[16][8];
  912. const uint32_t ETC2_EAC_MIN_VALUE_SELECTOR = 3, ETC2_EAC_MAX_VALUE_SELECTOR = 7;
  913. struct eac_a8_block
  914. {
  915. uint16_t m_base : 8;
  916. uint16_t m_table : 4;
  917. uint16_t m_multiplier : 4;
  918. uint8_t m_selectors[6];
  919. inline uint32_t get_selector(uint32_t x, uint32_t y, uint64_t selector_bits) const
  920. {
  921. assert((x < 4) && (y < 4));
  922. return static_cast<uint32_t>((selector_bits >> (45 - (y + x * 4) * 3)) & 7);
  923. }
  924. inline uint64_t get_selector_bits() const
  925. {
  926. uint64_t pixels = ((uint64_t)m_selectors[0] << 40) | ((uint64_t)m_selectors[1] << 32) | ((uint64_t)m_selectors[2] << 24) | ((uint64_t)m_selectors[3] << 16) | ((uint64_t)m_selectors[4] << 8) | m_selectors[5];
  927. return pixels;
  928. }
  929. inline void set_selector_bits(uint64_t pixels)
  930. {
  931. m_selectors[0] = (uint8_t)(pixels >> 40);
  932. m_selectors[1] = (uint8_t)(pixels >> 32);
  933. m_selectors[2] = (uint8_t)(pixels >> 24);
  934. m_selectors[3] = (uint8_t)(pixels >> 16);
  935. m_selectors[4] = (uint8_t)(pixels >> 8);
  936. m_selectors[5] = (uint8_t)(pixels);
  937. }
  938. void set_selector(uint32_t x, uint32_t y, uint32_t s)
  939. {
  940. assert((x < 4) && (y < 4) && (s < 8));
  941. const uint32_t ofs = 45 - (y + x * 4) * 3;
  942. uint64_t pixels = get_selector_bits();
  943. pixels &= ~(7ULL << ofs);
  944. pixels |= (static_cast<uint64_t>(s) << ofs);
  945. set_selector_bits(pixels);
  946. }
  947. };
  948. struct etc2_rgba_block
  949. {
  950. eac_a8_block m_alpha;
  951. etc_block m_rgb;
  952. };
  953. struct pack_eac_a8_results
  954. {
  955. uint32_t m_base;
  956. uint32_t m_table;
  957. uint32_t m_multiplier;
  958. uint8_vec m_selectors;
  959. uint8_vec m_selectors_temp;
  960. };
  961. 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 = UINT32_MAX);
  962. 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 = UINT32_MAX);
  963. } // namespace basisu