astc_decoder.comp 38 KB

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