ispc_texcomp_astc.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. /*
  2. Copyright (c) 2015, Intel Corporation
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are
  6. met:
  7. * Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. * Neither the name of Intel Corporation nor the names of its
  13. contributors may be used to endorse or promote products derived from
  14. this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  16. IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  17. TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  18. PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  19. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  22. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "ispc_texcomp.h"
  28. #include "kernel_astc_ispc.h"
  29. #include <cassert>
  30. #include <cstring>
  31. #include <algorithm>
  32. #include <vector>
  33. #include <limits>
  34. void GetProfile_astc_fast(astc_enc_settings* settings, int block_width, int block_height)
  35. {
  36. settings->block_width = block_width;
  37. settings->block_height = block_height;
  38. settings->channels = 3;
  39. settings->fastSkipTreshold = 5;
  40. settings->refineIterations = 2;
  41. }
  42. void GetProfile_astc_alpha_fast(astc_enc_settings* settings, int block_width, int block_height)
  43. {
  44. settings->block_width = block_width;
  45. settings->block_height = block_height;
  46. settings->channels = 4;
  47. settings->fastSkipTreshold = 5;
  48. settings->refineIterations = 2;
  49. }
  50. void GetProfile_astc_alpha_slow(astc_enc_settings* settings, int block_width, int block_height)
  51. {
  52. settings->block_width = block_width;
  53. settings->block_height = block_height;
  54. settings->channels = 4;
  55. settings->fastSkipTreshold = 64;
  56. settings->refineIterations = 2;
  57. }
  58. struct astc_block
  59. {
  60. int width;
  61. int height;
  62. bool dual_plane;
  63. int weight_range;
  64. uint8_t weights[64];
  65. int color_component_selector;
  66. int partitions;
  67. int partition_id;
  68. int color_endpoint_pairs;
  69. int channels;
  70. int color_endpoint_modes[4];
  71. int endpoint_range;
  72. uint8_t endpoints[18];
  73. };
  74. bool can_store(int value, int bits)
  75. {
  76. if (value < 0) return false;
  77. if (value >= 1 << bits) return false;
  78. return true;
  79. }
  80. int pack_block_mode(astc_block* block)
  81. {
  82. int block_mode = 0;
  83. int D = !!block->dual_plane;
  84. int H = !!(block->weight_range >= 6);
  85. int DH = D * 2 + H;
  86. int R = block->weight_range + 2 - ((H > 0) ? 6 : 0);
  87. R = R / 2 + R % 2 * 4;
  88. if (can_store(block->width - 4, 2) && can_store(block->height - 2, 2))
  89. {
  90. int B = block->width - 4;
  91. int A = block->height - 2;
  92. block_mode = (DH << 9) | (B << 7) | (A << 5) | ((R & 4) << 2) | (R & 3);
  93. }
  94. if (can_store(block->width - 8, 2) && can_store(block->height - 2, 2))
  95. {
  96. int B = block->width - 8;
  97. int A = block->height - 2;
  98. block_mode = (DH << 9) | (B << 7) | (A << 5) | ((R & 4) << 2) | 4 | (R & 3);
  99. }
  100. if (can_store(block->width - 2, 2) && can_store(block->height - 8, 2))
  101. {
  102. int A = block->width - 2;
  103. int B = block->height - 8;
  104. block_mode = (DH << 9) | (B << 7) | (A << 5) | ((R & 4) << 2) | 8 | (R & 3);
  105. }
  106. if (can_store(block->width - 2, 2) && can_store(block->height - 6, 1))
  107. {
  108. int A = block->width - 2;
  109. int B = block->height - 6;
  110. block_mode = (DH << 9) | (B << 7) | (A << 5) | ((R & 4) << 2) | 12 | (R & 3);
  111. }
  112. if (can_store(block->width - 2, 1) && can_store(block->height - 2, 2))
  113. {
  114. int B = block->width;
  115. int A = block->height - 2;
  116. block_mode = (DH << 9) | (B << 7) | (A << 5) | ((R & 4) << 2) | 12 | (R & 3);
  117. }
  118. if (DH == 0 && can_store(block->width - 6, 2) && can_store(block->height - 6, 2))
  119. {
  120. int A = block->width - 6;
  121. int B = block->height - 6;
  122. block_mode = (B << 9) | 256 | (A << 5) | (R << 2);
  123. }
  124. return block_mode;
  125. }
  126. int range_table[][3] =
  127. {
  128. //2^ 3^ 5^
  129. { 1, 0, 0 }, // 0..1
  130. { 0, 1, 0 }, // 0..2
  131. { 2, 0, 0 }, // 0..3
  132. { 0, 0, 1 }, // 0..4
  133. { 1, 1, 0 }, // 0..5
  134. { 3, 0, 0 }, // 0..7
  135. { 1, 0, 1 }, // 0..9
  136. { 2, 1, 0 }, // 0..11
  137. { 4, 0, 0 }, // 0..15
  138. { 2, 0, 1 }, // 0..19
  139. { 3, 1, 0 }, // 0..23
  140. { 5, 0, 0 }, // 0..31
  141. { 3, 0, 1 }, // 0..39
  142. { 4, 1, 0 }, // 0..47
  143. { 6, 0, 0 }, // 0..63
  144. { 4, 0, 1 }, // 0..79
  145. { 5, 1, 0 }, // 0..95
  146. { 7, 0, 0 }, // 0..127
  147. { 5, 0, 1 }, // 0..159
  148. { 6, 1, 0 }, // 0..191
  149. { 8, 0, 0 }, // 0..255
  150. };
  151. int get_levels(int range)
  152. {
  153. return (1 + 2 * range_table[range][1] + 4 * range_table[range][2]) << range_table[range][0];
  154. }
  155. int sequence_bits(int count, int range)
  156. {
  157. int bits = count * range_table[range][0];
  158. bits += (count * range_table[range][1] * 8 + 4) / 5;
  159. bits += (count * range_table[range][2] * 7 + 2) / 3;
  160. return bits;
  161. }
  162. void set_bits(uint32_t data[4], int* pos, int bits, uint32_t value)
  163. {
  164. assert(bits <= 25);
  165. uint32_t word = *(uint32_t*)(((uint8_t*)data) + *pos / 8);
  166. uint32_t mask = (1 << bits) - 1;
  167. word |= value << (*pos % 8);
  168. *(uint32_t*)(((uint8_t*)data) + *pos / 8) = word;
  169. *pos += bits;
  170. }
  171. uint32_t get_field(uint32_t input, int a, int b)
  172. {
  173. assert(a >= b);
  174. return (input >> b) & ((1 << (a - b + 1)) - 1);
  175. }
  176. uint32_t get_bit(uint32_t input, int a)
  177. {
  178. return get_field(input, a, a);
  179. }
  180. void pack_five_trits(uint32_t data[4], int sequence[5], int* pos, int n)
  181. {
  182. int t[5];
  183. int m[5];
  184. for (int i = 0; i < 5; i++)
  185. {
  186. t[i] = sequence[i] >> n;
  187. m[i] = sequence[i] - (t[i] << n);
  188. }
  189. int C;
  190. if (t[1] == 2 && t[2] == 2)
  191. {
  192. C = 3 * 4 + t[0];
  193. }
  194. else if (t[2] == 2)
  195. {
  196. C = t[1] * 16 + t[0] * 4 + 3;
  197. }
  198. else
  199. {
  200. C = t[2] * 16 + t[1] * 4 + t[0];
  201. }
  202. int T;
  203. if (t[3] == 2 && t[4] == 2)
  204. {
  205. T = get_field(C, 4, 2) * 32 + 7 * 4 + get_field(C, 1, 0);
  206. }
  207. else
  208. {
  209. T = get_field(C, 4, 0);
  210. if (t[4] == 2)
  211. {
  212. T += t[3] * 128 + 3 * 32;
  213. }
  214. else
  215. {
  216. T += t[4] * 128 + t[3] * 32;
  217. }
  218. }
  219. uint32_t pack1 = 0;
  220. pack1 |= m[0];
  221. pack1 |= get_field(T, 1, 0) << n;
  222. pack1 |= m[1] << (2 + n);
  223. uint32_t pack2 = 0;
  224. pack2 |= get_field(T, 3, 2);
  225. pack2 |= m[2] << 2;
  226. pack2 |= get_field(T, 4, 4) << (2 + n);
  227. pack2 |= m[3] << (3 + n);
  228. pack2 |= get_field(T, 6, 5) << (3 + n * 2);
  229. pack2 |= m[4] << (5 + n * 2);
  230. pack2 |= get_field(T, 7, 7) << (5 + n * 3);
  231. set_bits(data, pos, 2 + n * 2, pack1);
  232. set_bits(data, pos, 6 + n * 3, pack2);
  233. }
  234. void pack_three_quint(uint32_t data[4], int sequence[3], int* pos, int n)
  235. {
  236. int q[3];
  237. int m[3];
  238. for (int i = 0; i < 3; i++)
  239. {
  240. q[i] = sequence[i] >> n;
  241. m[i] = sequence[i] - (q[i] << n);
  242. }
  243. int Q;
  244. if (q[0] == 4 && q[1] == 4)
  245. {
  246. Q = get_field(q[2], 1, 0) * 8 + 3 * 2 + get_bit(q[2], 2);
  247. }
  248. else
  249. {
  250. int C;
  251. if (q[1] == 4)
  252. {
  253. C = (q[0] << 3) + 5;
  254. }
  255. else
  256. {
  257. C = (q[1] << 3) + q[0];
  258. }
  259. if (q[2] == 4)
  260. {
  261. Q = get_field(~C, 2, 1) * 32 + get_field(C, 4, 3) * 8 + 3 * 2 + get_bit(C, 0);
  262. }
  263. else
  264. {
  265. Q = q[2] * 32 + get_field(C, 4, 0);
  266. }
  267. }
  268. uint32_t pack = 0;
  269. pack |= m[0];
  270. pack |= get_field(Q, 2, 0) << n;
  271. pack |= m[1] << (3 + n);
  272. pack |= get_field(Q, 4, 3) << (3 + n * 2);
  273. pack |= m[2] << (5 + n * 2);
  274. pack |= get_field(Q, 6, 5) << (5 + n * 3);
  275. set_bits(data, pos, 7 + n * 3, pack);
  276. }
  277. void pack_integer_sequence(uint32_t output_data[4], uint8_t sequence[], int pos, int count, int range)
  278. {
  279. int n = range_table[range][0];
  280. int bits = sequence_bits(count, range);
  281. int pos0 = pos;
  282. uint32_t data[5] = { 0 };
  283. if (range_table[range][1] == 1)
  284. {
  285. for (int j = 0; j < (count + 4) / 5; j++)
  286. {
  287. int temp[5] = { 0 };
  288. for (int i = 0; i < std::min(count - j * 5, 5); i++) temp[i] = sequence[j * 5 + i];
  289. pack_five_trits(data, temp, &pos, n);
  290. }
  291. }
  292. else if (range_table[range][2] == 1)
  293. {
  294. for (int j = 0; j < (count + 2) / 3; j++)
  295. {
  296. int temp[3] = { 0 };
  297. for (int i = 0; i < std::min(count - j * 3, 3); i++) temp[i] = sequence[j * 3 + i];
  298. pack_three_quint(data, temp, &pos, n);
  299. }
  300. }
  301. else
  302. {
  303. for (int i = 0; i < count; i++)
  304. {
  305. set_bits(data, &pos, n, sequence[i]);
  306. }
  307. }
  308. if (pos0 + bits < 96) data[3] = 0;
  309. if (pos0 + bits < 64) data[2] = 0;
  310. if (pos0 + bits < 32) data[1] = 0;
  311. data[(pos0 + bits) / 32] &= (1 << ((pos0 + bits) % 32)) - 1;
  312. for (int k = 0; k < 4; k++) output_data[k] |= data[k];
  313. }
  314. uint32_t reverse_bits_32(uint32_t input)
  315. {
  316. uint32_t t = input;
  317. t = (t << 16) | (t >> 16);
  318. t = ((t & 0x00FF00FF) << 8) | ((t & 0xFF00FF00) >> 8);
  319. t = ((t & 0x0F0F0F0F) << 4) | ((t & 0xF0F0F0F0) >> 4);
  320. t = ((t & 0x33333333) << 2) | ((t & 0xCCCCCCCC) >> 2);
  321. t = ((t & 0x55555555) << 1) | ((t & 0xAAAAAAAA) >> 1);
  322. return t;
  323. }
  324. void pack_block(uint32_t data[4], astc_block* block)
  325. {
  326. memset(data, 0, 16);
  327. int pos = 0;
  328. set_bits(data, &pos, 11, pack_block_mode(block));
  329. int num_weights = block->width * block->height * (block->dual_plane ? 2 : 1);
  330. int weight_bits = sequence_bits(num_weights, block->weight_range);
  331. int extra_bits = 0;
  332. assert(num_weights <= 64);
  333. assert(24 <= weight_bits && weight_bits <= 96);
  334. set_bits(data, &pos, 2, block->partitions - 1);
  335. if (block->partitions > 1)
  336. {
  337. set_bits(data, &pos, 10, block->partition_id);
  338. int min_cem = 16;
  339. int max_cem = 0;
  340. for (int j = 0; j < block->partitions; j++)
  341. {
  342. min_cem = std::min(min_cem, block->color_endpoint_modes[j]);
  343. max_cem = std::max(max_cem, block->color_endpoint_modes[j]);
  344. }
  345. assert(max_cem / 4 <= min_cem / 4 + 1);
  346. int CEM = block->color_endpoint_modes[0] << 2;
  347. if (max_cem != min_cem)
  348. {
  349. CEM = std::min(3, min_cem / 4 + 1);
  350. for (int j = 0; j < block->partitions; j++)
  351. {
  352. int c = block->color_endpoint_modes[j] / 4 - ((CEM & 3) - 1);
  353. int m = block->color_endpoint_modes[j] % 4;
  354. assert(c == 0 || c == 1);
  355. CEM |= c << (2 + j);
  356. CEM |= m << (2 + block->partitions + 2 * j);
  357. }
  358. extra_bits = 3 * block->partitions - 4;
  359. int pos2 = 128 - weight_bits - extra_bits;
  360. set_bits(data, &pos2, extra_bits, CEM >> 6);
  361. }
  362. set_bits(data, &pos, 6, CEM & 63);
  363. }
  364. else
  365. {
  366. set_bits(data, &pos, 4, block->color_endpoint_modes[0]);
  367. }
  368. if (block->dual_plane)
  369. {
  370. assert(block->partitions < 4);
  371. extra_bits += 2;
  372. int pos2 = 128 - weight_bits - extra_bits;
  373. set_bits(data, &pos2, 2, block->color_component_selector);
  374. }
  375. int config_bits = pos + extra_bits;
  376. int remaining_bits = 128 - config_bits - weight_bits;
  377. int num_cem_pairs = 0;
  378. for (int j = 0; j < block->partitions; j++) num_cem_pairs += 1 + block->color_endpoint_modes[j] / 4;
  379. assert(num_cem_pairs <= 9);
  380. int endpoint_range = -1;
  381. for (int range = 20; range>0; range--)
  382. {
  383. int bits = sequence_bits(2 * num_cem_pairs, range);
  384. if (bits <= remaining_bits)
  385. {
  386. endpoint_range = range;
  387. break;
  388. }
  389. }
  390. assert(endpoint_range >= 4);
  391. assert(block->endpoint_range == endpoint_range);
  392. pack_integer_sequence(data, block->endpoints, pos, 2 * num_cem_pairs, endpoint_range);
  393. uint32_t rdata[4] = { 0, 0, 0, 0 };
  394. pack_integer_sequence(rdata, block->weights, 0, num_weights, block->weight_range);
  395. for (int i = 0; i < 4; i++) data[i] |= reverse_bits_32(rdata[3 - i]);
  396. }
  397. void atsc_rank(const rgba_surface* src, int xx, int yy, uint32_t* mode_buffer, astc_enc_settings* settings)
  398. {
  399. ispc::astc_rank_ispc((ispc::rgba_surface*)src, xx, yy, mode_buffer, (ispc::astc_enc_settings*)settings);
  400. }
  401. extern "C" void pack_block_c(uint32_t data[4], ispc::astc_block* block)
  402. {
  403. assert(sizeof(ispc::astc_block) == sizeof(astc_block));
  404. pack_block(data, (astc_block*)block);
  405. }
  406. void setup_list_context(ispc::astc_enc_context* ctx, uint32_t packed_mode)
  407. {
  408. ctx->width = 2 + get_field(packed_mode, 15, 13); // 2..8 <= 2^3
  409. ctx->height = 2 + get_field(packed_mode, 18, 16); // 2..8 <= 2^3
  410. ctx->dual_plane = !!get_field(packed_mode, 19, 19); // 0 or 1
  411. ctx->partitions = 1;
  412. int color_endpoint_modes0 = get_field(packed_mode, 7, 6) * 2 + 6; // 6, 8, 10 or 12
  413. ctx->color_endpoint_pairs = 1 + (color_endpoint_modes0 / 4);
  414. ctx->channels = (color_endpoint_modes0 > 8) ? 4 : 3;
  415. }
  416. void astc_encode(const rgba_surface* src, float* block_scores, uint8_t* dst, uint64_t* list, astc_enc_settings* settings)
  417. {
  418. ispc::astc_enc_context list_context;
  419. setup_list_context(&list_context, uint32_t(list[1] & 0xFFFFFFFF));
  420. assert(sizeof(ispc::rgba_surface) == sizeof(rgba_surface));
  421. assert(sizeof(ispc::astc_enc_settings) == sizeof(astc_enc_settings));
  422. ispc::astc_encode_ispc((ispc::rgba_surface*)src, block_scores, dst, list, &list_context, (ispc::astc_enc_settings*)settings);
  423. }
  424. void CompressBlocksASTC(const rgba_surface* src, uint8_t* dst, astc_enc_settings* settings)
  425. {
  426. assert(src->height % settings->block_height == 0);
  427. assert(src->width % settings->block_width == 0);
  428. assert(settings->block_height <= 8);
  429. assert(settings->block_width <= 8);
  430. int tex_width = src->width / settings->block_width;
  431. int programCount = ispc::get_programCount();
  432. std::vector<float> block_scores(tex_width * src->height / settings->block_height);
  433. for (int yy = 0; yy < src->height / settings->block_height; yy++)
  434. for (int xx = 0; xx < tex_width; xx++)
  435. {
  436. block_scores[yy * tex_width + xx] = std::numeric_limits<float>::infinity();
  437. }
  438. int mode_list_size = 3334;
  439. int list_size = programCount;
  440. std::vector<uint64_t> mode_lists(list_size * mode_list_size);
  441. std::vector<uint32_t> mode_buffer(programCount * settings->fastSkipTreshold);
  442. for (int yy = 0; yy < src->height / settings->block_height; yy++)
  443. for (int _x = 0; _x < (tex_width + programCount - 1) / programCount; _x++)
  444. {
  445. int xx = _x * programCount;
  446. atsc_rank(src, xx, yy, mode_buffer.data(), settings);
  447. for (int i = 0; i < settings->fastSkipTreshold; i++)
  448. for (int k = 0; k < programCount; k++)
  449. {
  450. if (xx + k >= tex_width) continue;
  451. uint32_t offset = (yy << 16) + (xx + k);
  452. uint32_t mode = mode_buffer[programCount * i + k];
  453. int mode_bin = mode >> 20;
  454. uint64_t* mode_list = &mode_lists[list_size * mode_bin];
  455. if (*mode_list < programCount - 1)
  456. {
  457. int index = int(mode_list[0] + 1);
  458. mode_list[0] = index;
  459. mode_list[index] = (uint64_t(offset) << 32) + mode;
  460. }
  461. else
  462. {
  463. mode_list[0] = (uint64_t(offset) << 32) + mode;
  464. astc_encode(src, block_scores.data(), dst, mode_list, settings);
  465. memset(mode_list, 0, list_size * sizeof(uint64_t));
  466. }
  467. }
  468. }
  469. for (int mode_bin = 0; mode_bin < mode_list_size; mode_bin++)
  470. {
  471. uint64_t* mode_list = &mode_lists[list_size * mode_bin];
  472. if (mode_list[0] == 0) continue;
  473. mode_list[0] = 0;
  474. astc_encode(src, block_scores.data(), dst, mode_list, settings);
  475. memset(mode_list, 0, list_size * sizeof(uint64_t));
  476. }
  477. }