Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
BC.cpp
Go to the documentation of this file.
1 //-------------------------------------------------------------------------------------
2 // BC.cpp
3 //
4 // Block-compression (BC) functionality for BC1, BC2, BC3 (orginal DXTn formats)
5 //
6 // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
7 // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
8 // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
9 // PARTICULAR PURPOSE.
10 //
11 // Copyright (c) Microsoft Corporation. All rights reserved.
12 //
13 // http://go.microsoft.com/fwlink/?LinkId=248926
14 //-------------------------------------------------------------------------------------
15 
16 #include "directxtexp.h"
17 
18 // Experiemental encoding variants, not enabled by default
19 //#define COLOR_WEIGHTS
20 //#define COLOR_AVG_0WEIGHTS
21 
22 #include "BC.h"
23 
24 using namespace DirectX::PackedVector;
25 
26 namespace DirectX
27 {
28 
29 //-------------------------------------------------------------------------------------
30 // Constants
31 //-------------------------------------------------------------------------------------
32 
33 // Perceptual weightings for the importance of each channel.
34 static const HDRColorA g_Luminance (0.2125f / 0.7154f, 1.0f, 0.0721f / 0.7154f, 1.0f);
35 static const HDRColorA g_LuminanceInv(0.7154f / 0.2125f, 1.0f, 0.7154f / 0.0721f, 1.0f);
36 
37 //-------------------------------------------------------------------------------------
38 // Decode/Encode RGB 5/6/5 colors
39 //-------------------------------------------------------------------------------------
40 inline static void Decode565(_Out_ HDRColorA *pColor, _In_ const uint16_t w565)
41 {
42  pColor->r = (float) ((w565 >> 11) & 31) * (1.0f / 31.0f);
43  pColor->g = (float) ((w565 >> 5) & 63) * (1.0f / 63.0f);
44  pColor->b = (float) ((w565 >> 0) & 31) * (1.0f / 31.0f);
45  pColor->a = 1.0f;
46 }
47 
48 inline static uint16_t Encode565(_In_ const HDRColorA *pColor)
49 {
51 
52  Color.r = (pColor->r < 0.0f) ? 0.0f : (pColor->r > 1.0f) ? 1.0f : pColor->r;
53  Color.g = (pColor->g < 0.0f) ? 0.0f : (pColor->g > 1.0f) ? 1.0f : pColor->g;
54  Color.b = (pColor->b < 0.0f) ? 0.0f : (pColor->b > 1.0f) ? 1.0f : pColor->b;
55 
56  uint16_t w;
57 
58  w = (uint16_t) ((static_cast<int32_t>(Color.r * 31.0f + 0.5f) << 11) |
59  (static_cast<int32_t>(Color.g * 63.0f + 0.5f) << 5) |
60  (static_cast<int32_t>(Color.b * 31.0f + 0.5f) << 0));
61 
62  return w;
63 }
64 
65 
66 //-------------------------------------------------------------------------------------
67 static void OptimizeRGB(_Out_ HDRColorA *pX, _Out_ HDRColorA *pY,
68  _In_reads_(NUM_PIXELS_PER_BLOCK) const HDRColorA *pPoints, _In_ size_t cSteps, _In_ DWORD flags)
69 {
70  static const float fEpsilon = (0.25f / 64.0f) * (0.25f / 64.0f);
71  static const float pC3[] = { 2.0f/2.0f, 1.0f/2.0f, 0.0f/2.0f };
72  static const float pD3[] = { 0.0f/2.0f, 1.0f/2.0f, 2.0f/2.0f };
73  static const float pC4[] = { 3.0f/3.0f, 2.0f/3.0f, 1.0f/3.0f, 0.0f/3.0f };
74  static const float pD4[] = { 0.0f/3.0f, 1.0f/3.0f, 2.0f/3.0f, 3.0f/3.0f };
75 
76  const float *pC = (3 == cSteps) ? pC3 : pC4;
77  const float *pD = (3 == cSteps) ? pD3 : pD4;
78 
79  // Find Min and Max points, as starting point
80  HDRColorA X = (flags & BC_FLAGS_UNIFORM) ? HDRColorA(1.f, 1.f, 1.f, 1.f) : g_Luminance;
81  HDRColorA Y = HDRColorA(0.0f, 0.0f, 0.0f, 1.0f);
82 
83  for(size_t iPoint = 0; iPoint < NUM_PIXELS_PER_BLOCK; iPoint++)
84  {
85 #ifdef COLOR_WEIGHTS
86  if(pPoints[iPoint].a > 0.0f)
87 #endif // COLOR_WEIGHTS
88  {
89  if(pPoints[iPoint].r < X.r)
90  X.r = pPoints[iPoint].r;
91 
92  if(pPoints[iPoint].g < X.g)
93  X.g = pPoints[iPoint].g;
94 
95  if(pPoints[iPoint].b < X.b)
96  X.b = pPoints[iPoint].b;
97 
98  if(pPoints[iPoint].r > Y.r)
99  Y.r = pPoints[iPoint].r;
100 
101  if(pPoints[iPoint].g > Y.g)
102  Y.g = pPoints[iPoint].g;
103 
104  if(pPoints[iPoint].b > Y.b)
105  Y.b = pPoints[iPoint].b;
106  }
107  }
108 
109  // Diagonal axis
110  HDRColorA AB;
111 
112  AB.r = Y.r - X.r;
113  AB.g = Y.g - X.g;
114  AB.b = Y.b - X.b;
115 
116  float fAB = AB.r * AB.r + AB.g * AB.g + AB.b * AB.b;
117 
118  // Single color block.. no need to root-find
119  if(fAB < FLT_MIN)
120  {
121  pX->r = X.r; pX->g = X.g; pX->b = X.b;
122  pY->r = Y.r; pY->g = Y.g; pY->b = Y.b;
123  return;
124  }
125 
126  // Try all four axis directions, to determine which diagonal best fits data
127  float fABInv = 1.0f / fAB;
128 
129  HDRColorA Dir;
130  Dir.r = AB.r * fABInv;
131  Dir.g = AB.g * fABInv;
132  Dir.b = AB.b * fABInv;
133 
134  HDRColorA Mid;
135  Mid.r = (X.r + Y.r) * 0.5f;
136  Mid.g = (X.g + Y.g) * 0.5f;
137  Mid.b = (X.b + Y.b) * 0.5f;
138 
139  float fDir[4];
140  fDir[0] = fDir[1] = fDir[2] = fDir[3] = 0.0f;
141 
142 
143  for(size_t iPoint = 0; iPoint < NUM_PIXELS_PER_BLOCK; iPoint++)
144  {
145  HDRColorA Pt;
146  Pt.r = (pPoints[iPoint].r - Mid.r) * Dir.r;
147  Pt.g = (pPoints[iPoint].g - Mid.g) * Dir.g;
148  Pt.b = (pPoints[iPoint].b - Mid.b) * Dir.b;
149 
150  float f;
151 
152 #ifdef COLOR_WEIGHTS
153  f = Pt.r + Pt.g + Pt.b;
154  fDir[0] += pPoints[iPoint].a * f * f;
155 
156  f = Pt.r + Pt.g - Pt.b;
157  fDir[1] += pPoints[iPoint].a * f * f;
158 
159  f = Pt.r - Pt.g + Pt.b;
160  fDir[2] += pPoints[iPoint].a * f * f;
161 
162  f = Pt.r - Pt.g - Pt.b;
163  fDir[3] += pPoints[iPoint].a * f * f;
164 #else
165  f = Pt.r + Pt.g + Pt.b;
166  fDir[0] += f * f;
167 
168  f = Pt.r + Pt.g - Pt.b;
169  fDir[1] += f * f;
170 
171  f = Pt.r - Pt.g + Pt.b;
172  fDir[2] += f * f;
173 
174  f = Pt.r - Pt.g - Pt.b;
175  fDir[3] += f * f;
176 #endif // COLOR_WEIGHTS
177  }
178 
179  float fDirMax = fDir[0];
180  size_t iDirMax = 0;
181 
182  for(size_t iDir = 1; iDir < 4; iDir++)
183  {
184  if(fDir[iDir] > fDirMax)
185  {
186  fDirMax = fDir[iDir];
187  iDirMax = iDir;
188  }
189  }
190 
191  if(iDirMax & 2)
192  {
193  float f = X.g; X.g = Y.g; Y.g = f;
194  }
195 
196  if(iDirMax & 1)
197  {
198  float f = X.b; X.b = Y.b; Y.b = f;
199  }
200 
201 
202  // Two color block.. no need to root-find
203  if(fAB < 1.0f / 4096.0f)
204  {
205  pX->r = X.r; pX->g = X.g; pX->b = X.b;
206  pY->r = Y.r; pY->g = Y.g; pY->b = Y.b;
207  return;
208  }
209 
210 
211  // Use Newton's Method to find local minima of sum-of-squares error.
212  float fSteps = (float) (cSteps - 1);
213 
214  for(size_t iIteration = 0; iIteration < 8; iIteration++)
215  {
216  // Calculate new steps
217  HDRColorA pSteps[4];
218 
219  for(size_t iStep = 0; iStep < cSteps; iStep++)
220  {
221  pSteps[iStep].r = X.r * pC[iStep] + Y.r * pD[iStep];
222  pSteps[iStep].g = X.g * pC[iStep] + Y.g * pD[iStep];
223  pSteps[iStep].b = X.b * pC[iStep] + Y.b * pD[iStep];
224  }
225 
226 
227  // Calculate color direction
228  Dir.r = Y.r - X.r;
229  Dir.g = Y.g - X.g;
230  Dir.b = Y.b - X.b;
231 
232  float fLen = (Dir.r * Dir.r + Dir.g * Dir.g + Dir.b * Dir.b);
233 
234  if(fLen < (1.0f / 4096.0f))
235  break;
236 
237  float fScale = fSteps / fLen;
238 
239  Dir.r *= fScale;
240  Dir.g *= fScale;
241  Dir.b *= fScale;
242 
243 
244  // Evaluate function, and derivatives
245  float d2X, d2Y;
246  HDRColorA dX, dY;
247  d2X = d2Y = dX.r = dX.g = dX.b = dY.r = dY.g = dY.b = 0.0f;
248 
249  for(size_t iPoint = 0; iPoint < NUM_PIXELS_PER_BLOCK; iPoint++)
250  {
251  float fDot = (pPoints[iPoint].r - X.r) * Dir.r +
252  (pPoints[iPoint].g - X.g) * Dir.g +
253  (pPoints[iPoint].b - X.b) * Dir.b;
254 
255 
256  size_t iStep;
257  if(fDot <= 0.0f)
258  iStep = 0;
259  else if(fDot >= fSteps)
260  iStep = cSteps - 1;
261  else
262  iStep = static_cast<size_t>(fDot + 0.5f);
263 
264 
265  HDRColorA Diff;
266  Diff.r = pSteps[iStep].r - pPoints[iPoint].r;
267  Diff.g = pSteps[iStep].g - pPoints[iPoint].g;
268  Diff.b = pSteps[iStep].b - pPoints[iPoint].b;
269 
270 #ifdef COLOR_WEIGHTS
271  float fC = pC[iStep] * pPoints[iPoint].a * (1.0f / 8.0f);
272  float fD = pD[iStep] * pPoints[iPoint].a * (1.0f / 8.0f);
273 #else
274  float fC = pC[iStep] * (1.0f / 8.0f);
275  float fD = pD[iStep] * (1.0f / 8.0f);
276 #endif // COLOR_WEIGHTS
277 
278  d2X += fC * pC[iStep];
279  dX.r += fC * Diff.r;
280  dX.g += fC * Diff.g;
281  dX.b += fC * Diff.b;
282 
283  d2Y += fD * pD[iStep];
284  dY.r += fD * Diff.r;
285  dY.g += fD * Diff.g;
286  dY.b += fD * Diff.b;
287  }
288 
289 
290  // Move endpoints
291  if(d2X > 0.0f)
292  {
293  float f = -1.0f / d2X;
294 
295  X.r += dX.r * f;
296  X.g += dX.g * f;
297  X.b += dX.b * f;
298  }
299 
300  if(d2Y > 0.0f)
301  {
302  float f = -1.0f / d2Y;
303 
304  Y.r += dY.r * f;
305  Y.g += dY.g * f;
306  Y.b += dY.b * f;
307  }
308 
309  if((dX.r * dX.r < fEpsilon) && (dX.g * dX.g < fEpsilon) && (dX.b * dX.b < fEpsilon) &&
310  (dY.r * dY.r < fEpsilon) && (dY.g * dY.g < fEpsilon) && (dY.b * dY.b < fEpsilon))
311  {
312  break;
313  }
314  }
315 
316  pX->r = X.r; pX->g = X.g; pX->b = X.b;
317  pY->r = Y.r; pY->g = Y.g; pY->b = Y.b;
318 }
319 
320 
321 //-------------------------------------------------------------------------------------
322 inline static void DecodeBC1( _Out_writes_(NUM_PIXELS_PER_BLOCK) XMVECTOR *pColor, _In_ const D3DX_BC1 *pBC, _In_ bool isbc1 )
323 {
324  assert( pColor && pBC );
325  static_assert( sizeof(D3DX_BC1) == 8, "D3DX_BC1 should be 8 bytes" );
326 
327  static XMVECTORF32 s_Scale = { 1.f/31.f, 1.f/63.f, 1.f/31.f, 1.f };
328 
329  XMVECTOR clr0 = XMLoadU565( reinterpret_cast<const XMU565*>(&pBC->rgb[0]) );
330  XMVECTOR clr1 = XMLoadU565( reinterpret_cast<const XMU565*>(&pBC->rgb[1]) );
331 
332  clr0 = XMVectorMultiply( clr0, s_Scale );
333  clr1 = XMVectorMultiply( clr1, s_Scale );
334 
335  clr0 = XMVectorSwizzle<2, 1, 0, 3>( clr0 );
336  clr1 = XMVectorSwizzle<2, 1, 0, 3>( clr1 );
337 
338  clr0 = XMVectorSelect( g_XMIdentityR3, clr0, g_XMSelect1110 );
339  clr1 = XMVectorSelect( g_XMIdentityR3, clr1, g_XMSelect1110 );
340 
341  XMVECTOR clr2, clr3;
342  if ( isbc1 && (pBC->rgb[0] <= pBC->rgb[1]) )
343  {
344  clr2 = XMVectorLerp( clr0, clr1, 0.5f );
345  clr3 = XMVectorZero(); // Alpha of 0
346  }
347  else
348  {
349  clr2 = XMVectorLerp( clr0, clr1, 1.f/3.f );
350  clr3 = XMVectorLerp( clr0, clr1, 2.f/3.f );
351  }
352 
353  uint32_t dw = pBC->bitmap;
354 
355  for(size_t i = 0; i < NUM_PIXELS_PER_BLOCK; ++i, dw >>= 2)
356  {
357  switch(dw & 3)
358  {
359  case 0: pColor[i] = clr0; break;
360  case 1: pColor[i] = clr1; break;
361  case 2: pColor[i] = clr2; break;
362 
363  case 3:
364  default: pColor[i] = clr3; break;
365  }
366  }
367 }
368 
369 
370 //-------------------------------------------------------------------------------------
371 
372 static void EncodeBC1(_Out_ D3DX_BC1 *pBC, _In_reads_(NUM_PIXELS_PER_BLOCK) const HDRColorA *pColor,
373  _In_ bool bColorKey, _In_ float alphaRef, _In_ DWORD flags)
374 {
375  assert( pBC && pColor );
376  static_assert( sizeof(D3DX_BC1) == 8, "D3DX_BC1 should be 8 bytes" );
377 
378  // Determine if we need to colorkey this block
379  size_t uSteps;
380 
381  if (bColorKey)
382  {
383  size_t uColorKey = 0;
384 
385  for(size_t i = 0; i < NUM_PIXELS_PER_BLOCK; ++i)
386  {
387  if(pColor[i].a < alphaRef)
388  uColorKey++;
389  }
390 
391  if(NUM_PIXELS_PER_BLOCK == uColorKey)
392  {
393  pBC->rgb[0] = 0x0000;
394  pBC->rgb[1] = 0xffff;
395  pBC->bitmap = 0xffffffff;
396  return;
397  }
398 
399  uSteps = (uColorKey > 0) ? 3 : 4;
400  }
401  else
402  {
403  uSteps = 4;
404  }
405 
406  // Quantize block to R56B5, using Floyd Stienberg error diffusion. This
407  // increases the chance that colors will map directly to the quantized
408  // axis endpoints.
411 
412  if (flags & BC_FLAGS_DITHER_RGB)
413  memset(Error, 0x00, NUM_PIXELS_PER_BLOCK * sizeof(HDRColorA));
414 
415  size_t i;
416  for(i = 0; i < NUM_PIXELS_PER_BLOCK; ++i)
417  {
418  HDRColorA Clr;
419  Clr.r = pColor[i].r;
420  Clr.g = pColor[i].g;
421  Clr.b = pColor[i].b;
422 
423  if (flags & BC_FLAGS_DITHER_RGB)
424  {
425  Clr.r += Error[i].r;
426  Clr.g += Error[i].g;
427  Clr.b += Error[i].b;
428  }
429 
430  Color[i].r = (float) static_cast<int32_t>(Clr.r * 31.0f + 0.5f) * (1.0f / 31.0f);
431  Color[i].g = (float) static_cast<int32_t>(Clr.g * 63.0f + 0.5f) * (1.0f / 63.0f);
432  Color[i].b = (float) static_cast<int32_t>(Clr.b * 31.0f + 0.5f) * (1.0f / 31.0f);
433 
434 #ifdef COLOR_WEIGHTS
435  Color[i].a = pColor[i].a;
436 #else
437  Color[i].a = 1.0f;
438 #endif // COLOR_WEIGHTS
439 
440  if (flags & BC_FLAGS_DITHER_RGB)
441  {
442  HDRColorA Diff;
443  Diff.r = Color[i].a * (Clr.r - Color[i].r);
444  Diff.g = Color[i].a * (Clr.g - Color[i].g);
445  Diff.b = Color[i].a * (Clr.b - Color[i].b);
446 
447  if(3 != (i & 3))
448  {
449  assert( i < 15 );
450  _Analysis_assume_( i < 15 );
451  Error[i + 1].r += Diff.r * (7.0f / 16.0f);
452  Error[i + 1].g += Diff.g * (7.0f / 16.0f);
453  Error[i + 1].b += Diff.b * (7.0f / 16.0f);
454  }
455 
456  if(i < 12)
457  {
458  if(i & 3)
459  {
460  Error[i + 3].r += Diff.r * (3.0f / 16.0f);
461  Error[i + 3].g += Diff.g * (3.0f / 16.0f);
462  Error[i + 3].b += Diff.b * (3.0f / 16.0f);
463  }
464 
465  Error[i + 4].r += Diff.r * (5.0f / 16.0f);
466  Error[i + 4].g += Diff.g * (5.0f / 16.0f);
467  Error[i + 4].b += Diff.b * (5.0f / 16.0f);
468 
469  if(3 != (i & 3))
470  {
471  assert( i < 11 );
472  _Analysis_assume_( i < 11 );
473  Error[i + 5].r += Diff.r * (1.0f / 16.0f);
474  Error[i + 5].g += Diff.g * (1.0f / 16.0f);
475  Error[i + 5].b += Diff.b * (1.0f / 16.0f);
476  }
477  }
478  }
479 
480  if ( !( flags & BC_FLAGS_UNIFORM ) )
481  {
482  Color[i].r *= g_Luminance.r;
483  Color[i].g *= g_Luminance.g;
484  Color[i].b *= g_Luminance.b;
485  }
486  }
487 
488  // Perform 6D root finding function to find two endpoints of color axis.
489  // Then quantize and sort the endpoints depending on mode.
490  HDRColorA ColorA, ColorB, ColorC, ColorD;
491 
492  OptimizeRGB(&ColorA, &ColorB, Color, uSteps, flags);
493 
494  if ( flags & BC_FLAGS_UNIFORM )
495  {
496  ColorC = ColorA;
497  ColorD = ColorB;
498  }
499  else
500  {
501  ColorC.r = ColorA.r * g_LuminanceInv.r;
502  ColorC.g = ColorA.g * g_LuminanceInv.g;
503  ColorC.b = ColorA.b * g_LuminanceInv.b;
504 
505  ColorD.r = ColorB.r * g_LuminanceInv.r;
506  ColorD.g = ColorB.g * g_LuminanceInv.g;
507  ColorD.b = ColorB.b * g_LuminanceInv.b;
508  }
509 
510  uint16_t wColorA = Encode565(&ColorC);
511  uint16_t wColorB = Encode565(&ColorD);
512 
513  if((uSteps == 4) && (wColorA == wColorB))
514  {
515  pBC->rgb[0] = wColorA;
516  pBC->rgb[1] = wColorB;
517  pBC->bitmap = 0x00000000;
518  return;
519  }
520 
521  Decode565(&ColorC, wColorA);
522  Decode565(&ColorD, wColorB);
523 
524  if ( flags & BC_FLAGS_UNIFORM )
525  {
526  ColorA = ColorC;
527  ColorB = ColorD;
528  }
529  else
530  {
531  ColorA.r = ColorC.r * g_Luminance.r;
532  ColorA.g = ColorC.g * g_Luminance.g;
533  ColorA.b = ColorC.b * g_Luminance.b;
534 
535  ColorB.r = ColorD.r * g_Luminance.r;
536  ColorB.g = ColorD.g * g_Luminance.g;
537  ColorB.b = ColorD.b * g_Luminance.b;
538  }
539 
540  // Calculate color steps
541  HDRColorA Step[4];
542 
543  if((3 == uSteps) == (wColorA <= wColorB))
544  {
545  pBC->rgb[0] = wColorA;
546  pBC->rgb[1] = wColorB;
547 
548  Step[0] = ColorA;
549  Step[1] = ColorB;
550  }
551  else
552  {
553  pBC->rgb[0] = wColorB;
554  pBC->rgb[1] = wColorA;
555 
556  Step[0] = ColorB;
557  Step[1] = ColorA;
558  }
559 
560  static const size_t pSteps3[] = { 0, 2, 1 };
561  static const size_t pSteps4[] = { 0, 2, 3, 1 };
562  const size_t *pSteps;
563 
564  if(3 == uSteps)
565  {
566  pSteps = pSteps3;
567 
568  HDRColorALerp(&Step[2], &Step[0], &Step[1], 0.5f);
569  }
570  else
571  {
572  pSteps = pSteps4;
573 
574  HDRColorALerp(&Step[2], &Step[0], &Step[1], 1.0f / 3.0f);
575  HDRColorALerp(&Step[3], &Step[0], &Step[1], 2.0f / 3.0f);
576  }
577 
578  // Calculate color direction
579  HDRColorA Dir;
580 
581  Dir.r = Step[1].r - Step[0].r;
582  Dir.g = Step[1].g - Step[0].g;
583  Dir.b = Step[1].b - Step[0].b;
584 
585  float fSteps = (float) (uSteps - 1);
586  float fScale = (wColorA != wColorB) ? (fSteps / (Dir.r * Dir.r + Dir.g * Dir.g + Dir.b * Dir.b)) : 0.0f;
587 
588  Dir.r *= fScale;
589  Dir.g *= fScale;
590  Dir.b *= fScale;
591 
592  // Encode colors
593  uint32_t dw = 0;
594  if (flags & BC_FLAGS_DITHER_RGB)
595  memset(Error, 0x00, NUM_PIXELS_PER_BLOCK * sizeof(HDRColorA));
596 
597  for(i = 0; i < NUM_PIXELS_PER_BLOCK; ++i)
598  {
599  if((3 == uSteps) && (pColor[i].a < alphaRef))
600  {
601  dw = (3 << 30) | (dw >> 2);
602  }
603  else
604  {
605  HDRColorA Clr;
606  if ( flags & BC_FLAGS_UNIFORM )
607  {
608  Clr.r = pColor[i].r;
609  Clr.g = pColor[i].g;
610  Clr.b = pColor[i].b;
611  }
612  else
613  {
614  Clr.r = pColor[i].r * g_Luminance.r;
615  Clr.g = pColor[i].g * g_Luminance.g;
616  Clr.b = pColor[i].b * g_Luminance.b;
617  }
618 
619  if (flags & BC_FLAGS_DITHER_RGB)
620  {
621  Clr.r += Error[i].r;
622  Clr.g += Error[i].g;
623  Clr.b += Error[i].b;
624  }
625 
626  float fDot = (Clr.r - Step[0].r) * Dir.r + (Clr.g - Step[0].g) * Dir.g + (Clr.b - Step[0].b) * Dir.b;
627  uint32_t iStep;
628 
629  if(fDot <= 0.0f)
630  iStep = 0;
631  else if(fDot >= fSteps)
632  iStep = 1;
633  else
634  iStep = static_cast<uint32_t>( pSteps[static_cast<size_t>(fDot + 0.5f)] );
635 
636  dw = (iStep << 30) | (dw >> 2);
637 
638  if (flags & BC_FLAGS_DITHER_RGB)
639  {
640  HDRColorA Diff;
641  Diff.r = Color[i].a * (Clr.r - Step[iStep].r);
642  Diff.g = Color[i].a * (Clr.g - Step[iStep].g);
643  Diff.b = Color[i].a * (Clr.b - Step[iStep].b);
644 
645  if(3 != (i & 3))
646  {
647  Error[i + 1].r += Diff.r * (7.0f / 16.0f);
648  Error[i + 1].g += Diff.g * (7.0f / 16.0f);
649  Error[i + 1].b += Diff.b * (7.0f / 16.0f);
650  }
651 
652  if(i < 12)
653  {
654  if(i & 3)
655  {
656  Error[i + 3].r += Diff.r * (3.0f / 16.0f);
657  Error[i + 3].g += Diff.g * (3.0f / 16.0f);
658  Error[i + 3].b += Diff.b * (3.0f / 16.0f);
659  }
660 
661  Error[i + 4].r += Diff.r * (5.0f / 16.0f);
662  Error[i + 4].g += Diff.g * (5.0f / 16.0f);
663  Error[i + 4].b += Diff.b * (5.0f / 16.0f);
664 
665  if(3 != (i & 3))
666  {
667  Error[i + 5].r += Diff.r * (1.0f / 16.0f);
668  Error[i + 5].g += Diff.g * (1.0f / 16.0f);
669  Error[i + 5].b += Diff.b * (1.0f / 16.0f);
670  }
671  }
672  }
673  }
674  }
675 
676  pBC->bitmap = dw;
677 }
678 
679 //-------------------------------------------------------------------------------------
680 #ifdef COLOR_WEIGHTS
681 static void EncodeSolidBC1(_Out_ D3DX_BC1 *pBC, _In_reads_(NUM_PIXELS_PER_BLOCK) const HDRColorA *pColor)
682 {
683 #ifdef COLOR_AVG_0WEIGHTS
684  // Compute avg color
685  HDRColorA Color;
686  Color.r = pColor[0].r;
687  Color.g = pColor[0].g;
688  Color.b = pColor[0].b;
689 
690  for(size_t i = 1; i < NUM_PIXELS_PER_BLOCK; ++i)
691  {
692  Color.r += pColor[i].r;
693  Color.g += pColor[i].g;
694  Color.b += pColor[i].b;
695  }
696 
697  Color.r *= 1.0f / 16.0f;
698  Color.g *= 1.0f / 16.0f;
699  Color.b *= 1.0f / 16.0f;
700 
701  uint16_t wColor = Encode565(&Color);
702 #else
703  uint16_t wColor = 0x0000;
704 #endif // COLOR_AVG_0WEIGHTS
705 
706  // Encode solid block
707  pBC->rgb[0] = wColor;
708  pBC->rgb[1] = wColor;
709  pBC->bitmap = 0x00000000;
710 }
711 #endif // COLOR_WEIGHTS
712 
713 
714 //=====================================================================================
715 // Entry points
716 //=====================================================================================
717 
718 //-------------------------------------------------------------------------------------
719 // BC1 Compression
720 //-------------------------------------------------------------------------------------
721 _Use_decl_annotations_
722 void D3DXDecodeBC1(XMVECTOR *pColor, const uint8_t *pBC)
723 {
724  auto pBC1 = reinterpret_cast<const D3DX_BC1 *>(pBC);
725  DecodeBC1( pColor, pBC1, true );
726 }
727 
728 _Use_decl_annotations_
729 void D3DXEncodeBC1(uint8_t *pBC, const XMVECTOR *pColor, float alphaRef, DWORD flags)
730 {
731  assert( pBC && pColor );
732 
734 
735  if (flags & BC_FLAGS_DITHER_A)
736  {
737  float fError[NUM_PIXELS_PER_BLOCK];
738  memset(fError, 0x00, NUM_PIXELS_PER_BLOCK * sizeof(float));
739 
740  for(size_t i = 0; i < NUM_PIXELS_PER_BLOCK; ++i)
741  {
742  HDRColorA clr;
743  XMStoreFloat4( reinterpret_cast<XMFLOAT4*>( &clr ), pColor[i] );
744 
745  float fAlph = clr.a + fError[i];
746 
747  Color[i].r = clr.r;
748  Color[i].g = clr.g;
749  Color[i].b = clr.b;
750  Color[i].a = (float) static_cast<int32_t>(clr.a + fError[i] + 0.5f);
751 
752  float fDiff = fAlph - Color[i].a;
753 
754  if(3 != (i & 3))
755  {
756  assert( i < 15 );
757  _Analysis_assume_( i < 15 );
758  fError[i + 1] += fDiff * (7.0f / 16.0f);
759  }
760 
761  if(i < 12)
762  {
763  if(i & 3)
764  fError[i + 3] += fDiff * (3.0f / 16.0f);
765 
766  fError[i + 4] += fDiff * (5.0f / 16.0f);
767 
768  if(3 != (i & 3))
769  {
770  assert( i < 11 );
771  _Analysis_assume_( i < 11 );
772  fError[i + 5] += fDiff * (1.0f / 16.0f);
773  }
774  }
775  }
776  }
777  else
778  {
779  for(size_t i = 0; i < NUM_PIXELS_PER_BLOCK; ++i)
780  {
781  XMStoreFloat4( reinterpret_cast<XMFLOAT4*>( &Color[i] ), pColor[i] );
782  }
783  }
784 
785  auto pBC1 = reinterpret_cast<D3DX_BC1 *>(pBC);
786  EncodeBC1(pBC1, Color, true, alphaRef, flags);
787 }
788 
789 
790 //-------------------------------------------------------------------------------------
791 // BC2 Compression
792 //-------------------------------------------------------------------------------------
793 _Use_decl_annotations_
794 void D3DXDecodeBC2(XMVECTOR *pColor, const uint8_t *pBC)
795 {
796  assert( pColor && pBC );
797  static_assert( sizeof(D3DX_BC2) == 16, "D3DX_BC2 should be 16 bytes" );
798 
799  auto pBC2 = reinterpret_cast<const D3DX_BC2 *>(pBC);
800 
801  // RGB part
802  DecodeBC1(pColor, &pBC2->bc1, false);
803 
804  // 4-bit alpha part
805  DWORD dw = pBC2->bitmap[0];
806 
807  for(size_t i = 0; i < 8; ++i, dw >>= 4)
808  {
809  #pragma prefast(suppress:22103, "writing blocks in two halves confuses tool")
810  pColor[i] = XMVectorSetW( pColor[i], (float) (dw & 0xf) * (1.0f / 15.0f) );
811  }
812 
813  dw = pBC2->bitmap[1];
814 
815  for(size_t i = 8; i < NUM_PIXELS_PER_BLOCK; ++i, dw >>= 4)
816  pColor[i] = XMVectorSetW( pColor[i], (float) (dw & 0xf) * (1.0f / 15.0f) );
817 }
818 
819 _Use_decl_annotations_
820 void D3DXEncodeBC2(uint8_t *pBC, const XMVECTOR *pColor, DWORD flags)
821 {
822  assert( pBC && pColor );
823  static_assert( sizeof(D3DX_BC2) == 16, "D3DX_BC2 should be 16 bytes" );
824 
826  for(size_t i = 0; i < NUM_PIXELS_PER_BLOCK; ++i)
827  {
828  XMStoreFloat4( reinterpret_cast<XMFLOAT4*>( &Color[i] ), pColor[i] );
829  }
830 
831  auto pBC2 = reinterpret_cast<D3DX_BC2 *>(pBC);
832 
833  // 4-bit alpha part. Dithered using Floyd Stienberg error diffusion.
834  pBC2->bitmap[0] = 0;
835  pBC2->bitmap[1] = 0;
836 
837  float fError[NUM_PIXELS_PER_BLOCK];
838  if (flags & BC_FLAGS_DITHER_A)
839  memset(fError, 0x00, NUM_PIXELS_PER_BLOCK * sizeof(float));
840 
841  for(size_t i = 0; i < NUM_PIXELS_PER_BLOCK; ++i)
842  {
843  float fAlph = Color[i].a;
844  if (flags & BC_FLAGS_DITHER_A)
845  fAlph += fError[i];
846 
847  uint32_t u = (uint32_t) static_cast<int32_t>(fAlph * 15.0f + 0.5f);
848 
849  pBC2->bitmap[i >> 3] >>= 4;
850  pBC2->bitmap[i >> 3] |= (u << 28);
851 
852  if (flags & BC_FLAGS_DITHER_A)
853  {
854  float fDiff = fAlph - (float) u * (1.0f / 15.0f);
855 
856  if(3 != (i & 3))
857  {
858  assert( i < 15 );
859  _Analysis_assume_( i < 15 );
860  fError[i + 1] += fDiff * (7.0f / 16.0f);
861  }
862 
863  if(i < 12)
864  {
865  if(i & 3)
866  fError[i + 3] += fDiff * (3.0f / 16.0f);
867 
868  fError[i + 4] += fDiff * (5.0f / 16.0f);
869 
870  if(3 != (i & 3))
871  {
872  assert( i < 11 );
873  _Analysis_assume_( i < 11 );
874  fError[i + 5] += fDiff * (1.0f / 16.0f);
875  }
876  }
877  }
878  }
879 
880  // RGB part
881 #ifdef COLOR_WEIGHTS
882  if(!pBC2->bitmap[0] && !pBC2->bitmap[1])
883  {
884  EncodeSolidBC1(pBC2->dxt1, Color);
885  return;
886  }
887 #endif // COLOR_WEIGHTS
888 
889  EncodeBC1(&pBC2->bc1, Color, false, 0.f, flags);
890 }
891 
892 
893 //-------------------------------------------------------------------------------------
894 // BC3 Compression
895 //-------------------------------------------------------------------------------------
896 _Use_decl_annotations_
897 void D3DXDecodeBC3(XMVECTOR *pColor, const uint8_t *pBC)
898 {
899  assert( pColor && pBC );
900  static_assert( sizeof(D3DX_BC3) == 16, "D3DX_BC3 should be 16 bytes" );
901 
902  auto pBC3 = reinterpret_cast<const D3DX_BC3 *>(pBC);
903 
904  // RGB part
905  DecodeBC1(pColor, &pBC3->bc1, false);
906 
907  // Adaptive 3-bit alpha part
908  float fAlpha[8];
909 
910  fAlpha[0] = ((float) pBC3->alpha[0]) * (1.0f / 255.0f);
911  fAlpha[1] = ((float) pBC3->alpha[1]) * (1.0f / 255.0f);
912 
913  if(pBC3->alpha[0] > pBC3->alpha[1])
914  {
915  for(size_t i = 1; i < 7; ++i)
916  fAlpha[i + 1] = (fAlpha[0] * (7 - i) + fAlpha[1] * i) * (1.0f / 7.0f);
917  }
918  else
919  {
920  for(size_t i = 1; i < 5; ++i)
921  fAlpha[i + 1] = (fAlpha[0] * (5 - i) + fAlpha[1] * i) * (1.0f / 5.0f);
922 
923  fAlpha[6] = 0.0f;
924  fAlpha[7] = 1.0f;
925  }
926 
927  DWORD dw = pBC3->bitmap[0] | (pBC3->bitmap[1] << 8) | (pBC3->bitmap[2] << 16);
928 
929  for(size_t i = 0; i < 8; ++i, dw >>= 3)
930  pColor[i] = XMVectorSetW( pColor[i], fAlpha[dw & 0x7] );
931 
932  dw = pBC3->bitmap[3] | (pBC3->bitmap[4] << 8) | (pBC3->bitmap[5] << 16);
933 
934  for(size_t i = 8; i < NUM_PIXELS_PER_BLOCK; ++i, dw >>= 3)
935  pColor[i] = XMVectorSetW( pColor[i], fAlpha[dw & 0x7] );
936 }
937 
938 _Use_decl_annotations_
939 void D3DXEncodeBC3(uint8_t *pBC, const XMVECTOR *pColor, DWORD flags)
940 {
941  assert( pBC && pColor );
942  static_assert( sizeof(D3DX_BC3) == 16, "D3DX_BC3 should be 16 bytes" );
943 
945  for(size_t i = 0; i < NUM_PIXELS_PER_BLOCK; ++i)
946  {
947  XMStoreFloat4( reinterpret_cast<XMFLOAT4*>( &Color[i] ), pColor[i] );
948  }
949 
950  auto pBC3 = reinterpret_cast<D3DX_BC3 *>(pBC);
951 
952  // Quantize block to A8, using Floyd Stienberg error diffusion. This
953  // increases the chance that colors will map directly to the quantized
954  // axis endpoints.
955  float fAlpha[NUM_PIXELS_PER_BLOCK];
956  float fError[NUM_PIXELS_PER_BLOCK];
957 
958  float fMinAlpha = Color[0].a;
959  float fMaxAlpha = Color[0].a;
960 
961  if (flags & BC_FLAGS_DITHER_A)
962  memset(fError, 0x00, NUM_PIXELS_PER_BLOCK * sizeof(float));
963 
964  for(size_t i = 0; i < NUM_PIXELS_PER_BLOCK; ++i)
965  {
966  float fAlph = Color[i].a;
967  if (flags & BC_FLAGS_DITHER_A)
968  fAlph += fError[i];
969 
970  fAlpha[i] = static_cast<int32_t>(fAlph * 255.0f + 0.5f) * (1.0f / 255.0f);
971 
972  if(fAlpha[i] < fMinAlpha)
973  fMinAlpha = fAlpha[i];
974  else if(fAlpha[i] > fMaxAlpha)
975  fMaxAlpha = fAlpha[i];
976 
977  if (flags & BC_FLAGS_DITHER_A)
978  {
979  float fDiff = fAlph - fAlpha[i];
980 
981  if(3 != (i & 3))
982  {
983  assert( i < 15 );
984  _Analysis_assume_( i < 15 );
985  fError[i + 1] += fDiff * (7.0f / 16.0f);
986  }
987 
988  if(i < 12)
989  {
990  if(i & 3)
991  fError[i + 3] += fDiff * (3.0f / 16.0f);
992 
993  fError[i + 4] += fDiff * (5.0f / 16.0f);
994 
995  if(3 != (i & 3))
996  {
997  assert( i < 11 );
998  _Analysis_assume_( i < 11 );
999  fError[i + 5] += fDiff * (1.0f / 16.0f);
1000  }
1001  }
1002  }
1003  }
1004 
1005 #ifdef COLOR_WEIGHTS
1006  if(0.0f == fMaxAlpha)
1007  {
1008  EncodeSolidBC1(&pBC3->dxt1, Color);
1009  pBC3->alpha[0] = 0x00;
1010  pBC3->alpha[1] = 0x00;
1011  memset(pBC3->bitmap, 0x00, 6);
1012  }
1013 #endif
1014 
1015  // RGB part
1016  EncodeBC1(&pBC3->bc1, Color, false, 0.f, flags);
1017 
1018  // Alpha part
1019  if(1.0f == fMinAlpha)
1020  {
1021  pBC3->alpha[0] = 0xff;
1022  pBC3->alpha[1] = 0xff;
1023  memset(pBC3->bitmap, 0x00, 6);
1024  return;
1025  }
1026 
1027  // Optimize and Quantize Min and Max values
1028  size_t uSteps = ((0.0f == fMinAlpha) || (1.0f == fMaxAlpha)) ? 6 : 8;
1029 
1030  float fAlphaA, fAlphaB;
1031  OptimizeAlpha<false>(&fAlphaA, &fAlphaB, fAlpha, uSteps);
1032 
1033  uint8_t bAlphaA = (uint8_t) static_cast<int32_t>(fAlphaA * 255.0f + 0.5f);
1034  uint8_t bAlphaB = (uint8_t) static_cast<int32_t>(fAlphaB * 255.0f + 0.5f);
1035 
1036  fAlphaA = (float) bAlphaA * (1.0f / 255.0f);
1037  fAlphaB = (float) bAlphaB * (1.0f / 255.0f);
1038 
1039  // Setup block
1040  if((8 == uSteps) && (bAlphaA == bAlphaB))
1041  {
1042  pBC3->alpha[0] = bAlphaA;
1043  pBC3->alpha[1] = bAlphaB;
1044  memset(pBC3->bitmap, 0x00, 6);
1045  return;
1046  }
1047 
1048  static const size_t pSteps6[] = { 0, 2, 3, 4, 5, 1 };
1049  static const size_t pSteps8[] = { 0, 2, 3, 4, 5, 6, 7, 1 };
1050 
1051  const size_t *pSteps;
1052  float fStep[8];
1053 
1054  if(6 == uSteps)
1055  {
1056  pBC3->alpha[0] = bAlphaA;
1057  pBC3->alpha[1] = bAlphaB;
1058 
1059  fStep[0] = fAlphaA;
1060  fStep[1] = fAlphaB;
1061 
1062  for(size_t i = 1; i < 5; ++i)
1063  fStep[i + 1] = (fStep[0] * (5 - i) + fStep[1] * i) * (1.0f / 5.0f);
1064 
1065  fStep[6] = 0.0f;
1066  fStep[7] = 1.0f;
1067 
1068  pSteps = pSteps6;
1069  }
1070  else
1071  {
1072  pBC3->alpha[0] = bAlphaB;
1073  pBC3->alpha[1] = bAlphaA;
1074 
1075  fStep[0] = fAlphaB;
1076  fStep[1] = fAlphaA;
1077 
1078  for(size_t i = 1; i < 7; ++i)
1079  fStep[i + 1] = (fStep[0] * (7 - i) + fStep[1] * i) * (1.0f / 7.0f);
1080 
1081  pSteps = pSteps8;
1082  }
1083 
1084  // Encode alpha bitmap
1085  float fSteps = (float) (uSteps - 1);
1086  float fScale = (fStep[0] != fStep[1]) ? (fSteps / (fStep[1] - fStep[0])) : 0.0f;
1087 
1088  if (flags & BC_FLAGS_DITHER_A)
1089  memset(fError, 0x00, NUM_PIXELS_PER_BLOCK * sizeof(float));
1090 
1091  for(size_t iSet = 0; iSet < 2; iSet++)
1092  {
1093  uint32_t dw = 0;
1094 
1095  size_t iMin = iSet * 8;
1096  size_t iLim = iMin + 8;
1097 
1098  for(size_t i = iMin; i < iLim; ++i)
1099  {
1100  float fAlph = Color[i].a;
1101  if (flags & BC_FLAGS_DITHER_A)
1102  fAlph += fError[i];
1103  float fDot = (fAlph - fStep[0]) * fScale;
1104 
1105  uint32_t iStep;
1106  if(fDot <= 0.0f)
1107  iStep = ((6 == uSteps) && (fAlph <= fStep[0] * 0.5f)) ? 6 : 0;
1108  else if(fDot >= fSteps)
1109  iStep = ((6 == uSteps) && (fAlph >= (fStep[1] + 1.0f) * 0.5f)) ? 7 : 1;
1110  else
1111  iStep = static_cast<uint32_t>( pSteps[static_cast<size_t>(fDot + 0.5f)] );
1112 
1113  dw = (iStep << 21) | (dw >> 3);
1114 
1115  if (flags & BC_FLAGS_DITHER_A)
1116  {
1117  float fDiff = (fAlph - fStep[iStep]);
1118 
1119  if(3 != (i & 3))
1120  fError[i + 1] += fDiff * (7.0f / 16.0f);
1121 
1122  if(i < 12)
1123  {
1124  if(i & 3)
1125  fError[i + 3] += fDiff * (3.0f / 16.0f);
1126 
1127  fError[i + 4] += fDiff * (5.0f / 16.0f);
1128 
1129  if(3 != (i & 3))
1130  fError[i + 5] += fDiff * (1.0f / 16.0f);
1131  }
1132  }
1133  }
1134 
1135  pBC3->bitmap[0 + iSet * 3] = ((uint8_t *) &dw)[0];
1136  pBC3->bitmap[1 + iSet * 3] = ((uint8_t *) &dw)[1];
1137  pBC3->bitmap[2 + iSet * 3] = ((uint8_t *) &dw)[2];
1138  }
1139 }
1140 
1141 } // namespace
_Use_decl_annotations_ void D3DXEncodeBC3(uint8_t *pBC, const XMVECTOR *pColor, DWORD flags)
Definition: BC.cpp:939
static const float pD4[]
Definition: BC6HBC7.cpp:33
function b
#define NUM_PIXELS_PER_BLOCK
Definition: BC.h:38
function a
static const HDRColorA g_LuminanceInv(0.7154f/0.2125f, 1.0f, 0.7154f/0.0721f, 1.0f)
_In_ size_t _In_ DXGI_FORMAT _In_ size_t _In_ DXGI_FORMAT _In_ DWORD flags
Definition: DirectXTexP.h:170
static void DecodeBC1(_Out_writes_(NUM_PIXELS_PER_BLOCK) XMVECTOR *pColor, _In_ const D3DX_BC1 *pBC, _In_ bool isbc1)
Definition: BC.cpp:322
_Use_decl_annotations_ void D3DXDecodeBC3(XMVECTOR *pColor, const uint8_t *pBC)
Definition: BC.cpp:897
size_t _In_ DXGI_FORMAT size_t _In_ TEXP_LEGACY_FORMAT _In_ DWORD flags assert(pDestination &&outSize > 0)
static uint16_t Encode565(_In_ const HDRColorA *pColor)
Definition: BC.cpp:48
static void EncodeBC1(_Out_ D3DX_BC1 *pBC, _In_reads_(NUM_PIXELS_PER_BLOCK) const HDRColorA *pColor, _In_ bool bColorKey, _In_ float alphaRef, _In_ DWORD flags)
Definition: BC.cpp:372
static const float pC4[]
Definition: BC6HBC7.cpp:32
static void OptimizeRGB(_Out_ HDRColorA *pX, _Out_ HDRColorA *pY, _In_reads_(NUM_PIXELS_PER_BLOCK) const HDRColorA *pPoints, _In_ size_t cSteps, _In_ DWORD flags)
Definition: BC.cpp:67
uint32_t bitmap[2]
Definition: BC.h:292
static const float pD3[]
Definition: BC6HBC7.cpp:31
HDRColorA * HDRColorALerp(_Out_ HDRColorA *pOut, _In_ const HDRColorA *pC1, _In_ const HDRColorA *pC2, _In_ float s)
Definition: BC.h:272
SiliconStudio.Core.Mathematics.Color Color
Definition: ColorPicker.cs:14
_In_ size_t _In_ const TexMetadata _In_ DWORD _Out_writes_(nImages) Image *images
static const HDRColorA g_Luminance(0.2125f/0.7154f, 1.0f, 0.0721f/0.7154f, 1.0f)
_Use_decl_annotations_ void D3DXEncodeBC1(uint8_t *pBC, const XMVECTOR *pColor, float alphaRef, DWORD flags)
Definition: BC.cpp:729
_In_ size_t _In_ DXGI_FORMAT _In_reads_(count) const XMVECTOR *pSource
static const float pC3[]
Definition: BC6HBC7.cpp:30
static void Decode565(_Out_ HDRColorA *pColor, _In_ const uint16_t w565)
Definition: BC.cpp:40
_Use_decl_annotations_ void D3DXEncodeBC2(uint8_t *pBC, const XMVECTOR *pColor, DWORD flags)
Definition: BC.cpp:820
static const float fEpsilon
Definition: BC6HBC7.cpp:29
_Use_decl_annotations_ void D3DXDecodeBC1(XMVECTOR *pColor, const uint8_t *pBC)
Definition: BC.cpp:722
_Use_decl_annotations_ void D3DXDecodeBC2(XMVECTOR *pColor, const uint8_t *pBC)
Definition: BC.cpp:794