crossxtex/DirectXTex/BC.cpp

1138 lines
34 KiB
C++
Raw Normal View History

2016-08-22 18:26:36 +00:00
//-------------------------------------------------------------------------------------
// BC.cpp
//
// Block-compression (BC) functionality for BC1, BC2, BC3 (orginal DXTn formats)
//
// Copyright (c) Microsoft Corporation. All rights reserved.
2018-02-24 06:24:46 +00:00
// Licensed under the MIT License.
2016-08-22 18:26:36 +00:00
//
// http://go.microsoft.com/fwlink/?LinkId=248926
//-------------------------------------------------------------------------------------
#include "directxtexp.h"
// Experiemental encoding variants, not enabled by default
//#define COLOR_WEIGHTS
//#define COLOR_AVG_0WEIGHTS
#include "BC.h"
2016-09-09 02:09:46 +00:00
using namespace DirectX;
2016-08-22 18:26:36 +00:00
using namespace DirectX::PackedVector;
2016-09-09 02:09:46 +00:00
namespace
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
//-------------------------------------------------------------------------------------
// 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;
}
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
inline uint16_t Encode565(_In_ const HDRColorA *pColor)
{
HDRColorA Color;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
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;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
uint16_t w;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
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));
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
return w;
}
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
//-------------------------------------------------------------------------------------
void OptimizeRGB(
_Out_ HDRColorA *pX,
_Out_ HDRColorA *pY,
_In_reads_(NUM_PIXELS_PER_BLOCK) const HDRColorA *pPoints,
2018-05-02 07:20:07 +00:00
uint32_t cSteps,
2016-09-09 02:09:46 +00:00
DWORD flags)
{
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 };
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
const float *pC = (3 == cSteps) ? pC3 : pC4;
const float *pD = (3 == cSteps) ? pD3 : pD4;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
// Find Min and Max points, as starting point
HDRColorA X = (flags & BC_FLAGS_UNIFORM) ? HDRColorA(1.f, 1.f, 1.f, 1.f) : g_Luminance;
HDRColorA Y = HDRColorA(0.0f, 0.0f, 0.0f, 1.0f);
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
for (size_t iPoint = 0; iPoint < NUM_PIXELS_PER_BLOCK; iPoint++)
{
2016-08-22 18:26:36 +00:00
#ifdef COLOR_WEIGHTS
2016-09-09 02:09:46 +00:00
if (pPoints[iPoint].a > 0.0f)
2016-08-22 18:26:36 +00:00
#endif // COLOR_WEIGHTS
2016-09-09 02:09:46 +00:00
{
if (pPoints[iPoint].r < X.r)
X.r = pPoints[iPoint].r;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
if (pPoints[iPoint].g < X.g)
X.g = pPoints[iPoint].g;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
if (pPoints[iPoint].b < X.b)
X.b = pPoints[iPoint].b;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
if (pPoints[iPoint].r > Y.r)
Y.r = pPoints[iPoint].r;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
if (pPoints[iPoint].g > Y.g)
Y.g = pPoints[iPoint].g;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
if (pPoints[iPoint].b > Y.b)
Y.b = pPoints[iPoint].b;
}
2016-08-22 18:26:36 +00:00
}
2016-09-09 02:09:46 +00:00
// Diagonal axis
HDRColorA AB;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
AB.r = Y.r - X.r;
AB.g = Y.g - X.g;
AB.b = Y.b - X.b;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
float fAB = AB.r * AB.r + AB.g * AB.g + AB.b * AB.b;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
// 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;
}
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
// Try all four axis directions, to determine which diagonal best fits data
float fABInv = 1.0f / fAB;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
HDRColorA Dir;
Dir.r = AB.r * fABInv;
Dir.g = AB.g * fABInv;
Dir.b = AB.b * fABInv;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
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;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
float fDir[4];
fDir[0] = fDir[1] = fDir[2] = fDir[3] = 0.0f;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
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;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
float f;
2016-08-22 18:26:36 +00:00
#ifdef COLOR_WEIGHTS
2016-09-09 02:09:46 +00:00
f = Pt.r + Pt.g + Pt.b;
fDir[0] += pPoints[iPoint].a * f * f;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
f = Pt.r + Pt.g - Pt.b;
fDir[1] += pPoints[iPoint].a * f * f;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
f = Pt.r - Pt.g + Pt.b;
fDir[2] += pPoints[iPoint].a * f * f;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
f = Pt.r - Pt.g - Pt.b;
fDir[3] += pPoints[iPoint].a * f * f;
2016-08-22 18:26:36 +00:00
#else
2016-09-09 02:09:46 +00:00
f = Pt.r + Pt.g + Pt.b;
fDir[0] += f * f;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
f = Pt.r + Pt.g - Pt.b;
fDir[1] += f * f;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
f = Pt.r - Pt.g + Pt.b;
fDir[2] += f * f;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
f = Pt.r - Pt.g - Pt.b;
fDir[3] += f * f;
2016-08-22 18:26:36 +00:00
#endif // COLOR_WEIGHTS
2016-09-09 02:09:46 +00:00
}
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
float fDirMax = fDir[0];
size_t iDirMax = 0;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
for (size_t iDir = 1; iDir < 4; iDir++)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
if (fDir[iDir] > fDirMax)
{
fDirMax = fDir[iDir];
iDirMax = iDir;
}
2016-08-22 18:26:36 +00:00
}
2016-09-09 02:09:46 +00:00
if (iDirMax & 2)
{
float f = X.g; X.g = Y.g; Y.g = f;
}
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
if (iDirMax & 1)
{
float f = X.b; X.b = Y.b; Y.b = f;
}
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
// 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;
}
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
// Use Newton's Method to find local minima of sum-of-squares error.
float fSteps = (float)(cSteps - 1);
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
for (size_t iIteration = 0; iIteration < 8; iIteration++)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
// Calculate new steps
HDRColorA pSteps[4];
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
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];
}
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
// Calculate color direction
Dir.r = Y.r - X.r;
Dir.g = Y.g - X.g;
Dir.b = Y.b - X.b;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
float fLen = (Dir.r * Dir.r + Dir.g * Dir.g + Dir.b * Dir.b);
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
if (fLen < (1.0f / 4096.0f))
break;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
float fScale = fSteps / fLen;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
Dir.r *= fScale;
Dir.g *= fScale;
Dir.b *= fScale;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
// 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;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
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;
2016-08-22 18:26:36 +00:00
2018-05-02 07:20:07 +00:00
uint32_t iStep;
2016-09-09 02:09:46 +00:00
if (fDot <= 0.0f)
iStep = 0;
else if (fDot >= fSteps)
iStep = cSteps - 1;
else
2018-05-02 07:20:07 +00:00
iStep = uint32_t(fDot + 0.5f);
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
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;
2016-08-22 18:26:36 +00:00
#ifdef COLOR_WEIGHTS
2016-09-09 02:09:46 +00:00
float fC = pC[iStep] * pPoints[iPoint].a * (1.0f / 8.0f);
float fD = pD[iStep] * pPoints[iPoint].a * (1.0f / 8.0f);
2016-08-22 18:26:36 +00:00
#else
2016-09-09 02:09:46 +00:00
float fC = pC[iStep] * (1.0f / 8.0f);
float fD = pD[iStep] * (1.0f / 8.0f);
2016-08-22 18:26:36 +00:00
#endif // COLOR_WEIGHTS
2016-09-09 02:09:46 +00:00
d2X += fC * pC[iStep];
dX.r += fC * Diff.r;
dX.g += fC * Diff.g;
dX.b += fC * Diff.b;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
d2Y += fD * pD[iStep];
dY.r += fD * Diff.r;
dY.g += fD * Diff.g;
dY.b += fD * Diff.b;
}
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
// Move endpoints
if (d2X > 0.0f)
{
float f = -1.0f / d2X;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
X.r += dX.r * f;
X.g += dX.g * f;
X.b += dX.b * f;
}
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
if (d2Y > 0.0f)
{
float f = -1.0f / d2Y;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
Y.r += dY.r * f;
Y.g += dY.g * f;
Y.b += dY.b * f;
}
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
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;
}
2016-08-22 18:26:36 +00:00
}
2016-09-09 02:09:46 +00:00
pX->r = X.r; pX->g = X.g; pX->b = X.b;
pY->r = Y.r; pY->g = Y.g; pY->b = Y.b;
2016-08-22 18:26:36 +00:00
}
2016-09-09 02:09:46 +00:00
//-------------------------------------------------------------------------------------
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");
2016-08-22 18:26:36 +00:00
2017-07-12 07:56:51 +00:00
static XMVECTORF32 s_Scale = { { { 1.f / 31.f, 1.f / 63.f, 1.f / 31.f, 1.f } } };
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
XMVECTOR clr0 = XMLoadU565(reinterpret_cast<const XMU565*>(&pBC->rgb[0]));
XMVECTOR clr1 = XMLoadU565(reinterpret_cast<const XMU565*>(&pBC->rgb[1]));
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
clr0 = XMVectorMultiply(clr0, s_Scale);
clr1 = XMVectorMultiply(clr1, s_Scale);
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
clr0 = XMVectorSwizzle<2, 1, 0, 3>(clr0);
clr1 = XMVectorSwizzle<2, 1, 0, 3>(clr1);
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
clr0 = XMVectorSelect(g_XMIdentityR3, clr0, g_XMSelect1110);
clr1 = XMVectorSelect(g_XMIdentityR3, clr1, g_XMSelect1110);
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
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);
}
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
uint32_t dw = pBC->bitmap;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
for (size_t i = 0; i < NUM_PIXELS_PER_BLOCK; ++i, dw >>= 2)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
switch (dw & 3)
{
case 0: pColor[i] = clr0; break;
case 1: pColor[i] = clr1; break;
case 2: pColor[i] = clr2; break;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
case 3:
default: pColor[i] = clr3; break;
}
2016-08-22 18:26:36 +00:00
}
}
2016-09-09 02:09:46 +00:00
//-------------------------------------------------------------------------------------
void EncodeBC1(
_Out_ D3DX_BC1 *pBC,
_In_reads_(NUM_PIXELS_PER_BLOCK) const HDRColorA *pColor,
bool bColorKey,
2016-09-14 17:56:38 +00:00
float threshold,
2016-09-09 02:09:46 +00:00
DWORD flags)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
assert(pBC && pColor);
static_assert(sizeof(D3DX_BC1) == 8, "D3DX_BC1 should be 8 bytes");
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
// Determine if we need to colorkey this block
2018-05-02 07:20:07 +00:00
uint32_t uSteps;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
if (bColorKey)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
size_t uColorKey = 0;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
for (size_t i = 0; i < NUM_PIXELS_PER_BLOCK; ++i)
{
2016-09-14 17:56:38 +00:00
if (pColor[i].a < threshold)
2016-09-09 02:09:46 +00:00
uColorKey++;
}
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
if (NUM_PIXELS_PER_BLOCK == uColorKey)
{
pBC->rgb[0] = 0x0000;
pBC->rgb[1] = 0xffff;
pBC->bitmap = 0xffffffff;
return;
}
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
uSteps = (uColorKey > 0) ? 3 : 4;
}
else
{
uSteps = 4;
}
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
// 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];
2016-08-22 18:26:36 +00:00
if (flags & BC_FLAGS_DITHER_RGB)
2016-09-09 02:09:46 +00:00
memset(Error, 0x00, NUM_PIXELS_PER_BLOCK * sizeof(HDRColorA));
size_t i;
for (i = 0; i < NUM_PIXELS_PER_BLOCK; ++i)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
HDRColorA Clr;
Clr.r = pColor[i].r;
Clr.g = pColor[i].g;
Clr.b = pColor[i].b;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
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);
2016-08-22 18:26:36 +00:00
#ifdef COLOR_WEIGHTS
2016-09-09 02:09:46 +00:00
Color[i].a = pColor[i].a;
2016-08-22 18:26:36 +00:00
#else
2016-09-09 02:09:46 +00:00
Color[i].a = 1.0f;
2016-08-22 18:26:36 +00:00
#endif // COLOR_WEIGHTS
2016-09-09 02:09:46 +00:00
if (flags & BC_FLAGS_DITHER_RGB)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
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);
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
if (3 != (i & 3))
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
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);
2016-08-22 18:26:36 +00:00
}
2016-09-09 02:09:46 +00:00
if (i < 12)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
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);
}
2016-08-22 18:26:36 +00:00
}
}
2016-09-09 02:09:46 +00:00
if (!(flags & BC_FLAGS_UNIFORM))
{
Color[i].r *= g_Luminance.r;
Color[i].g *= g_Luminance.g;
Color[i].b *= g_Luminance.b;
}
2016-08-22 18:26:36 +00:00
}
2016-09-09 02:09:46 +00:00
// 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;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
OptimizeRGB(&ColorA, &ColorB, Color, uSteps, flags);
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
if (flags & BC_FLAGS_UNIFORM)
{
ColorC = ColorA;
ColorD = ColorB;
}
else
{
ColorC.r = ColorA.r * g_LuminanceInv.r;
ColorC.g = ColorA.g * g_LuminanceInv.g;
ColorC.b = ColorA.b * g_LuminanceInv.b;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
ColorD.r = ColorB.r * g_LuminanceInv.r;
ColorD.g = ColorB.g * g_LuminanceInv.g;
ColorD.b = ColorB.b * g_LuminanceInv.b;
}
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
uint16_t wColorA = Encode565(&ColorC);
uint16_t wColorB = Encode565(&ColorD);
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
if ((uSteps == 4) && (wColorA == wColorB))
{
pBC->rgb[0] = wColorA;
pBC->rgb[1] = wColorB;
pBC->bitmap = 0x00000000;
return;
}
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
Decode565(&ColorC, wColorA);
Decode565(&ColorD, wColorB);
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
if (flags & BC_FLAGS_UNIFORM)
{
ColorA = ColorC;
ColorB = ColorD;
}
else
{
ColorA.r = ColorC.r * g_Luminance.r;
ColorA.g = ColorC.g * g_Luminance.g;
ColorA.b = ColorC.b * g_Luminance.b;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
ColorB.r = ColorD.r * g_Luminance.r;
ColorB.g = ColorD.g * g_Luminance.g;
ColorB.b = ColorD.b * g_Luminance.b;
}
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
// Calculate color steps
HDRColorA Step[4];
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
if ((3 == uSteps) == (wColorA <= wColorB))
{
pBC->rgb[0] = wColorA;
pBC->rgb[1] = wColorB;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
Step[0] = ColorA;
Step[1] = ColorB;
}
else
{
pBC->rgb[0] = wColorB;
pBC->rgb[1] = wColorA;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
Step[0] = ColorB;
Step[1] = ColorA;
}
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
static const size_t pSteps3[] = { 0, 2, 1 };
static const size_t pSteps4[] = { 0, 2, 3, 1 };
const size_t *pSteps;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
if (3 == uSteps)
{
pSteps = pSteps3;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
HDRColorALerp(&Step[2], &Step[0], &Step[1], 0.5f);
}
else
{
pSteps = pSteps4;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
HDRColorALerp(&Step[2], &Step[0], &Step[1], 1.0f / 3.0f);
HDRColorALerp(&Step[3], &Step[0], &Step[1], 2.0f / 3.0f);
}
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
// Calculate color direction
HDRColorA Dir;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
Dir.r = Step[1].r - Step[0].r;
Dir.g = Step[1].g - Step[0].g;
Dir.b = Step[1].b - Step[0].b;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
float fSteps = (float)(uSteps - 1);
float fScale = (wColorA != wColorB) ? (fSteps / (Dir.r * Dir.r + Dir.g * Dir.g + Dir.b * Dir.b)) : 0.0f;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
Dir.r *= fScale;
Dir.g *= fScale;
Dir.b *= fScale;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
// Encode colors
uint32_t dw = 0;
if (flags & BC_FLAGS_DITHER_RGB)
memset(Error, 0x00, NUM_PIXELS_PER_BLOCK * sizeof(HDRColorA));
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
for (i = 0; i < NUM_PIXELS_PER_BLOCK; ++i)
2016-08-22 18:26:36 +00:00
{
2016-09-14 17:56:38 +00:00
if ((3 == uSteps) && (pColor[i].a < threshold))
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
dw = (3 << 30) | (dw >> 2);
2016-08-22 18:26:36 +00:00
}
else
{
2016-09-09 02:09:46 +00:00
HDRColorA Clr;
if (flags & BC_FLAGS_UNIFORM)
{
Clr.r = pColor[i].r;
Clr.g = pColor[i].g;
Clr.b = pColor[i].b;
}
else
{
Clr.r = pColor[i].r * g_Luminance.r;
Clr.g = pColor[i].g * g_Luminance.g;
Clr.b = pColor[i].b * g_Luminance.b;
}
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
if (flags & BC_FLAGS_DITHER_RGB)
{
Clr.r += Error[i].r;
Clr.g += Error[i].g;
Clr.b += Error[i].b;
}
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
float fDot = (Clr.r - Step[0].r) * Dir.r + (Clr.g - Step[0].g) * Dir.g + (Clr.b - Step[0].b) * Dir.b;
2016-08-22 18:26:36 +00:00
2018-05-02 07:20:07 +00:00
uint32_t iStep;
2016-09-09 02:09:46 +00:00
if (fDot <= 0.0f)
iStep = 0;
else if (fDot >= fSteps)
iStep = 1;
else
2018-05-02 07:20:07 +00:00
iStep = uint32_t(pSteps[uint32_t(fDot + 0.5f)]);
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
dw = (iStep << 30) | (dw >> 2);
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
if (flags & BC_FLAGS_DITHER_RGB)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
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);
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
if (3 != (i & 3))
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
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);
2016-08-22 18:26:36 +00:00
}
2016-09-09 02:09:46 +00:00
if (i < 12)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
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);
}
2016-08-22 18:26:36 +00:00
}
}
}
}
2016-09-09 02:09:46 +00:00
pBC->bitmap = dw;
}
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
//-------------------------------------------------------------------------------------
2016-08-22 18:26:36 +00:00
#ifdef COLOR_WEIGHTS
2016-09-09 02:09:46 +00:00
void EncodeSolidBC1(_Out_ D3DX_BC1 *pBC, _In_reads_(NUM_PIXELS_PER_BLOCK) const HDRColorA *pColor)
{
2016-08-22 18:26:36 +00:00
#ifdef COLOR_AVG_0WEIGHTS
2016-09-09 02:09:46 +00:00
// Compute avg color
HDRColorA Color;
Color.r = pColor[0].r;
Color.g = pColor[0].g;
Color.b = pColor[0].b;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
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;
}
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
Color.r *= 1.0f / 16.0f;
Color.g *= 1.0f / 16.0f;
Color.b *= 1.0f / 16.0f;
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
uint16_t wColor = Encode565(&Color);
2016-08-22 18:26:36 +00:00
#else
2016-09-09 02:09:46 +00:00
uint16_t wColor = 0x0000;
2016-08-22 18:26:36 +00:00
#endif // COLOR_AVG_0WEIGHTS
2016-09-09 02:09:46 +00:00
// Encode solid block
pBC->rgb[0] = wColor;
pBC->rgb[1] = wColor;
pBC->bitmap = 0x00000000;
}
2016-08-22 18:26:36 +00:00
#endif // COLOR_WEIGHTS
2016-09-09 02:09:46 +00:00
}
2016-08-22 18:26:36 +00:00
//=====================================================================================
// Entry points
//=====================================================================================
//-------------------------------------------------------------------------------------
// BC1 Compression
//-------------------------------------------------------------------------------------
_Use_decl_annotations_
2016-09-09 02:09:46 +00:00
void DirectX::D3DXDecodeBC1(XMVECTOR *pColor, const uint8_t *pBC)
2016-08-22 18:26:36 +00:00
{
auto pBC1 = reinterpret_cast<const D3DX_BC1 *>(pBC);
2016-09-09 02:09:46 +00:00
DecodeBC1(pColor, pBC1, true);
2016-08-22 18:26:36 +00:00
}
_Use_decl_annotations_
2016-09-14 17:56:38 +00:00
void DirectX::D3DXEncodeBC1(uint8_t *pBC, const XMVECTOR *pColor, float threshold, DWORD flags)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
assert(pBC && pColor);
2016-08-22 18:26:36 +00:00
HDRColorA Color[NUM_PIXELS_PER_BLOCK];
if (flags & BC_FLAGS_DITHER_A)
{
float fError[NUM_PIXELS_PER_BLOCK] = {};
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
for (size_t i = 0; i < NUM_PIXELS_PER_BLOCK; ++i)
2016-08-22 18:26:36 +00:00
{
HDRColorA clr;
2016-09-09 02:09:46 +00:00
XMStoreFloat4(reinterpret_cast<XMFLOAT4*>(&clr), pColor[i]);
2016-08-22 18:26:36 +00:00
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;
2016-09-09 02:09:46 +00:00
if (3 != (i & 3))
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
assert(i < 15);
_Analysis_assume_(i < 15);
2016-08-22 18:26:36 +00:00
fError[i + 1] += fDiff * (7.0f / 16.0f);
}
2016-09-09 02:09:46 +00:00
if (i < 12)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
if (i & 3)
2016-08-22 18:26:36 +00:00
fError[i + 3] += fDiff * (3.0f / 16.0f);
fError[i + 4] += fDiff * (5.0f / 16.0f);
2016-09-09 02:09:46 +00:00
if (3 != (i & 3))
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
assert(i < 11);
_Analysis_assume_(i < 11);
2016-08-22 18:26:36 +00:00
fError[i + 5] += fDiff * (1.0f / 16.0f);
}
}
}
}
else
{
2016-09-09 02:09:46 +00:00
for (size_t i = 0; i < NUM_PIXELS_PER_BLOCK; ++i)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
XMStoreFloat4(reinterpret_cast<XMFLOAT4*>(&Color[i]), pColor[i]);
2016-08-22 18:26:36 +00:00
}
}
auto pBC1 = reinterpret_cast<D3DX_BC1 *>(pBC);
2016-09-14 17:56:38 +00:00
EncodeBC1(pBC1, Color, true, threshold, flags);
2016-08-22 18:26:36 +00:00
}
//-------------------------------------------------------------------------------------
// BC2 Compression
//-------------------------------------------------------------------------------------
_Use_decl_annotations_
2016-09-09 02:09:46 +00:00
void DirectX::D3DXDecodeBC2(XMVECTOR *pColor, const uint8_t *pBC)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
assert(pColor && pBC);
static_assert(sizeof(D3DX_BC2) == 16, "D3DX_BC2 should be 16 bytes");
2016-08-22 18:26:36 +00:00
auto pBC2 = reinterpret_cast<const D3DX_BC2 *>(pBC);
// RGB part
DecodeBC1(pColor, &pBC2->bc1, false);
// 4-bit alpha part
DWORD dw = pBC2->bitmap[0];
2016-09-09 02:09:46 +00:00
for (size_t i = 0; i < 8; ++i, dw >>= 4)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
#pragma prefast(suppress:22103, "writing blocks in two halves confuses tool")
pColor[i] = XMVectorSetW(pColor[i], (float)(dw & 0xf) * (1.0f / 15.0f));
2016-08-22 18:26:36 +00:00
}
dw = pBC2->bitmap[1];
2016-09-09 02:09:46 +00:00
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));
2016-08-22 18:26:36 +00:00
}
_Use_decl_annotations_
2016-09-09 02:09:46 +00:00
void DirectX::D3DXEncodeBC2(uint8_t *pBC, const XMVECTOR *pColor, DWORD flags)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
assert(pBC && pColor);
static_assert(sizeof(D3DX_BC2) == 16, "D3DX_BC2 should be 16 bytes");
2016-08-22 18:26:36 +00:00
HDRColorA Color[NUM_PIXELS_PER_BLOCK];
2016-09-09 02:09:46 +00:00
for (size_t i = 0; i < NUM_PIXELS_PER_BLOCK; ++i)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
XMStoreFloat4(reinterpret_cast<XMFLOAT4*>(&Color[i]), pColor[i]);
2016-08-22 18:26:36 +00:00
}
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] = {};
2016-09-09 02:09:46 +00:00
for (size_t i = 0; i < NUM_PIXELS_PER_BLOCK; ++i)
2016-08-22 18:26:36 +00:00
{
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)
2016-09-09 02:09:46 +00:00
{
float fDiff = fAlph - (float)u * (1.0f / 15.0f);
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
if (3 != (i & 3))
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
assert(i < 15);
_Analysis_assume_(i < 15);
2016-08-22 18:26:36 +00:00
fError[i + 1] += fDiff * (7.0f / 16.0f);
}
2016-09-09 02:09:46 +00:00
if (i < 12)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
if (i & 3)
2016-08-22 18:26:36 +00:00
fError[i + 3] += fDiff * (3.0f / 16.0f);
fError[i + 4] += fDiff * (5.0f / 16.0f);
2016-09-09 02:09:46 +00:00
if (3 != (i & 3))
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
assert(i < 11);
_Analysis_assume_(i < 11);
2016-08-22 18:26:36 +00:00
fError[i + 5] += fDiff * (1.0f / 16.0f);
}
}
}
}
// RGB part
#ifdef COLOR_WEIGHTS
2016-09-09 02:09:46 +00:00
if (!pBC2->bitmap[0] && !pBC2->bitmap[1])
2016-08-22 18:26:36 +00:00
{
EncodeSolidBC1(pBC2->dxt1, Color);
return;
}
#endif // COLOR_WEIGHTS
EncodeBC1(&pBC2->bc1, Color, false, 0.f, flags);
}
//-------------------------------------------------------------------------------------
// BC3 Compression
//-------------------------------------------------------------------------------------
_Use_decl_annotations_
2016-09-09 02:09:46 +00:00
void DirectX::D3DXDecodeBC3(XMVECTOR *pColor, const uint8_t *pBC)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
assert(pColor && pBC);
static_assert(sizeof(D3DX_BC3) == 16, "D3DX_BC3 should be 16 bytes");
2016-08-22 18:26:36 +00:00
auto pBC3 = reinterpret_cast<const D3DX_BC3 *>(pBC);
// RGB part
DecodeBC1(pColor, &pBC3->bc1, false);
// Adaptive 3-bit alpha part
float fAlpha[8];
2016-09-09 02:09:46 +00:00
fAlpha[0] = ((float)pBC3->alpha[0]) * (1.0f / 255.0f);
fAlpha[1] = ((float)pBC3->alpha[1]) * (1.0f / 255.0f);
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
if (pBC3->alpha[0] > pBC3->alpha[1])
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
for (size_t i = 1; i < 7; ++i)
2016-08-22 18:26:36 +00:00
fAlpha[i + 1] = (fAlpha[0] * (7 - i) + fAlpha[1] * i) * (1.0f / 7.0f);
}
2016-09-09 02:09:46 +00:00
else
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
for (size_t i = 1; i < 5; ++i)
2016-08-22 18:26:36 +00:00
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);
2016-09-09 02:09:46 +00:00
for (size_t i = 0; i < 8; ++i, dw >>= 3)
pColor[i] = XMVectorSetW(pColor[i], fAlpha[dw & 0x7]);
2016-08-22 18:26:36 +00:00
dw = pBC3->bitmap[3] | (pBC3->bitmap[4] << 8) | (pBC3->bitmap[5] << 16);
2016-09-09 02:09:46 +00:00
for (size_t i = 8; i < NUM_PIXELS_PER_BLOCK; ++i, dw >>= 3)
pColor[i] = XMVectorSetW(pColor[i], fAlpha[dw & 0x7]);
2016-08-22 18:26:36 +00:00
}
_Use_decl_annotations_
2016-09-09 02:09:46 +00:00
void DirectX::D3DXEncodeBC3(uint8_t *pBC, const XMVECTOR *pColor, DWORD flags)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
assert(pBC && pColor);
static_assert(sizeof(D3DX_BC3) == 16, "D3DX_BC3 should be 16 bytes");
2016-08-22 18:26:36 +00:00
HDRColorA Color[NUM_PIXELS_PER_BLOCK];
2016-09-09 02:09:46 +00:00
for (size_t i = 0; i < NUM_PIXELS_PER_BLOCK; ++i)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
XMStoreFloat4(reinterpret_cast<XMFLOAT4*>(&Color[i]), pColor[i]);
2016-08-22 18:26:36 +00:00
}
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] = {};
2016-08-22 18:26:36 +00:00
float fMinAlpha = Color[0].a;
float fMaxAlpha = Color[0].a;
2016-09-09 02:09:46 +00:00
for (size_t i = 0; i < NUM_PIXELS_PER_BLOCK; ++i)
2016-08-22 18:26:36 +00:00
{
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);
2016-09-09 02:09:46 +00:00
if (fAlpha[i] < fMinAlpha)
2016-08-22 18:26:36 +00:00
fMinAlpha = fAlpha[i];
2016-09-09 02:09:46 +00:00
else if (fAlpha[i] > fMaxAlpha)
2016-08-22 18:26:36 +00:00
fMaxAlpha = fAlpha[i];
2016-09-09 02:09:46 +00:00
2016-08-22 18:26:36 +00:00
if (flags & BC_FLAGS_DITHER_A)
{
float fDiff = fAlph - fAlpha[i];
2016-09-09 02:09:46 +00:00
if (3 != (i & 3))
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
assert(i < 15);
_Analysis_assume_(i < 15);
2016-08-22 18:26:36 +00:00
fError[i + 1] += fDiff * (7.0f / 16.0f);
}
2016-09-09 02:09:46 +00:00
if (i < 12)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
if (i & 3)
2016-08-22 18:26:36 +00:00
fError[i + 3] += fDiff * (3.0f / 16.0f);
fError[i + 4] += fDiff * (5.0f / 16.0f);
2016-09-09 02:09:46 +00:00
if (3 != (i & 3))
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
assert(i < 11);
_Analysis_assume_(i < 11);
2016-08-22 18:26:36 +00:00
fError[i + 5] += fDiff * (1.0f / 16.0f);
}
}
}
}
#ifdef COLOR_WEIGHTS
2016-09-09 02:09:46 +00:00
if (0.0f == fMaxAlpha)
2016-08-22 18:26:36 +00:00
{
EncodeSolidBC1(&pBC3->dxt1, Color);
pBC3->alpha[0] = 0x00;
pBC3->alpha[1] = 0x00;
memset(pBC3->bitmap, 0x00, 6);
}
#endif
// RGB part
EncodeBC1(&pBC3->bc1, Color, false, 0.f, flags);
// Alpha part
2016-09-09 02:09:46 +00:00
if (1.0f == fMinAlpha)
2016-08-22 18:26:36 +00:00
{
pBC3->alpha[0] = 0xff;
pBC3->alpha[1] = 0xff;
memset(pBC3->bitmap, 0x00, 6);
return;
}
// Optimize and Quantize Min and Max values
2018-05-02 07:20:07 +00:00
uint32_t uSteps = ((0.0f == fMinAlpha) || (1.0f == fMaxAlpha)) ? 6 : 8;
2016-08-22 18:26:36 +00:00
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);
2016-09-09 02:09:46 +00:00
fAlphaA = (float)bAlphaA * (1.0f / 255.0f);
fAlphaB = (float)bAlphaB * (1.0f / 255.0f);
2016-08-22 18:26:36 +00:00
// Setup block
2016-09-09 02:09:46 +00:00
if ((8 == uSteps) && (bAlphaA == bAlphaB))
2016-08-22 18:26:36 +00:00
{
pBC3->alpha[0] = bAlphaA;
pBC3->alpha[1] = bAlphaB;
memset(pBC3->bitmap, 0x00, 6);
return;
}
static const size_t pSteps6[] = { 0, 2, 3, 4, 5, 1 };
static const size_t pSteps8[] = { 0, 2, 3, 4, 5, 6, 7, 1 };
const size_t *pSteps;
float fStep[8] = {};
2016-08-22 18:26:36 +00:00
2016-09-09 02:09:46 +00:00
if (6 == uSteps)
2016-08-22 18:26:36 +00:00
{
pBC3->alpha[0] = bAlphaA;
pBC3->alpha[1] = bAlphaB;
fStep[0] = fAlphaA;
fStep[1] = fAlphaB;
2016-09-09 02:09:46 +00:00
for (size_t i = 1; i < 5; ++i)
2016-08-22 18:26:36 +00:00
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;
2016-09-09 02:09:46 +00:00
for (size_t i = 1; i < 7; ++i)
2016-08-22 18:26:36 +00:00
fStep[i + 1] = (fStep[0] * (7 - i) + fStep[1] * i) * (1.0f / 7.0f);
pSteps = pSteps8;
}
// Encode alpha bitmap
2016-09-09 02:09:46 +00:00
float fSteps = (float)(uSteps - 1);
2016-08-22 18:26:36 +00:00
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));
2016-09-09 02:09:46 +00:00
for (size_t iSet = 0; iSet < 2; iSet++)
2016-08-22 18:26:36 +00:00
{
uint32_t dw = 0;
size_t iMin = iSet * 8;
size_t iLim = iMin + 8;
2016-09-09 02:09:46 +00:00
for (size_t i = iMin; i < iLim; ++i)
2016-08-22 18:26:36 +00:00
{
float fAlph = Color[i].a;
if (flags & BC_FLAGS_DITHER_A)
fAlph += fError[i];
float fDot = (fAlph - fStep[0]) * fScale;
uint32_t iStep;
2016-09-09 02:09:46 +00:00
if (fDot <= 0.0f)
2016-08-22 18:26:36 +00:00
iStep = ((6 == uSteps) && (fAlph <= fStep[0] * 0.5f)) ? 6 : 0;
2016-09-09 02:09:46 +00:00
else if (fDot >= fSteps)
2016-08-22 18:26:36 +00:00
iStep = ((6 == uSteps) && (fAlph >= (fStep[1] + 1.0f) * 0.5f)) ? 7 : 1;
else
2018-05-02 07:20:07 +00:00
iStep = uint32_t(pSteps[uint32_t(fDot + 0.5f)]);
2016-08-22 18:26:36 +00:00
dw = (iStep << 21) | (dw >> 3);
if (flags & BC_FLAGS_DITHER_A)
{
float fDiff = (fAlph - fStep[iStep]);
2016-09-09 02:09:46 +00:00
if (3 != (i & 3))
2016-08-22 18:26:36 +00:00
fError[i + 1] += fDiff * (7.0f / 16.0f);
2016-09-09 02:09:46 +00:00
if (i < 12)
2016-08-22 18:26:36 +00:00
{
2016-09-09 02:09:46 +00:00
if (i & 3)
2016-08-22 18:26:36 +00:00
fError[i + 3] += fDiff * (3.0f / 16.0f);
fError[i + 4] += fDiff * (5.0f / 16.0f);
2016-09-09 02:09:46 +00:00
if (3 != (i & 3))
2016-08-22 18:26:36 +00:00
fError[i + 5] += fDiff * (1.0f / 16.0f);
}
}
}
2016-09-09 02:09:46 +00:00
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];
2016-08-22 18:26:36 +00:00
}
}