BC7Encode_TryMode137CS.hlsl 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  1. // RUN: %dxc -E main -T cs_6_0 %s | FileCheck %s
  2. // CHECK: flattenedThreadIdInGroup
  3. // CHECK: groupId
  4. // CHECK: bufferLoad
  5. // CHECK: textureLoad
  6. // CHECK: UMax
  7. // CHECK: UMin
  8. // CHECK: barrier
  9. // CHECK: IMad
  10. // CHECK: barrier
  11. // CHECK: bufferStore
  12. //--------------------------------------------------------------------------------------
  13. // File: BC7Encode.hlsl
  14. //
  15. // The Compute Shader for BC7 Encoder
  16. //
  17. // Copyright (c) Microsoft Corporation. All rights reserved.
  18. //--------------------------------------------------------------------------------------
  19. #define REF_DEVICE
  20. #define CHAR_LENGTH 8
  21. #define NCHANNELS 4
  22. #define BC7_UNORM 98
  23. #define MAX_UINT 0xFFFFFFFF
  24. #define MIN_UINT 0
  25. static const uint candidateSectionBit[64] = //Associated to partition 0-63
  26. {
  27. 0xCCCC, 0x8888, 0xEEEE, 0xECC8,
  28. 0xC880, 0xFEEC, 0xFEC8, 0xEC80,
  29. 0xC800, 0xFFEC, 0xFE80, 0xE800,
  30. 0xFFE8, 0xFF00, 0xFFF0, 0xF000,
  31. 0xF710, 0x008E, 0x7100, 0x08CE,
  32. 0x008C, 0x7310, 0x3100, 0x8CCE,
  33. 0x088C, 0x3110, 0x6666, 0x366C,
  34. 0x17E8, 0x0FF0, 0x718E, 0x399C,
  35. 0xaaaa, 0xf0f0, 0x5a5a, 0x33cc,
  36. 0x3c3c, 0x55aa, 0x9696, 0xa55a,
  37. 0x73ce, 0x13c8, 0x324c, 0x3bdc,
  38. 0x6996, 0xc33c, 0x9966, 0x660,
  39. 0x272, 0x4e4, 0x4e40, 0x2720,
  40. 0xc936, 0x936c, 0x39c6, 0x639c,
  41. 0x9336, 0x9cc6, 0x817e, 0xe718,
  42. 0xccf0, 0xfcc, 0x7744, 0xee22,
  43. };
  44. static const uint candidateSectionBit2[64] = //Associated to partition 64-127
  45. {
  46. 0xf60008cc, 0x73008cc8, 0x3310cc80, 0xceec00,
  47. 0xcc003300, 0xcc0000cc, 0xccff00, 0x3300cccc,
  48. 0xf0000f00, 0xf0000ff0, 0xff0000f0, 0x88884444,
  49. 0x88886666, 0xcccc2222, 0xec80136c, 0x7310008c,
  50. 0xc80036c8, 0x310008ce, 0xccc03330, 0xcccf000,
  51. 0xee0000ee, 0x77008888, 0xcc0022c0, 0x33004430,
  52. 0xcc0c22, 0xfc880344, 0x6606996, 0x66009960,
  53. 0xc88c0330, 0xf9000066, 0xcc0c22c, 0x73108c00,
  54. 0xec801300, 0x8cec400, 0xec80004c, 0x44442222,
  55. 0xf0000f0, 0x49242492, 0x42942942, 0xc30c30c,
  56. 0x3c0c03c, 0xff0000aa, 0x5500aa00, 0xcccc3030,
  57. 0xc0cc0c0, 0x66669090, 0xff0a00a, 0x5550aaa0,
  58. 0xf0000aaa, 0xe0ee0e0, 0x88887070, 0x99906660,
  59. 0xe00e0ee0, 0x88880770, 0xf0000666, 0x99006600,
  60. 0xff000066, 0xc00c0cc0, 0xcccc0330, 0x90006000,
  61. 0x8088080, 0xeeee1010, 0xfff0000a, 0x731008ce,
  62. };
  63. static const uint2 candidateFixUpIndex1D[128] =
  64. {
  65. {15, 0},{15, 0},{15, 0},{15, 0},
  66. {15, 0},{15, 0},{15, 0},{15, 0},
  67. {15, 0},{15, 0},{15, 0},{15, 0},
  68. {15, 0},{15, 0},{15, 0},{15, 0},
  69. {15, 0},{ 2, 0},{ 8, 0},{ 2, 0},
  70. { 2, 0},{ 8, 0},{ 8, 0},{15, 0},
  71. { 2, 0},{ 8, 0},{ 2, 0},{ 2, 0},
  72. { 8, 0},{ 8, 0},{ 2, 0},{ 2, 0},
  73. {15, 0},{15, 0},{ 6, 0},{ 8, 0},
  74. { 2, 0},{ 8, 0},{15, 0},{15, 0},
  75. { 2, 0},{ 8, 0},{ 2, 0},{ 2, 0},
  76. { 2, 0},{15, 0},{15, 0},{ 6, 0},
  77. { 6, 0},{ 2, 0},{ 6, 0},{ 8, 0},
  78. {15, 0},{15, 0},{ 2, 0},{ 2, 0},
  79. {15, 0},{15, 0},{15, 0},{15, 0},
  80. {15, 0},{ 2, 0},{ 2, 0},{15, 0},
  81. //candidateFixUpIndex1D[i][1], i < 64 should not be used
  82. { 3,15},{ 3, 8},{15, 8},{15, 3},
  83. { 8,15},{ 3,15},{15, 3},{15, 8},
  84. { 8,15},{ 8,15},{ 6,15},{ 6,15},
  85. { 6,15},{ 5,15},{ 3,15},{ 3, 8},
  86. { 3,15},{ 3, 8},{ 8,15},{15, 3},
  87. { 3,15},{ 3, 8},{ 6,15},{10, 8},
  88. { 5, 3},{ 8,15},{ 8, 6},{ 6,10},
  89. { 8,15},{ 5,15},{15,10},{15, 8},
  90. { 8,15},{15, 3},{ 3,15},{ 5,10},
  91. { 6,10},{10, 8},{ 8, 9},{15,10},
  92. {15, 6},{ 3,15},{15, 8},{ 5,15},
  93. {15, 3},{15, 6},{15, 6},{15, 8}, //The Spec doesn't mark the first fixed up index in this row, so I apply 15 for them, and seems correct
  94. { 3,15},{15, 3},{ 5,15},{ 5,15},
  95. { 5,15},{ 8,15},{ 5,15},{10,15},
  96. { 5,15},{10,15},{ 8,15},{13,15},
  97. {15, 3},{12,15},{ 3,15},{ 3, 8},
  98. };
  99. static const uint2 candidateFixUpIndex1DOrdered[128] = //Same with candidateFixUpIndex1D but order the result when i >= 64
  100. {
  101. {15, 0},{15, 0},{15, 0},{15, 0},
  102. {15, 0},{15, 0},{15, 0},{15, 0},
  103. {15, 0},{15, 0},{15, 0},{15, 0},
  104. {15, 0},{15, 0},{15, 0},{15, 0},
  105. {15, 0},{ 2, 0},{ 8, 0},{ 2, 0},
  106. { 2, 0},{ 8, 0},{ 8, 0},{15, 0},
  107. { 2, 0},{ 8, 0},{ 2, 0},{ 2, 0},
  108. { 8, 0},{ 8, 0},{ 2, 0},{ 2, 0},
  109. {15, 0},{15, 0},{ 6, 0},{ 8, 0},
  110. { 2, 0},{ 8, 0},{15, 0},{15, 0},
  111. { 2, 0},{ 8, 0},{ 2, 0},{ 2, 0},
  112. { 2, 0},{15, 0},{15, 0},{ 6, 0},
  113. { 6, 0},{ 2, 0},{ 6, 0},{ 8, 0},
  114. {15, 0},{15, 0},{ 2, 0},{ 2, 0},
  115. {15, 0},{15, 0},{15, 0},{15, 0},
  116. {15, 0},{ 2, 0},{ 2, 0},{15, 0},
  117. //candidateFixUpIndex1DOrdered[i][1], i < 64 should not be used
  118. { 3,15},{ 3, 8},{ 8,15},{ 3,15},
  119. { 8,15},{ 3,15},{ 3,15},{ 8,15},
  120. { 8,15},{ 8,15},{ 6,15},{ 6,15},
  121. { 6,15},{ 5,15},{ 3,15},{ 3, 8},
  122. { 3,15},{ 3, 8},{ 8,15},{ 3,15},
  123. { 3,15},{ 3, 8},{ 6,15},{ 8,10},
  124. { 3, 5},{ 8,15},{ 6, 8},{ 6,10},
  125. { 8,15},{ 5,15},{10,15},{ 8,15},
  126. { 8,15},{ 3,15},{ 3,15},{ 5,10},
  127. { 6,10},{ 8,10},{ 8, 9},{10,15},
  128. { 6,15},{ 3,15},{ 8,15},{ 5,15},
  129. { 3,15},{ 6,15},{ 6,15},{ 8,15}, //The Spec doesn't mark the first fixed up index in this row, so I apply 15 for them, and seems correct
  130. { 3,15},{ 3,15},{ 5,15},{ 5,15},
  131. { 5,15},{ 8,15},{ 5,15},{10,15},
  132. { 5,15},{10,15},{ 8,15},{13,15},
  133. { 3,15},{12,15},{ 3,15},{ 3, 8},
  134. };
  135. static const uint4x4 candidateRotation[4] =
  136. {
  137. {1,0,0,0},{0,1,0,0},{0,0,1,0},{0,0,0,1},
  138. {0,0,0,1},{0,1,0,0},{0,0,1,0},{1,0,0,0},
  139. {1,0,0,0},{0,0,0,1},{0,0,1,0},{0,1,0,0},
  140. {1,0,0,0},{0,1,0,0},{0,0,0,1},{0,0,1,0}
  141. };
  142. static const uint2 candidateIndexPrec[8] = {{3,0},{3,0},{2,0},{2,0},
  143. {2,3}, //color index and alpha index can exchange
  144. {2,2},{4,4},{2,2}};
  145. static const uint aWeight[3][16] = { {0, 4, 9, 13, 17, 21, 26, 30, 34, 38, 43, 47, 51, 55, 60, 64},
  146. {0, 9, 18, 27, 37, 46, 55, 64, 0, 0, 0, 0, 0, 0, 0, 0},
  147. {0, 21, 43, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} };
  148. //0, 4, 9, 13, 17, 21, 26, 30, 34, 38, 43, 47, 51, 55, 60, 64
  149. static const uint aStep[3][64] = { { 0, 0, 0, 1, 1, 1, 1, 2,
  150. 2, 2, 2, 2, 3, 3, 3, 3,
  151. 4, 4, 4, 4, 5, 5, 5, 5,
  152. 6, 6, 6, 6, 6, 7, 7, 7,
  153. 7, 8, 8, 8, 8, 9, 9, 9,
  154. 9,10,10,10,10,10,11,11,
  155. 11,11,12,12,12,12,13,13,
  156. 13,13,14,14,14,14,15,15 },
  157. //0, 9, 18, 27, 37, 46, 55, 64
  158. { 0,0,0,0,0,1,1,1,
  159. 1,1,1,1,1,1,2,2,
  160. 2,2,2,2,2,2,2,3,
  161. 3,3,3,3,3,3,3,3,
  162. 3,4,4,4,4,4,4,4,
  163. 4,4,5,5,5,5,5,5,
  164. 5,5,5,6,6,6,6,6,
  165. 6,6,6,6,7,7,7,7 },
  166. //0, 21, 43, 64
  167. { 0,0,0,0,0,0,0,0,
  168. 0,0,0,1,1,1,1,1,
  169. 1,1,1,1,1,1,1,1,
  170. 1,1,1,1,1,1,1,1,
  171. 1,2,2,2,2,2,2,2,
  172. 2,2,2,2,2,2,2,2,
  173. 2,2,2,2,2,2,3,3,
  174. 3,3,3,3,3,3,3,3 } };
  175. cbuffer cbCS : register( b0 )
  176. {
  177. uint g_tex_width;
  178. uint g_num_block_x;
  179. uint g_format;
  180. uint g_mode_id;
  181. uint g_start_block_id;
  182. uint g_num_total_blocks;
  183. };
  184. //Forward declaration
  185. void compress_endpoints0( inout uint2x4 endPoint ); //Mode = 0
  186. void compress_endpoints1( inout uint2x4 endPoint ); //Mode = 1
  187. void compress_endpoints2( inout uint2x4 endPoint ); //Mode = 2
  188. void compress_endpoints3( inout uint2x4 endPoint ); //Mode = 3
  189. void compress_endpoints7( inout uint2x4 endPoint ); //Mode = 7
  190. void compress_endpoints6( inout uint2x4 endPoint ); //Mode = 6
  191. void compress_endpoints4( inout uint2x4 endPoint ); //Mode = 4
  192. void compress_endpoints5( inout uint2x4 endPoint ); //Mode = 5
  193. void block_package0( out uint4 block, uint partition, uint threadBase ); //Mode0
  194. void block_package1( out uint4 block, uint partition, uint threadBase ); //Mode1
  195. void block_package2( out uint4 block, uint partition, uint threadBase ); //Mode2
  196. void block_package3( out uint4 block, uint partition, uint threadBase ); //Mode3
  197. void block_package4( out uint4 block, uint rotation, uint index_selector, uint threadBase ); //Mode4
  198. void block_package5( out uint4 block, uint rotation, uint threadBase ); //Mode5
  199. void block_package6( out uint4 block, uint threadBase ); //Mode6
  200. void block_package7( out uint4 block, uint partition, uint threadBase ); //Mode7
  201. void swap(inout uint4 lhs, inout uint4 rhs)
  202. {
  203. int4 tmp = lhs;
  204. lhs = rhs;
  205. rhs = tmp;
  206. }
  207. void swap(inout uint3 lhs, inout uint3 rhs)
  208. {
  209. int3 tmp = lhs;
  210. lhs = rhs;
  211. rhs = tmp;
  212. }
  213. void swap(inout uint lhs, inout uint rhs)
  214. {
  215. int tmp = lhs;
  216. lhs = rhs;
  217. rhs = tmp;
  218. }
  219. Texture2D g_Input : register( t0 );
  220. StructuredBuffer<uint4> g_InBuff : register( t1 );
  221. RWStructuredBuffer<uint4> g_OutBuff : register( u0 );
  222. #define THREAD_GROUP_SIZE 64
  223. #define BLOCK_SIZE_Y 4
  224. #define BLOCK_SIZE_X 4
  225. #define BLOCK_SIZE (BLOCK_SIZE_Y * BLOCK_SIZE_X)
  226. struct BufferShared
  227. {
  228. uint4 pixel;
  229. uint error;
  230. uint mode;
  231. uint partition;
  232. uint index_selector;
  233. uint rotation;
  234. uint4 endPoint_low;
  235. uint4 endPoint_high;
  236. };
  237. groupshared BufferShared shared_temp[THREAD_GROUP_SIZE];
  238. [numthreads( THREAD_GROUP_SIZE, 1, 1 )]
  239. void TryMode456CS( uint GI : SV_GroupIndex, uint3 groupID : SV_GroupID )
  240. {
  241. const uint MAX_USED_THREAD = 16;
  242. uint BLOCK_IN_GROUP = THREAD_GROUP_SIZE / MAX_USED_THREAD;
  243. uint blockInGroup = GI / MAX_USED_THREAD;
  244. uint blockID = g_start_block_id + groupID.x * BLOCK_IN_GROUP + blockInGroup;
  245. uint threadBase = blockInGroup * MAX_USED_THREAD;
  246. uint threadInBlock = GI - threadBase;
  247. #ifndef REF_DEVICE
  248. if (blockID >= g_num_total_blocks)
  249. {
  250. return;
  251. }
  252. #endif
  253. uint block_y = blockID / g_num_block_x;
  254. uint block_x = blockID - block_y * g_num_block_x;
  255. uint base_x = block_x * BLOCK_SIZE_X;
  256. uint base_y = block_y * BLOCK_SIZE_Y;
  257. if (threadInBlock < 16)
  258. {
  259. shared_temp[GI].pixel = clamp(uint4(g_Input.Load( uint3( base_x + threadInBlock % 4, base_y + threadInBlock / 4, 0 ) ) * 255), 0, 255);
  260. shared_temp[GI].endPoint_low = shared_temp[GI].pixel;
  261. shared_temp[GI].endPoint_high = shared_temp[GI].pixel;
  262. }
  263. #ifdef REF_DEVICE
  264. GroupMemoryBarrierWithGroupSync();
  265. #endif
  266. if (threadInBlock < 8)
  267. {
  268. shared_temp[GI].endPoint_low = min(shared_temp[GI].endPoint_low, shared_temp[GI + 8].endPoint_low);
  269. shared_temp[GI].endPoint_high = max(shared_temp[GI].endPoint_high, shared_temp[GI + 8].endPoint_high);
  270. }
  271. #ifdef REF_DEVICE
  272. GroupMemoryBarrierWithGroupSync();
  273. #endif
  274. if (threadInBlock < 4)
  275. {
  276. shared_temp[GI].endPoint_low = min(shared_temp[GI].endPoint_low, shared_temp[GI + 4].endPoint_low);
  277. shared_temp[GI].endPoint_high = max(shared_temp[GI].endPoint_high, shared_temp[GI + 4].endPoint_high);
  278. }
  279. #ifdef REF_DEVICE
  280. GroupMemoryBarrierWithGroupSync();
  281. #endif
  282. if (threadInBlock < 2)
  283. {
  284. shared_temp[GI].endPoint_low = min(shared_temp[GI].endPoint_low, shared_temp[GI + 2].endPoint_low);
  285. shared_temp[GI].endPoint_high = max(shared_temp[GI].endPoint_high, shared_temp[GI + 2].endPoint_high);
  286. }
  287. #ifdef REF_DEVICE
  288. GroupMemoryBarrierWithGroupSync();
  289. #endif
  290. if (threadInBlock < 1)
  291. {
  292. shared_temp[GI].endPoint_low = min(shared_temp[GI].endPoint_low, shared_temp[GI + 1].endPoint_low);
  293. shared_temp[GI].endPoint_high = max(shared_temp[GI].endPoint_high, shared_temp[GI + 1].endPoint_high);
  294. }
  295. #ifdef REF_DEVICE
  296. GroupMemoryBarrierWithGroupSync();
  297. #endif
  298. uint2x4 endPoint;
  299. endPoint[0] = shared_temp[threadBase].endPoint_low;
  300. endPoint[1] = shared_temp[threadBase].endPoint_high;
  301. uint error = 0xFFFFFFFF;
  302. uint mode = 0;
  303. uint index_selector = 0;
  304. uint rotation = 0;
  305. uint2 indexPrec;
  306. if (threadInBlock < 8)
  307. {
  308. if (0 == (threadInBlock & 1))
  309. {
  310. //2 represents 2bit index precision; 1 represents 3bit index precision
  311. indexPrec = uint2( 2, 1 );
  312. }
  313. else
  314. {
  315. //2 represents 2bit index precision; 1 represents 3bit index precision
  316. index_selector = 1;
  317. indexPrec = uint2( 1, 2 );
  318. }
  319. }
  320. else
  321. {
  322. //2 represents 2bit index precision
  323. indexPrec = uint2( 2, 2 );
  324. }
  325. uint4 pixel_r;
  326. uint color_index;
  327. uint alpha_index;
  328. int4 span;
  329. int2 span_norm_sqr;
  330. int2 dotProduct;
  331. if (threadInBlock < 12)
  332. {
  333. if ((threadInBlock < 2) || (8 == threadInBlock)) // rotation = 0
  334. {
  335. rotation = 0;
  336. }
  337. else if ((threadInBlock < 4) || (9 == threadInBlock)) // rotation = 1
  338. {
  339. endPoint[0].ra = endPoint[0].ar;
  340. endPoint[1].ra = endPoint[1].ar;
  341. rotation = 1;
  342. }
  343. else if ((threadInBlock < 6) || (10 == threadInBlock)) // rotation = 2
  344. {
  345. endPoint[0].ga = endPoint[0].ag;
  346. endPoint[1].ga = endPoint[1].ag;
  347. rotation = 2;
  348. }
  349. else if ((threadInBlock < 8) || (11 == threadInBlock)) // rotation = 3
  350. {
  351. endPoint[0].ba = endPoint[0].ab;
  352. endPoint[1].ba = endPoint[1].ab;
  353. rotation = 3;
  354. }
  355. if (threadInBlock < 8)
  356. {
  357. mode = 4;
  358. compress_endpoints4( endPoint );
  359. }
  360. else
  361. {
  362. mode = 5;
  363. compress_endpoints5( endPoint );
  364. }
  365. uint4 pixel = shared_temp[threadBase + 0].pixel;
  366. if (1 == rotation)
  367. {
  368. pixel.ra = pixel.ar;
  369. }
  370. else if (2 == rotation)
  371. {
  372. pixel.ga = pixel.ag;
  373. }
  374. else if (3 == rotation)
  375. {
  376. pixel.ba = pixel.ab;
  377. }
  378. span = endPoint[1] - endPoint[0];
  379. span_norm_sqr = uint2( dot( span.rgb, span.rgb ), span.a * span.a );
  380. dotProduct = int2( dot( span.rgb, pixel.rgb - endPoint[0].rgb ), span.a * ( pixel.a - endPoint[0].a ) );
  381. if ( span_norm_sqr.x > 0 && dotProduct.x > 0 && uint( dotProduct.x * 63.49999 ) > uint( 32 * span_norm_sqr.x ) )
  382. {
  383. span.rgb = -span.rgb;
  384. swap(endPoint[0].rgb, endPoint[1].rgb);
  385. }
  386. if ( span_norm_sqr.y > 0 && dotProduct.y > 0 && uint( dotProduct.y * 63.49999 ) > uint( 32 * span_norm_sqr.y ) )
  387. {
  388. span.a = -span.a;
  389. swap(endPoint[0].a, endPoint[1].a);
  390. }
  391. error = 0;
  392. for ( uint i = 0; i < 16; i ++ )
  393. {
  394. pixel = shared_temp[threadBase + i].pixel;
  395. if (1 == rotation)
  396. {
  397. pixel.ra = pixel.ar;
  398. }
  399. else if (2 == rotation)
  400. {
  401. pixel.ga = pixel.ag;
  402. }
  403. else if (3 == rotation)
  404. {
  405. pixel.ba = pixel.ab;
  406. }
  407. dotProduct.x = dot( span.rgb, pixel.rgb - endPoint[0].rgb );
  408. color_index = ( span_norm_sqr.x <= 0 || dotProduct.x <= 0 ) ? 0
  409. : ( ( dotProduct.x < span_norm_sqr.x ) ? aStep[indexPrec.x][ uint( dotProduct.x * 63.49999 / span_norm_sqr.x ) ] : aStep[indexPrec.x][63] );
  410. dotProduct.y = dot( span.a, pixel.a - endPoint[0].a );
  411. alpha_index = ( span_norm_sqr.y <= 0 || dotProduct.y <= 0 ) ? 0
  412. : ( ( dotProduct.y < span_norm_sqr.y ) ? aStep[indexPrec.y][ uint( dotProduct.y * 63.49999 / span_norm_sqr.y ) ] : aStep[indexPrec.y][63] );
  413. if (index_selector)
  414. {
  415. swap(color_index, alpha_index);
  416. }
  417. pixel_r.rgb = ( ( 64 - aWeight[indexPrec.x][color_index] ) * endPoint[0].rgb
  418. + aWeight[indexPrec.x][color_index] * endPoint[1] + 32 ) >> 6;
  419. pixel_r.a = ( ( 64 - aWeight[indexPrec.y][alpha_index] ) * endPoint[0].a
  420. + aWeight[indexPrec.y][alpha_index] * endPoint[1] + 32 ) >> 6;
  421. pixel_r -= pixel;
  422. error += dot(pixel_r, pixel_r);
  423. }
  424. }
  425. else if (12 == threadInBlock)//Mode6
  426. {
  427. compress_endpoints6( endPoint );
  428. uint4 pixel = shared_temp[threadBase + 0].pixel;
  429. span = endPoint[1] - endPoint[0];
  430. span_norm_sqr = dot( span, span );
  431. dotProduct = dot( span, pixel - endPoint[0] );
  432. if ( span_norm_sqr.x > 0 && dotProduct.x >= 0 && uint( dotProduct.x * 63.49999 ) > uint( 32 * span_norm_sqr.x ) )
  433. {
  434. span = -span;
  435. swap(endPoint[0], endPoint[1]);
  436. }
  437. error = 0;
  438. for ( uint i = 0; i < 16; i ++ )
  439. {
  440. pixel = shared_temp[threadBase + i].pixel;
  441. dotProduct.x = dot( span, pixel - endPoint[0] );
  442. color_index = ( span_norm_sqr.x <= 0 || dotProduct.x <= 0 ) ? 0
  443. : ( ( dotProduct.x < span_norm_sqr.x ) ? aStep[0][ uint( dotProduct.x * 63.49999 / span_norm_sqr.x ) ] : aStep[0][63] );
  444. pixel_r = ( ( 64 - aWeight[0][color_index] ) * endPoint[0]
  445. + aWeight[0][color_index] * endPoint[1] + 32 ) >> 6;
  446. pixel_r -= pixel;
  447. error += dot(pixel_r, pixel_r);
  448. }
  449. mode = 6;
  450. rotation = 0;
  451. }
  452. shared_temp[GI].error = error;
  453. shared_temp[GI].mode = mode;
  454. shared_temp[GI].index_selector = index_selector;
  455. shared_temp[GI].rotation = rotation;
  456. #ifdef REF_DEVICE
  457. GroupMemoryBarrierWithGroupSync();
  458. #endif
  459. if (threadInBlock < 8)
  460. {
  461. if ( shared_temp[GI].error > shared_temp[GI + 8].error )
  462. {
  463. shared_temp[GI].error = shared_temp[GI + 8].error;
  464. shared_temp[GI].mode = shared_temp[GI + 8].mode;
  465. shared_temp[GI].index_selector = shared_temp[GI + 8].index_selector;
  466. shared_temp[GI].rotation = shared_temp[GI + 8].rotation;
  467. }
  468. }
  469. #ifdef REF_DEVICE
  470. GroupMemoryBarrierWithGroupSync();
  471. #endif
  472. if (threadInBlock < 4)
  473. {
  474. if ( shared_temp[GI].error > shared_temp[GI + 4].error )
  475. {
  476. shared_temp[GI].error = shared_temp[GI + 4].error;
  477. shared_temp[GI].mode = shared_temp[GI + 4].mode;
  478. shared_temp[GI].index_selector = shared_temp[GI + 4].index_selector;
  479. shared_temp[GI].rotation = shared_temp[GI + 4].rotation;
  480. }
  481. }
  482. #ifdef REF_DEVICE
  483. GroupMemoryBarrierWithGroupSync();
  484. #endif
  485. if (threadInBlock < 2)
  486. {
  487. if ( shared_temp[GI].error > shared_temp[GI + 2].error )
  488. {
  489. shared_temp[GI].error = shared_temp[GI + 2].error;
  490. shared_temp[GI].mode = shared_temp[GI + 2].mode;
  491. shared_temp[GI].index_selector = shared_temp[GI + 2].index_selector;
  492. shared_temp[GI].rotation = shared_temp[GI + 2].rotation;
  493. }
  494. }
  495. #ifdef REF_DEVICE
  496. GroupMemoryBarrierWithGroupSync();
  497. #endif
  498. if (threadInBlock < 1)
  499. {
  500. if ( shared_temp[GI].error > shared_temp[GI + 1].error )
  501. {
  502. shared_temp[GI].error = shared_temp[GI + 1].error;
  503. shared_temp[GI].mode = shared_temp[GI + 1].mode;
  504. shared_temp[GI].index_selector = shared_temp[GI + 1].index_selector;
  505. shared_temp[GI].rotation = shared_temp[GI + 1].rotation;
  506. }
  507. g_OutBuff[blockID] = uint4(shared_temp[GI].error, (shared_temp[GI].index_selector << 31) | shared_temp[GI].mode,
  508. 0, shared_temp[GI].rotation);
  509. }
  510. }
  511. [numthreads( THREAD_GROUP_SIZE, 1, 1 )]
  512. void main( uint GI : SV_GroupIndex, uint3 groupID : SV_GroupID )
  513. {
  514. const uint MAX_USED_THREAD = 64;
  515. uint BLOCK_IN_GROUP = THREAD_GROUP_SIZE / MAX_USED_THREAD;
  516. uint blockInGroup = GI / MAX_USED_THREAD;
  517. uint blockID = g_start_block_id + groupID.x * BLOCK_IN_GROUP + blockInGroup;
  518. uint threadBase = blockInGroup * MAX_USED_THREAD;
  519. uint threadInBlock = GI - threadBase;
  520. uint block_y = blockID / g_num_block_x;
  521. uint block_x = blockID - block_y * g_num_block_x;
  522. uint base_x = block_x * BLOCK_SIZE_X;
  523. uint base_y = block_y * BLOCK_SIZE_Y;
  524. if (threadInBlock < 16)
  525. {
  526. shared_temp[GI].pixel = clamp(uint4(g_Input.Load( uint3( base_x + threadInBlock % 4, base_y + threadInBlock / 4, 0 ) ) * 255), 0, 255);
  527. }
  528. GroupMemoryBarrierWithGroupSync();
  529. shared_temp[GI].error = 0xFFFFFFFF;
  530. uint4 pixel_r;
  531. uint2x4 endPoint[2];
  532. uint color_index;
  533. if (threadInBlock < 64)
  534. {
  535. uint partition = threadInBlock;
  536. uint error = 0;
  537. endPoint[0][0] = MAX_UINT;
  538. endPoint[0][1] = MIN_UINT;
  539. endPoint[1][0] = MAX_UINT;
  540. endPoint[1][1] = MIN_UINT;
  541. uint bits = candidateSectionBit[partition];
  542. for ( uint i = 0; i < 16; i ++ )
  543. {
  544. uint4 pixel = shared_temp[threadBase + i].pixel;
  545. if ( (( bits >> i ) & 0x01) == 1 )
  546. {
  547. endPoint[1][0] = min( endPoint[1][0], pixel );
  548. endPoint[1][1] = max( endPoint[1][1], pixel );
  549. }
  550. else
  551. {
  552. endPoint[0][0] = min( endPoint[0][0], pixel );
  553. endPoint[0][1] = max( endPoint[0][1], pixel );
  554. }
  555. }
  556. for ( uint i = 0; i < 2; i ++ )
  557. {
  558. if (g_mode_id == 1)
  559. {
  560. compress_endpoints1( endPoint[i] );
  561. }
  562. else if (g_mode_id == 3)
  563. {
  564. compress_endpoints3( endPoint[i] );
  565. }
  566. else //if (g_mode_id == 7)
  567. {
  568. compress_endpoints7( endPoint[i] );
  569. }
  570. }
  571. int4 span[2];
  572. span[0] = endPoint[0][1] - endPoint[0][0];
  573. span[1] = endPoint[1][1] - endPoint[1][0];
  574. if (g_mode_id != 7)
  575. {
  576. span[0].w = span[1].w = 0;
  577. }
  578. int span_norm_sqr[2];
  579. span_norm_sqr[0] = dot( span[0], span[0] );
  580. span_norm_sqr[1] = dot( span[1], span[1] );
  581. int dotProduct = dot( span[0], shared_temp[threadBase + 0].pixel - endPoint[0][0] );
  582. if ( span_norm_sqr[0] > 0 && dotProduct > 0 && uint( dotProduct * 63.49999 ) > uint( 32 * span_norm_sqr[0] ) )
  583. {
  584. span[0] = -span[0];
  585. swap(endPoint[0][0], endPoint[0][1]);
  586. }
  587. dotProduct = dot( span[1], shared_temp[threadBase + candidateFixUpIndex1D[partition].x].pixel - endPoint[1][0] );
  588. if ( span_norm_sqr[1] > 0 && dotProduct > 0 && uint( dotProduct * 63.49999 ) > uint( 32 * span_norm_sqr[1] ) )
  589. {
  590. span[1] = -span[1];
  591. swap(endPoint[1][0], endPoint[1][1]);
  592. }
  593. uint step_selector;
  594. if (g_mode_id != 1)
  595. {
  596. step_selector = 2;
  597. }
  598. else
  599. {
  600. step_selector = 1;
  601. }
  602. uint bits2 = candidateSectionBit2[partition];
  603. for ( uint i = 0; i < 16; i ++ )
  604. {
  605. if (((bits >> i) & 0x01) == 1)
  606. {
  607. dotProduct = dot( span[1], shared_temp[threadBase + i].pixel - endPoint[1][0] );
  608. color_index = (span_norm_sqr[1] <= 0 || dotProduct <= 0) ? 0
  609. : ((dotProduct < span_norm_sqr[1]) ? aStep[step_selector][uint(dotProduct * 63.49999 / span_norm_sqr[1])] : aStep[step_selector][63]);
  610. }
  611. else
  612. {
  613. dotProduct = dot( span[0], shared_temp[threadBase + i].pixel - endPoint[0][0] );
  614. color_index = (span_norm_sqr[0] <= 0 || dotProduct <= 0) ? 0
  615. : ((dotProduct < span_norm_sqr[0]) ? aStep[step_selector][uint(dotProduct * 63.49999 / span_norm_sqr[0])] : aStep[step_selector][63]);
  616. }
  617. uint subset_index;
  618. if (g_mode_id == 7)
  619. {
  620. subset_index = (bits >> i) & 0x01;
  621. }
  622. else
  623. {
  624. subset_index = ((bits2 >> (i + 15)) & 0x02) | ((bits2 >> i) & 0x01);
  625. }
  626. pixel_r = ((64 - aWeight[step_selector][color_index]) * endPoint[subset_index][0]
  627. + aWeight[step_selector][color_index] * endPoint[subset_index][1] + 32) >> 6;
  628. pixel_r -= shared_temp[threadBase + i].pixel;
  629. if (g_mode_id != 7)
  630. {
  631. pixel_r.a = 0;
  632. }
  633. error += dot(pixel_r, pixel_r);
  634. }
  635. shared_temp[GI].error = error;
  636. shared_temp[GI].mode = g_mode_id;
  637. shared_temp[GI].partition = partition;
  638. }
  639. GroupMemoryBarrierWithGroupSync();
  640. if (threadInBlock < 32)
  641. {
  642. if ( shared_temp[GI].error > shared_temp[GI + 32].error )
  643. {
  644. shared_temp[GI].error = shared_temp[GI + 32].error;
  645. shared_temp[GI].mode = shared_temp[GI + 32].mode;
  646. shared_temp[GI].partition = shared_temp[GI + 32].partition;
  647. }
  648. }
  649. #ifdef REF_DEVICE
  650. GroupMemoryBarrierWithGroupSync();
  651. #endif
  652. if (threadInBlock < 16)
  653. {
  654. if ( shared_temp[GI].error > shared_temp[GI + 16].error )
  655. {
  656. shared_temp[GI].error = shared_temp[GI + 16].error;
  657. shared_temp[GI].mode = shared_temp[GI + 16].mode;
  658. shared_temp[GI].partition = shared_temp[GI + 16].partition;
  659. }
  660. }
  661. #ifdef REF_DEVICE
  662. GroupMemoryBarrierWithGroupSync();
  663. #endif
  664. if (threadInBlock < 8)
  665. {
  666. if ( shared_temp[GI].error > shared_temp[GI + 8].error )
  667. {
  668. shared_temp[GI].error = shared_temp[GI + 8].error;
  669. shared_temp[GI].mode = shared_temp[GI + 8].mode;
  670. shared_temp[GI].partition = shared_temp[GI + 8].partition;
  671. }
  672. }
  673. #ifdef REF_DEVICE
  674. GroupMemoryBarrierWithGroupSync();
  675. #endif
  676. if (threadInBlock < 4)
  677. {
  678. if ( shared_temp[GI].error > shared_temp[GI + 4].error )
  679. {
  680. shared_temp[GI].error = shared_temp[GI + 4].error;
  681. shared_temp[GI].mode = shared_temp[GI + 4].mode;
  682. shared_temp[GI].partition = shared_temp[GI + 4].partition;
  683. }
  684. }
  685. #ifdef REF_DEVICE
  686. GroupMemoryBarrierWithGroupSync();
  687. #endif
  688. if (threadInBlock < 2)
  689. {
  690. if ( shared_temp[GI].error > shared_temp[GI + 2].error )
  691. {
  692. shared_temp[GI].error = shared_temp[GI + 2].error;
  693. shared_temp[GI].mode = shared_temp[GI + 2].mode;
  694. shared_temp[GI].partition = shared_temp[GI + 2].partition;
  695. }
  696. }
  697. #ifdef REF_DEVICE
  698. GroupMemoryBarrierWithGroupSync();
  699. #endif
  700. if (threadInBlock < 1)
  701. {
  702. if ( shared_temp[GI].error > shared_temp[GI + 1].error )
  703. {
  704. shared_temp[GI].error = shared_temp[GI + 1].error;
  705. shared_temp[GI].mode = shared_temp[GI + 1].mode;
  706. shared_temp[GI].partition = shared_temp[GI + 1].partition;
  707. }
  708. if (g_InBuff[blockID].x > shared_temp[GI].error)
  709. {
  710. g_OutBuff[blockID] = uint4(shared_temp[GI].error, shared_temp[GI].mode, shared_temp[GI].partition, 0);
  711. }
  712. else
  713. {
  714. g_OutBuff[blockID] = g_InBuff[blockID];
  715. }
  716. }
  717. }
  718. void compress_endpoints0( inout uint2x4 endPoint )
  719. {
  720. uint3 tmp;
  721. for ( uint j = 0; j < 2; j ++ )
  722. {
  723. tmp = endPoint[j].rgb & 0x0F;
  724. tmp.x += tmp.y + tmp.z;
  725. endPoint[j].rgb = ( endPoint[j].rgb & 0xF0 ) | ( ( tmp.x / 3 ) & 0x08 );
  726. }
  727. }
  728. void compress_endpoints1( inout uint2x4 endPoint )
  729. {
  730. uint3 tmp;
  731. tmp = ( endPoint[0].rgb & 0x03 ) + ( endPoint[1].rgb & 0x03 );
  732. tmp.x += tmp.y + tmp.z;
  733. tmp.x = ( tmp.x / 6 ) & 0x02;
  734. for ( uint j = 0; j < 2; j ++ )
  735. {
  736. endPoint[j].rgb = ( endPoint[j].rgb & 0xFC ) | tmp.x;
  737. }
  738. }
  739. void compress_endpoints2( inout uint2x4 endPoint )
  740. {
  741. for ( uint j = 0; j < 2; j ++ )
  742. {
  743. endPoint[j].rgb = min(255, ( endPoint[j].rgb + 0x04 ) ) & 0xF8;
  744. }
  745. }
  746. void compress_endpoints3( inout uint2x4 endPoint )
  747. {
  748. uint3 tmp;
  749. for ( uint j = 0; j < 2; j ++ )
  750. {
  751. tmp = endPoint[j].rgb & 0x01;
  752. tmp.x += tmp.y + tmp.z;
  753. endPoint[j].rgb = ( endPoint[j].rgb & 0xFE ) | ( tmp.x / 3 );
  754. }
  755. }
  756. void compress_endpoints4( inout uint2x4 endPoint )
  757. {
  758. for ( uint j = 0; j < 2; j ++ )
  759. {
  760. endPoint[j] = min(255, ( endPoint[j] + uint4(0x04.xxx, 0x02) ) ) & uint4(0xF8.xxx, 0xFC);
  761. }
  762. }
  763. void compress_endpoints5( inout uint2x4 endPoint )
  764. {
  765. for ( uint j = 0; j < 2; j ++ )
  766. {
  767. endPoint[j].rgb = min(255, ( endPoint[j].rgb + 0x01 ) ) & 0xFE;
  768. }
  769. }
  770. void compress_endpoints6( inout uint2x4 endPoint )
  771. {
  772. uint4 tmp;
  773. for ( uint j = 0; j < 2; j ++ )
  774. {
  775. tmp = endPoint[j] & 0x01;
  776. tmp.x += tmp.y + tmp.z + tmp.w;
  777. endPoint[j] = ( endPoint[j] & 0xFE ) | ( ( tmp.x >> 2 ) & 0x01 );
  778. }
  779. }
  780. void compress_endpoints7( inout uint2x4 endPoint )
  781. {
  782. uint4 tmp;
  783. for ( uint j = 0; j < 2; j ++ )
  784. {
  785. tmp = endPoint[j] & 0x07;
  786. tmp.x += tmp.y + tmp.z + tmp.w;
  787. endPoint[j] = ( endPoint[j] & 0xF8 ) | ( ( tmp.x >> 2 ) & 0x04 );
  788. }
  789. }
  790. #define get_end_point_l(subset) shared_temp[threadBase + subset].endPoint_low
  791. #define get_end_point_h(subset) shared_temp[threadBase + subset].endPoint_high
  792. #define get_color_index(index) shared_temp[threadBase + index].error
  793. #define get_alpha_index(index) shared_temp[threadBase + index].mode
  794. void block_package0( out uint4 block, uint partition, uint threadBase )
  795. {
  796. block.x = 0x01 | ( (partition - 64) << 1 )
  797. | ( ( get_end_point_l(0).r & 0xF0 ) << 1 ) | ( ( get_end_point_h(0).r & 0xF0 ) << 5 )
  798. | ( ( get_end_point_l(1).r & 0xF0 ) << 9 ) | ( ( get_end_point_h(1).r & 0xF0 ) << 13 )
  799. | ( ( get_end_point_l(2).r & 0xF0 ) << 17 ) | ( ( get_end_point_h(2).r & 0xF0 ) << 21 )
  800. | ( ( get_end_point_l(0).g & 0xF0 ) << 25 );
  801. block.y = ( ( get_end_point_l(0).g & 0xF0 ) >> 7 ) | ( ( get_end_point_h(0).g & 0xF0 ) >> 3 )
  802. | ( ( get_end_point_l(1).g & 0xF0 ) << 1 ) | ( ( get_end_point_h(1).g & 0xF0 ) << 5 )
  803. | ( ( get_end_point_l(2).g & 0xF0 ) << 9 ) | ( ( get_end_point_h(2).g & 0xF0 ) << 13 )
  804. | ( ( get_end_point_l(0).b & 0xF0 ) << 17 ) | ( ( get_end_point_h(0).b & 0xF0 ) << 21 )
  805. | ( ( get_end_point_l(1).b & 0xF0 ) << 25 );
  806. block.z = ( ( get_end_point_l(1).b & 0xF0 ) >> 7 ) | ( ( get_end_point_h(1).b & 0xF0 ) >> 3 )
  807. | ( ( get_end_point_l(2).b & 0xF0 ) << 1 ) | ( ( get_end_point_h(2).b & 0xF0 ) << 5 )
  808. | ( ( get_end_point_l(0).r & 0x08 ) << 10 ) | ( ( get_end_point_h(0).r & 0x08 ) << 11 )
  809. | ( ( get_end_point_l(1).r & 0x08 ) << 12 ) | ( ( get_end_point_h(1).r & 0x08 ) << 13 )
  810. | ( ( get_end_point_l(2).r & 0x08 ) << 14 ) | ( ( get_end_point_h(2).r & 0x08 ) << 15 )
  811. | ( get_color_index(0) << 19 );
  812. block.w = 0;
  813. uint i = 1;
  814. for ( ; i <= min( candidateFixUpIndex1DOrdered[partition][0], 4 ); i ++ )
  815. {
  816. block.z |= get_color_index(i) << ( i * 3 + 18 );
  817. }
  818. if ( candidateFixUpIndex1DOrdered[partition][0] < 4 ) //i = 4
  819. {
  820. block.z |= get_color_index(4) << 29;
  821. i += 1;
  822. }
  823. else //i = 5
  824. {
  825. block.w |= ( get_color_index(4) & 0x04 ) >> 2;
  826. for ( ; i <= candidateFixUpIndex1DOrdered[partition][0]; i ++ )
  827. block.w |= get_color_index(i) << ( i * 3 - 14 );
  828. }
  829. for ( ; i <= candidateFixUpIndex1DOrdered[partition][1]; i ++ )
  830. {
  831. block.w |= get_color_index(i) << ( i * 3 - 15 );
  832. }
  833. for ( ; i < 16; i ++ )
  834. {
  835. block.w |= get_color_index(i) << ( i * 3 - 16 );
  836. }
  837. }
  838. void block_package1( out uint4 block, uint partition, uint threadBase )
  839. {
  840. block.x = 0x02 | ( partition << 2 )
  841. | ( ( get_end_point_l(0).r & 0xFC ) << 6 ) | ( ( get_end_point_h(0).r & 0xFC ) << 12 )
  842. | ( ( get_end_point_l(1).r & 0xFC ) << 18 ) | ( ( get_end_point_h(1).r & 0xFC ) << 24 );
  843. block.y = ( ( get_end_point_l(0).g & 0xFC ) >> 2 ) | ( ( get_end_point_h(0).g & 0xFC ) << 4 )
  844. | ( ( get_end_point_l(1).g & 0xFC ) << 10 ) | ( ( get_end_point_h(1).g & 0xFC ) << 16 )
  845. | ( ( get_end_point_l(0).b & 0xFC ) << 22 ) | ( ( get_end_point_h(0).b & 0xFC ) << 28 );
  846. block.z = ( ( get_end_point_h(0).b & 0xFC ) >> 4 ) | ( ( get_end_point_l(1).b & 0xFC ) << 2 )
  847. | ( ( get_end_point_h(1).b & 0xFC ) << 8 )
  848. | ( ( get_end_point_l(0).r & 0x02 ) << 15 ) | ( ( get_end_point_l(1).r & 0x02 ) << 16 )
  849. | ( get_color_index(0) << 18 );
  850. if ( candidateFixUpIndex1DOrdered[partition][0] == 15 )
  851. {
  852. block.w = (get_color_index(15) << 30) | (get_color_index(14) << 27) | (get_color_index(13) << 24) | (get_color_index(12) << 21) | (get_color_index(11) << 18) | (get_color_index(10) << 15)
  853. | (get_color_index(9) << 12) | (get_color_index(8) << 9) | (get_color_index(7) << 6) | (get_color_index(6) << 3) | get_color_index(5);
  854. block.z |= (get_color_index(4) << 29) | (get_color_index(3) << 26) | (get_color_index(2) << 23) | (get_color_index(1) << 20) | (get_color_index(0) << 18);
  855. }
  856. else if ( candidateFixUpIndex1DOrdered[partition][0] == 2 )
  857. {
  858. block.w = (get_color_index(15) << 29) | (get_color_index(14) << 26) | (get_color_index(13) << 23) | (get_color_index(12) << 20) | (get_color_index(11) << 17) | (get_color_index(10) << 14)
  859. | (get_color_index(9) << 11) | (get_color_index(8) << 8) | (get_color_index(7) << 5) | (get_color_index(6) << 2) | (get_color_index(5) >> 1);
  860. block.z |= (get_color_index(5) << 31) | (get_color_index(4) << 28) | (get_color_index(3) << 25) | (get_color_index(2) << 23) | (get_color_index(1) << 20) | (get_color_index(0) << 18);
  861. }
  862. else if ( candidateFixUpIndex1DOrdered[partition][0] == 8 )
  863. {
  864. block.w = (get_color_index(15) << 29) | (get_color_index(14) << 26) | (get_color_index(13) << 23) | (get_color_index(12) << 20) | (get_color_index(11) << 17) | (get_color_index(10) << 14)
  865. | (get_color_index(9) << 11) | (get_color_index(8) << 9) | (get_color_index(7) << 6) | (get_color_index(6) << 3) | get_color_index(5);
  866. block.z |= (get_color_index(4) << 29) | (get_color_index(3) << 26) | (get_color_index(2) << 23) | (get_color_index(1) << 20) | (get_color_index(0) << 18);
  867. }
  868. else //candidateFixUpIndex1DOrdered[partition] == 6
  869. {
  870. block.w = (get_color_index(15) << 29) | (get_color_index(14) << 26) | (get_color_index(13) << 23) | (get_color_index(12) << 20) | (get_color_index(11) << 17) | (get_color_index(10) << 14)
  871. | (get_color_index(9) << 11) | (get_color_index(8) << 8) | (get_color_index(7) << 6) | (get_color_index(6) << 4) | get_color_index(5);
  872. block.z |= (get_color_index(4) << 29) | (get_color_index(3) << 26) | (get_color_index(2) << 23) | (get_color_index(1) << 20) | (get_color_index(0) << 18);
  873. }
  874. }
  875. void block_package2( out uint4 block, uint partition, uint threadBase )
  876. {
  877. block.x = 0x04 | ( (partition - 64) << 3 )
  878. | ( ( get_end_point_l(0).r & 0xF8 ) << 6 ) | ( ( get_end_point_h(0).r & 0xF8 ) << 11 )
  879. | ( ( get_end_point_l(1).r & 0xF8 ) << 16 ) | ( ( get_end_point_h(1).r & 0xF8 ) << 21 )
  880. | ( ( get_end_point_l(2).r & 0xF8 ) << 26 );
  881. block.y = ( ( get_end_point_l(2).r & 0xF8 ) >> 6 ) | ( ( get_end_point_h(2).r & 0xF8 ) >> 1 )
  882. | ( ( get_end_point_l(0).g & 0xF8 ) << 4 ) | ( ( get_end_point_h(0).g & 0xF8 ) << 9 )
  883. | ( ( get_end_point_l(1).g & 0xF8 ) << 14 ) | ( ( get_end_point_h(1).g & 0xF8 ) << 19 )
  884. | ( ( get_end_point_l(2).g & 0xF8 ) << 24 );
  885. block.z = ( ( get_end_point_h(2).g & 0xF8 ) >> 3 ) | ( ( get_end_point_l(0).b & 0xF8 ) << 2 )
  886. | ( ( get_end_point_h(0).b & 0xF8 ) << 7 ) | ( ( get_end_point_l(1).b & 0xF8 ) << 12 )
  887. | ( ( get_end_point_h(1).b & 0xF8 ) << 17 ) | ( ( get_end_point_l(2).b & 0xF8 ) << 22 )
  888. | ( ( get_end_point_h(2).b & 0xF8 ) << 27 );
  889. block.w = ( ( get_end_point_h(2).b & 0xF8 ) >> 5 )
  890. | ( get_color_index(0) << 3 );
  891. uint i = 1;
  892. for ( ; i <= candidateFixUpIndex1DOrdered[partition][0]; i ++ )
  893. {
  894. block.w |= get_color_index(i) << ( i * 2 + 2 );
  895. }
  896. for ( ; i <= candidateFixUpIndex1DOrdered[partition][1]; i ++ )
  897. {
  898. block.w |= get_color_index(i) << ( i * 2 + 1 );
  899. }
  900. for ( ; i < 16; i ++ )
  901. {
  902. block.w |= get_color_index(i) << ( i * 2 );
  903. }
  904. }
  905. void block_package3( out uint4 block, uint partition, uint threadBase )
  906. {
  907. block.x = 0x08 | ( partition << 4 )
  908. | ( ( get_end_point_l(0).r & 0xFE ) << 9 ) | ( ( get_end_point_h(0).r & 0xFE ) << 16 )
  909. | ( ( get_end_point_l(1).r & 0xFE ) << 23 ) | ( ( get_end_point_h(1).r & 0xFE ) << 30 );
  910. block.y = ( ( get_end_point_h(1).r & 0xFE ) >> 2 ) | ( ( get_end_point_l(0).g & 0xFE ) << 5 )
  911. | ( ( get_end_point_h(0).g & 0xFE ) << 12 ) | ( ( get_end_point_l(1).g & 0xFE ) << 19 )
  912. | ( ( get_end_point_h(1).g & 0xFE ) << 26 );
  913. block.z = ( ( get_end_point_h(1).g & 0xFE ) >> 6 ) | ( ( get_end_point_l(0).b & 0xFE ) << 1 )
  914. | ( ( get_end_point_h(0).b & 0xFE ) << 8 ) | ( ( get_end_point_l(1).b & 0xFE ) << 15 )
  915. | ( ( get_end_point_h(1).b & 0xFE ) << 22 )
  916. | ( ( get_end_point_l(0).r & 0x01 ) << 30 ) | ( ( get_end_point_h(0).r & 0x01 ) << 31 );
  917. block.w = ( ( get_end_point_l(1).r & 0x01 ) << 0 ) | ( ( get_end_point_h(1).r & 0x01 ) << 1 )
  918. | ( get_color_index(0) << 2 );
  919. uint i = 1;
  920. for ( ; i <= candidateFixUpIndex1DOrdered[partition][0]; i ++ )
  921. {
  922. block.w |= get_color_index(i) << ( i * 2 + 1 );
  923. }
  924. for ( ; i < 16; i ++ )
  925. {
  926. block.w |= get_color_index(i) << ( i * 2 );
  927. }
  928. }
  929. void block_package4( out uint4 block, uint rotation, uint index_selector, uint threadBase )
  930. {
  931. block.x = 0x10 | ( rotation << 5 ) | ( index_selector << 7 )
  932. | ( ( get_end_point_l(0).r & 0xF8 ) << 5 ) | ( ( get_end_point_h(0).r & 0xF8 ) << 10 )
  933. | ( ( get_end_point_l(0).g & 0xF8 ) << 15 ) | ( ( get_end_point_h(0).g & 0xF8 ) << 20 )
  934. | ( ( get_end_point_l(0).b & 0xF8 ) << 25 );
  935. block.y = ( ( get_end_point_l(0).b & 0xF8 ) >> 7 ) | ( ( get_end_point_h(0).b & 0xF8 ) >> 2 )
  936. | ( ( get_end_point_l(0).a & 0xFC ) << 4 ) | ( ( get_end_point_h(0).a & 0xFC ) << 10 )
  937. | ( get_color_index(0) << 18 ) | ( get_color_index(1) << 19 ) | ( get_color_index(2) << 21 ) | ( get_color_index(3) << 23 )
  938. | ( get_color_index(4) << 25 ) | ( get_color_index(5) << 27 ) | ( get_color_index(6) << 29 ) | ( get_color_index(7) << 31 );
  939. block.z = ( get_color_index(7) >> 1 ) | ( get_color_index(8) << 1 ) | ( get_color_index(9) << 3 ) | ( get_color_index(10)<< 5 )
  940. | ( get_color_index(11)<< 7 ) | ( get_color_index(12)<< 9 ) | ( get_color_index(13)<< 11 ) | ( get_color_index(14)<< 13 )
  941. | ( get_color_index(15)<< 15 ) | ( get_alpha_index(0) << 17 ) | ( get_alpha_index(1) << 19 ) | ( get_alpha_index(2) << 22 )
  942. | ( get_alpha_index(3) << 25 ) | ( get_alpha_index(4) << 28 ) | ( get_alpha_index(5) << 31 );
  943. block.w = ( get_alpha_index(5) >> 1 ) | ( get_alpha_index(6) << 2 ) | ( get_alpha_index(7) << 5 ) | ( get_alpha_index(8) << 8 )
  944. | ( get_alpha_index(9) << 11 ) | ( get_alpha_index(10)<< 14 ) | ( get_alpha_index(11)<< 17 ) | ( get_alpha_index(12)<< 20 )
  945. | ( get_alpha_index(13)<< 23 ) | ( get_alpha_index(14)<< 26 ) | ( get_alpha_index(15)<< 29 );
  946. }
  947. void block_package5( out uint4 block, uint rotation, uint threadBase )
  948. {
  949. block.x = 0x20 | ( rotation << 6 )
  950. | ( ( get_end_point_l(0).r & 0xFE ) << 7 ) | ( ( get_end_point_h(0).r & 0xFE ) << 14 )
  951. | ( ( get_end_point_l(0).g & 0xFE ) << 21 ) | ( ( get_end_point_h(0).g & 0xFE ) << 28 );
  952. block.y = ( ( get_end_point_h(0).g & 0xFE ) >> 4 ) | ( ( get_end_point_l(0).b & 0xFE ) << 3 )
  953. | ( ( get_end_point_h(0).b & 0xFE ) << 10 ) | ( get_end_point_l(0).a << 18 ) | ( get_end_point_h(0).a << 26 );
  954. block.z = ( get_end_point_h(0).a >> 6 )
  955. | ( get_color_index(0) << 2 ) | ( get_color_index(1) << 3 ) | ( get_color_index(2) << 5 ) | ( get_color_index(3) << 7 )
  956. | ( get_color_index(4) << 9 ) | ( get_color_index(5) << 11 ) | ( get_color_index(6) << 13 ) | ( get_color_index(7) << 15 )
  957. | ( get_color_index(8) << 17 ) | ( get_color_index(9) << 19 ) | ( get_color_index(10)<< 21 ) | ( get_color_index(11)<< 23 )
  958. | ( get_color_index(12)<< 25 ) | ( get_color_index(13)<< 27 ) | ( get_color_index(14)<< 29 ) | ( get_color_index(15)<< 31 );
  959. block.w = ( get_color_index(15)>> 1 ) | ( get_alpha_index(0) << 0 ) | ( get_alpha_index(1) << 2 ) | ( get_alpha_index(2) << 4 )
  960. | ( get_alpha_index(3) << 6 ) | ( get_alpha_index(4) << 8 ) | ( get_alpha_index(5) << 10 ) | ( get_alpha_index(6) << 12 )
  961. | ( get_alpha_index(7) << 14 ) | ( get_alpha_index(8) << 16 ) | ( get_alpha_index(9) << 18 ) | ( get_alpha_index(10)<< 20 )
  962. | ( get_alpha_index(11)<< 22 ) | ( get_alpha_index(12)<< 24 ) | ( get_alpha_index(13)<< 26 ) | ( get_alpha_index(14)<< 28 )
  963. | ( get_alpha_index(15)<< 30 );
  964. }
  965. void block_package6( out uint4 block, uint threadBase )
  966. {
  967. block.x = 0x40
  968. | ( ( get_end_point_l(0).r & 0xFE ) << 6 ) | ( ( get_end_point_h(0).r & 0xFE ) << 13 )
  969. | ( ( get_end_point_l(0).g & 0xFE ) << 20 ) | ( ( get_end_point_h(0).g & 0xFE ) << 27 );
  970. block.y = ( ( get_end_point_h(0).g & 0xFE ) >> 5 ) | ( ( get_end_point_l(0).b & 0xFE ) << 2 )
  971. | ( ( get_end_point_h(0).b & 0xFE ) << 9 ) | ( ( get_end_point_l(0).a & 0xFE ) << 16 )
  972. | ( ( get_end_point_h(0).a & 0xFE ) << 23 )
  973. | ( get_end_point_l(0).r & 0x01 ) << 31;
  974. block.z = ( get_end_point_h(0).r & 0x01 )
  975. | ( get_color_index(0) << 1 ) | ( get_color_index(1) << 4 ) | ( get_color_index(2) << 8 ) | ( get_color_index(3) << 12 )
  976. | ( get_color_index(4) << 16 ) | ( get_color_index(5) << 20 ) | ( get_color_index(6) << 24 ) | ( get_color_index(7) << 28 );
  977. block.w = ( get_color_index(8) << 0 ) | ( get_color_index(9) << 4 ) | ( get_color_index(10)<< 8 ) | ( get_color_index(11)<< 12 )
  978. | ( get_color_index(12)<< 16 ) | ( get_color_index(13)<< 20 ) | ( get_color_index(14)<< 24 ) | ( get_color_index(15)<< 28 );
  979. }
  980. void block_package7( out uint4 block, uint partition, uint threadBase )
  981. {
  982. block.x = 0x80 | ( partition << 8 )
  983. | ( ( get_end_point_l(0).r & 0xF8 ) << 11 ) | ( ( get_end_point_h(0).r & 0xF8 ) << 16 )
  984. | ( ( get_end_point_l(1).r & 0xF8 ) << 21 ) | ( ( get_end_point_h(1).r & 0xF8 ) << 26 );
  985. block.y = ( ( get_end_point_h(1).r & 0xF8 ) >> 6 ) | ( ( get_end_point_l(0).g & 0xF8 ) >> 1 )
  986. | ( ( get_end_point_h(0).g & 0xF8 ) << 4 ) | ( ( get_end_point_l(1).g & 0xF8 ) << 9 )
  987. | ( ( get_end_point_h(1).g & 0xF8 ) << 14 ) | ( ( get_end_point_l(0).b & 0xF8 ) << 19 )
  988. | ( ( get_end_point_h(0).b & 0xF8 ) << 24 );
  989. block.z = ( ( get_end_point_l(1).b & 0xF8 ) >> 3 ) | ( ( get_end_point_h(1).b & 0xF8 ) << 2 )
  990. | ( ( get_end_point_l(0).a & 0xF8 ) << 7 ) | ( ( get_end_point_h(0).a & 0xF8 ) << 12 )
  991. | ( ( get_end_point_l(1).a & 0xF8 ) << 17 ) | ( ( get_end_point_h(1).a & 0xF8 ) << 22 )
  992. | ( ( get_end_point_l(0).r & 0x04 ) << 27 ) | ( ( get_end_point_h(0).r & 0x04 ) << 28 );
  993. block.w = ( ( get_end_point_l(1).r & 0x04 ) >> 2 ) | ( ( get_end_point_h(1).r & 0x04 ) >> 1 )
  994. | ( get_color_index(0) << 2 );
  995. uint i = 1;
  996. for ( ; i <= candidateFixUpIndex1DOrdered[partition][0]; i ++ )
  997. {
  998. block.w |= get_color_index(i) << ( i * 2 + 1 );
  999. }
  1000. for ( ; i < 16; i ++ )
  1001. {
  1002. block.w |= get_color_index(i) << ( i * 2 );
  1003. }
  1004. }