astc_decoder.comp 39 KB

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