| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147 |
- //-------------------------------------------------------------------------------------
- // BC.cpp
- //
- // Block-compression (BC) functionality for BC1, BC2, BC3 (orginal DXTn formats)
- //
- // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
- // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
- // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
- // PARTICULAR PURPOSE.
- //
- // Copyright (c) Microsoft Corporation. All rights reserved.
- //
- // http://go.microsoft.com/fwlink/?LinkId=248926
- //-------------------------------------------------------------------------------------
- //#include "directxtexp.h" ESENTHEL CHANGED
- // Experiemental encoding variants, not enabled by default
- //#define COLOR_WEIGHTS
- //#define COLOR_AVG_0WEIGHTS
- //#include "BC.h" ESENTHEL CHANGED
- using namespace DirectX;
- using namespace DirectX::PackedVector;
- namespace
- {
- //-------------------------------------------------------------------------------------
- // Constants
- //-------------------------------------------------------------------------------------
- // Perceptual weightings for the importance of each channel.
- const HDRColorA g_Luminance(0.2125f / 0.7154f, 1.0f, 0.0721f / 0.7154f, 1.0f);
- const HDRColorA g_LuminanceInv(0.7154f / 0.2125f, 1.0f, 0.7154f / 0.0721f, 1.0f);
- //-------------------------------------------------------------------------------------
- // Decode/Encode RGB 5/6/5 colors
- //-------------------------------------------------------------------------------------
- inline void Decode565(_Out_ HDRColorA *pColor, _In_ const uint16_t w565)
- {
- pColor->r = (float)((w565 >> 11) & 31) * (1.0f / 31.0f);
- pColor->g = (float)((w565 >> 5) & 63) * (1.0f / 63.0f);
- pColor->b = (float)((w565 >> 0) & 31) * (1.0f / 31.0f);
- pColor->a = 1.0f;
- }
- inline uint16_t Encode565(_In_ const HDRColorA *pColor)
- {
- HDRColorA Color;
- Color.r = (pColor->r < 0.0f) ? 0.0f : (pColor->r > 1.0f) ? 1.0f : pColor->r;
- Color.g = (pColor->g < 0.0f) ? 0.0f : (pColor->g > 1.0f) ? 1.0f : pColor->g;
- Color.b = (pColor->b < 0.0f) ? 0.0f : (pColor->b > 1.0f) ? 1.0f : pColor->b;
- uint16_t w;
- w = (uint16_t) ((static_cast<int32_t>(Color.r * 31.0f + 0.5f) << 11) |
- (static_cast<int32_t>(Color.g * 63.0f + 0.5f) << 5) |
- (static_cast<int32_t>(Color.b * 31.0f + 0.5f) << 0));
- return w;
- }
- //-------------------------------------------------------------------------------------
- static const float fEpsilon = (0.25f / 64.0f) * (0.25f / 64.0f);
- static const float pC3[] = { 2.0f/2.0f, 1.0f/2.0f, 0.0f/2.0f };
- static const float pD3[] = { 0.0f/2.0f, 1.0f/2.0f, 2.0f/2.0f };
- static const float pC4[] = { 3.0f/3.0f, 2.0f/3.0f, 1.0f/3.0f, 0.0f/3.0f };
- static const float pD4[] = { 0.0f/3.0f, 1.0f/3.0f, 2.0f/3.0f, 3.0f/3.0f };
- void OptimizeRGB(
- _Out_ HDRColorA *pX,
- _Out_ HDRColorA *pY,
- _In_reads_(NUM_PIXELS_PER_BLOCK) const HDRColorA *pPoints,
- _In_ size_t cSteps,
- const HDRColorA *pWeights)
- {
- const float *pC = (3 == cSteps) ? pC3 : pC4;
- const float *pD = (3 == cSteps) ? pD3 : pD4;
- // Find Min and Max points, as starting point
- HDRColorA X = pWeights ? *pWeights : HDRColorA(1.0f, 1.0f, 1.0f, 1.0f);
- HDRColorA Y = HDRColorA(0.0f, 0.0f, 0.0f, 1.0f);
- for(size_t iPoint = 0; iPoint < NUM_PIXELS_PER_BLOCK; iPoint++)
- {
- #ifdef COLOR_WEIGHTS
- if(pPoints[iPoint].a > 0.0f)
- #endif // COLOR_WEIGHTS
- {
- if(pPoints[iPoint].r < X.r)
- X.r = pPoints[iPoint].r;
- if(pPoints[iPoint].g < X.g)
- X.g = pPoints[iPoint].g;
- if(pPoints[iPoint].b < X.b)
- X.b = pPoints[iPoint].b;
- if(pPoints[iPoint].r > Y.r)
- Y.r = pPoints[iPoint].r;
- if(pPoints[iPoint].g > Y.g)
- Y.g = pPoints[iPoint].g;
- if(pPoints[iPoint].b > Y.b)
- Y.b = pPoints[iPoint].b;
- }
- }
- // Diagonal axis
- HDRColorA AB;
- AB.r = Y.r - X.r;
- AB.g = Y.g - X.g;
- AB.b = Y.b - X.b;
- float fAB = AB.r * AB.r + AB.g * AB.g + AB.b * AB.b;
- // Single color block.. no need to root-find
- if(fAB < FLT_MIN)
- {
- pX->r = X.r; pX->g = X.g; pX->b = X.b;
- pY->r = Y.r; pY->g = Y.g; pY->b = Y.b;
- return;
- }
- // Try all four axis directions, to determine which diagonal best fits data
- float fABInv = 1.0f / fAB;
- HDRColorA Dir;
- Dir.r = AB.r * fABInv;
- Dir.g = AB.g * fABInv;
- Dir.b = AB.b * fABInv;
- HDRColorA Mid;
- Mid.r = (X.r + Y.r) * 0.5f;
- Mid.g = (X.g + Y.g) * 0.5f;
- Mid.b = (X.b + Y.b) * 0.5f;
- float fDir[4];
- fDir[0] = fDir[1] = fDir[2] = fDir[3] = 0.0f;
- for(size_t iPoint = 0; iPoint < NUM_PIXELS_PER_BLOCK; iPoint++)
- {
- HDRColorA Pt;
- Pt.r = (pPoints[iPoint].r - Mid.r) * Dir.r;
- Pt.g = (pPoints[iPoint].g - Mid.g) * Dir.g;
- Pt.b = (pPoints[iPoint].b - Mid.b) * Dir.b;
- float f;
- #ifdef COLOR_WEIGHTS
- f = Pt.r + Pt.g + Pt.b;
- fDir[0] += pPoints[iPoint].a * f * f;
- f = Pt.r + Pt.g - Pt.b;
- fDir[1] += pPoints[iPoint].a * f * f;
- f = Pt.r - Pt.g + Pt.b;
- fDir[2] += pPoints[iPoint].a * f * f;
- f = Pt.r - Pt.g - Pt.b;
- fDir[3] += pPoints[iPoint].a * f * f;
- #else
- f = Pt.r + Pt.g + Pt.b;
- fDir[0] += f * f;
- f = Pt.r + Pt.g - Pt.b;
- fDir[1] += f * f;
- f = Pt.r - Pt.g + Pt.b;
- fDir[2] += f * f;
- f = Pt.r - Pt.g - Pt.b;
- fDir[3] += f * f;
- #endif // COLOR_WEIGHTS
- }
- float fDirMax = fDir[0];
- size_t iDirMax = 0;
- for(size_t iDir = 1; iDir < 4; iDir++)
- {
- if(fDir[iDir] > fDirMax)
- {
- fDirMax = fDir[iDir];
- iDirMax = iDir;
- }
- }
- if(iDirMax & 2)
- {
- float f = X.g; X.g = Y.g; Y.g = f;
- }
- if(iDirMax & 1)
- {
- float f = X.b; X.b = Y.b; Y.b = f;
- }
- // Two color block.. no need to root-find
- if(fAB < 1.0f / 4096.0f)
- {
- pX->r = X.r; pX->g = X.g; pX->b = X.b;
- pY->r = Y.r; pY->g = Y.g; pY->b = Y.b;
- return;
- }
- // Use Newton's Method to find local minima of sum-of-squares error.
- float fSteps = (float) (cSteps - 1);
- for(size_t iIteration = 0; iIteration < 8; iIteration++)
- {
- // Calculate new steps
- HDRColorA pSteps[4];
- for(size_t iStep = 0; iStep < cSteps; iStep++)
- {
- pSteps[iStep].r = X.r * pC[iStep] + Y.r * pD[iStep];
- pSteps[iStep].g = X.g * pC[iStep] + Y.g * pD[iStep];
- pSteps[iStep].b = X.b * pC[iStep] + Y.b * pD[iStep];
- }
- // Calculate color direction
- Dir.r = Y.r - X.r;
- Dir.g = Y.g - X.g;
- Dir.b = Y.b - X.b;
- float fLen = (Dir.r * Dir.r + Dir.g * Dir.g + Dir.b * Dir.b);
- if(fLen < (1.0f / 4096.0f))
- break;
- float fScale = fSteps / fLen;
- Dir.r *= fScale;
- Dir.g *= fScale;
- Dir.b *= fScale;
- // Evaluate function, and derivatives
- float d2X, d2Y;
- HDRColorA dX, dY;
- d2X = d2Y = dX.r = dX.g = dX.b = dY.r = dY.g = dY.b = 0.0f;
- for(size_t iPoint = 0; iPoint < NUM_PIXELS_PER_BLOCK; iPoint++)
- {
- float fDot = (pPoints[iPoint].r - X.r) * Dir.r +
- (pPoints[iPoint].g - X.g) * Dir.g +
- (pPoints[iPoint].b - X.b) * Dir.b;
- size_t iStep;
- if(fDot <= 0.0f)
- iStep = 0;
- else if(fDot >= fSteps)
- iStep = cSteps - 1;
- else
- iStep = static_cast<size_t>(fDot + 0.5f);
- HDRColorA Diff;
- Diff.r = pSteps[iStep].r - pPoints[iPoint].r;
- Diff.g = pSteps[iStep].g - pPoints[iPoint].g;
- Diff.b = pSteps[iStep].b - pPoints[iPoint].b;
- #ifdef COLOR_WEIGHTS
- float fC = pC[iStep] * pPoints[iPoint].a * (1.0f / 8.0f);
- float fD = pD[iStep] * pPoints[iPoint].a * (1.0f / 8.0f);
- #else
- float fC = pC[iStep] * (1.0f / 8.0f);
- float fD = pD[iStep] * (1.0f / 8.0f);
- #endif // COLOR_WEIGHTS
- d2X += fC * pC[iStep];
- dX.r += fC * Diff.r;
- dX.g += fC * Diff.g;
- dX.b += fC * Diff.b;
- d2Y += fD * pD[iStep];
- dY.r += fD * Diff.r;
- dY.g += fD * Diff.g;
- dY.b += fD * Diff.b;
- }
- // Move endpoints
- if(d2X > 0.0f)
- {
- float f = -1.0f / d2X;
- X.r += dX.r * f;
- X.g += dX.g * f;
- X.b += dX.b * f;
- }
- if(d2Y > 0.0f)
- {
- float f = -1.0f / d2Y;
- Y.r += dY.r * f;
- Y.g += dY.g * f;
- Y.b += dY.b * f;
- }
- if((dX.r * dX.r < fEpsilon) && (dX.g * dX.g < fEpsilon) && (dX.b * dX.b < fEpsilon) &&
- (dY.r * dY.r < fEpsilon) && (dY.g * dY.g < fEpsilon) && (dY.b * dY.b < fEpsilon))
- {
- break;
- }
- }
- pX->r = X.r; pX->g = X.g; pX->b = X.b;
- pY->r = Y.r; pY->g = Y.g; pY->b = Y.b;
- }
- //-------------------------------------------------------------------------------------
- static XMVECTORF32 s_Scale = { 1.f/31.f, 1.f/63.f, 1.f/31.f, 1.f };
- inline void DecodeBC1(
- _Out_writes_(NUM_PIXELS_PER_BLOCK) XMVECTOR *pColor,
- _In_ const D3DX_BC1 *pBC,
- bool isbc1)
- {
- assert( pColor && pBC );
- static_assert( sizeof(D3DX_BC1) == 8, "D3DX_BC1 should be 8 bytes" );
- XMVECTOR clr0 = XMLoadU565( reinterpret_cast<const XMU565*>(&pBC->rgb[0]) );
- XMVECTOR clr1 = XMLoadU565( reinterpret_cast<const XMU565*>(&pBC->rgb[1]) );
- clr0 = XMVectorMultiply( clr0, s_Scale );
- clr1 = XMVectorMultiply( clr1, s_Scale );
- clr0 = XMVectorSwizzle<2, 1, 0, 3>( clr0 );
- clr1 = XMVectorSwizzle<2, 1, 0, 3>( clr1 );
- clr0 = XMVectorSelect( g_XMIdentityR3, clr0, g_XMSelect1110 );
- clr1 = XMVectorSelect( g_XMIdentityR3, clr1, g_XMSelect1110 );
- XMVECTOR clr2, clr3;
- if ( isbc1 && (pBC->rgb[0] <= pBC->rgb[1]) )
- {
- clr2 = XMVectorLerp( clr0, clr1, 0.5f );
- clr3 = XMVectorZero(); // Alpha of 0
- }
- else
- {
- clr2 = XMVectorLerp( clr0, clr1, 1.f/3.f );
- clr3 = XMVectorLerp( clr0, clr1, 2.f/3.f );
- }
- uint32_t dw = pBC->bitmap;
- for(size_t i = 0; i < NUM_PIXELS_PER_BLOCK; ++i, dw >>= 2)
- {
- switch(dw & 3)
- {
- case 0: pColor[i] = clr0; break;
- case 1: pColor[i] = clr1; break;
- case 2: pColor[i] = clr2; break;
- case 3:
- default: pColor[i] = clr3; break;
- }
- }
- }
- //-------------------------------------------------------------------------------------
- static const size_t pSteps3[] = { 0, 2, 1 }; // ESENTHEL
- static const size_t pSteps4[] = { 0, 2, 3, 1 }; // ESENTHEL
- void EncodeBC1(
- _Out_ D3DX_BC1 *pBC,
- _In_reads_(NUM_PIXELS_PER_BLOCK) const HDRColorA *pColor,
- const HDRColorA *pWeights, // ESENTHEL
- bool bColorKey,
- float threshold,
- DWORD flags)
- {
- assert( pBC && pColor );
- static_assert( sizeof(D3DX_BC1) == 8, "D3DX_BC1 should be 8 bytes" );
- // Determine if we need to colorkey this block
- size_t uSteps;
-
- if (bColorKey)
- {
- size_t uColorKey = 0;
- for(size_t i = 0; i < NUM_PIXELS_PER_BLOCK; ++i)
- {
- if (pColor[i].a < threshold)
- uColorKey++;
- }
- if(NUM_PIXELS_PER_BLOCK == uColorKey)
- {
- pBC->rgb[0] = 0x0000;
- pBC->rgb[1] = 0xffff;
- pBC->bitmap = 0xffffffff;
- return;
- }
- uSteps = (uColorKey > 0) ? 3 : 4;
- }
- else
- {
- uSteps = 4;
- }
- // Quantize block to R56B5, using Floyd Stienberg error diffusion. This
- // increases the chance that colors will map directly to the quantized
- // axis endpoints.
- HDRColorA Color[NUM_PIXELS_PER_BLOCK];
- HDRColorA Error[NUM_PIXELS_PER_BLOCK];
- if (flags & BC_FLAGS_DITHER_RGB)
- memset(Error, 0x00, NUM_PIXELS_PER_BLOCK * sizeof(HDRColorA));
- size_t i;
- for(i = 0; i < NUM_PIXELS_PER_BLOCK; ++i)
- {
- HDRColorA Clr;
- Clr.r = pColor[i].r;
- Clr.g = pColor[i].g;
- Clr.b = pColor[i].b;
- if (flags & BC_FLAGS_DITHER_RGB)
- {
- Clr.r += Error[i].r;
- Clr.g += Error[i].g;
- Clr.b += Error[i].b;
- }
- Color[i].r = (float) static_cast<int32_t>(Clr.r * 31.0f + 0.5f) * (1.0f / 31.0f);
- Color[i].g = (float) static_cast<int32_t>(Clr.g * 63.0f + 0.5f) * (1.0f / 63.0f);
- Color[i].b = (float) static_cast<int32_t>(Clr.b * 31.0f + 0.5f) * (1.0f / 31.0f);
- #ifdef COLOR_WEIGHTS
- Color[i].a = pColor[i].a;
- #else
- Color[i].a = 1.0f;
- #endif // COLOR_WEIGHTS
- if (flags & BC_FLAGS_DITHER_RGB)
- {
- HDRColorA Diff;
- Diff.r = Color[i].a * (Clr.r - Color[i].r);
- Diff.g = Color[i].a * (Clr.g - Color[i].g);
- Diff.b = Color[i].a * (Clr.b - Color[i].b);
- if(3 != (i & 3))
- {
- assert( i < 15 );
- _Analysis_assume_( i < 15 );
- Error[i + 1].r += Diff.r * (7.0f / 16.0f);
- Error[i + 1].g += Diff.g * (7.0f / 16.0f);
- Error[i + 1].b += Diff.b * (7.0f / 16.0f);
- }
- if(i < 12)
- {
- if(i & 3)
- {
- Error[i + 3].r += Diff.r * (3.0f / 16.0f);
- Error[i + 3].g += Diff.g * (3.0f / 16.0f);
- Error[i + 3].b += Diff.b * (3.0f / 16.0f);
- }
- Error[i + 4].r += Diff.r * (5.0f / 16.0f);
- Error[i + 4].g += Diff.g * (5.0f / 16.0f);
- Error[i + 4].b += Diff.b * (5.0f / 16.0f);
- if(3 != (i & 3))
- {
- assert( i < 11 );
- _Analysis_assume_( i < 11 );
- Error[i + 5].r += Diff.r * (1.0f / 16.0f);
- Error[i + 5].g += Diff.g * (1.0f / 16.0f);
- Error[i + 5].b += Diff.b * (1.0f / 16.0f);
- }
- }
- }
- if (pWeights) // ESENTHEL
- {
- Color[i].r *= pWeights->r;
- Color[i].g *= pWeights->g;
- Color[i].b *= pWeights->b;
- }
- }
- // Perform 6D root finding function to find two endpoints of color axis.
- // Then quantize and sort the endpoints depending on mode.
- HDRColorA ColorA, ColorB, ColorC, ColorD;
- OptimizeRGB(&ColorA, &ColorB, Color, uSteps, pWeights); // ESENTHEL
- if ( pWeights ) // ESENTHEL
- {
- ColorC.r = ColorA.r / pWeights->r;
- ColorC.g = ColorA.g / pWeights->g;
- ColorC.b = ColorA.b / pWeights->b;
- ColorD.r = ColorB.r / pWeights->r;
- ColorD.g = ColorB.g / pWeights->g;
- ColorD.b = ColorB.b / pWeights->b;
- }
- else
- {
- ColorC = ColorA;
- ColorD = ColorB;
- }
- uint16_t wColorA = Encode565(&ColorC);
- uint16_t wColorB = Encode565(&ColorD);
- if((uSteps == 4) && (wColorA == wColorB))
- {
- pBC->rgb[0] = wColorA;
- pBC->rgb[1] = wColorB;
- pBC->bitmap = 0x00000000;
- return;
- }
- Decode565(&ColorC, wColorA);
- Decode565(&ColorD, wColorB);
- if ( pWeights ) // ESENTHEL
- {
- ColorA.r = ColorC.r * pWeights->r;
- ColorA.g = ColorC.g * pWeights->g;
- ColorA.b = ColorC.b * pWeights->b;
- ColorB.r = ColorD.r * pWeights->r;
- ColorB.g = ColorD.g * pWeights->g;
- ColorB.b = ColorD.b * pWeights->b;
- }
- else
- {
- ColorA = ColorC;
- ColorB = ColorD;
- }
- // Calculate color steps
- HDRColorA Step[4];
- if((3 == uSteps) == (wColorA <= wColorB))
- {
- pBC->rgb[0] = wColorA;
- pBC->rgb[1] = wColorB;
- Step[0] = ColorA;
- Step[1] = ColorB;
- }
- else
- {
- pBC->rgb[0] = wColorB;
- pBC->rgb[1] = wColorA;
- Step[0] = ColorB;
- Step[1] = ColorA;
- }
- const size_t *pSteps;
- if(3 == uSteps)
- {
- pSteps = pSteps3;
- HDRColorALerp(&Step[2], &Step[0], &Step[1], 0.5f);
- }
- else
- {
- pSteps = pSteps4;
- HDRColorALerp(&Step[2], &Step[0], &Step[1], 1.0f / 3.0f);
- HDRColorALerp(&Step[3], &Step[0], &Step[1], 2.0f / 3.0f);
- }
- // Calculate color direction
- HDRColorA Dir;
- Dir.r = Step[1].r - Step[0].r;
- Dir.g = Step[1].g - Step[0].g;
- Dir.b = Step[1].b - Step[0].b;
- float fSteps = (float) (uSteps - 1);
- float fScale = (wColorA != wColorB) ? (fSteps / (Dir.r * Dir.r + Dir.g * Dir.g + Dir.b * Dir.b)) : 0.0f;
- Dir.r *= fScale;
- Dir.g *= fScale;
- Dir.b *= fScale;
- // Encode colors
- uint32_t dw = 0;
- if (flags & BC_FLAGS_DITHER_RGB)
- memset(Error, 0x00, NUM_PIXELS_PER_BLOCK * sizeof(HDRColorA));
- for(i = 0; i < NUM_PIXELS_PER_BLOCK; ++i)
- {
- if ((3 == uSteps) && (pColor[i].a < threshold))
- {
- dw = (3 << 30) | (dw >> 2);
- }
- else
- {
- HDRColorA Clr;
- if ( pWeights ) // ESENTHEL
- {
- Clr.r = pColor[i].r * pWeights->r;
- Clr.g = pColor[i].g * pWeights->g;
- Clr.b = pColor[i].b * pWeights->b;
- }
- else
- {
- Clr.r = pColor[i].r;
- Clr.g = pColor[i].g;
- Clr.b = pColor[i].b;
- }
- if (flags & BC_FLAGS_DITHER_RGB)
- {
- Clr.r += Error[i].r;
- Clr.g += Error[i].g;
- Clr.b += Error[i].b;
- }
- float fDot = (Clr.r - Step[0].r) * Dir.r + (Clr.g - Step[0].g) * Dir.g + (Clr.b - Step[0].b) * Dir.b;
- uint32_t iStep;
- if(fDot <= 0.0f)
- iStep = 0;
- else if(fDot >= fSteps)
- iStep = 1;
- else
- iStep = static_cast<uint32_t>( pSteps[static_cast<size_t>(fDot + 0.5f)] );
- dw = (iStep << 30) | (dw >> 2);
- if (flags & BC_FLAGS_DITHER_RGB)
- {
- HDRColorA Diff;
- Diff.r = Color[i].a * (Clr.r - Step[iStep].r);
- Diff.g = Color[i].a * (Clr.g - Step[iStep].g);
- Diff.b = Color[i].a * (Clr.b - Step[iStep].b);
- if(3 != (i & 3))
- {
- Error[i + 1].r += Diff.r * (7.0f / 16.0f);
- Error[i + 1].g += Diff.g * (7.0f / 16.0f);
- Error[i + 1].b += Diff.b * (7.0f / 16.0f);
- }
- if(i < 12)
- {
- if(i & 3)
- {
- Error[i + 3].r += Diff.r * (3.0f / 16.0f);
- Error[i + 3].g += Diff.g * (3.0f / 16.0f);
- Error[i + 3].b += Diff.b * (3.0f / 16.0f);
- }
- Error[i + 4].r += Diff.r * (5.0f / 16.0f);
- Error[i + 4].g += Diff.g * (5.0f / 16.0f);
- Error[i + 4].b += Diff.b * (5.0f / 16.0f);
- if(3 != (i & 3))
- {
- Error[i + 5].r += Diff.r * (1.0f / 16.0f);
- Error[i + 5].g += Diff.g * (1.0f / 16.0f);
- Error[i + 5].b += Diff.b * (1.0f / 16.0f);
- }
- }
- }
- }
- }
- pBC->bitmap = dw;
- }
- //-------------------------------------------------------------------------------------
- #ifdef COLOR_WEIGHTS
- void EncodeSolidBC1(_Out_ D3DX_BC1 *pBC, _In_reads_(NUM_PIXELS_PER_BLOCK) const HDRColorA *pColor)
- {
- #ifdef COLOR_AVG_0WEIGHTS
- // Compute avg color
- HDRColorA Color;
- Color.r = pColor[0].r;
- Color.g = pColor[0].g;
- Color.b = pColor[0].b;
- for(size_t i = 1; i < NUM_PIXELS_PER_BLOCK; ++i)
- {
- Color.r += pColor[i].r;
- Color.g += pColor[i].g;
- Color.b += pColor[i].b;
- }
- Color.r *= 1.0f / 16.0f;
- Color.g *= 1.0f / 16.0f;
- Color.b *= 1.0f / 16.0f;
- uint16_t wColor = Encode565(&Color);
- #else
- uint16_t wColor = 0x0000;
- #endif // COLOR_AVG_0WEIGHTS
- // Encode solid block
- pBC->rgb[0] = wColor;
- pBC->rgb[1] = wColor;
- pBC->bitmap = 0x00000000;
- }
- #endif // COLOR_WEIGHTS
- }
- //=====================================================================================
- // Entry points
- //=====================================================================================
- //-------------------------------------------------------------------------------------
- // BC1 Compression
- //-------------------------------------------------------------------------------------
- _Use_decl_annotations_
- void DirectX::D3DXDecodeBC1(XMVECTOR *pColor, const uint8_t *pBC)
- {
- auto pBC1 = reinterpret_cast<const D3DX_BC1 *>(pBC);
- DecodeBC1(pColor, pBC1, true);
- }
- _Use_decl_annotations_
- void DirectX::D3DXEncodeBC1(uint8_t *pBC, const XMVECTOR *pColor, const HDRColorA *pWeights, float threshold, DWORD flags) // ESENTHEL CHANGED
- {
- assert(pBC && pColor);
- HDRColorA Color[NUM_PIXELS_PER_BLOCK];
- if (flags & BC_FLAGS_DITHER_A)
- {
- float fError[NUM_PIXELS_PER_BLOCK];
- memset(fError, 0x00, NUM_PIXELS_PER_BLOCK * sizeof(float));
- for(size_t i = 0; i < NUM_PIXELS_PER_BLOCK; ++i)
- {
- HDRColorA clr;
- XMStoreFloat4( reinterpret_cast<XMFLOAT4*>( &clr ), pColor[i] );
- float fAlph = clr.a + fError[i];
- Color[i].r = clr.r;
- Color[i].g = clr.g;
- Color[i].b = clr.b;
- Color[i].a = (float) static_cast<int32_t>(clr.a + fError[i] + 0.5f);
- float fDiff = fAlph - Color[i].a;
- if(3 != (i & 3))
- {
- assert( i < 15 );
- _Analysis_assume_( i < 15 );
- fError[i + 1] += fDiff * (7.0f / 16.0f);
- }
- if(i < 12)
- {
- if(i & 3)
- fError[i + 3] += fDiff * (3.0f / 16.0f);
- fError[i + 4] += fDiff * (5.0f / 16.0f);
- if(3 != (i & 3))
- {
- assert( i < 11 );
- _Analysis_assume_( i < 11 );
- fError[i + 5] += fDiff * (1.0f / 16.0f);
- }
- }
- }
- }
- else
- {
- for(size_t i = 0; i < NUM_PIXELS_PER_BLOCK; ++i)
- {
- XMStoreFloat4( reinterpret_cast<XMFLOAT4*>( &Color[i] ), pColor[i] );
- }
- }
- auto pBC1 = reinterpret_cast<D3DX_BC1 *>(pBC);
- EncodeBC1(pBC1, Color, pWeights, true, threshold, flags);
- }
- //-------------------------------------------------------------------------------------
- // BC2 Compression
- //-------------------------------------------------------------------------------------
- _Use_decl_annotations_
- void DirectX::D3DXDecodeBC2(XMVECTOR *pColor, const uint8_t *pBC)
- {
- assert(pColor && pBC);
- static_assert(sizeof(D3DX_BC2) == 16, "D3DX_BC2 should be 16 bytes");
- auto pBC2 = reinterpret_cast<const D3DX_BC2 *>(pBC);
- // RGB part
- DecodeBC1(pColor, &pBC2->bc1, false);
- // 4-bit alpha part
- DWORD dw = pBC2->bitmap[0];
- for(size_t i = 0; i < 8; ++i, dw >>= 4)
- {
- #pragma prefast(suppress:22103, "writing blocks in two halves confuses tool")
- pColor[i] = XMVectorSetW( pColor[i], (float) (dw & 0xf) * (1.0f / 15.0f) );
- }
- dw = pBC2->bitmap[1];
- for (size_t i = 8; i < NUM_PIXELS_PER_BLOCK; ++i, dw >>= 4)
- pColor[i] = XMVectorSetW(pColor[i], (float)(dw & 0xf) * (1.0f / 15.0f));
- }
- _Use_decl_annotations_
- void DirectX::D3DXEncodeBC2(uint8_t *pBC, const XMVECTOR *pColor, const HDRColorA *pWeights, DWORD flags)
- {
- assert( pBC && pColor );
- static_assert( sizeof(D3DX_BC2) == 16, "D3DX_BC2 should be 16 bytes" );
- HDRColorA Color[NUM_PIXELS_PER_BLOCK];
- for(size_t i = 0; i < NUM_PIXELS_PER_BLOCK; ++i)
- {
- XMStoreFloat4( reinterpret_cast<XMFLOAT4*>( &Color[i] ), pColor[i] );
- }
- auto pBC2 = reinterpret_cast<D3DX_BC2 *>(pBC);
- // 4-bit alpha part. Dithered using Floyd Stienberg error diffusion.
- pBC2->bitmap[0] = 0;
- pBC2->bitmap[1] = 0;
- float fError[NUM_PIXELS_PER_BLOCK];
- if (flags & BC_FLAGS_DITHER_A)
- memset(fError, 0x00, NUM_PIXELS_PER_BLOCK * sizeof(float));
- for(size_t i = 0; i < NUM_PIXELS_PER_BLOCK; ++i)
- {
- float fAlph = Color[i].a;
- if (flags & BC_FLAGS_DITHER_A)
- fAlph += fError[i];
- uint32_t u = (uint32_t) static_cast<int32_t>(fAlph * 15.0f + 0.5f);
- pBC2->bitmap[i >> 3] >>= 4;
- pBC2->bitmap[i >> 3] |= (u << 28);
- if (flags & BC_FLAGS_DITHER_A)
- {
- float fDiff = fAlph - (float) u * (1.0f / 15.0f);
- if(3 != (i & 3))
- {
- assert( i < 15 );
- _Analysis_assume_( i < 15 );
- fError[i + 1] += fDiff * (7.0f / 16.0f);
- }
- if(i < 12)
- {
- if(i & 3)
- fError[i + 3] += fDiff * (3.0f / 16.0f);
- fError[i + 4] += fDiff * (5.0f / 16.0f);
- if(3 != (i & 3))
- {
- assert( i < 11 );
- _Analysis_assume_( i < 11 );
- fError[i + 5] += fDiff * (1.0f / 16.0f);
- }
- }
- }
- }
- // RGB part
- #ifdef COLOR_WEIGHTS
- if(!pBC2->bitmap[0] && !pBC2->bitmap[1])
- {
- EncodeSolidBC1(pBC2->dxt1, Color);
- return;
- }
- #endif // COLOR_WEIGHTS
- EncodeBC1(&pBC2->bc1, Color, pWeights, false, 0.f, flags);
- }
- //-------------------------------------------------------------------------------------
- // BC3 Compression
- //-------------------------------------------------------------------------------------
- _Use_decl_annotations_
- void DirectX::D3DXDecodeBC3(XMVECTOR *pColor, const uint8_t *pBC)
- {
- assert(pColor && pBC);
- static_assert( sizeof(D3DX_BC3) == 16, "D3DX_BC3 should be 16 bytes" );
- auto pBC3 = reinterpret_cast<const D3DX_BC3 *>(pBC);
- // RGB part
- DecodeBC1(pColor, &pBC3->bc1, false);
- // Adaptive 3-bit alpha part
- float fAlpha[8];
- fAlpha[0] = ((float) pBC3->alpha[0]) * (1.0f / 255.0f);
- fAlpha[1] = ((float) pBC3->alpha[1]) * (1.0f / 255.0f);
- if(pBC3->alpha[0] > pBC3->alpha[1])
- {
- for(size_t i = 1; i < 7; ++i)
- fAlpha[i + 1] = (fAlpha[0] * (7 - i) + fAlpha[1] * i) * (1.0f / 7.0f);
- }
- else
- {
- for(size_t i = 1; i < 5; ++i)
- fAlpha[i + 1] = (fAlpha[0] * (5 - i) + fAlpha[1] * i) * (1.0f / 5.0f);
- fAlpha[6] = 0.0f;
- fAlpha[7] = 1.0f;
- }
- DWORD dw = pBC3->bitmap[0] | (pBC3->bitmap[1] << 8) | (pBC3->bitmap[2] << 16);
- for(size_t i = 0; i < 8; ++i, dw >>= 3)
- pColor[i] = XMVectorSetW( pColor[i], fAlpha[dw & 0x7] );
- dw = pBC3->bitmap[3] | (pBC3->bitmap[4] << 8) | (pBC3->bitmap[5] << 16);
- for(size_t i = 8; i < NUM_PIXELS_PER_BLOCK; ++i, dw >>= 3)
- pColor[i] = XMVectorSetW(pColor[i], fAlpha[dw & 0x7]);
- }
- static const size_t pSteps6[] = { 0, 2, 3, 4, 5, 1 };
- static const size_t pSteps8[] = { 0, 2, 3, 4, 5, 6, 7, 1 };
- _Use_decl_annotations_
- void DirectX::D3DXEncodeBC3(uint8_t *pBC, const XMVECTOR *pColor, const HDRColorA *pWeights, DWORD flags)
- {
- assert( pBC && pColor );
- static_assert( sizeof(D3DX_BC3) == 16, "D3DX_BC3 should be 16 bytes" );
- HDRColorA Color[NUM_PIXELS_PER_BLOCK];
- for(size_t i = 0; i < NUM_PIXELS_PER_BLOCK; ++i)
- {
- XMStoreFloat4( reinterpret_cast<XMFLOAT4*>( &Color[i] ), pColor[i] );
- }
- auto pBC3 = reinterpret_cast<D3DX_BC3 *>(pBC);
- // Quantize block to A8, using Floyd Stienberg error diffusion. This
- // increases the chance that colors will map directly to the quantized
- // axis endpoints.
- float fAlpha[NUM_PIXELS_PER_BLOCK];
- float fError[NUM_PIXELS_PER_BLOCK];
- float fMinAlpha = Color[0].a;
- float fMaxAlpha = Color[0].a;
- if (flags & BC_FLAGS_DITHER_A)
- memset(fError, 0x00, NUM_PIXELS_PER_BLOCK * sizeof(float));
- for(size_t i = 0; i < NUM_PIXELS_PER_BLOCK; ++i)
- {
- float fAlph = Color[i].a;
- if (flags & BC_FLAGS_DITHER_A)
- fAlph += fError[i];
- fAlpha[i] = static_cast<int32_t>(fAlph * 255.0f + 0.5f) * (1.0f / 255.0f);
- if(fAlpha[i] < fMinAlpha)
- fMinAlpha = fAlpha[i];
- else if(fAlpha[i] > fMaxAlpha)
- fMaxAlpha = fAlpha[i];
-
- if (flags & BC_FLAGS_DITHER_A)
- {
- float fDiff = fAlph - fAlpha[i];
- if(3 != (i & 3))
- {
- assert( i < 15 );
- _Analysis_assume_( i < 15 );
- fError[i + 1] += fDiff * (7.0f / 16.0f);
- }
- if(i < 12)
- {
- if(i & 3)
- fError[i + 3] += fDiff * (3.0f / 16.0f);
- fError[i + 4] += fDiff * (5.0f / 16.0f);
- if(3 != (i & 3))
- {
- assert( i < 11 );
- _Analysis_assume_( i < 11 );
- fError[i + 5] += fDiff * (1.0f / 16.0f);
- }
- }
- }
- }
- #ifdef COLOR_WEIGHTS
- if(0.0f == fMaxAlpha)
- {
- EncodeSolidBC1(&pBC3->dxt1, Color);
- pBC3->alpha[0] = 0x00;
- pBC3->alpha[1] = 0x00;
- memset(pBC3->bitmap, 0x00, 6);
- }
- #endif
- // RGB part
- EncodeBC1(&pBC3->bc1, Color, pWeights, false, 0.f, flags); // ESENTHEL
- // Alpha part
- if(1.0f == fMinAlpha)
- {
- pBC3->alpha[0] = 0xff;
- pBC3->alpha[1] = 0xff;
- memset(pBC3->bitmap, 0x00, 6);
- return;
- }
- // Optimize and Quantize Min and Max values
- size_t uSteps = ((0.0f == fMinAlpha) || (1.0f == fMaxAlpha)) ? 6 : 8;
- float fAlphaA, fAlphaB;
- OptimizeAlpha<false>(&fAlphaA, &fAlphaB, fAlpha, uSteps);
- uint8_t bAlphaA = (uint8_t) static_cast<int32_t>(fAlphaA * 255.0f + 0.5f);
- uint8_t bAlphaB = (uint8_t) static_cast<int32_t>(fAlphaB * 255.0f + 0.5f);
- fAlphaA = (float) bAlphaA * (1.0f / 255.0f);
- fAlphaB = (float) bAlphaB * (1.0f / 255.0f);
- // Setup block
- if((8 == uSteps) && (bAlphaA == bAlphaB))
- {
- pBC3->alpha[0] = bAlphaA;
- pBC3->alpha[1] = bAlphaB;
- memset(pBC3->bitmap, 0x00, 6);
- return;
- }
- const size_t *pSteps;
- float fStep[8];
- if(6 == uSteps)
- {
- pBC3->alpha[0] = bAlphaA;
- pBC3->alpha[1] = bAlphaB;
- fStep[0] = fAlphaA;
- fStep[1] = fAlphaB;
- for(size_t i = 1; i < 5; ++i)
- fStep[i + 1] = (fStep[0] * (5 - i) + fStep[1] * i) * (1.0f / 5.0f);
- fStep[6] = 0.0f;
- fStep[7] = 1.0f;
- pSteps = pSteps6;
- }
- else
- {
- pBC3->alpha[0] = bAlphaB;
- pBC3->alpha[1] = bAlphaA;
- fStep[0] = fAlphaB;
- fStep[1] = fAlphaA;
- for(size_t i = 1; i < 7; ++i)
- fStep[i + 1] = (fStep[0] * (7 - i) + fStep[1] * i) * (1.0f / 7.0f);
- pSteps = pSteps8;
- }
- // Encode alpha bitmap
- float fSteps = (float) (uSteps - 1);
- float fScale = (fStep[0] != fStep[1]) ? (fSteps / (fStep[1] - fStep[0])) : 0.0f;
- if (flags & BC_FLAGS_DITHER_A)
- memset(fError, 0x00, NUM_PIXELS_PER_BLOCK * sizeof(float));
- for(size_t iSet = 0; iSet < 2; iSet++)
- {
- uint32_t dw = 0;
- size_t iMin = iSet * 8;
- size_t iLim = iMin + 8;
- for(size_t i = iMin; i < iLim; ++i)
- {
- float fAlph = Color[i].a;
- if (flags & BC_FLAGS_DITHER_A)
- fAlph += fError[i];
- float fDot = (fAlph - fStep[0]) * fScale;
- uint32_t iStep;
- if(fDot <= 0.0f)
- iStep = ((6 == uSteps) && (fAlph <= fStep[0] * 0.5f)) ? 6 : 0;
- else if(fDot >= fSteps)
- iStep = ((6 == uSteps) && (fAlph >= (fStep[1] + 1.0f) * 0.5f)) ? 7 : 1;
- else
- iStep = static_cast<uint32_t>( pSteps[static_cast<size_t>(fDot + 0.5f)] );
- dw = (iStep << 21) | (dw >> 3);
- if (flags & BC_FLAGS_DITHER_A)
- {
- float fDiff = (fAlph - fStep[iStep]);
- if(3 != (i & 3))
- fError[i + 1] += fDiff * (7.0f / 16.0f);
- if(i < 12)
- {
- if(i & 3)
- fError[i + 3] += fDiff * (3.0f / 16.0f);
- fError[i + 4] += fDiff * (5.0f / 16.0f);
- if(3 != (i & 3))
- fError[i + 5] += fDiff * (1.0f / 16.0f);
- }
- }
- }
- pBC3->bitmap[0 + iSet * 3] = ((uint8_t *) &dw)[0];
- pBC3->bitmap[1 + iSet * 3] = ((uint8_t *) &dw)[1];
- pBC3->bitmap[2 + iSet * 3] = ((uint8_t *) &dw)[2];
- }
- }
|