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 //-------------------------------------------------------------------------------------
Decode565(_Out_ HDRColorA * pColor,_In_ const uint16_t w565)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 
Encode565(_In_ const HDRColorA * pColor)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 //-------------------------------------------------------------------------------------
OptimizeRGB(_Out_ HDRColorA * pX,_Out_ HDRColorA * pY,_In_reads_ (NUM_PIXELS_PER_BLOCK)const HDRColorA * pPoints,_In_ size_t cSteps,_In_ DWORD flags)65 static void OptimizeRGB(_Out_ HDRColorA *pX, _Out_ HDRColorA *pY,
66                         _In_reads_(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             else 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 //-------------------------------------------------------------------------------------
DecodeBC1(_Out_writes_ (NUM_PIXELS_PER_BLOCK)XMVECTOR * pColor,_In_ const D3DX_BC1 * pBC,_In_ bool isbc1)320 inline static void DecodeBC1( _Out_writes_(NUM_PIXELS_PER_BLOCK) XMVECTOR *pColor, _In_ const D3DX_BC1 *pBC, _In_ bool isbc1 )
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<2, 1, 0, 3>( clr0 );
334     clr1 = XMVectorSwizzle<2, 1, 0, 3>( clr1 );
335 
336     clr0 = XMVectorSelect( g_XMIdentityR3, clr0, g_XMSelect1110 );
337     clr1 = XMVectorSelect( g_XMIdentityR3, clr1, g_XMSelect1110 );
338 
339     XMVECTOR clr2, clr3;
340     if ( isbc1 && (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 
EncodeBC1(_Out_ D3DX_BC1 * pBC,_In_reads_ (NUM_PIXELS_PER_BLOCK)const HDRColorA * pColor,_In_ bool bColorKey,_In_ float alphaRef,_In_ DWORD flags)370 static void EncodeBC1(_Out_ D3DX_BC1 *pBC, _In_reads_(NUM_PIXELS_PER_BLOCK) const HDRColorA *pColor,
371                       _In_ bool bColorKey, _In_ float alphaRef, _In_ DWORD flags)
372 {
373     assert( pBC && pColor );
374     static_assert( sizeof(D3DX_BC1) == 8, "D3DX_BC1 should be 8 bytes" );
375 
376     // Determine if we need to colorkey this block
377     size_t uSteps;
378 
379     if (bColorKey)
380     {
381         size_t uColorKey = 0;
382 
383         for(size_t i = 0; i < NUM_PIXELS_PER_BLOCK; ++i)
384         {
385             if(pColor[i].a < alphaRef)
386                 uColorKey++;
387         }
388 
389         if(NUM_PIXELS_PER_BLOCK == uColorKey)
390         {
391             pBC->rgb[0] = 0x0000;
392             pBC->rgb[1] = 0xffff;
393             pBC->bitmap = 0xffffffff;
394             return;
395         }
396 
397         uSteps = (uColorKey > 0) ? 3 : 4;
398     }
399     else
400     {
401         uSteps = 4;
402     }
403 
404     // Quantize block to R56B5, using Floyd Stienberg error diffusion.  This
405     // increases the chance that colors will map directly to the quantized
406     // axis endpoints.
407     HDRColorA Color[NUM_PIXELS_PER_BLOCK];
408     HDRColorA Error[NUM_PIXELS_PER_BLOCK];
409 
410     if (flags & BC_FLAGS_DITHER_RGB)
411         memset(Error, 0x00, NUM_PIXELS_PER_BLOCK * sizeof(HDRColorA));
412 
413     size_t i;
414     for(i = 0; i < NUM_PIXELS_PER_BLOCK; ++i)
415     {
416         HDRColorA Clr;
417         Clr.r = pColor[i].r;
418         Clr.g = pColor[i].g;
419         Clr.b = pColor[i].b;
420 
421         if (flags & BC_FLAGS_DITHER_RGB)
422         {
423             Clr.r += Error[i].r;
424             Clr.g += Error[i].g;
425             Clr.b += Error[i].b;
426         }
427 
428         Color[i].r = (float) static_cast<int32_t>(Clr.r * 31.0f + 0.5f) * (1.0f / 31.0f);
429         Color[i].g = (float) static_cast<int32_t>(Clr.g * 63.0f + 0.5f) * (1.0f / 63.0f);
430         Color[i].b = (float) static_cast<int32_t>(Clr.b * 31.0f + 0.5f) * (1.0f / 31.0f);
431 
432 #ifdef COLOR_WEIGHTS
433         Color[i].a = pColor[i].a;
434 #else
435         Color[i].a = 1.0f;
436 #endif // COLOR_WEIGHTS
437 
438         if (flags & BC_FLAGS_DITHER_RGB)
439         {
440             HDRColorA Diff;
441             Diff.r = Color[i].a * (Clr.r - Color[i].r);
442             Diff.g = Color[i].a * (Clr.g - Color[i].g);
443             Diff.b = Color[i].a * (Clr.b - Color[i].b);
444 
445             if(3 != (i & 3))
446             {
447                 assert( i < 15 );
448                 _Analysis_assume_( i < 15 );
449                 Error[i + 1].r += Diff.r * (7.0f / 16.0f);
450                 Error[i + 1].g += Diff.g * (7.0f / 16.0f);
451                 Error[i + 1].b += Diff.b * (7.0f / 16.0f);
452             }
453 
454             if(i < 12)
455             {
456                 if(i & 3)
457                 {
458                     Error[i + 3].r += Diff.r * (3.0f / 16.0f);
459                     Error[i + 3].g += Diff.g * (3.0f / 16.0f);
460                     Error[i + 3].b += Diff.b * (3.0f / 16.0f);
461                 }
462 
463                 Error[i + 4].r += Diff.r * (5.0f / 16.0f);
464                 Error[i + 4].g += Diff.g * (5.0f / 16.0f);
465                 Error[i + 4].b += Diff.b * (5.0f / 16.0f);
466 
467                 if(3 != (i & 3))
468                 {
469                     assert( i < 11 );
470                     _Analysis_assume_( i < 11 );
471                     Error[i + 5].r += Diff.r * (1.0f / 16.0f);
472                     Error[i + 5].g += Diff.g * (1.0f / 16.0f);
473                     Error[i + 5].b += Diff.b * (1.0f / 16.0f);
474                 }
475             }
476         }
477 
478         if ( !( flags & BC_FLAGS_UNIFORM ) )
479         {
480             Color[i].r *= g_Luminance.r;
481             Color[i].g *= g_Luminance.g;
482             Color[i].b *= g_Luminance.b;
483         }
484     }
485 
486     // Perform 6D root finding function to find two endpoints of color axis.
487     // Then quantize and sort the endpoints depending on mode.
488     HDRColorA ColorA, ColorB, ColorC, ColorD;
489 
490     OptimizeRGB(&ColorA, &ColorB, Color, uSteps, flags);
491 
492     if ( flags & BC_FLAGS_UNIFORM )
493     {
494         ColorC = ColorA;
495         ColorD = ColorB;
496     }
497     else
498     {
499         ColorC.r = ColorA.r * g_LuminanceInv.r;
500         ColorC.g = ColorA.g * g_LuminanceInv.g;
501         ColorC.b = ColorA.b * g_LuminanceInv.b;
502 
503         ColorD.r = ColorB.r * g_LuminanceInv.r;
504         ColorD.g = ColorB.g * g_LuminanceInv.g;
505         ColorD.b = ColorB.b * g_LuminanceInv.b;
506     }
507 
508     uint16_t wColorA = Encode565(&ColorC);
509     uint16_t wColorB = Encode565(&ColorD);
510 
511     if((uSteps == 4) && (wColorA == wColorB))
512     {
513         pBC->rgb[0] = wColorA;
514         pBC->rgb[1] = wColorB;
515         pBC->bitmap = 0x00000000;
516         return;
517     }
518 
519     Decode565(&ColorC, wColorA);
520     Decode565(&ColorD, wColorB);
521 
522     if ( flags & BC_FLAGS_UNIFORM )
523     {
524         ColorA = ColorC;
525         ColorB = ColorD;
526     }
527     else
528     {
529         ColorA.r = ColorC.r * g_Luminance.r;
530         ColorA.g = ColorC.g * g_Luminance.g;
531         ColorA.b = ColorC.b * g_Luminance.b;
532 
533         ColorB.r = ColorD.r * g_Luminance.r;
534         ColorB.g = ColorD.g * g_Luminance.g;
535         ColorB.b = ColorD.b * g_Luminance.b;
536     }
537 
538     // Calculate color steps
539     HDRColorA Step[4];
540 
541     if((3 == uSteps) == (wColorA <= wColorB))
542     {
543         pBC->rgb[0] = wColorA;
544         pBC->rgb[1] = wColorB;
545 
546         Step[0] = ColorA;
547         Step[1] = ColorB;
548     }
549     else
550     {
551         pBC->rgb[0] = wColorB;
552         pBC->rgb[1] = wColorA;
553 
554         Step[0] = ColorB;
555         Step[1] = ColorA;
556     }
557 
558     static const size_t pSteps3[] = { 0, 2, 1 };
559     static const size_t pSteps4[] = { 0, 2, 3, 1 };
560     const size_t *pSteps;
561 
562     if(3 == uSteps)
563     {
564         pSteps = pSteps3;
565 
566         HDRColorALerp(&Step[2], &Step[0], &Step[1], 0.5f);
567     }
568     else
569     {
570         pSteps = pSteps4;
571 
572         HDRColorALerp(&Step[2], &Step[0], &Step[1], 1.0f / 3.0f);
573         HDRColorALerp(&Step[3], &Step[0], &Step[1], 2.0f / 3.0f);
574     }
575 
576     // Calculate color direction
577     HDRColorA Dir;
578 
579     Dir.r = Step[1].r - Step[0].r;
580     Dir.g = Step[1].g - Step[0].g;
581     Dir.b = Step[1].b - Step[0].b;
582 
583     float fSteps = (float) (uSteps - 1);
584     float fScale = (wColorA != wColorB) ? (fSteps / (Dir.r * Dir.r + Dir.g * Dir.g + Dir.b * Dir.b)) : 0.0f;
585 
586     Dir.r *= fScale;
587     Dir.g *= fScale;
588     Dir.b *= fScale;
589 
590     // Encode colors
591     uint32_t dw = 0;
592     if (flags & BC_FLAGS_DITHER_RGB)
593         memset(Error, 0x00, NUM_PIXELS_PER_BLOCK * sizeof(HDRColorA));
594 
595     for(i = 0; i < NUM_PIXELS_PER_BLOCK; ++i)
596     {
597         if((3 == uSteps) && (pColor[i].a < alphaRef))
598         {
599             dw = (3 << 30) | (dw >> 2);
600         }
601         else
602         {
603             HDRColorA Clr;
604             if ( flags & BC_FLAGS_UNIFORM )
605             {
606                 Clr.r = pColor[i].r;
607                 Clr.g = pColor[i].g;
608                 Clr.b = pColor[i].b;
609             }
610             else
611             {
612                 Clr.r = pColor[i].r * g_Luminance.r;
613                 Clr.g = pColor[i].g * g_Luminance.g;
614                 Clr.b = pColor[i].b * g_Luminance.b;
615             }
616 
617             if (flags & BC_FLAGS_DITHER_RGB)
618             {
619                 Clr.r += Error[i].r;
620                 Clr.g += Error[i].g;
621                 Clr.b += Error[i].b;
622             }
623 
624             float fDot = (Clr.r - Step[0].r) * Dir.r + (Clr.g - Step[0].g) * Dir.g + (Clr.b - Step[0].b) * Dir.b;
625             uint32_t iStep;
626 
627             if(fDot <= 0.0f)
628                 iStep = 0;
629             else if(fDot >= fSteps)
630                 iStep = 1;
631             else
632                 iStep = static_cast<uint32_t>( pSteps[static_cast<size_t>(fDot + 0.5f)] );
633 
634             dw = (iStep << 30) | (dw >> 2);
635 
636             if (flags & BC_FLAGS_DITHER_RGB)
637             {
638                 HDRColorA Diff;
639                 Diff.r = Color[i].a * (Clr.r - Step[iStep].r);
640                 Diff.g = Color[i].a * (Clr.g - Step[iStep].g);
641                 Diff.b = Color[i].a * (Clr.b - Step[iStep].b);
642 
643                 if(3 != (i & 3))
644                 {
645                     Error[i + 1].r += Diff.r * (7.0f / 16.0f);
646                     Error[i + 1].g += Diff.g * (7.0f / 16.0f);
647                     Error[i + 1].b += Diff.b * (7.0f / 16.0f);
648                 }
649 
650                 if(i < 12)
651                 {
652                     if(i & 3)
653                     {
654                         Error[i + 3].r += Diff.r * (3.0f / 16.0f);
655                         Error[i + 3].g += Diff.g * (3.0f / 16.0f);
656                         Error[i + 3].b += Diff.b * (3.0f / 16.0f);
657                     }
658 
659                     Error[i + 4].r += Diff.r * (5.0f / 16.0f);
660                     Error[i + 4].g += Diff.g * (5.0f / 16.0f);
661                     Error[i + 4].b += Diff.b * (5.0f / 16.0f);
662 
663                     if(3 != (i & 3))
664                     {
665                         Error[i + 5].r += Diff.r * (1.0f / 16.0f);
666                         Error[i + 5].g += Diff.g * (1.0f / 16.0f);
667                         Error[i + 5].b += Diff.b * (1.0f / 16.0f);
668                     }
669                 }
670             }
671         }
672     }
673 
674     pBC->bitmap = dw;
675 }
676 
677 //-------------------------------------------------------------------------------------
678 #ifdef COLOR_WEIGHTS
EncodeSolidBC1(_Out_ D3DX_BC1 * pBC,_In_reads_ (NUM_PIXELS_PER_BLOCK)const HDRColorA * pColor)679 static void EncodeSolidBC1(_Out_ D3DX_BC1 *pBC, _In_reads_(NUM_PIXELS_PER_BLOCK) const HDRColorA *pColor)
680 {
681 #ifdef COLOR_AVG_0WEIGHTS
682     // Compute avg color
683     HDRColorA Color;
684     Color.r = pColor[0].r;
685     Color.g = pColor[0].g;
686     Color.b = pColor[0].b;
687 
688     for(size_t i = 1; i < NUM_PIXELS_PER_BLOCK; ++i)
689     {
690         Color.r += pColor[i].r;
691         Color.g += pColor[i].g;
692         Color.b += pColor[i].b;
693     }
694 
695     Color.r *= 1.0f / 16.0f;
696     Color.g *= 1.0f / 16.0f;
697     Color.b *= 1.0f / 16.0f;
698 
699     uint16_t wColor = Encode565(&Color);
700 #else
701     uint16_t wColor = 0x0000;
702 #endif // COLOR_AVG_0WEIGHTS
703 
704     // Encode solid block
705     pBC->rgb[0] = wColor;
706     pBC->rgb[1] = wColor;
707     pBC->bitmap = 0x00000000;
708 }
709 #endif // COLOR_WEIGHTS
710 
711 
712 //=====================================================================================
713 // Entry points
714 //=====================================================================================
715 
716 //-------------------------------------------------------------------------------------
717 // BC1 Compression
718 //-------------------------------------------------------------------------------------
719 _Use_decl_annotations_
D3DXDecodeBC1(XMVECTOR * pColor,const uint8_t * pBC)720 void D3DXDecodeBC1(XMVECTOR *pColor, const uint8_t *pBC)
721 {
722     auto pBC1 = reinterpret_cast<const D3DX_BC1 *>(pBC);
723     DecodeBC1( pColor, pBC1, true );
724 }
725 
726 _Use_decl_annotations_
D3DXEncodeBC1(uint8_t * pBC,const XMVECTOR * pColor,float alphaRef,DWORD flags)727 void D3DXEncodeBC1(uint8_t *pBC, const XMVECTOR *pColor, float alphaRef, DWORD flags)
728 {
729     assert( pBC && pColor );
730 
731     HDRColorA Color[NUM_PIXELS_PER_BLOCK];
732 
733     if (flags & BC_FLAGS_DITHER_A)
734     {
735         float fError[NUM_PIXELS_PER_BLOCK];
736         memset(fError, 0x00, NUM_PIXELS_PER_BLOCK * sizeof(float));
737 
738         for(size_t i = 0; i < NUM_PIXELS_PER_BLOCK; ++i)
739         {
740             HDRColorA clr;
741             XMStoreFloat4( reinterpret_cast<XMFLOAT4*>( &clr ), pColor[i] );
742 
743             float fAlph = clr.a + fError[i];
744 
745             Color[i].r = clr.r;
746             Color[i].g = clr.g;
747             Color[i].b = clr.b;
748             Color[i].a = (float) static_cast<int32_t>(clr.a + fError[i] + 0.5f);
749 
750             float fDiff = fAlph - Color[i].a;
751 
752             if(3 != (i & 3))
753             {
754                 assert( i < 15 );
755                 _Analysis_assume_( i < 15 );
756                 fError[i + 1] += fDiff * (7.0f / 16.0f);
757             }
758 
759             if(i < 12)
760             {
761                 if(i & 3)
762                     fError[i + 3] += fDiff * (3.0f / 16.0f);
763 
764                 fError[i + 4] += fDiff * (5.0f / 16.0f);
765 
766                 if(3 != (i & 3))
767                 {
768                     assert( i < 11 );
769                     _Analysis_assume_( i < 11 );
770                     fError[i + 5] += fDiff * (1.0f / 16.0f);
771                 }
772             }
773         }
774     }
775     else
776     {
777         for(size_t i = 0; i < NUM_PIXELS_PER_BLOCK; ++i)
778         {
779             XMStoreFloat4( reinterpret_cast<XMFLOAT4*>( &Color[i] ), pColor[i] );
780         }
781     }
782 
783     auto pBC1 = reinterpret_cast<D3DX_BC1 *>(pBC);
784     EncodeBC1(pBC1, Color, true, alphaRef, flags);
785 }
786 
787 
788 //-------------------------------------------------------------------------------------
789 // BC2 Compression
790 //-------------------------------------------------------------------------------------
791 _Use_decl_annotations_
D3DXDecodeBC2(XMVECTOR * pColor,const uint8_t * pBC)792 void D3DXDecodeBC2(XMVECTOR *pColor, const uint8_t *pBC)
793 {
794     assert( pColor && pBC );
795     static_assert( sizeof(D3DX_BC2) == 16, "D3DX_BC2 should be 16 bytes" );
796 
797     auto pBC2 = reinterpret_cast<const D3DX_BC2 *>(pBC);
798 
799     // RGB part
800     DecodeBC1(pColor, &pBC2->bc1, false);
801 
802     // 4-bit alpha part
803     DWORD dw = pBC2->bitmap[0];
804 
805     for(size_t i = 0; i < 8; ++i, dw >>= 4)
806     {
807         #pragma prefast(suppress:22103, "writing blocks in two halves confuses tool")
808         pColor[i] = XMVectorSetW( pColor[i], (float) (dw & 0xf) * (1.0f / 15.0f) );
809     }
810 
811     dw = pBC2->bitmap[1];
812 
813     for(size_t i = 8; i < NUM_PIXELS_PER_BLOCK; ++i, dw >>= 4)
814         pColor[i] = XMVectorSetW( pColor[i], (float) (dw & 0xf) * (1.0f / 15.0f) );
815 }
816 
817 _Use_decl_annotations_
D3DXEncodeBC2(uint8_t * pBC,const XMVECTOR * pColor,DWORD flags)818 void D3DXEncodeBC2(uint8_t *pBC, const XMVECTOR *pColor, DWORD flags)
819 {
820     assert( pBC && pColor );
821     static_assert( sizeof(D3DX_BC2) == 16, "D3DX_BC2 should be 16 bytes" );
822 
823     HDRColorA Color[NUM_PIXELS_PER_BLOCK];
824     for(size_t i = 0; i < NUM_PIXELS_PER_BLOCK; ++i)
825     {
826         XMStoreFloat4( reinterpret_cast<XMFLOAT4*>( &Color[i] ), pColor[i] );
827     }
828 
829     auto pBC2 = reinterpret_cast<D3DX_BC2 *>(pBC);
830 
831     // 4-bit alpha part.  Dithered using Floyd Stienberg error diffusion.
832     pBC2->bitmap[0] = 0;
833     pBC2->bitmap[1] = 0;
834 
835     float fError[NUM_PIXELS_PER_BLOCK];
836     if (flags & BC_FLAGS_DITHER_A)
837         memset(fError, 0x00, NUM_PIXELS_PER_BLOCK * sizeof(float));
838 
839     for(size_t i = 0; i < NUM_PIXELS_PER_BLOCK; ++i)
840     {
841         float fAlph = Color[i].a;
842         if (flags & BC_FLAGS_DITHER_A)
843             fAlph += fError[i];
844 
845         uint32_t u = (uint32_t) static_cast<int32_t>(fAlph * 15.0f + 0.5f);
846 
847         pBC2->bitmap[i >> 3] >>= 4;
848         pBC2->bitmap[i >> 3] |= (u << 28);
849 
850         if (flags & BC_FLAGS_DITHER_A)
851         {
852             float fDiff = fAlph - (float) u * (1.0f / 15.0f);
853 
854             if(3 != (i & 3))
855             {
856                 assert( i < 15 );
857                 _Analysis_assume_( i < 15 );
858                 fError[i + 1] += fDiff * (7.0f / 16.0f);
859             }
860 
861             if(i < 12)
862             {
863                 if(i & 3)
864                     fError[i + 3] += fDiff * (3.0f / 16.0f);
865 
866                 fError[i + 4] += fDiff * (5.0f / 16.0f);
867 
868                 if(3 != (i & 3))
869                 {
870                     assert( i < 11 );
871                     _Analysis_assume_( i < 11 );
872                     fError[i + 5] += fDiff * (1.0f / 16.0f);
873                 }
874             }
875         }
876     }
877 
878     // RGB part
879 #ifdef COLOR_WEIGHTS
880     if(!pBC2->bitmap[0] && !pBC2->bitmap[1])
881     {
882         EncodeSolidBC1(pBC2->dxt1, Color);
883         return;
884     }
885 #endif // COLOR_WEIGHTS
886 
887     EncodeBC1(&pBC2->bc1, Color, false, 0.f, flags);
888 }
889 
890 
891 //-------------------------------------------------------------------------------------
892 // BC3 Compression
893 //-------------------------------------------------------------------------------------
894 _Use_decl_annotations_
D3DXDecodeBC3(XMVECTOR * pColor,const uint8_t * pBC)895 void D3DXDecodeBC3(XMVECTOR *pColor, const uint8_t *pBC)
896 {
897     assert( pColor && pBC );
898     static_assert( sizeof(D3DX_BC3) == 16, "D3DX_BC3 should be 16 bytes" );
899 
900     auto pBC3 = reinterpret_cast<const D3DX_BC3 *>(pBC);
901 
902     // RGB part
903     DecodeBC1(pColor, &pBC3->bc1, false);
904 
905     // Adaptive 3-bit alpha part
906     float fAlpha[8];
907 
908     fAlpha[0] = ((float) pBC3->alpha[0]) * (1.0f / 255.0f);
909     fAlpha[1] = ((float) pBC3->alpha[1]) * (1.0f / 255.0f);
910 
911     if(pBC3->alpha[0] > pBC3->alpha[1])
912     {
913         for(size_t i = 1; i < 7; ++i)
914             fAlpha[i + 1] = (fAlpha[0] * (7 - i) + fAlpha[1] * i) * (1.0f / 7.0f);
915     }
916     else
917     {
918         for(size_t i = 1; i < 5; ++i)
919             fAlpha[i + 1] = (fAlpha[0] * (5 - i) + fAlpha[1] * i) * (1.0f / 5.0f);
920 
921         fAlpha[6] = 0.0f;
922         fAlpha[7] = 1.0f;
923     }
924 
925     DWORD dw = pBC3->bitmap[0] | (pBC3->bitmap[1] << 8) | (pBC3->bitmap[2] << 16);
926 
927     for(size_t i = 0; i < 8; ++i, dw >>= 3)
928         pColor[i] = XMVectorSetW( pColor[i], fAlpha[dw & 0x7] );
929 
930     dw = pBC3->bitmap[3] | (pBC3->bitmap[4] << 8) | (pBC3->bitmap[5] << 16);
931 
932     for(size_t i = 8; i < NUM_PIXELS_PER_BLOCK; ++i, dw >>= 3)
933         pColor[i] = XMVectorSetW( pColor[i], fAlpha[dw & 0x7] );
934 }
935 
936 _Use_decl_annotations_
D3DXEncodeBC3(uint8_t * pBC,const XMVECTOR * pColor,DWORD flags)937 void D3DXEncodeBC3(uint8_t *pBC, const XMVECTOR *pColor, DWORD flags)
938 {
939     assert( pBC && pColor );
940     static_assert( sizeof(D3DX_BC3) == 16, "D3DX_BC3 should be 16 bytes" );
941 
942     HDRColorA Color[NUM_PIXELS_PER_BLOCK];
943     for(size_t i = 0; i < NUM_PIXELS_PER_BLOCK; ++i)
944     {
945         XMStoreFloat4( reinterpret_cast<XMFLOAT4*>( &Color[i] ), pColor[i] );
946     }
947 
948     auto pBC3 = reinterpret_cast<D3DX_BC3 *>(pBC);
949 
950     // Quantize block to A8, using Floyd Stienberg error diffusion.  This
951     // increases the chance that colors will map directly to the quantized
952     // axis endpoints.
953     float fAlpha[NUM_PIXELS_PER_BLOCK];
954     float fError[NUM_PIXELS_PER_BLOCK];
955 
956     float fMinAlpha = Color[0].a;
957     float fMaxAlpha = Color[0].a;
958 
959     if (flags & BC_FLAGS_DITHER_A)
960         memset(fError, 0x00, NUM_PIXELS_PER_BLOCK * sizeof(float));
961 
962     for(size_t i = 0; i < NUM_PIXELS_PER_BLOCK; ++i)
963     {
964         float fAlph = Color[i].a;
965         if (flags & BC_FLAGS_DITHER_A)
966             fAlph += fError[i];
967 
968         fAlpha[i] = static_cast<int32_t>(fAlph * 255.0f + 0.5f) * (1.0f / 255.0f);
969 
970         if(fAlpha[i] < fMinAlpha)
971             fMinAlpha = fAlpha[i];
972         else if(fAlpha[i] > fMaxAlpha)
973             fMaxAlpha = fAlpha[i];
974 
975         if (flags & BC_FLAGS_DITHER_A)
976         {
977             float fDiff = fAlph - fAlpha[i];
978 
979             if(3 != (i & 3))
980             {
981                 assert( i < 15 );
982                 _Analysis_assume_( i < 15 );
983                 fError[i + 1] += fDiff * (7.0f / 16.0f);
984             }
985 
986             if(i < 12)
987             {
988                 if(i & 3)
989                     fError[i + 3] += fDiff * (3.0f / 16.0f);
990 
991                 fError[i + 4] += fDiff * (5.0f / 16.0f);
992 
993                 if(3 != (i & 3))
994                 {
995                     assert( i < 11 );
996                     _Analysis_assume_( i < 11 );
997                     fError[i + 5] += fDiff * (1.0f / 16.0f);
998                 }
999             }
1000         }
1001     }
1002 
1003 #ifdef COLOR_WEIGHTS
1004     if(0.0f == fMaxAlpha)
1005     {
1006         EncodeSolidBC1(&pBC3->dxt1, Color);
1007         pBC3->alpha[0] = 0x00;
1008         pBC3->alpha[1] = 0x00;
1009         memset(pBC3->bitmap, 0x00, 6);
1010     }
1011 #endif
1012 
1013     // RGB part
1014     EncodeBC1(&pBC3->bc1, Color, false, 0.f, flags);
1015 
1016     // Alpha part
1017     if(1.0f == fMinAlpha)
1018     {
1019         pBC3->alpha[0] = 0xff;
1020         pBC3->alpha[1] = 0xff;
1021         memset(pBC3->bitmap, 0x00, 6);
1022         return;
1023     }
1024 
1025     // Optimize and Quantize Min and Max values
1026     size_t uSteps = ((0.0f == fMinAlpha) || (1.0f == fMaxAlpha)) ? 6 : 8;
1027 
1028     float fAlphaA, fAlphaB;
1029     OptimizeAlpha<false>(&fAlphaA, &fAlphaB, fAlpha, uSteps);
1030 
1031     uint8_t bAlphaA = (uint8_t) static_cast<int32_t>(fAlphaA * 255.0f + 0.5f);
1032     uint8_t bAlphaB = (uint8_t) static_cast<int32_t>(fAlphaB * 255.0f + 0.5f);
1033 
1034     fAlphaA = (float) bAlphaA * (1.0f / 255.0f);
1035     fAlphaB = (float) bAlphaB * (1.0f / 255.0f);
1036 
1037     // Setup block
1038     if((8 == uSteps) && (bAlphaA == bAlphaB))
1039     {
1040         pBC3->alpha[0] = bAlphaA;
1041         pBC3->alpha[1] = bAlphaB;
1042         memset(pBC3->bitmap, 0x00, 6);
1043         return;
1044     }
1045 
1046     static const size_t pSteps6[] = { 0, 2, 3, 4, 5, 1 };
1047     static const size_t pSteps8[] = { 0, 2, 3, 4, 5, 6, 7, 1 };
1048 
1049     const size_t *pSteps;
1050     float fStep[8];
1051 
1052     if(6 == uSteps)
1053     {
1054         pBC3->alpha[0] = bAlphaA;
1055         pBC3->alpha[1] = bAlphaB;
1056 
1057         fStep[0] = fAlphaA;
1058         fStep[1] = fAlphaB;
1059 
1060         for(size_t i = 1; i < 5; ++i)
1061             fStep[i + 1] = (fStep[0] * (5 - i) + fStep[1] * i) * (1.0f / 5.0f);
1062 
1063         fStep[6] = 0.0f;
1064         fStep[7] = 1.0f;
1065 
1066         pSteps = pSteps6;
1067     }
1068     else
1069     {
1070         pBC3->alpha[0] = bAlphaB;
1071         pBC3->alpha[1] = bAlphaA;
1072 
1073         fStep[0] = fAlphaB;
1074         fStep[1] = fAlphaA;
1075 
1076         for(size_t i = 1; i < 7; ++i)
1077             fStep[i + 1] = (fStep[0] * (7 - i) + fStep[1] * i) * (1.0f / 7.0f);
1078 
1079         pSteps = pSteps8;
1080     }
1081 
1082     // Encode alpha bitmap
1083     float fSteps = (float) (uSteps - 1);
1084     float fScale = (fStep[0] != fStep[1]) ? (fSteps / (fStep[1] - fStep[0])) : 0.0f;
1085 
1086     if (flags & BC_FLAGS_DITHER_A)
1087         memset(fError, 0x00, NUM_PIXELS_PER_BLOCK * sizeof(float));
1088 
1089     for(size_t iSet = 0; iSet < 2; iSet++)
1090     {
1091         uint32_t dw = 0;
1092 
1093         size_t iMin = iSet * 8;
1094         size_t iLim = iMin + 8;
1095 
1096         for(size_t i = iMin; i < iLim; ++i)
1097         {
1098             float fAlph = Color[i].a;
1099             if (flags & BC_FLAGS_DITHER_A)
1100                 fAlph += fError[i];
1101             float fDot = (fAlph - fStep[0]) * fScale;
1102 
1103             uint32_t iStep;
1104             if(fDot <= 0.0f)
1105                 iStep = ((6 == uSteps) && (fAlph <= fStep[0] * 0.5f)) ? 6 : 0;
1106             else if(fDot >= fSteps)
1107                 iStep = ((6 == uSteps) && (fAlph >= (fStep[1] + 1.0f) * 0.5f)) ? 7 : 1;
1108             else
1109                 iStep = static_cast<uint32_t>( pSteps[static_cast<size_t>(fDot + 0.5f)] );
1110 
1111             dw = (iStep << 21) | (dw >> 3);
1112 
1113             if (flags & BC_FLAGS_DITHER_A)
1114             {
1115                 float fDiff = (fAlph - fStep[iStep]);
1116 
1117                 if(3 != (i & 3))
1118                     fError[i + 1] += fDiff * (7.0f / 16.0f);
1119 
1120                 if(i < 12)
1121                 {
1122                     if(i & 3)
1123                         fError[i + 3] += fDiff * (3.0f / 16.0f);
1124 
1125                     fError[i + 4] += fDiff * (5.0f / 16.0f);
1126 
1127                     if(3 != (i & 3))
1128                         fError[i + 5] += fDiff * (1.0f / 16.0f);
1129                 }
1130             }
1131         }
1132 
1133         pBC3->bitmap[0 + iSet * 3] = ((uint8_t *) &dw)[0];
1134         pBC3->bitmap[1 + iSet * 3] = ((uint8_t *) &dw)[1];
1135         pBC3->bitmap[2 + iSet * 3] = ((uint8_t *) &dw)[2];
1136     }
1137 }
1138 
1139 } // namespace
1140