1 //-------------------------------------------------------------------------------------
4 // Block-compression (BC) functionality for BC1, BC2, BC3 (orginal DXTn formats)
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
11 // Copyright (c) Microsoft Corporation. All rights reserved.
13 // http://go.microsoft.com/fwlink/?LinkId=248926
14 //-------------------------------------------------------------------------------------
16 #include "DirectXTexP.h"
18 // Experiemental encoding variants, not enabled by default
19 //#define COLOR_WEIGHTS
20 //#define COLOR_AVG_0WEIGHTS
27 //-------------------------------------------------------------------------------------
29 //-------------------------------------------------------------------------------------
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);
35 //-------------------------------------------------------------------------------------
36 // Decode/Encode RGB 5/6/5 colors
37 //-------------------------------------------------------------------------------------
38 inline static void Decode565(_Out_ HDRColorA *pColor, _In_ const uint16_t w565)
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);
46 inline static uint16_t Encode565(_In_ const HDRColorA *pColor)
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;
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));
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)
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 };
74 const float *pC = (3 == cSteps) ? pC3 : pC4;
75 const float *pD = (3 == cSteps) ? pD3 : pD4;
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);
81 for(size_t iPoint = 0; iPoint < NUM_PIXELS_PER_BLOCK; iPoint++)
84 if(pPoints[iPoint].a > 0.0f)
85 #endif // COLOR_WEIGHTS
87 if(pPoints[iPoint].r < X.r)
88 X.r = pPoints[iPoint].r;
90 if(pPoints[iPoint].g < X.g)
91 X.g = pPoints[iPoint].g;
93 if(pPoints[iPoint].b < X.b)
94 X.b = pPoints[iPoint].b;
96 if(pPoints[iPoint].r > Y.r)
97 Y.r = pPoints[iPoint].r;
99 if(pPoints[iPoint].g > Y.g)
100 Y.g = pPoints[iPoint].g;
102 if(pPoints[iPoint].b > Y.b)
103 Y.b = pPoints[iPoint].b;
114 float fAB = AB.r * AB.r + AB.g * AB.g + AB.b * AB.b;
116 // Single color block.. no need to root-find
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;
124 // Try all four axis directions, to determine which diagonal best fits data
125 float fABInv = 1.0f / fAB;
128 Dir.r = AB.r * fABInv;
129 Dir.g = AB.g * fABInv;
130 Dir.b = AB.b * fABInv;
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;
138 fDir[0] = fDir[1] = fDir[2] = fDir[3] = 0.0f;
141 for(size_t iPoint = 0; iPoint < NUM_PIXELS_PER_BLOCK; iPoint++)
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;
151 f = Pt.r + Pt.g + Pt.b;
152 fDir[0] += pPoints[iPoint].a * f * f;
154 f = Pt.r + Pt.g - Pt.b;
155 fDir[1] += pPoints[iPoint].a * f * f;
157 f = Pt.r - Pt.g + Pt.b;
158 fDir[2] += pPoints[iPoint].a * f * f;
160 f = Pt.r - Pt.g - Pt.b;
161 fDir[3] += pPoints[iPoint].a * f * f;
163 f = Pt.r + Pt.g + Pt.b;
166 f = Pt.r + Pt.g - Pt.b;
169 f = Pt.r - Pt.g + Pt.b;
172 f = Pt.r - Pt.g - Pt.b;
174 #endif // COLOR_WEIGHTS
177 float fDirMax = fDir[0];
180 for(size_t iDir = 1; iDir < 4; iDir++)
182 if(fDir[iDir] > fDirMax)
184 fDirMax = fDir[iDir];
191 float f = X.g; X.g = Y.g; Y.g = f;
196 float f = X.b; X.b = Y.b; Y.b = f;
200 // Two color block.. no need to root-find
201 if(fAB < 1.0f / 4096.0f)
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;
209 // Use Newton's Method to find local minima of sum-of-squares error.
210 float fSteps = (float) (cSteps - 1);
212 for(size_t iIteration = 0; iIteration < 8; iIteration++)
214 // Calculate new steps
217 for(size_t iStep = 0; iStep < cSteps; iStep++)
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];
225 // Calculate color direction
230 float fLen = (Dir.r * Dir.r + Dir.g * Dir.g + Dir.b * Dir.b);
232 if(fLen < (1.0f / 4096.0f))
235 float fScale = fSteps / fLen;
242 // Evaluate function, and derivatives
245 d2X = d2Y = dX.r = dX.g = dX.b = dY.r = dY.g = dY.b = 0.0f;
247 for(size_t iPoint = 0; iPoint < NUM_PIXELS_PER_BLOCK; iPoint++)
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;
260 iStep = static_cast<size_t>(fDot + 0.5f);
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;
269 float fC = pC[iStep] * pPoints[iPoint].a * (1.0f / 8.0f);
270 float fD = pD[iStep] * pPoints[iPoint].a * (1.0f / 8.0f);
272 float fC = pC[iStep] * (1.0f / 8.0f);
273 float fD = pD[iStep] * (1.0f / 8.0f);
274 #endif // COLOR_WEIGHTS
276 d2X += fC * pC[iStep];
281 d2Y += fD * pD[iStep];
291 float f = -1.0f / d2X;
300 float f = -1.0f / d2Y;
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))
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;
319 //-------------------------------------------------------------------------------------
320 inline static void DecodeBC1( _Out_cap_c_(NUM_PIXELS_PER_BLOCK) XMVECTOR *pColor, _In_ const D3DX_BC1 *pBC )
322 assert( pColor && pBC );
323 static_assert( sizeof(D3DX_BC1) == 8, "D3DX_BC1 should be 8 bytes" );
325 static XMVECTORF32 s_Scale = { 1.f/31.f, 1.f/63.f, 1.f/31.f, 1.f };
327 XMVECTOR clr0 = XMLoadU565( reinterpret_cast<const XMU565*>(&pBC->rgb[0]) );
328 XMVECTOR clr1 = XMLoadU565( reinterpret_cast<const XMU565*>(&pBC->rgb[1]) );
330 clr0 = XMVectorMultiply( clr0, s_Scale );
331 clr1 = XMVectorMultiply( clr1, s_Scale );
333 clr0 = XMVectorSwizzle( clr0, 2, 1, 0, 3 );
334 clr1 = XMVectorSwizzle( clr1, 2, 1, 0, 3 );
336 clr0 = XMVectorSelect( g_XMIdentityR3, clr0, g_XMSelect1110 );
337 clr1 = XMVectorSelect( g_XMIdentityR3, clr1, g_XMSelect1110 );
340 if(pBC->rgb[0] <= pBC->rgb[1])
342 clr2 = XMVectorLerp( clr0, clr1, 0.5f );
343 clr3 = XMVectorZero(); // Alpha of 0
347 clr2 = XMVectorLerp( clr0, clr1, 1.f/3.f );
348 clr3 = XMVectorLerp( clr0, clr1, 2.f/3.f );
351 uint32_t dw = pBC->bitmap;
353 for(size_t i = 0; i < NUM_PIXELS_PER_BLOCK; ++i, dw >>= 2)
357 case 0: pColor[i] = clr0; break;
358 case 1: pColor[i] = clr1; break;
359 case 2: pColor[i] = clr2; break;
362 default: pColor[i] = clr3; break;
368 //-------------------------------------------------------------------------------------
369 #pragma warning(disable: 4616 6001 6201)
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)
374 assert( pBC && pColor );
375 static_assert( sizeof(D3DX_BC1) == 8, "D3DX_BC1 should be 8 bytes" );
377 // Determine if we need to colorkey this block
382 size_t uColorKey = 0;
384 for(size_t i = 0; i < NUM_PIXELS_PER_BLOCK; ++i)
386 if(pColor[i].a < alphaRef)
390 if(NUM_PIXELS_PER_BLOCK == uColorKey)
392 pBC->rgb[0] = 0x0000;
393 pBC->rgb[1] = 0xffff;
394 pBC->bitmap = 0xffffffff;
398 uSteps = (uColorKey > 0) ? 3 : 4;
405 // Quantize block to R56B5, using Floyd Stienberg error diffusion. This
406 // increases the chance that colors will map directly to the quantized
408 HDRColorA Color[NUM_PIXELS_PER_BLOCK];
409 HDRColorA Error[NUM_PIXELS_PER_BLOCK];
411 if (flags & BC_FLAGS_DITHER_RGB)
412 memset(Error, 0x00, NUM_PIXELS_PER_BLOCK * sizeof(HDRColorA));
415 for(i = 0; i < NUM_PIXELS_PER_BLOCK; ++i)
422 if (flags & BC_FLAGS_DITHER_RGB)
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);
434 Color[i].a = pColor[i].a;
437 #endif // COLOR_WEIGHTS
439 if (flags & BC_FLAGS_DITHER_RGB)
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);
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);
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);
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);
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);
479 if ( !( flags & BC_FLAGS_UNIFORM ) )
481 Color[i].r *= g_Luminance.r;
482 Color[i].g *= g_Luminance.g;
483 Color[i].b *= g_Luminance.b;
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;
491 OptimizeRGB(&ColorA, &ColorB, Color, uSteps, flags);
493 if ( flags & BC_FLAGS_UNIFORM )
500 ColorC.r = ColorA.r * g_LuminanceInv.r;
501 ColorC.g = ColorA.g * g_LuminanceInv.g;
502 ColorC.b = ColorA.b * g_LuminanceInv.b;
504 ColorD.r = ColorB.r * g_LuminanceInv.r;
505 ColorD.g = ColorB.g * g_LuminanceInv.g;
506 ColorD.b = ColorB.b * g_LuminanceInv.b;
509 uint16_t wColorA = Encode565(&ColorC);
510 uint16_t wColorB = Encode565(&ColorD);
512 if((uSteps == 4) && (wColorA == wColorB))
514 pBC->rgb[0] = wColorA;
515 pBC->rgb[1] = wColorB;
516 pBC->bitmap = 0x00000000;
520 Decode565(&ColorC, wColorA);
521 Decode565(&ColorD, wColorB);
523 if ( flags & BC_FLAGS_UNIFORM )
530 ColorA.r = ColorC.r * g_Luminance.r;
531 ColorA.g = ColorC.g * g_Luminance.g;
532 ColorA.b = ColorC.b * g_Luminance.b;
534 ColorB.r = ColorD.r * g_Luminance.r;
535 ColorB.g = ColorD.g * g_Luminance.g;
536 ColorB.b = ColorD.b * g_Luminance.b;
539 // Calculate color steps
542 if((3 == uSteps) == (wColorA <= wColorB))
544 pBC->rgb[0] = wColorA;
545 pBC->rgb[1] = wColorB;
552 pBC->rgb[0] = wColorB;
553 pBC->rgb[1] = wColorA;
559 static const size_t pSteps3[] = { 0, 2, 1 };
560 static const size_t pSteps4[] = { 0, 2, 3, 1 };
561 const size_t *pSteps;
567 HDRColorALerp(&Step[2], &Step[0], &Step[1], 0.5f);
573 HDRColorALerp(&Step[2], &Step[0], &Step[1], 1.0f / 3.0f);
574 HDRColorALerp(&Step[3], &Step[0], &Step[1], 2.0f / 3.0f);
577 // Calculate color direction
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;
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;
593 if (flags & BC_FLAGS_DITHER_RGB)
594 memset(Error, 0x00, NUM_PIXELS_PER_BLOCK * sizeof(HDRColorA));
596 for(i = 0; i < NUM_PIXELS_PER_BLOCK; ++i)
598 if((3 == uSteps) && (pColor[i].a < alphaRef))
600 dw = (3 << 30) | (dw >> 2);
605 if ( flags & BC_FLAGS_UNIFORM )
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;
618 if (flags & BC_FLAGS_DITHER_RGB)
625 float fDot = (Clr.r - Step[0].r) * Dir.r + (Clr.g - Step[0].g) * Dir.g + (Clr.b - Step[0].b) * Dir.b;
630 else if(fDot >= fSteps)
633 iStep = static_cast<uint32_t>( pSteps[static_cast<size_t>(fDot + 0.5f)] );
635 dw = (iStep << 30) | (dw >> 2);
637 if (flags & BC_FLAGS_DITHER_RGB)
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);
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);
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);
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);
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);
678 //-------------------------------------------------------------------------------------
680 static void EncodeSolidBC1(_Out_ D3DX_BC1 *pBC, _In_count_c_(NUM_PIXELS_PER_BLOCK) const HDRColorA *pColor)
682 #ifdef COLOR_AVG_0WEIGHTS
685 Color.r = pColor[0].r;
686 Color.g = pColor[0].g;
687 Color.b = pColor[0].b;
689 for(size_t i = 1; i < NUM_PIXELS_PER_BLOCK; ++i)
691 Color.r += pColor[i].r;
692 Color.g += pColor[i].g;
693 Color.b += pColor[i].b;
696 Color.r *= 1.0f / 16.0f;
697 Color.g *= 1.0f / 16.0f;
698 Color.b *= 1.0f / 16.0f;
700 uint16_t wColor = Encode565(&Color);
702 uint16_t wColor = 0x0000;
703 #endif // COLOR_AVG_0WEIGHTS
705 // Encode solid block
706 pBC->rgb[0] = wColor;
707 pBC->rgb[1] = wColor;
708 pBC->bitmap = 0x00000000;
710 #endif // COLOR_WEIGHTS
713 //=====================================================================================
715 //=====================================================================================
717 //-------------------------------------------------------------------------------------
719 //-------------------------------------------------------------------------------------
720 void D3DXDecodeBC1(XMVECTOR *pColor, const uint8_t *pBC)
722 const D3DX_BC1 *pBC1 = reinterpret_cast<const D3DX_BC1 *>(pBC);
723 DecodeBC1( pColor, pBC1 );
726 void D3DXEncodeBC1(uint8_t *pBC, const XMVECTOR *pColor, float alphaRef, DWORD flags)
728 assert( pBC && pColor );
730 HDRColorA Color[NUM_PIXELS_PER_BLOCK];
732 if (flags & BC_FLAGS_DITHER_A)
734 float fError[NUM_PIXELS_PER_BLOCK];
735 memset(fError, 0x00, NUM_PIXELS_PER_BLOCK * sizeof(float));
737 for(size_t i = 0; i < NUM_PIXELS_PER_BLOCK; ++i)
740 XMStoreFloat4( reinterpret_cast<XMFLOAT4*>( &clr ), pColor[i] );
742 float fAlph = clr.a + fError[i];
747 Color[i].a = (float) static_cast<int32_t>(clr.a + fError[i] + 0.5f);
749 float fDiff = fAlph - Color[i].a;
754 __analysis_assume( i < 15 );
755 fError[i + 1] += fDiff * (7.0f / 16.0f);
761 fError[i + 3] += fDiff * (3.0f / 16.0f);
763 fError[i + 4] += fDiff * (5.0f / 16.0f);
768 __analysis_assume( i < 11 );
769 fError[i + 5] += fDiff * (1.0f / 16.0f);
776 for(size_t i = 0; i < NUM_PIXELS_PER_BLOCK; ++i)
778 XMStoreFloat4( reinterpret_cast<XMFLOAT4*>( &Color[i] ), pColor[i] );
782 D3DX_BC1 *pBC1 = reinterpret_cast<D3DX_BC1 *>(pBC);
783 EncodeBC1(pBC1, Color, true, alphaRef, flags);
787 //-------------------------------------------------------------------------------------
789 //-------------------------------------------------------------------------------------
790 void D3DXDecodeBC2(XMVECTOR *pColor, const uint8_t *pBC)
792 assert( pColor && pBC );
793 static_assert( sizeof(D3DX_BC2) == 16, "D3DX_BC2 should be 16 bytes" );
795 const D3DX_BC2 *pBC2 = reinterpret_cast<const D3DX_BC2 *>(pBC);
798 DecodeBC1(pColor, &pBC2->bc1);
801 DWORD dw = pBC2->bitmap[0];
803 for(size_t i = 0; i < 8; ++i, dw >>= 4)
804 pColor[i] = XMVectorSetW( pColor[i], (float) (dw & 0xf) * (1.0f / 15.0f) );
806 dw = pBC2->bitmap[1];
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) );
812 void D3DXEncodeBC2(uint8_t *pBC, const XMVECTOR *pColor, DWORD flags)
814 assert( pBC && pColor );
815 static_assert( sizeof(D3DX_BC2) == 16, "D3DX_BC2 should be 16 bytes" );
817 HDRColorA Color[NUM_PIXELS_PER_BLOCK];
818 for(size_t i = 0; i < NUM_PIXELS_PER_BLOCK; ++i)
820 XMStoreFloat4( reinterpret_cast<XMFLOAT4*>( &Color[i] ), pColor[i] );
823 D3DX_BC2 *pBC2 = reinterpret_cast<D3DX_BC2 *>(pBC);
825 // 4-bit alpha part. Dithered using Floyd Stienberg error diffusion.
829 float fError[NUM_PIXELS_PER_BLOCK];
830 if (flags & BC_FLAGS_DITHER_A)
831 memset(fError, 0x00, NUM_PIXELS_PER_BLOCK * sizeof(float));
833 for(size_t i = 0; i < NUM_PIXELS_PER_BLOCK; ++i)
835 float fAlph = Color[i].a;
836 if (flags & BC_FLAGS_DITHER_A)
839 uint32_t u = (uint32_t) static_cast<int32_t>(fAlph * 15.0f + 0.5f);
841 pBC2->bitmap[i >> 3] >>= 4;
842 pBC2->bitmap[i >> 3] |= (u << 28);
844 if (flags & BC_FLAGS_DITHER_A)
846 float fDiff = fAlph - (float) u * (1.0f / 15.0f);
851 __analysis_assume( i < 15 );
852 fError[i + 1] += fDiff * (7.0f / 16.0f);
858 fError[i + 3] += fDiff * (3.0f / 16.0f);
860 fError[i + 4] += fDiff * (5.0f / 16.0f);
865 __analysis_assume( i < 11 );
866 fError[i + 5] += fDiff * (1.0f / 16.0f);
874 if(!pBC2->bitmap[0] && !pBC2->bitmap[1])
876 EncodeSolidBC1(pBC2->dxt1, Color);
879 #endif // COLOR_WEIGHTS
881 EncodeBC1(&pBC2->bc1, Color, false, 0.f, flags);
885 //-------------------------------------------------------------------------------------
887 //-------------------------------------------------------------------------------------
888 void D3DXDecodeBC3(XMVECTOR *pColor, const uint8_t *pBC)
890 assert( pColor && pBC );
891 static_assert( sizeof(D3DX_BC3) == 16, "D3DX_BC3 should be 16 bytes" );
893 const D3DX_BC3 *pBC3 = reinterpret_cast<const D3DX_BC3 *>(pBC);
896 DecodeBC1(pColor, &pBC3->bc1);
898 // Adaptive 3-bit alpha part
901 fAlpha[0] = ((float) pBC3->alpha[0]) * (1.0f / 255.0f);
902 fAlpha[1] = ((float) pBC3->alpha[1]) * (1.0f / 255.0f);
904 if(pBC3->alpha[0] > pBC3->alpha[1])
906 for(size_t i = 1; i < 7; ++i)
907 fAlpha[i + 1] = (fAlpha[0] * (7 - i) + fAlpha[1] * i) * (1.0f / 7.0f);
911 for(size_t i = 1; i < 5; ++i)
912 fAlpha[i + 1] = (fAlpha[0] * (5 - i) + fAlpha[1] * i) * (1.0f / 5.0f);
918 DWORD dw = pBC3->bitmap[0] | (pBC3->bitmap[1] << 8) | (pBC3->bitmap[2] << 16);
920 for(size_t i = 0; i < 8; ++i, dw >>= 3)
921 pColor[i] = XMVectorSetW( pColor[i], fAlpha[dw & 0x7] );
923 dw = pBC3->bitmap[3] | (pBC3->bitmap[4] << 8) | (pBC3->bitmap[5] << 16);
925 for(size_t i = 8; i < NUM_PIXELS_PER_BLOCK; ++i, dw >>= 3)
926 pColor[i] = XMVectorSetW( pColor[i], fAlpha[dw & 0x7] );
929 void D3DXEncodeBC3(uint8_t *pBC, const XMVECTOR *pColor, DWORD flags)
931 assert( pBC && pColor );
932 static_assert( sizeof(D3DX_BC3) == 16, "D3DX_BC3 should be 16 bytes" );
934 HDRColorA Color[NUM_PIXELS_PER_BLOCK];
935 for(size_t i = 0; i < NUM_PIXELS_PER_BLOCK; ++i)
937 XMStoreFloat4( reinterpret_cast<XMFLOAT4*>( &Color[i] ), pColor[i] );
940 D3DX_BC3 *pBC3 = reinterpret_cast<D3DX_BC3 *>(pBC);
942 // Quantize block to A8, using Floyd Stienberg error diffusion. This
943 // increases the chance that colors will map directly to the quantized
945 float fAlpha[NUM_PIXELS_PER_BLOCK];
946 float fError[NUM_PIXELS_PER_BLOCK];
948 float fMinAlpha = Color[0].a;
949 float fMaxAlpha = Color[0].a;
951 if (flags & BC_FLAGS_DITHER_A)
952 memset(fError, 0x00, NUM_PIXELS_PER_BLOCK * sizeof(float));
954 for(size_t i = 0; i < NUM_PIXELS_PER_BLOCK; ++i)
956 float fAlph = Color[i].a;
957 if (flags & BC_FLAGS_DITHER_A)
960 fAlpha[i] = static_cast<int32_t>(fAlph * 255.0f + 0.5f) * (1.0f / 255.0f);
962 if(fAlpha[i] < fMinAlpha)
963 fMinAlpha = fAlpha[i];
964 else if(fAlpha[i] > fMaxAlpha)
965 fMaxAlpha = fAlpha[i];
967 if (flags & BC_FLAGS_DITHER_A)
969 float fDiff = fAlph - fAlpha[i];
974 __analysis_assume( i < 15 );
975 fError[i + 1] += fDiff * (7.0f / 16.0f);
981 fError[i + 3] += fDiff * (3.0f / 16.0f);
983 fError[i + 4] += fDiff * (5.0f / 16.0f);
988 __analysis_assume( i < 11 );
989 fError[i + 5] += fDiff * (1.0f / 16.0f);
996 if(0.0f == fMaxAlpha)
998 EncodeSolidBC1(&pBC3->dxt1, Color);
999 pBC3->alpha[0] = 0x00;
1000 pBC3->alpha[1] = 0x00;
1001 memset(pBC3->bitmap, 0x00, 6);
1006 EncodeBC1(&pBC3->bc1, Color, false, 0.f, flags);
1009 if(1.0f == fMinAlpha)
1011 pBC3->alpha[0] = 0xff;
1012 pBC3->alpha[1] = 0xff;
1013 memset(pBC3->bitmap, 0x00, 6);
1017 // Optimize and Quantize Min and Max values
1018 size_t uSteps = ((0.0f == fMinAlpha) || (1.0f == fMaxAlpha)) ? 6 : 8;
1020 float fAlphaA, fAlphaB;
1021 OptimizeAlpha<false>(&fAlphaA, &fAlphaB, fAlpha, uSteps);
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);
1026 fAlphaA = (float) bAlphaA * (1.0f / 255.0f);
1027 fAlphaB = (float) bAlphaB * (1.0f / 255.0f);
1030 if((8 == uSteps) && (bAlphaA == bAlphaB))
1032 pBC3->alpha[0] = bAlphaA;
1033 pBC3->alpha[1] = bAlphaB;
1034 memset(pBC3->bitmap, 0x00, 6);
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 };
1041 const size_t *pSteps;
1046 pBC3->alpha[0] = bAlphaA;
1047 pBC3->alpha[1] = bAlphaB;
1052 for(size_t i = 1; i < 5; ++i)
1053 fStep[i + 1] = (fStep[0] * (5 - i) + fStep[1] * i) * (1.0f / 5.0f);
1062 pBC3->alpha[0] = bAlphaB;
1063 pBC3->alpha[1] = bAlphaA;
1068 for(size_t i = 1; i < 7; ++i)
1069 fStep[i + 1] = (fStep[0] * (7 - i) + fStep[1] * i) * (1.0f / 7.0f);
1074 // Encode alpha bitmap
1075 float fSteps = (float) (uSteps - 1);
1076 float fScale = (fStep[0] != fStep[1]) ? (fSteps / (fStep[1] - fStep[0])) : 0.0f;
1078 if (flags & BC_FLAGS_DITHER_A)
1079 memset(fError, 0x00, NUM_PIXELS_PER_BLOCK * sizeof(float));
1081 for(size_t iSet = 0; iSet < 2; iSet++)
1085 size_t iMin = iSet * 8;
1086 size_t iLim = iMin + 8;
1088 for(size_t i = iMin; i < iLim; ++i)
1090 float fAlph = Color[i].a;
1091 if (flags & BC_FLAGS_DITHER_A)
1093 float fDot = (fAlph - fStep[0]) * fScale;
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;
1101 iStep = static_cast<uint32_t>( pSteps[static_cast<size_t>(fDot + 0.5f)] );
1103 dw = (iStep << 21) | (dw >> 3);
1105 if (flags & BC_FLAGS_DITHER_A)
1107 float fDiff = (fAlph - fStep[iStep]);
1110 fError[i + 1] += fDiff * (7.0f / 16.0f);
1115 fError[i + 3] += fDiff * (3.0f / 16.0f);
1117 fError[i + 4] += fDiff * (5.0f / 16.0f);
1120 fError[i + 5] += fDiff * (1.0f / 16.0f);
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];