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