astc_decoder.comp 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269
  1. // SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #version 450
  4. #ifdef VULKAN
  5. #define BEGIN_PUSH_CONSTANTS layout(push_constant) uniform PushConstants {
  6. #define END_PUSH_CONSTANTS };
  7. #define UNIFORM(n)
  8. #define BINDING_INPUT_BUFFER 0
  9. #define BINDING_OUTPUT_IMAGE 1
  10. #else // ^^^ Vulkan ^^^ // vvv OpenGL vvv
  11. #define BEGIN_PUSH_CONSTANTS
  12. #define END_PUSH_CONSTANTS
  13. #define UNIFORM(n) layout(location = n) uniform
  14. #define BINDING_INPUT_BUFFER 0
  15. #define BINDING_OUTPUT_IMAGE 0
  16. #endif
  17. layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
  18. BEGIN_PUSH_CONSTANTS
  19. UNIFORM(1) uvec2 block_dims;
  20. UNIFORM(2) uint layer_stride;
  21. UNIFORM(3) uint block_size;
  22. UNIFORM(4) uint x_shift;
  23. UNIFORM(5) uint block_height;
  24. UNIFORM(6) uint block_height_mask;
  25. END_PUSH_CONSTANTS
  26. struct EncodingData {
  27. uint encoding;
  28. uint num_bits;
  29. uint bit_value;
  30. uint quint_trit_value;
  31. };
  32. struct TexelWeightParams {
  33. uvec2 size;
  34. uint max_weight;
  35. bool dual_plane;
  36. bool error_state;
  37. bool void_extent_ldr;
  38. bool void_extent_hdr;
  39. };
  40. layout(binding = BINDING_INPUT_BUFFER, std430) readonly buffer InputBufferU32 {
  41. uvec4 astc_data[];
  42. };
  43. layout(binding = BINDING_OUTPUT_IMAGE, rgba8) uniform writeonly image2DArray dest_image;
  44. const uint GOB_SIZE_X_SHIFT = 6;
  45. const uint GOB_SIZE_Y_SHIFT = 3;
  46. const uint GOB_SIZE_SHIFT = GOB_SIZE_X_SHIFT + GOB_SIZE_Y_SHIFT;
  47. const uint BYTES_PER_BLOCK_LOG2 = 4;
  48. const int JUST_BITS = 0;
  49. const int QUINT = 1;
  50. const int TRIT = 2;
  51. // ASTC Encodings data, sorted in ascending order based on their BitLength value
  52. // (see GetBitLength() function)
  53. EncodingData encoding_values[22] = EncodingData[](
  54. EncodingData(JUST_BITS, 0, 0, 0), EncodingData(JUST_BITS, 1, 0, 0), EncodingData(TRIT, 0, 0, 0),
  55. EncodingData(JUST_BITS, 2, 0, 0), EncodingData(QUINT, 0, 0, 0), EncodingData(TRIT, 1, 0, 0),
  56. EncodingData(JUST_BITS, 3, 0, 0), EncodingData(QUINT, 1, 0, 0), EncodingData(TRIT, 2, 0, 0),
  57. EncodingData(JUST_BITS, 4, 0, 0), EncodingData(QUINT, 2, 0, 0), EncodingData(TRIT, 3, 0, 0),
  58. EncodingData(JUST_BITS, 5, 0, 0), EncodingData(QUINT, 3, 0, 0), EncodingData(TRIT, 4, 0, 0),
  59. EncodingData(JUST_BITS, 6, 0, 0), EncodingData(QUINT, 4, 0, 0), EncodingData(TRIT, 5, 0, 0),
  60. EncodingData(JUST_BITS, 7, 0, 0), EncodingData(QUINT, 5, 0, 0), EncodingData(TRIT, 6, 0, 0),
  61. EncodingData(JUST_BITS, 8, 0, 0)
  62. );
  63. // The following constants are expanded variants of the Replicate()
  64. // function calls corresponding to the following arguments:
  65. // value: index into the generated table
  66. // num_bits: the after "REPLICATE" in the table name. i.e. 4 is num_bits in REPLICATE_4.
  67. // to_bit: the integer after "TO_"
  68. const uint REPLICATE_BIT_TO_7_TABLE[2] = uint[](0, 127);
  69. const uint REPLICATE_1_BIT_TO_9_TABLE[2] = uint[](0, 511);
  70. const uint REPLICATE_1_BIT_TO_8_TABLE[2] = uint[](0, 255);
  71. const uint REPLICATE_2_BIT_TO_8_TABLE[4] = uint[](0, 85, 170, 255);
  72. const uint REPLICATE_3_BIT_TO_8_TABLE[8] = uint[](0, 36, 73, 109, 146, 182, 219, 255);
  73. const uint REPLICATE_4_BIT_TO_8_TABLE[16] =
  74. uint[](0, 17, 34, 51, 68, 85, 102, 119, 136, 153, 170, 187, 204, 221, 238, 255);
  75. const uint REPLICATE_5_BIT_TO_8_TABLE[32] =
  76. uint[](0, 8, 16, 24, 33, 41, 49, 57, 66, 74, 82, 90, 99, 107, 115, 123, 132, 140, 148, 156, 165,
  77. 173, 181, 189, 198, 206, 214, 222, 231, 239, 247, 255);
  78. const uint REPLICATE_1_BIT_TO_6_TABLE[2] = uint[](0, 63);
  79. const uint REPLICATE_2_BIT_TO_6_TABLE[4] = uint[](0, 21, 42, 63);
  80. const uint REPLICATE_3_BIT_TO_6_TABLE[8] = uint[](0, 9, 18, 27, 36, 45, 54, 63);
  81. const uint REPLICATE_4_BIT_TO_6_TABLE[16] =
  82. uint[](0, 4, 8, 12, 17, 21, 25, 29, 34, 38, 42, 46, 51, 55, 59, 63);
  83. const uint REPLICATE_5_BIT_TO_6_TABLE[32] =
  84. uint[](0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 33, 35, 37, 39, 41, 43, 45,
  85. 47, 49, 51, 53, 55, 57, 59, 61, 63);
  86. const uint REPLICATE_6_BIT_TO_8_TABLE[64] =
  87. uint[](0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 65, 69, 73, 77, 81, 85, 89,
  88. 93, 97, 101, 105, 109, 113, 117, 121, 125, 130, 134, 138, 142, 146, 150, 154, 158, 162,
  89. 166, 170, 174, 178, 182, 186, 190, 195, 199, 203, 207, 211, 215, 219, 223, 227, 231, 235,
  90. 239, 243, 247, 251, 255);
  91. const uint REPLICATE_7_BIT_TO_8_TABLE[128] =
  92. uint[](0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44,
  93. 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88,
  94. 90, 92, 94, 96, 98, 100, 102, 104, 106, 108, 110, 112, 114, 116, 118, 120, 122, 124, 126,
  95. 129, 131, 133, 135, 137, 139, 141, 143, 145, 147, 149, 151, 153, 155, 157, 159, 161, 163,
  96. 165, 167, 169, 171, 173, 175, 177, 179, 181, 183, 185, 187, 189, 191, 193, 195, 197, 199,
  97. 201, 203, 205, 207, 209, 211, 213, 215, 217, 219, 221, 223, 225, 227, 229, 231, 233, 235,
  98. 237, 239, 241, 243, 245, 247, 249, 251, 253, 255);
  99. // Input ASTC texture globals
  100. uint current_index = 0;
  101. int bitsread = 0;
  102. int total_bitsread = 0;
  103. uvec4 local_buff;
  104. // Color data globals
  105. uvec4 color_endpoint_data;
  106. int color_bitsread = 0;
  107. // Four values, two endpoints, four maximum paritions
  108. uint color_values[32];
  109. int colvals_index = 0;
  110. // Weight data globals
  111. uvec4 texel_weight_data;
  112. int texel_bitsread = 0;
  113. bool texel_flag = false;
  114. // Global "vectors" to be pushed into when decoding
  115. EncodingData result_vector[144];
  116. int result_index = 0;
  117. EncodingData texel_vector[144];
  118. int texel_vector_index = 0;
  119. uint unquantized_texel_weights[2][144];
  120. uint SwizzleOffset(uvec2 pos) {
  121. uint x = pos.x;
  122. uint y = pos.y;
  123. return ((x % 64) / 32) * 256 + ((y % 8) / 2) * 64 + ((x % 32) / 16) * 32 +
  124. (y % 2) * 16 + (x % 16);
  125. }
  126. // Replicates low num_bits such that [(to_bit - 1):(to_bit - 1 - from_bit)]
  127. // is the same as [(num_bits - 1):0] and repeats all the way down.
  128. uint Replicate(uint val, uint num_bits, uint to_bit) {
  129. const uint v = val & uint((1 << num_bits) - 1);
  130. uint res = v;
  131. uint reslen = num_bits;
  132. while (reslen < to_bit) {
  133. uint comp = 0;
  134. if (num_bits > to_bit - reslen) {
  135. uint newshift = to_bit - reslen;
  136. comp = num_bits - newshift;
  137. num_bits = newshift;
  138. }
  139. res = uint(res << num_bits);
  140. res = uint(res | (v >> comp));
  141. reslen += num_bits;
  142. }
  143. return res;
  144. }
  145. uvec4 ReplicateByteTo16(uvec4 value) {
  146. return value * 0x101;
  147. }
  148. uint ReplicateBitTo7(uint value) {
  149. return REPLICATE_BIT_TO_7_TABLE[value];
  150. }
  151. uint ReplicateBitTo9(uint value) {
  152. return REPLICATE_1_BIT_TO_9_TABLE[value];
  153. }
  154. uint FastReplicate(uint value, uint num_bits, uint to_bit) {
  155. if (num_bits == 0) {
  156. return 0;
  157. }
  158. if (num_bits == to_bit) {
  159. return value;
  160. }
  161. if (to_bit == 6) {
  162. switch (num_bits) {
  163. case 1:
  164. return REPLICATE_1_BIT_TO_6_TABLE[value];
  165. case 2:
  166. return REPLICATE_2_BIT_TO_6_TABLE[value];
  167. case 3:
  168. return REPLICATE_3_BIT_TO_6_TABLE[value];
  169. case 4:
  170. return REPLICATE_4_BIT_TO_6_TABLE[value];
  171. case 5:
  172. return REPLICATE_5_BIT_TO_6_TABLE[value];
  173. default:
  174. break;
  175. }
  176. } else { /* if (to_bit == 8) */
  177. switch (num_bits) {
  178. case 1:
  179. return REPLICATE_1_BIT_TO_8_TABLE[value];
  180. case 2:
  181. return REPLICATE_2_BIT_TO_8_TABLE[value];
  182. case 3:
  183. return REPLICATE_3_BIT_TO_8_TABLE[value];
  184. case 4:
  185. return REPLICATE_4_BIT_TO_8_TABLE[value];
  186. case 5:
  187. return REPLICATE_5_BIT_TO_8_TABLE[value];
  188. case 6:
  189. return REPLICATE_6_BIT_TO_8_TABLE[value];
  190. case 7:
  191. return REPLICATE_7_BIT_TO_8_TABLE[value];
  192. default:
  193. break;
  194. }
  195. }
  196. return Replicate(value, num_bits, to_bit);
  197. }
  198. uint FastReplicateTo8(uint value, uint num_bits) {
  199. return FastReplicate(value, num_bits, 8);
  200. }
  201. uint FastReplicateTo6(uint value, uint num_bits) {
  202. return FastReplicate(value, num_bits, 6);
  203. }
  204. uint Div3Floor(uint v) {
  205. return (v * 0x5556) >> 16;
  206. }
  207. uint Div3Ceil(uint v) {
  208. return Div3Floor(v + 2);
  209. }
  210. uint Div5Floor(uint v) {
  211. return (v * 0x3334) >> 16;
  212. }
  213. uint Div5Ceil(uint v) {
  214. return Div5Floor(v + 4);
  215. }
  216. uint Hash52(uint p) {
  217. p ^= p >> 15;
  218. p -= p << 17;
  219. p += p << 7;
  220. p += p << 4;
  221. p ^= p >> 5;
  222. p += p << 16;
  223. p ^= p >> 7;
  224. p ^= p >> 3;
  225. p ^= p << 6;
  226. p ^= p >> 17;
  227. return p;
  228. }
  229. uint Select2DPartition(uint seed, uint x, uint y, uint partition_count, bool small_block) {
  230. if (small_block) {
  231. x <<= 1;
  232. y <<= 1;
  233. }
  234. seed += (partition_count - 1) * 1024;
  235. uint rnum = Hash52(uint(seed));
  236. uint seed1 = uint(rnum & 0xF);
  237. uint seed2 = uint((rnum >> 4) & 0xF);
  238. uint seed3 = uint((rnum >> 8) & 0xF);
  239. uint seed4 = uint((rnum >> 12) & 0xF);
  240. uint seed5 = uint((rnum >> 16) & 0xF);
  241. uint seed6 = uint((rnum >> 20) & 0xF);
  242. uint seed7 = uint((rnum >> 24) & 0xF);
  243. uint seed8 = uint((rnum >> 28) & 0xF);
  244. seed1 = (seed1 * seed1);
  245. seed2 = (seed2 * seed2);
  246. seed3 = (seed3 * seed3);
  247. seed4 = (seed4 * seed4);
  248. seed5 = (seed5 * seed5);
  249. seed6 = (seed6 * seed6);
  250. seed7 = (seed7 * seed7);
  251. seed8 = (seed8 * seed8);
  252. uint sh1, sh2;
  253. if ((seed & 1) > 0) {
  254. sh1 = (seed & 2) > 0 ? 4 : 5;
  255. sh2 = (partition_count == 3) ? 6 : 5;
  256. } else {
  257. sh1 = (partition_count == 3) ? 6 : 5;
  258. sh2 = (seed & 2) > 0 ? 4 : 5;
  259. }
  260. seed1 >>= sh1;
  261. seed2 >>= sh2;
  262. seed3 >>= sh1;
  263. seed4 >>= sh2;
  264. seed5 >>= sh1;
  265. seed6 >>= sh2;
  266. seed7 >>= sh1;
  267. seed8 >>= sh2;
  268. uint a = seed1 * x + seed2 * y + (rnum >> 14);
  269. uint b = seed3 * x + seed4 * y + (rnum >> 10);
  270. uint c = seed5 * x + seed6 * y + (rnum >> 6);
  271. uint d = seed7 * x + seed8 * y + (rnum >> 2);
  272. a &= 0x3F;
  273. b &= 0x3F;
  274. c &= 0x3F;
  275. d &= 0x3F;
  276. if (partition_count < 4) {
  277. d = 0;
  278. }
  279. if (partition_count < 3) {
  280. c = 0;
  281. }
  282. if (a >= b && a >= c && a >= d) {
  283. return 0;
  284. } else if (b >= c && b >= d) {
  285. return 1;
  286. } else if (c >= d) {
  287. return 2;
  288. } else {
  289. return 3;
  290. }
  291. }
  292. uint ExtractBits(uvec4 payload, int offset, int bits) {
  293. if (bits <= 0) {
  294. return 0;
  295. }
  296. int last_offset = offset + bits - 1;
  297. int shifted_offset = offset >> 5;
  298. if ((last_offset >> 5) == shifted_offset) {
  299. return bitfieldExtract(payload[shifted_offset], offset & 31, bits);
  300. }
  301. int first_bits = 32 - (offset & 31);
  302. int result_first = int(bitfieldExtract(payload[shifted_offset], offset & 31, first_bits));
  303. int result_second = int(bitfieldExtract(payload[shifted_offset + 1], 0, bits - first_bits));
  304. return result_first | (result_second << first_bits);
  305. }
  306. uint StreamBits(uint num_bits) {
  307. int int_bits = int(num_bits);
  308. uint ret = ExtractBits(local_buff, total_bitsread, int_bits);
  309. total_bitsread += int_bits;
  310. return ret;
  311. }
  312. uint StreamColorBits(uint num_bits) {
  313. uint ret = 0;
  314. int int_bits = int(num_bits);
  315. if (texel_flag) {
  316. ret = ExtractBits(texel_weight_data, texel_bitsread, int_bits);
  317. texel_bitsread += int_bits;
  318. } else {
  319. ret = ExtractBits(color_endpoint_data, color_bitsread, int_bits);
  320. color_bitsread += int_bits;
  321. }
  322. return ret;
  323. }
  324. void ResultEmplaceBack(EncodingData val) {
  325. if (texel_flag) {
  326. texel_vector[texel_vector_index] = val;
  327. ++texel_vector_index;
  328. } else {
  329. result_vector[result_index] = val;
  330. ++result_index;
  331. }
  332. }
  333. // Returns the number of bits required to encode n_vals values.
  334. uint GetBitLength(uint n_vals, uint encoding_index) {
  335. uint total_bits = encoding_values[encoding_index].num_bits * n_vals;
  336. if (encoding_values[encoding_index].encoding == TRIT) {
  337. total_bits += Div5Ceil(n_vals * 8);
  338. } else if (encoding_values[encoding_index].encoding == QUINT) {
  339. total_bits += Div3Ceil(n_vals * 7);
  340. }
  341. return total_bits;
  342. }
  343. uint GetNumWeightValues(uvec2 size, bool dual_plane) {
  344. uint n_vals = size.x * size.y;
  345. if (dual_plane) {
  346. n_vals *= 2;
  347. }
  348. return n_vals;
  349. }
  350. uint GetPackedBitSize(uvec2 size, bool dual_plane, uint max_weight) {
  351. uint n_vals = GetNumWeightValues(size, dual_plane);
  352. return GetBitLength(n_vals, max_weight);
  353. }
  354. uint BitsBracket(uint bits, uint pos) {
  355. return ((bits >> pos) & 1);
  356. }
  357. uint BitsOp(uint bits, uint start, uint end) {
  358. if (start == end) {
  359. return BitsBracket(bits, start);
  360. } else if (start > end) {
  361. uint t = start;
  362. start = end;
  363. end = t;
  364. }
  365. uint mask = (1 << (end - start + 1)) - 1;
  366. return ((bits >> start) & mask);
  367. }
  368. void DecodeQuintBlock(uint num_bits) {
  369. uint m[3];
  370. uint q[3];
  371. uint Q;
  372. m[0] = StreamColorBits(num_bits);
  373. Q = StreamColorBits(3);
  374. m[1] = StreamColorBits(num_bits);
  375. Q |= StreamColorBits(2) << 3;
  376. m[2] = StreamColorBits(num_bits);
  377. Q |= StreamColorBits(2) << 5;
  378. if (BitsOp(Q, 1, 2) == 3 && BitsOp(Q, 5, 6) == 0) {
  379. q[0] = 4;
  380. q[1] = 4;
  381. q[2] = (BitsBracket(Q, 0) << 2) | ((BitsBracket(Q, 4) & ~BitsBracket(Q, 0)) << 1) |
  382. (BitsBracket(Q, 3) & ~BitsBracket(Q, 0));
  383. } else {
  384. uint C = 0;
  385. if (BitsOp(Q, 1, 2) == 3) {
  386. q[2] = 4;
  387. C = (BitsOp(Q, 3, 4) << 3) | ((~BitsOp(Q, 5, 6) & 3) << 1) | BitsBracket(Q, 0);
  388. } else {
  389. q[2] = BitsOp(Q, 5, 6);
  390. C = BitsOp(Q, 0, 4);
  391. }
  392. if (BitsOp(C, 0, 2) == 5) {
  393. q[1] = 4;
  394. q[0] = BitsOp(C, 3, 4);
  395. } else {
  396. q[1] = BitsOp(C, 3, 4);
  397. q[0] = BitsOp(C, 0, 2);
  398. }
  399. }
  400. for (uint i = 0; i < 3; i++) {
  401. EncodingData val;
  402. val.encoding = QUINT;
  403. val.num_bits = num_bits;
  404. val.bit_value = m[i];
  405. val.quint_trit_value = q[i];
  406. ResultEmplaceBack(val);
  407. }
  408. }
  409. void DecodeTritBlock(uint num_bits) {
  410. uint m[5];
  411. uint t[5];
  412. uint T;
  413. m[0] = StreamColorBits(num_bits);
  414. T = StreamColorBits(2);
  415. m[1] = StreamColorBits(num_bits);
  416. T |= StreamColorBits(2) << 2;
  417. m[2] = StreamColorBits(num_bits);
  418. T |= StreamColorBits(1) << 4;
  419. m[3] = StreamColorBits(num_bits);
  420. T |= StreamColorBits(2) << 5;
  421. m[4] = StreamColorBits(num_bits);
  422. T |= StreamColorBits(1) << 7;
  423. uint C = 0;
  424. if (BitsOp(T, 2, 4) == 7) {
  425. C = (BitsOp(T, 5, 7) << 2) | BitsOp(T, 0, 1);
  426. t[4] = 2;
  427. t[3] = 2;
  428. } else {
  429. C = BitsOp(T, 0, 4);
  430. if (BitsOp(T, 5, 6) == 3) {
  431. t[4] = 2;
  432. t[3] = BitsBracket(T, 7);
  433. } else {
  434. t[4] = BitsBracket(T, 7);
  435. t[3] = BitsOp(T, 5, 6);
  436. }
  437. }
  438. if (BitsOp(C, 0, 1) == 3) {
  439. t[2] = 2;
  440. t[1] = BitsBracket(C, 4);
  441. t[0] = (BitsBracket(C, 3) << 1) | (BitsBracket(C, 2) & ~BitsBracket(C, 3));
  442. } else if (BitsOp(C, 2, 3) == 3) {
  443. t[2] = 2;
  444. t[1] = 2;
  445. t[0] = BitsOp(C, 0, 1);
  446. } else {
  447. t[2] = BitsBracket(C, 4);
  448. t[1] = BitsOp(C, 2, 3);
  449. t[0] = (BitsBracket(C, 1) << 1) | (BitsBracket(C, 0) & ~BitsBracket(C, 1));
  450. }
  451. for (uint i = 0; i < 5; i++) {
  452. EncodingData val;
  453. val.encoding = TRIT;
  454. val.num_bits = num_bits;
  455. val.bit_value = m[i];
  456. val.quint_trit_value = t[i];
  457. ResultEmplaceBack(val);
  458. }
  459. }
  460. void DecodeIntegerSequence(uint max_range, uint num_values) {
  461. EncodingData val = encoding_values[max_range];
  462. uint vals_decoded = 0;
  463. while (vals_decoded < num_values) {
  464. switch (val.encoding) {
  465. case QUINT:
  466. DecodeQuintBlock(val.num_bits);
  467. vals_decoded += 3;
  468. break;
  469. case TRIT:
  470. DecodeTritBlock(val.num_bits);
  471. vals_decoded += 5;
  472. break;
  473. case JUST_BITS:
  474. val.bit_value = StreamColorBits(val.num_bits);
  475. ResultEmplaceBack(val);
  476. vals_decoded++;
  477. break;
  478. }
  479. }
  480. }
  481. void DecodeColorValues(uvec4 modes, uint num_partitions, uint color_data_bits) {
  482. uint num_values = 0;
  483. for (uint i = 0; i < num_partitions; i++) {
  484. num_values += ((modes[i] >> 2) + 1) << 1;
  485. }
  486. // Find the largest encoding that's within color_data_bits
  487. // TODO(ameerj): profile with binary search
  488. int range = 0;
  489. while (++range < encoding_values.length()) {
  490. uint bit_length = GetBitLength(num_values, range);
  491. if (bit_length > color_data_bits) {
  492. break;
  493. }
  494. }
  495. DecodeIntegerSequence(range - 1, num_values);
  496. uint out_index = 0;
  497. for (int itr = 0; itr < result_index; ++itr) {
  498. if (out_index >= num_values) {
  499. break;
  500. }
  501. EncodingData val = result_vector[itr];
  502. uint bitlen = val.num_bits;
  503. uint bitval = val.bit_value;
  504. uint A = 0, B = 0, C = 0, D = 0;
  505. A = ReplicateBitTo9((bitval & 1));
  506. switch (val.encoding) {
  507. case JUST_BITS:
  508. color_values[out_index++] = FastReplicateTo8(bitval, bitlen);
  509. break;
  510. case TRIT: {
  511. D = val.quint_trit_value;
  512. switch (bitlen) {
  513. case 1:
  514. C = 204;
  515. break;
  516. case 2: {
  517. C = 93;
  518. uint b = (bitval >> 1) & 1;
  519. B = (b << 8) | (b << 4) | (b << 2) | (b << 1);
  520. break;
  521. }
  522. case 3: {
  523. C = 44;
  524. uint cb = (bitval >> 1) & 3;
  525. B = (cb << 7) | (cb << 2) | cb;
  526. break;
  527. }
  528. case 4: {
  529. C = 22;
  530. uint dcb = (bitval >> 1) & 7;
  531. B = (dcb << 6) | dcb;
  532. break;
  533. }
  534. case 5: {
  535. C = 11;
  536. uint edcb = (bitval >> 1) & 0xF;
  537. B = (edcb << 5) | (edcb >> 2);
  538. break;
  539. }
  540. case 6: {
  541. C = 5;
  542. uint fedcb = (bitval >> 1) & 0x1F;
  543. B = (fedcb << 4) | (fedcb >> 4);
  544. break;
  545. }
  546. }
  547. break;
  548. }
  549. case QUINT: {
  550. D = val.quint_trit_value;
  551. switch (bitlen) {
  552. case 1:
  553. C = 113;
  554. break;
  555. case 2: {
  556. C = 54;
  557. uint b = (bitval >> 1) & 1;
  558. B = (b << 8) | (b << 3) | (b << 2);
  559. break;
  560. }
  561. case 3: {
  562. C = 26;
  563. uint cb = (bitval >> 1) & 3;
  564. B = (cb << 7) | (cb << 1) | (cb >> 1);
  565. break;
  566. }
  567. case 4: {
  568. C = 13;
  569. uint dcb = (bitval >> 1) & 7;
  570. B = (dcb << 6) | (dcb >> 1);
  571. break;
  572. }
  573. case 5: {
  574. C = 6;
  575. uint edcb = (bitval >> 1) & 0xF;
  576. B = (edcb << 5) | (edcb >> 3);
  577. break;
  578. }
  579. }
  580. break;
  581. }
  582. }
  583. if (val.encoding != JUST_BITS) {
  584. uint T = (D * C) + B;
  585. T ^= A;
  586. T = (A & 0x80) | (T >> 2);
  587. color_values[out_index++] = T;
  588. }
  589. }
  590. }
  591. ivec2 BitTransferSigned(int a, int b) {
  592. ivec2 transferred;
  593. transferred.y = b >> 1;
  594. transferred.y |= a & 0x80;
  595. transferred.x = a >> 1;
  596. transferred.x &= 0x3F;
  597. if ((transferred.x & 0x20) > 0) {
  598. transferred.x -= 0x40;
  599. }
  600. return transferred;
  601. }
  602. uvec4 ClampByte(ivec4 color) {
  603. for (uint i = 0; i < 4; ++i) {
  604. color[i] = (color[i] < 0) ? 0 : ((color[i] > 255) ? 255 : color[i]);
  605. }
  606. return uvec4(color);
  607. }
  608. ivec4 BlueContract(int a, int r, int g, int b) {
  609. return ivec4(a, (r + b) >> 1, (g + b) >> 1, b);
  610. }
  611. void ComputeEndpoints(out uvec4 ep1, out uvec4 ep2, uint color_endpoint_mode) {
  612. #define READ_UINT_VALUES(N) \
  613. uint v[N]; \
  614. for (uint i = 0; i < N; i++) { \
  615. v[i] = color_values[colvals_index++]; \
  616. }
  617. #define READ_INT_VALUES(N) \
  618. int v[N]; \
  619. for (uint i = 0; i < N; i++) { \
  620. v[i] = int(color_values[colvals_index++]); \
  621. }
  622. switch (color_endpoint_mode) {
  623. case 0: {
  624. READ_UINT_VALUES(2)
  625. ep1 = uvec4(0xFF, v[0], v[0], v[0]);
  626. ep2 = uvec4(0xFF, v[1], v[1], v[1]);
  627. break;
  628. }
  629. case 1: {
  630. READ_UINT_VALUES(2)
  631. uint L0 = (v[0] >> 2) | (v[1] & 0xC0);
  632. uint L1 = min(L0 + (v[1] & 0x3F), 0xFFU);
  633. ep1 = uvec4(0xFF, L0, L0, L0);
  634. ep2 = uvec4(0xFF, L1, L1, L1);
  635. break;
  636. }
  637. case 4: {
  638. READ_UINT_VALUES(4)
  639. ep1 = uvec4(v[2], v[0], v[0], v[0]);
  640. ep2 = uvec4(v[3], v[1], v[1], v[1]);
  641. break;
  642. }
  643. case 5: {
  644. READ_INT_VALUES(4)
  645. ivec2 transferred = BitTransferSigned(v[1], v[0]);
  646. v[1] = transferred.x;
  647. v[0] = transferred.y;
  648. transferred = BitTransferSigned(v[3], v[2]);
  649. v[3] = transferred.x;
  650. v[2] = transferred.y;
  651. ep1 = ClampByte(ivec4(v[2], v[0], v[0], v[0]));
  652. ep2 = ClampByte(ivec4(v[2] + v[3], v[0] + v[1], v[0] + v[1], v[0] + v[1]));
  653. break;
  654. }
  655. case 6: {
  656. READ_UINT_VALUES(4)
  657. ep1 = uvec4(0xFF, (v[0] * v[3]) >> 8, (v[1] * v[3]) >> 8, (v[2] * v[3]) >> 8);
  658. ep2 = uvec4(0xFF, v[0], v[1], v[2]);
  659. break;
  660. }
  661. case 8: {
  662. READ_UINT_VALUES(6)
  663. if ((v[1] + v[3] + v[5]) >= (v[0] + v[2] + v[4])) {
  664. ep1 = uvec4(0xFF, v[0], v[2], v[4]);
  665. ep2 = uvec4(0xFF, v[1], v[3], v[5]);
  666. } else {
  667. ep1 = uvec4(BlueContract(0xFF, int(v[1]), int(v[3]), int(v[5])));
  668. ep2 = uvec4(BlueContract(0xFF, int(v[0]), int(v[2]), int(v[4])));
  669. }
  670. break;
  671. }
  672. case 9: {
  673. READ_INT_VALUES(6)
  674. ivec2 transferred = BitTransferSigned(v[1], v[0]);
  675. v[1] = transferred.x;
  676. v[0] = transferred.y;
  677. transferred = BitTransferSigned(v[3], v[2]);
  678. v[3] = transferred.x;
  679. v[2] = transferred.y;
  680. transferred = BitTransferSigned(v[5], v[4]);
  681. v[5] = transferred.x;
  682. v[4] = transferred.y;
  683. if ((v[1] + v[3] + v[5]) >= 0) {
  684. ep1 = ClampByte(ivec4(0xFF, v[0], v[2], v[4]));
  685. ep2 = ClampByte(ivec4(0xFF, v[0] + v[1], v[2] + v[3], v[4] + v[5]));
  686. } else {
  687. ep1 = ClampByte(BlueContract(0xFF, v[0] + v[1], v[2] + v[3], v[4] + v[5]));
  688. ep2 = ClampByte(BlueContract(0xFF, v[0], v[2], v[4]));
  689. }
  690. break;
  691. }
  692. case 10: {
  693. READ_UINT_VALUES(6)
  694. ep1 = uvec4(v[4], (v[0] * v[3]) >> 8, (v[1] * v[3]) >> 8, (v[2] * v[3]) >> 8);
  695. ep2 = uvec4(v[5], v[0], v[1], v[2]);
  696. break;
  697. }
  698. case 12: {
  699. READ_UINT_VALUES(8)
  700. if ((v[1] + v[3] + v[5]) >= (v[0] + v[2] + v[4])) {
  701. ep1 = uvec4(v[6], v[0], v[2], v[4]);
  702. ep2 = uvec4(v[7], v[1], v[3], v[5]);
  703. } else {
  704. ep1 = uvec4(BlueContract(int(v[7]), int(v[1]), int(v[3]), int(v[5])));
  705. ep2 = uvec4(BlueContract(int(v[6]), int(v[0]), int(v[2]), int(v[4])));
  706. }
  707. break;
  708. }
  709. case 13: {
  710. READ_INT_VALUES(8)
  711. ivec2 transferred = BitTransferSigned(v[1], v[0]);
  712. v[1] = transferred.x;
  713. v[0] = transferred.y;
  714. transferred = BitTransferSigned(v[3], v[2]);
  715. v[3] = transferred.x;
  716. v[2] = transferred.y;
  717. transferred = BitTransferSigned(v[5], v[4]);
  718. v[5] = transferred.x;
  719. v[4] = transferred.y;
  720. transferred = BitTransferSigned(v[7], v[6]);
  721. v[7] = transferred.x;
  722. v[6] = transferred.y;
  723. if ((v[1] + v[3] + v[5]) >= 0) {
  724. ep1 = ClampByte(ivec4(v[6], v[0], v[2], v[4]));
  725. ep2 = ClampByte(ivec4(v[7] + v[6], v[0] + v[1], v[2] + v[3], v[4] + v[5]));
  726. } else {
  727. ep1 = ClampByte(BlueContract(v[6] + v[7], v[0] + v[1], v[2] + v[3], v[4] + v[5]));
  728. ep2 = ClampByte(BlueContract(v[6], v[0], v[2], v[4]));
  729. }
  730. break;
  731. }
  732. default: {
  733. // HDR mode, or more likely a bug computing the color_endpoint_mode
  734. ep1 = uvec4(0xFF, 0xFF, 0, 0);
  735. ep2 = uvec4(0xFF, 0xFF, 0, 0);
  736. break;
  737. }
  738. }
  739. #undef READ_UINT_VALUES
  740. #undef READ_INT_VALUES
  741. }
  742. uint UnquantizeTexelWeight(EncodingData val) {
  743. uint bitval = val.bit_value;
  744. uint bitlen = val.num_bits;
  745. uint A = ReplicateBitTo7((bitval & 1));
  746. uint B = 0, C = 0, D = 0;
  747. uint result = 0;
  748. switch (val.encoding) {
  749. case JUST_BITS:
  750. result = FastReplicateTo6(bitval, bitlen);
  751. break;
  752. case TRIT: {
  753. D = val.quint_trit_value;
  754. switch (bitlen) {
  755. case 0: {
  756. uint results[3] = {0, 32, 63};
  757. result = results[D];
  758. break;
  759. }
  760. case 1: {
  761. C = 50;
  762. break;
  763. }
  764. case 2: {
  765. C = 23;
  766. uint b = (bitval >> 1) & 1;
  767. B = (b << 6) | (b << 2) | b;
  768. break;
  769. }
  770. case 3: {
  771. C = 11;
  772. uint cb = (bitval >> 1) & 3;
  773. B = (cb << 5) | cb;
  774. break;
  775. }
  776. default:
  777. break;
  778. }
  779. break;
  780. }
  781. case QUINT: {
  782. D = val.quint_trit_value;
  783. switch (bitlen) {
  784. case 0: {
  785. uint results[5] = {0, 16, 32, 47, 63};
  786. result = results[D];
  787. break;
  788. }
  789. case 1: {
  790. C = 28;
  791. break;
  792. }
  793. case 2: {
  794. C = 13;
  795. uint b = (bitval >> 1) & 1;
  796. B = (b << 6) | (b << 1);
  797. break;
  798. }
  799. }
  800. break;
  801. }
  802. }
  803. if (val.encoding != JUST_BITS && bitlen > 0) {
  804. result = D * C + B;
  805. result ^= A;
  806. result = (A & 0x20) | (result >> 2);
  807. }
  808. if (result > 32) {
  809. result += 1;
  810. }
  811. return result;
  812. }
  813. void UnquantizeTexelWeights(bool dual_plane, uvec2 size) {
  814. uint weight_idx = 0;
  815. uint unquantized[2][144];
  816. uint area = size.x * size.y;
  817. for (uint itr = 0; itr < texel_vector_index; itr++) {
  818. unquantized[0][weight_idx] = UnquantizeTexelWeight(texel_vector[itr]);
  819. if (dual_plane) {
  820. ++itr;
  821. unquantized[1][weight_idx] = UnquantizeTexelWeight(texel_vector[itr]);
  822. if (itr == texel_vector_index) {
  823. break;
  824. }
  825. }
  826. if (++weight_idx >= (area))
  827. break;
  828. }
  829. const uint Ds = uint((block_dims.x * 0.5f + 1024) / (block_dims.x - 1));
  830. const uint Dt = uint((block_dims.y * 0.5f + 1024) / (block_dims.y - 1));
  831. const uint k_plane_scale = dual_plane ? 2 : 1;
  832. for (uint plane = 0; plane < k_plane_scale; plane++) {
  833. for (uint t = 0; t < block_dims.y; t++) {
  834. for (uint s = 0; s < block_dims.x; s++) {
  835. uint cs = Ds * s;
  836. uint ct = Dt * t;
  837. uint gs = (cs * (size.x - 1) + 32) >> 6;
  838. uint gt = (ct * (size.y - 1) + 32) >> 6;
  839. uint js = gs >> 4;
  840. uint fs = gs & 0xF;
  841. uint jt = gt >> 4;
  842. uint ft = gt & 0x0F;
  843. uint w11 = (fs * ft + 8) >> 4;
  844. uint w10 = ft - w11;
  845. uint w01 = fs - w11;
  846. uint w00 = 16 - fs - ft + w11;
  847. uvec4 w = uvec4(w00, w01, w10, w11);
  848. uint v0 = jt * size.x + js;
  849. uvec4 p = uvec4(0);
  850. if (v0 < area) {
  851. p.x = unquantized[plane][v0];
  852. }
  853. if ((v0 + 1) < (area)) {
  854. p.y = unquantized[plane][v0 + 1];
  855. }
  856. if ((v0 + size.x) < (area)) {
  857. p.z = unquantized[plane][(v0 + size.x)];
  858. }
  859. if ((v0 + size.x + 1) < (area)) {
  860. p.w = unquantized[plane][(v0 + size.x + 1)];
  861. }
  862. unquantized_texel_weights[plane][t * block_dims.x + s] = (uint(dot(p, w)) + 8) >> 4;
  863. }
  864. }
  865. }
  866. }
  867. int FindLayout(uint mode) {
  868. if ((mode & 3) != 0) {
  869. if ((mode & 8) != 0) {
  870. if ((mode & 4) != 0) {
  871. if ((mode & 0x100) != 0) {
  872. return 4;
  873. }
  874. return 3;
  875. }
  876. return 2;
  877. }
  878. if ((mode & 4) != 0) {
  879. return 1;
  880. }
  881. return 0;
  882. }
  883. if ((mode & 0x100) != 0) {
  884. if ((mode & 0x80) != 0) {
  885. if ((mode & 0x20) != 0) {
  886. return 8;
  887. }
  888. return 7;
  889. }
  890. return 9;
  891. }
  892. if ((mode & 0x80) != 0) {
  893. return 6;
  894. }
  895. return 5;
  896. }
  897. TexelWeightParams DecodeBlockInfo() {
  898. TexelWeightParams params = TexelWeightParams(uvec2(0), 0, false, false, false, false);
  899. uint mode = StreamBits(11);
  900. if ((mode & 0x1ff) == 0x1fc) {
  901. if ((mode & 0x200) != 0) {
  902. params.void_extent_hdr = true;
  903. } else {
  904. params.void_extent_ldr = true;
  905. }
  906. if ((mode & 0x400) == 0 || StreamBits(1) == 0) {
  907. params.error_state = true;
  908. }
  909. return params;
  910. }
  911. if ((mode & 0xf) == 0) {
  912. params.error_state = true;
  913. return params;
  914. }
  915. if ((mode & 3) == 0 && (mode & 0x1c0) == 0x1c0) {
  916. params.error_state = true;
  917. return params;
  918. }
  919. uint A, B;
  920. uint mode_layout = FindLayout(mode);
  921. switch (mode_layout) {
  922. case 0:
  923. A = (mode >> 5) & 0x3;
  924. B = (mode >> 7) & 0x3;
  925. params.size = uvec2(B + 4, A + 2);
  926. break;
  927. case 1:
  928. A = (mode >> 5) & 0x3;
  929. B = (mode >> 7) & 0x3;
  930. params.size = uvec2(B + 8, A + 2);
  931. break;
  932. case 2:
  933. A = (mode >> 5) & 0x3;
  934. B = (mode >> 7) & 0x3;
  935. params.size = uvec2(A + 2, B + 8);
  936. break;
  937. case 3:
  938. A = (mode >> 5) & 0x3;
  939. B = (mode >> 7) & 0x1;
  940. params.size = uvec2(A + 2, B + 6);
  941. break;
  942. case 4:
  943. A = (mode >> 5) & 0x3;
  944. B = (mode >> 7) & 0x1;
  945. params.size = uvec2(B + 2, A + 2);
  946. break;
  947. case 5:
  948. A = (mode >> 5) & 0x3;
  949. params.size = uvec2(12, A + 2);
  950. break;
  951. case 6:
  952. A = (mode >> 5) & 0x3;
  953. params.size = uvec2(A + 2, 12);
  954. break;
  955. case 7:
  956. params.size = uvec2(6, 10);
  957. break;
  958. case 8:
  959. params.size = uvec2(10, 6);
  960. break;
  961. case 9:
  962. A = (mode >> 5) & 0x3;
  963. B = (mode >> 9) & 0x3;
  964. params.size = uvec2(A + 6, B + 6);
  965. break;
  966. default:
  967. params.error_state = true;
  968. break;
  969. }
  970. params.dual_plane = (mode_layout != 9) && ((mode & 0x400) != 0);
  971. uint weight_index = (mode & 0x10) != 0 ? 1 : 0;
  972. if (mode_layout < 5) {
  973. weight_index |= (mode & 0x3) << 1;
  974. } else {
  975. weight_index |= (mode & 0xc) >> 1;
  976. }
  977. weight_index -= 2;
  978. if ((mode_layout != 9) && ((mode & 0x200) != 0)) {
  979. const int max_weights[6] = int[6](7, 8, 9, 10, 11, 12);
  980. params.max_weight = max_weights[weight_index];
  981. } else {
  982. const int max_weights[6] = int[6](1, 2, 3, 4, 5, 6);
  983. params.max_weight = max_weights[weight_index];
  984. }
  985. return params;
  986. }
  987. void FillError(ivec3 coord) {
  988. for (uint j = 0; j < block_dims.y; j++) {
  989. for (uint i = 0; i < block_dims.x; i++) {
  990. imageStore(dest_image, coord + ivec3(i, j, 0), vec4(0.0, 0.0, 0.0, 0.0));
  991. }
  992. }
  993. }
  994. void FillVoidExtentLDR(ivec3 coord) {
  995. StreamBits(52);
  996. uint r_u = StreamBits(16);
  997. uint g_u = StreamBits(16);
  998. uint b_u = StreamBits(16);
  999. uint a_u = StreamBits(16);
  1000. float a = float(a_u) / 65535.0f;
  1001. float r = float(r_u) / 65535.0f;
  1002. float g = float(g_u) / 65535.0f;
  1003. float b = float(b_u) / 65535.0f;
  1004. for (uint j = 0; j < block_dims.y; j++) {
  1005. for (uint i = 0; i < block_dims.x; i++) {
  1006. imageStore(dest_image, coord + ivec3(i, j, 0), vec4(r, g, b, a));
  1007. }
  1008. }
  1009. }
  1010. void DecompressBlock(ivec3 coord) {
  1011. TexelWeightParams params = DecodeBlockInfo();
  1012. if (params.error_state) {
  1013. FillError(coord);
  1014. return;
  1015. }
  1016. if (params.void_extent_hdr) {
  1017. FillError(coord);
  1018. return;
  1019. }
  1020. if (params.void_extent_ldr) {
  1021. FillVoidExtentLDR(coord);
  1022. return;
  1023. }
  1024. if ((params.size.x > block_dims.x) || (params.size.y > block_dims.y)) {
  1025. FillError(coord);
  1026. return;
  1027. }
  1028. uint num_partitions = StreamBits(2) + 1;
  1029. if (num_partitions > 4 || (num_partitions == 4 && params.dual_plane)) {
  1030. FillError(coord);
  1031. return;
  1032. }
  1033. int plane_index = -1;
  1034. uint partition_index = 1;
  1035. uvec4 color_endpoint_mode = uvec4(0);
  1036. uint ced_pointer = 0;
  1037. uint base_cem = 0;
  1038. if (num_partitions == 1) {
  1039. color_endpoint_mode.x = StreamBits(4);
  1040. partition_index = 0;
  1041. } else {
  1042. partition_index = StreamBits(10);
  1043. base_cem = StreamBits(6);
  1044. }
  1045. uint base_mode = base_cem & 3;
  1046. uint weight_bits = GetPackedBitSize(params.size, params.dual_plane, params.max_weight);
  1047. uint remaining_bits = 128 - weight_bits - total_bitsread;
  1048. uint extra_cem_bits = 0;
  1049. if (base_mode > 0) {
  1050. switch (num_partitions) {
  1051. case 2:
  1052. extra_cem_bits += 2;
  1053. break;
  1054. case 3:
  1055. extra_cem_bits += 5;
  1056. break;
  1057. case 4:
  1058. extra_cem_bits += 8;
  1059. break;
  1060. default:
  1061. return;
  1062. }
  1063. }
  1064. remaining_bits -= extra_cem_bits;
  1065. uint plane_selector_bits = 0;
  1066. if (params.dual_plane) {
  1067. plane_selector_bits = 2;
  1068. }
  1069. remaining_bits -= plane_selector_bits;
  1070. if (remaining_bits > 128) {
  1071. // Bad data, more remaining bits than 4 bytes
  1072. // return early
  1073. return;
  1074. }
  1075. // Read color data...
  1076. uint color_data_bits = remaining_bits;
  1077. while (remaining_bits > 0) {
  1078. int nb = int(min(remaining_bits, 32U));
  1079. uint b = StreamBits(nb);
  1080. color_endpoint_data[ced_pointer] = uint(bitfieldExtract(b, 0, nb));
  1081. ++ced_pointer;
  1082. remaining_bits -= nb;
  1083. }
  1084. plane_index = int(StreamBits(plane_selector_bits));
  1085. if (base_mode > 0) {
  1086. uint extra_cem = StreamBits(extra_cem_bits);
  1087. uint cem = (extra_cem << 6) | base_cem;
  1088. cem >>= 2;
  1089. uvec4 C = uvec4(0);
  1090. for (uint i = 0; i < num_partitions; i++) {
  1091. C[i] = (cem & 1);
  1092. cem >>= 1;
  1093. }
  1094. uvec4 M = uvec4(0);
  1095. for (uint i = 0; i < num_partitions; i++) {
  1096. M[i] = cem & 3;
  1097. cem >>= 2;
  1098. }
  1099. for (uint i = 0; i < num_partitions; i++) {
  1100. color_endpoint_mode[i] = base_mode;
  1101. if (C[i] == 0) {
  1102. --color_endpoint_mode[i];
  1103. }
  1104. color_endpoint_mode[i] <<= 2;
  1105. color_endpoint_mode[i] |= M[i];
  1106. }
  1107. } else if (num_partitions > 1) {
  1108. uint cem = base_cem >> 2;
  1109. for (uint i = 0; i < num_partitions; i++) {
  1110. color_endpoint_mode[i] = cem;
  1111. }
  1112. }
  1113. DecodeColorValues(color_endpoint_mode, num_partitions, color_data_bits);
  1114. uvec4 endpoints[4][2];
  1115. for (uint i = 0; i < num_partitions; i++) {
  1116. ComputeEndpoints(endpoints[i][0], endpoints[i][1], color_endpoint_mode[i]);
  1117. }
  1118. texel_weight_data = local_buff;
  1119. texel_weight_data = bitfieldReverse(texel_weight_data).wzyx;
  1120. uint clear_byte_start =
  1121. (GetPackedBitSize(params.size, params.dual_plane, params.max_weight) >> 3) + 1;
  1122. uint byte_insert = ExtractBits(texel_weight_data, int(clear_byte_start - 1) * 8, 8) &
  1123. uint(
  1124. ((1 << (GetPackedBitSize(params.size, params.dual_plane, params.max_weight) % 8)) - 1));
  1125. uint vec_index = (clear_byte_start - 1) >> 2;
  1126. texel_weight_data[vec_index] =
  1127. bitfieldInsert(texel_weight_data[vec_index], byte_insert, int((clear_byte_start - 1) % 4) * 8, 8);
  1128. for (uint i = clear_byte_start; i < 16; ++i) {
  1129. uint idx = i >> 2;
  1130. texel_weight_data[idx] = bitfieldInsert(texel_weight_data[idx], 0, int(i % 4) * 8, 8);
  1131. }
  1132. texel_flag = true; // use texel "vector" and bit stream in integer decoding
  1133. DecodeIntegerSequence(params.max_weight, GetNumWeightValues(params.size, params.dual_plane));
  1134. UnquantizeTexelWeights(params.dual_plane, params.size);
  1135. for (uint j = 0; j < block_dims.y; j++) {
  1136. for (uint i = 0; i < block_dims.x; i++) {
  1137. uint local_partition = 0;
  1138. if (num_partitions > 1) {
  1139. local_partition = Select2DPartition(partition_index, i, j, num_partitions,
  1140. (block_dims.y * block_dims.x) < 32);
  1141. }
  1142. vec4 p;
  1143. uvec4 C0 = ReplicateByteTo16(endpoints[local_partition][0]);
  1144. uvec4 C1 = ReplicateByteTo16(endpoints[local_partition][1]);
  1145. uvec4 plane_vec = uvec4(0);
  1146. uvec4 weight_vec = uvec4(0);
  1147. for (uint c = 0; c < 4; c++) {
  1148. if (params.dual_plane && (((plane_index + 1) & 3) == c)) {
  1149. plane_vec[c] = 1;
  1150. }
  1151. weight_vec[c] = unquantized_texel_weights[plane_vec[c]][j * block_dims.x + i];
  1152. }
  1153. vec4 Cf = vec4((C0 * (uvec4(64) - weight_vec) + C1 * weight_vec + uvec4(32)) / 64);
  1154. p = (Cf / 65535.0);
  1155. imageStore(dest_image, coord + ivec3(i, j, 0), p.gbar);
  1156. }
  1157. }
  1158. }
  1159. void main() {
  1160. uvec3 pos = gl_GlobalInvocationID;
  1161. pos.x <<= BYTES_PER_BLOCK_LOG2;
  1162. // Read as soon as possible due to its latency
  1163. const uint swizzle = SwizzleOffset(pos.xy);
  1164. const uint block_y = pos.y >> GOB_SIZE_Y_SHIFT;
  1165. uint offset = 0;
  1166. offset += pos.z * layer_stride;
  1167. offset += (block_y >> block_height) * block_size;
  1168. offset += (block_y & block_height_mask) << GOB_SIZE_SHIFT;
  1169. offset += (pos.x >> GOB_SIZE_X_SHIFT) << x_shift;
  1170. offset += swizzle;
  1171. const ivec3 coord = ivec3(gl_GlobalInvocationID * uvec3(block_dims, 1));
  1172. if (any(greaterThanEqual(coord, imageSize(dest_image)))) {
  1173. return;
  1174. }
  1175. current_index = 0;
  1176. bitsread = 0;
  1177. local_buff = astc_data[offset / 16];
  1178. DecompressBlock(coord);
  1179. }