astc_decoder.comp 38 KB

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