clusterfit.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /* -----------------------------------------------------------------------------
  2. Copyright (c) 2006 Simon Brown si@sjbrown.co.uk
  3. Copyright (c) 2007 Ignacio Castano icastano@nvidia.com
  4. Permission is hereby granted, free of charge, to any person obtaining
  5. a copy of this software and associated documentation files (the
  6. "Software"), to deal in the Software without restriction, including
  7. without limitation the rights to use, copy, modify, merge, publish,
  8. distribute, sublicense, and/or sell copies of the Software, and to
  9. permit persons to whom the Software is furnished to do so, subject to
  10. the following conditions:
  11. The above copyright notice and this permission notice shall be included
  12. in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  14. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  16. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  17. CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  18. TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  19. SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. -------------------------------------------------------------------------- */
  21. #include "clusterfit.h"
  22. #include "colourset.h"
  23. #include "colourblock.h"
  24. #include <cfloat>
  25. namespace squish {
  26. ClusterFit::ClusterFit( ColourSet const* colours, int flags, float* metric )
  27. : ColourFit( colours, flags )
  28. {
  29. // set the iteration count
  30. m_iterationCount = ( m_flags & kColourIterativeClusterFit ) ? kMaxIterations : 1;
  31. // initialise the metric (old perceptual = 0.2126f, 0.7152f, 0.0722f)
  32. if( metric )
  33. m_metric = Vec4( metric[0], metric[1], metric[2], 1.0f );
  34. else
  35. m_metric = VEC4_CONST( 1.0f );
  36. // initialise the best error
  37. m_besterror = VEC4_CONST( FLT_MAX );
  38. // cache some values
  39. int const count = m_colours->GetCount();
  40. Vec3 const* values = m_colours->GetPoints();
  41. // get the covariance matrix
  42. Sym3x3 covariance = ComputeWeightedCovariance( count, values, m_colours->GetWeights() );
  43. // compute the principle component
  44. m_principle = ComputePrincipleComponent( covariance );
  45. }
  46. bool ClusterFit::ConstructOrdering( Vec3 const& axis, int iteration )
  47. {
  48. // cache some values
  49. int const count = m_colours->GetCount();
  50. Vec3 const* values = m_colours->GetPoints();
  51. // build the list of dot products
  52. float dps[16];
  53. u8* order = ( u8* )m_order + 16*iteration;
  54. for( int i = 0; i < count; ++i )
  55. {
  56. dps[i] = Dot( values[i], axis );
  57. order[i] = ( u8 )i;
  58. }
  59. // stable sort using them
  60. for( int i = 0; i < count; ++i )
  61. {
  62. for( int j = i; j > 0 && dps[j] < dps[j - 1]; --j )
  63. {
  64. std::swap( dps[j], dps[j - 1] );
  65. std::swap( order[j], order[j - 1] );
  66. }
  67. }
  68. // check this ordering is unique
  69. for( int it = 0; it < iteration; ++it )
  70. {
  71. u8 const* prev = ( u8* )m_order + 16*it;
  72. bool same = true;
  73. for( int i = 0; i < count; ++i )
  74. {
  75. if( order[i] != prev[i] )
  76. {
  77. same = false;
  78. break;
  79. }
  80. }
  81. if( same )
  82. return false;
  83. }
  84. // copy the ordering and weight all the points
  85. Vec3 const* unweighted = m_colours->GetPoints();
  86. float const* weights = m_colours->GetWeights();
  87. m_xsum_wsum = VEC4_CONST( 0.0f );
  88. for( int i = 0; i < count; ++i )
  89. {
  90. int j = order[i];
  91. Vec4 p( unweighted[j].X(), unweighted[j].Y(), unweighted[j].Z(), 1.0f );
  92. Vec4 w( weights[j] );
  93. Vec4 x = p*w;
  94. m_points_weights[i] = x;
  95. m_xsum_wsum += x;
  96. }
  97. return true;
  98. }
  99. void ClusterFit::Compress3( void* block )
  100. {
  101. // declare variables
  102. int const count = m_colours->GetCount();
  103. Vec4 const two = VEC4_CONST( 2.0 );
  104. Vec4 const one = VEC4_CONST( 1.0f );
  105. Vec4 const half_half2( 0.5f, 0.5f, 0.5f, 0.25f );
  106. Vec4 const zero = VEC4_CONST( 0.0f );
  107. Vec4 const half = VEC4_CONST( 0.5f );
  108. Vec4 const grid( 31.0f, 63.0f, 31.0f, 0.0f );
  109. Vec4 const gridrcp( 1.0f/31.0f, 1.0f/63.0f, 1.0f/31.0f, 0.0f );
  110. // prepare an ordering using the principle axis
  111. ConstructOrdering( m_principle, 0 );
  112. // check all possible clusters and iterate on the total order
  113. Vec4 beststart = VEC4_CONST( 0.0f );
  114. Vec4 bestend = VEC4_CONST( 0.0f );
  115. Vec4 besterror = m_besterror;
  116. u8 bestindices[16];
  117. int bestiteration = 0;
  118. int besti = 0, bestj = 0;
  119. // loop over iterations (we avoid the case that all points in first or last cluster)
  120. for( int iterationIndex = 0;; )
  121. {
  122. // first cluster [0,i) is at the start
  123. Vec4 part0 = VEC4_CONST( 0.0f );
  124. for( int i = 0; i < count; ++i )
  125. {
  126. // second cluster [i,j) is half along
  127. Vec4 part1 = ( i == 0 ) ? m_points_weights[0] : VEC4_CONST( 0.0f );
  128. int jmin = ( i == 0 ) ? 1 : i;
  129. for( int j = jmin;; )
  130. {
  131. // last cluster [j,count) is at the end
  132. Vec4 part2 = m_xsum_wsum - part1 - part0;
  133. // compute least squares terms directly
  134. Vec4 alphax_sum = MultiplyAdd( part1, half_half2, part0 );
  135. Vec4 alpha2_sum = alphax_sum.SplatW();
  136. Vec4 betax_sum = MultiplyAdd( part1, half_half2, part2 );
  137. Vec4 beta2_sum = betax_sum.SplatW();
  138. Vec4 alphabeta_sum = ( part1*half_half2 ).SplatW();
  139. // compute the least-squares optimal points
  140. Vec4 factor = Reciprocal( NegativeMultiplySubtract( alphabeta_sum, alphabeta_sum, alpha2_sum*beta2_sum ) );
  141. Vec4 a = NegativeMultiplySubtract( betax_sum, alphabeta_sum, alphax_sum*beta2_sum )*factor;
  142. Vec4 b = NegativeMultiplySubtract( alphax_sum, alphabeta_sum, betax_sum*alpha2_sum )*factor;
  143. // clamp to the grid
  144. a = Min( one, Max( zero, a ) );
  145. b = Min( one, Max( zero, b ) );
  146. a = Truncate( MultiplyAdd( grid, a, half ) )*gridrcp;
  147. b = Truncate( MultiplyAdd( grid, b, half ) )*gridrcp;
  148. // compute the error (we skip the constant xxsum)
  149. Vec4 e1 = MultiplyAdd( a*a, alpha2_sum, b*b*beta2_sum );
  150. Vec4 e2 = NegativeMultiplySubtract( a, alphax_sum, a*b*alphabeta_sum );
  151. Vec4 e3 = NegativeMultiplySubtract( b, betax_sum, e2 );
  152. Vec4 e4 = MultiplyAdd( two, e3, e1 );
  153. // apply the metric to the error term
  154. Vec4 e5 = e4*m_metric;
  155. Vec4 error = e5.SplatX() + e5.SplatY() + e5.SplatZ();
  156. // keep the solution if it wins
  157. if( CompareAnyLessThan( error, besterror ) )
  158. {
  159. beststart = a;
  160. bestend = b;
  161. besti = i;
  162. bestj = j;
  163. besterror = error;
  164. bestiteration = iterationIndex;
  165. }
  166. // advance
  167. if( j == count )
  168. break;
  169. part1 += m_points_weights[j];
  170. ++j;
  171. }
  172. // advance
  173. part0 += m_points_weights[i];
  174. }
  175. // stop if we didn't improve in this iteration
  176. if( bestiteration != iterationIndex )
  177. break;
  178. // advance if possible
  179. ++iterationIndex;
  180. if( iterationIndex == m_iterationCount )
  181. break;
  182. // stop if a new iteration is an ordering that has already been tried
  183. Vec3 axis = ( bestend - beststart ).GetVec3();
  184. if( !ConstructOrdering( axis, iterationIndex ) )
  185. break;
  186. }
  187. // save the block if necessary
  188. if( CompareAnyLessThan( besterror, m_besterror ) )
  189. {
  190. // remap the indices
  191. u8 const* order = ( u8* )m_order + 16*bestiteration;
  192. u8 unordered[16];
  193. for( int m = 0; m < besti; ++m )
  194. unordered[order[m]] = 0;
  195. for( int m = besti; m < bestj; ++m )
  196. unordered[order[m]] = 2;
  197. for( int m = bestj; m < count; ++m )
  198. unordered[order[m]] = 1;
  199. m_colours->RemapIndices( unordered, bestindices );
  200. // save the block
  201. WriteColourBlock3( beststart.GetVec3(), bestend.GetVec3(), bestindices, block );
  202. // save the error
  203. m_besterror = besterror;
  204. }
  205. }
  206. void ClusterFit::Compress4( void* block )
  207. {
  208. // declare variables
  209. int const count = m_colours->GetCount();
  210. Vec4 const two = VEC4_CONST( 2.0f );
  211. Vec4 const one = VEC4_CONST( 1.0f );
  212. Vec4 const onethird_onethird2( 1.0f/3.0f, 1.0f/3.0f, 1.0f/3.0f, 1.0f/9.0f );
  213. Vec4 const twothirds_twothirds2( 2.0f/3.0f, 2.0f/3.0f, 2.0f/3.0f, 4.0f/9.0f );
  214. Vec4 const twonineths = VEC4_CONST( 2.0f/9.0f );
  215. Vec4 const zero = VEC4_CONST( 0.0f );
  216. Vec4 const half = VEC4_CONST( 0.5f );
  217. Vec4 const grid( 31.0f, 63.0f, 31.0f, 0.0f );
  218. Vec4 const gridrcp( 1.0f/31.0f, 1.0f/63.0f, 1.0f/31.0f, 0.0f );
  219. // prepare an ordering using the principle axis
  220. ConstructOrdering( m_principle, 0 );
  221. // check all possible clusters and iterate on the total order
  222. Vec4 beststart = VEC4_CONST( 0.0f );
  223. Vec4 bestend = VEC4_CONST( 0.0f );
  224. Vec4 besterror = m_besterror;
  225. u8 bestindices[16];
  226. int bestiteration = 0;
  227. int besti = 0, bestj = 0, bestk = 0;
  228. // loop over iterations (we avoid the case that all points in first or last cluster)
  229. for( int iterationIndex = 0;; )
  230. {
  231. // first cluster [0,i) is at the start
  232. Vec4 part0 = VEC4_CONST( 0.0f );
  233. for( int i = 0; i < count; ++i )
  234. {
  235. // second cluster [i,j) is one third along
  236. Vec4 part1 = VEC4_CONST( 0.0f );
  237. for( int j = i;; )
  238. {
  239. // third cluster [j,k) is two thirds along
  240. Vec4 part2 = ( j == 0 ) ? m_points_weights[0] : VEC4_CONST( 0.0f );
  241. int kmin = ( j == 0 ) ? 1 : j;
  242. for( int k = kmin;; )
  243. {
  244. // last cluster [k,count) is at the end
  245. Vec4 part3 = m_xsum_wsum - part2 - part1 - part0;
  246. // compute least squares terms directly
  247. Vec4 const alphax_sum = MultiplyAdd( part2, onethird_onethird2, MultiplyAdd( part1, twothirds_twothirds2, part0 ) );
  248. Vec4 const alpha2_sum = alphax_sum.SplatW();
  249. Vec4 const betax_sum = MultiplyAdd( part1, onethird_onethird2, MultiplyAdd( part2, twothirds_twothirds2, part3 ) );
  250. Vec4 const beta2_sum = betax_sum.SplatW();
  251. Vec4 const alphabeta_sum = twonineths*( part1 + part2 ).SplatW();
  252. // compute the least-squares optimal points
  253. Vec4 factor = Reciprocal( NegativeMultiplySubtract( alphabeta_sum, alphabeta_sum, alpha2_sum*beta2_sum ) );
  254. Vec4 a = NegativeMultiplySubtract( betax_sum, alphabeta_sum, alphax_sum*beta2_sum )*factor;
  255. Vec4 b = NegativeMultiplySubtract( alphax_sum, alphabeta_sum, betax_sum*alpha2_sum )*factor;
  256. // clamp to the grid
  257. a = Min( one, Max( zero, a ) );
  258. b = Min( one, Max( zero, b ) );
  259. a = Truncate( MultiplyAdd( grid, a, half ) )*gridrcp;
  260. b = Truncate( MultiplyAdd( grid, b, half ) )*gridrcp;
  261. // compute the error (we skip the constant xxsum)
  262. Vec4 e1 = MultiplyAdd( a*a, alpha2_sum, b*b*beta2_sum );
  263. Vec4 e2 = NegativeMultiplySubtract( a, alphax_sum, a*b*alphabeta_sum );
  264. Vec4 e3 = NegativeMultiplySubtract( b, betax_sum, e2 );
  265. Vec4 e4 = MultiplyAdd( two, e3, e1 );
  266. // apply the metric to the error term
  267. Vec4 e5 = e4*m_metric;
  268. Vec4 error = e5.SplatX() + e5.SplatY() + e5.SplatZ();
  269. // keep the solution if it wins
  270. if( CompareAnyLessThan( error, besterror ) )
  271. {
  272. beststart = a;
  273. bestend = b;
  274. besterror = error;
  275. besti = i;
  276. bestj = j;
  277. bestk = k;
  278. bestiteration = iterationIndex;
  279. }
  280. // advance
  281. if( k == count )
  282. break;
  283. part2 += m_points_weights[k];
  284. ++k;
  285. }
  286. // advance
  287. if( j == count )
  288. break;
  289. part1 += m_points_weights[j];
  290. ++j;
  291. }
  292. // advance
  293. part0 += m_points_weights[i];
  294. }
  295. // stop if we didn't improve in this iteration
  296. if( bestiteration != iterationIndex )
  297. break;
  298. // advance if possible
  299. ++iterationIndex;
  300. if( iterationIndex == m_iterationCount )
  301. break;
  302. // stop if a new iteration is an ordering that has already been tried
  303. Vec3 axis = ( bestend - beststart ).GetVec3();
  304. if( !ConstructOrdering( axis, iterationIndex ) )
  305. break;
  306. }
  307. // save the block if necessary
  308. if( CompareAnyLessThan( besterror, m_besterror ) )
  309. {
  310. // remap the indices
  311. u8 const* order = ( u8* )m_order + 16*bestiteration;
  312. u8 unordered[16];
  313. for( int m = 0; m < besti; ++m )
  314. unordered[order[m]] = 0;
  315. for( int m = besti; m < bestj; ++m )
  316. unordered[order[m]] = 2;
  317. for( int m = bestj; m < bestk; ++m )
  318. unordered[order[m]] = 3;
  319. for( int m = bestk; m < count; ++m )
  320. unordered[order[m]] = 1;
  321. m_colours->RemapIndices( unordered, bestindices );
  322. // save the block
  323. WriteColourBlock4( beststart.GetVec3(), bestend.GetVec3(), bestindices, block );
  324. // save the error
  325. m_besterror = besterror;
  326. }
  327. }
  328. } // namespace squish