astc_decoder.comp 39 KB

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